diff --git a/src/test/java/spoon/test/processing/ProcessingTest.java b/src/test/java/spoon/test/processing/ProcessingTest.java index 0687f2f01d9..85dce429ae4 100644 --- a/src/test/java/spoon/test/processing/ProcessingTest.java +++ b/src/test/java/spoon/test/processing/ProcessingTest.java @@ -64,6 +64,26 @@ public class ProcessingTest { + @Test + public void testUnknownSourceWithLambdaInsideInnerClass() throws Exception { + CtClassProcessor myProcessor = new CtClassProcessor(){ + @Override + public void process(CtClass element) { + super.process(element); + assertNotNull(element.getPosition()); + assertNotNull(element.getPosition().getFile()); + } + }; + + Launcher launcher = new Launcher(); + + launcher.addInputResource("./src/test/resources/noclasspath/lambdas/UnknownSourceLambdaInnerClass.java"); + launcher.getEnvironment().setNoClasspath(true); + launcher.buildModel(); + launcher.addProcessor(myProcessor); + launcher.process(); + } + @Test public void testInsertBegin() throws Exception { CtClass type = build("spoon.test.processing.testclasses", "SampleForInsertBefore"); diff --git a/src/test/resources/noclasspath/lambdas/UnknownSourceLambdaInnerClass.java b/src/test/resources/noclasspath/lambdas/UnknownSourceLambdaInnerClass.java new file mode 100644 index 00000000000..cf35d694195 --- /dev/null +++ b/src/test/resources/noclasspath/lambdas/UnknownSourceLambdaInnerClass.java @@ -0,0 +1,29 @@ +public class Test { + /** + * This class will be built with an '(Unknown source)' as position + */ + static class Failing { + ClickListener listener; + } + + private static class ExtendedClass extends Failing { + public void test(View itemView) { + // The difference in behaviour happens if I comment this line out + itemView.setOnClickListener(v -> listener.onItemClick(getAdapterPosition())); + } + } + +// TODO: I wanted to add a class showing a successful input in the processor but adding it raise another error from JDTTreeBuilder, if you could have a look :) + /** + * This class will be built with the right position + */ +// static class Success { +// ClickListener listener; +// } +// +// private static class ExtendedClass extends Success { +// public void test(View itemView) { +// itemView.setOnClickListener(new Runnable()); +// } +// } +} \ No newline at end of file