From 2ba40bc564729df670baf346a91b97f73d6c1cbe Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Fri, 22 Sep 2017 16:42:03 +0200 Subject: [PATCH 1/2] Show a new bug related with "--lines" argument --- .../spoon/test/prettyprinter/LinesTest.java | 13 +++ .../prettyprinter/testclasses/FooCasper.java | 90 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/test/java/spoon/test/prettyprinter/testclasses/FooCasper.java diff --git a/src/test/java/spoon/test/prettyprinter/LinesTest.java b/src/test/java/spoon/test/prettyprinter/LinesTest.java index b937bf62732..5f631cec9cb 100644 --- a/src/test/java/spoon/test/prettyprinter/LinesTest.java +++ b/src/test/java/spoon/test/prettyprinter/LinesTest.java @@ -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; @@ -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 fooCasperClass = launcher.getModel().getElements(new NamedElementFilter<>(CtType.class, "FooCasper")); + assertEquals(1, fooCasperClass.size()); + } } diff --git a/src/test/java/spoon/test/prettyprinter/testclasses/FooCasper.java b/src/test/java/spoon/test/prettyprinter/testclasses/FooCasper.java new file mode 100644 index 00000000000..d1f29e0ac81 --- /dev/null +++ b/src/test/java/spoon/test/prettyprinter/testclasses/FooCasper.java @@ -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(); + } +} + From cb3f4be343d60e76b14c075d4dca750e525e62a8 Mon Sep 17 00:00:00 2001 From: Pavel Vojtechovsky Date: Fri, 22 Sep 2017 21:12:26 +0200 Subject: [PATCH 2/2] fix: Do not adjust position when NoSourcePosition --- src/main/java/spoon/reflect/visitor/PrinterHelper.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/spoon/reflect/visitor/PrinterHelper.java b/src/main/java/spoon/reflect/visitor/PrinterHelper.java index a43d2fad8a5..1ff64a4427c 100644 --- a/src/main/java/spoon/reflect/visitor/PrinterHelper.java +++ b/src/main/java/spoon/reflect/visitor/PrinterHelper.java @@ -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; @@ -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();