Skip to content

Instantly share code, notes, and snippets.

@oowekyala oowekyala/MWE.java
Last active Jul 31, 2019

Embed
What would you like to do?
import spoon.Launcher;
import spoon.reflect.declaration.CtClass;
import spoon.support.sniper.SniperJavaPrettyPrinter;
public class MWE {
/*
<dependency>
<groupId>fr.inria.gforge.spoon</groupId>
<artifactId>spoon-core</artifactId>
<version>7.6.0-beta-1</version>
</dependency>
*/
public static void main(String[] args) {
final Launcher spoon = new Launcher();
// This one throws an NPE:
// CtClass<?> ctClass = Launcher.parseClass("class Foo { private int[] arr = new int[3]; { for (int i = 0; i < 3; i++) { arr[i] = 5; }}}");
// This one throws a SpoonException: The element of class class spoon.support.reflect.code.CtForImpl does not have CtRole.STATEMENT
// The snippet is the same except there are no braces around the loop body
CtClass<?> ctClass = Launcher.parseClass("class Foo { private int[] arr = new int[3]; { for (int i = 0; i < 3; i++) arr[i] = 5;}}");
spoon.getEnvironment().setPrettyPrinterCreator(() -> new SniperJavaPrettyPrinter(spoon.getEnvironment()));
// this statement causes the exception
// stack traces below
spoon.createOutputWriter().createJavaFile(ctClass);
}
}
/* Stack traces:
Exception in thread "main" java.lang.NullPointerException
at spoon.support.sniper.internal.ElementSourceFragment.forEachConstantFragment(ElementSourceFragment.java:664)
at spoon.support.sniper.internal.ElementSourceFragment.getChildrenFragments(ElementSourceFragment.java:464)
at spoon.support.sniper.SniperJavaPrettyPrinter.printTypes(SniperJavaPrettyPrinter.java:146)
at spoon.reflect.visitor.DefaultJavaPrettyPrinter.calculate(DefaultJavaPrettyPrinter.java:1984)
at spoon.support.sniper.SniperJavaPrettyPrinter.calculate(SniperJavaPrettyPrinter.java:105)
at spoon.support.JavaOutputProcessor.createJavaFile(JavaOutputProcessor.java:114)
at MWE.main(App.java:22)
Exception in thread "main" spoon.SpoonException: The element of class class spoon.support.reflect.code.CtForImpl does not have CtRole.STATEMENT
at spoon.reflect.meta.impl.RoleHandlerHelper.getRoleHandler(RoleHandlerHelper.java:55)
at spoon.support.sniper.internal.ElementSourceFragment.getRoleHandler(ElementSourceFragment.java:241)
at spoon.support.sniper.internal.ElementSourceFragment.addChild(ElementSourceFragment.java:181)
at spoon.support.sniper.internal.ElementSourceFragment.access$000(ElementSourceFragment.java:44)
at spoon.support.sniper.internal.ElementSourceFragment$1.enter(ElementSourceFragment.java:145)
at spoon.reflect.visitor.CtScanner.visitCtAssignment(CtScanner.java:270)
at spoon.support.reflect.code.CtAssignmentImpl.accept(CtAssignmentImpl.java:43)
at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:143)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:119)
at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:165)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:104)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:81)
at spoon.reflect.visitor.CtScanner.visitCtBlock(CtScanner.java:294)
at spoon.support.reflect.code.CtBlockImpl.accept(CtBlockImpl.java:55)
at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:143)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:119)
at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:165)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:104)
at spoon.reflect.visitor.CtScanner.visitCtFor(CtScanner.java:458)
at spoon.support.reflect.code.CtForImpl.accept(CtForImpl.java:38)
at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:143)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:119)
at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:165)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:104)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:81)
at spoon.reflect.visitor.CtScanner.visitCtBlock(CtScanner.java:294)
at spoon.support.reflect.code.CtBlockImpl.accept(CtBlockImpl.java:55)
at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:143)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:119)
at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:165)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:104)
at spoon.reflect.visitor.CtScanner.visitCtAnonymousExecutable(CtScanner.java:221)
at spoon.support.reflect.declaration.CtAnonymousExecutableImpl.accept(CtAnonymousExecutableImpl.java:35)
at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:143)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:119)
at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:165)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:104)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:81)
at spoon.reflect.visitor.CtScanner.visitCtClass(CtScanner.java:330)
at spoon.support.reflect.declaration.CtClassImpl.accept(CtClassImpl.java:56)
at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:143)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:119)
at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:165)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:104)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:81)
at spoon.reflect.visitor.EarlyTerminatingScanner.visitCtCompilationUnit(EarlyTerminatingScanner.java:158)
at spoon.support.reflect.declaration.CtCompilationUnitImpl.accept(CtCompilationUnitImpl.java:406)
at spoon.reflect.visitor.EarlyTerminatingScanner.doScan(EarlyTerminatingScanner.java:143)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:119)
at spoon.reflect.visitor.CtScanner.scan(CtScanner.java:165)
at spoon.reflect.visitor.EarlyTerminatingScanner.scan(EarlyTerminatingScanner.java:104)
at spoon.support.sniper.internal.ElementSourceFragment.addTreeOfSourceFragmentsOfElement(ElementSourceFragment.java:169)
at spoon.support.reflect.declaration.CtCompilationUnitImpl.getOriginalSourceFragment(CtCompilationUnitImpl.java:358)
at spoon.support.sniper.SniperJavaPrettyPrinter.printTypes(SniperJavaPrettyPrinter.java:145)
at spoon.reflect.visitor.DefaultJavaPrettyPrinter.calculate(DefaultJavaPrettyPrinter.java:1984)
at spoon.support.sniper.SniperJavaPrettyPrinter.calculate(SniperJavaPrettyPrinter.java:105)
at spoon.support.JavaOutputProcessor.createJavaFile(JavaOutputProcessor.java:114)
at MWE.main(App.java:22)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.