Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: Fix new bug related with "--lines" argument #1558

Merged
merged 2 commits into from
Sep 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/spoon/reflect/visitor/PrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import spoon.compiler.Environment;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.CtElement;

import java.util.ArrayDeque;
Expand Down Expand Up @@ -180,7 +181,7 @@ private boolean isWhite(char c) {
}

public PrinterHelper adjustStartPosition(CtElement e) {
if (e.getPosition() != null && !e.isImplicit()) {
if (e.getPosition() != null && !e.isImplicit() && !(e.getPosition() instanceof NoSourcePosition)) {
// we should add some lines
while (line < e.getPosition().getLine()) {
writeln();
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/spoon/test/prettyprinter/LinesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.junit.Test;
import spoon.Launcher;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.reflect.visitor.filter.NamedElementFilter;
import spoon.reflect.visitor.filter.TypeFilter;

import java.util.ArrayList;
Expand Down Expand Up @@ -105,4 +107,15 @@ public void testIdenticalPrettyPrinter() throws Exception{
}
assertTrue(n>20);
}

@Test
public void testCompileWhenUsingLinesArgument() {
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] {"--compile", "--with-imports", "--lines"});
launcher.addInputResource("./src/test/java/spoon/test/prettyprinter/testclasses/FooCasper.java");
launcher.run();

List<CtType> fooCasperClass = launcher.getModel().getElements(new NamedElementFilter<>(CtType.class, "FooCasper"));
assertEquals(1, fooCasperClass.size());
}
}
90 changes: 90 additions & 0 deletions src/test/java/spoon/test/prettyprinter/testclasses/FooCasper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package spoon.test.prettyprinter.testclasses;

public class FooCasper {
// public static void main(String[] args) {
//// new FooCasper().selfTest();
// }
FooCasper f;

public FooCasper bug1() {
if (new FooCasper(1).foo() != null) {
throw new Error();
}
FooCasper g = new FooCasper(1).foo();
f=g;
System.out.println(f);
// the NPE
f.bar();
return null;
}

public FooCasper foo() {
return foo2();
// return null;
}

public FooCasper foo2() {
return null;
}

public FooCasper foo3() {
return f;
}

public void bar() {
}

public FooCasper foo5(FooCasper o) {
return o;
}

public void bug2() {
foo5(null).f.bar();
}

public void bug3() {
FooCasper[] tab = null;
if (0==1) {tab = new FooCasper[0];}
tab[0].bar();
}

// testing the given ObjectNullified
public void bug4() {
Object tab =null;
if (0==1) {tab = new Object();}
tab.toString();
}

public FooCasper(int i) {
}

public FooCasper() {
}

// toString support
public void toString_support() {
FooCasper o = null;
o.toString();
}

// testing arrays
public void array_support() {
FooCasper o = null;
FooCasper[] array = new FooCasper[10];
array[1] = o;
array[2] = array[1];
array[2].bar();
}

public void literal() {
FooCasper tab = null;
tab.literal();
}

public void literal2() {
FooCasper tab = new FooCasper();
tab = null;
tab.literal();
}
}