Skip to content

fix: NPE in JDTCommentBuilder#2822

Merged
pvojtechovsky merged 2 commits intoINRIA:masterfrom
Egor18:fixNPE
Dec 7, 2018
Merged

fix: NPE in JDTCommentBuilder#2822
pvojtechovsky merged 2 commits intoINRIA:masterfrom
Egor18:fixNPE

Conversation

@Egor18
Copy link
Copy Markdown
Contributor

@Egor18 Egor18 commented Dec 6, 2018

Hello.
I was trying to migrate from spoon 7.0 to 7.2, and encountered some mode build failures due to the recent changes. (We have a large test base with ~80 open-source projects).

Spoon 7.2 failed to build a model with NPE for the following code (it's from sakai open-source project):

if (itemGrading.getAttemptsRemaining() == null){
    // this is not possible unless the question is submitted without the
    // attempt set correctly
    if ((item.getTriesAllowed()).intValue() >= 9999)
      attemptsRemaining = 9999;  
} else{
  if ((item.getTriesAllowed()).intValue() >= 9999 )
    attemptsRemaining = 9999;
  else if (itemGrading.getAttemptsRemaining().intValue() > 0);
  // We're now getting the applet to tell us how many attempts remain
  // attemptsRemaining = itemGrading.getAttemptsRemaining().intValue() - 1;
  else
    throw new Exception("This page must have been reached by mistake. Our record shows that no more attempt for this question is allowed.");
}

Stacktrace:

[INFO] java.lang.NullPointerException
[INFO] 	at spoon.support.compiler.jdt.JDTCommentBuilder$1.visitCtIf(JDTCommentBuilder.java:376)
[INFO] 	at spoon.support.reflect.code.CtIfImpl.accept(CtIfImpl.java:45)
[INFO] 	at spoon.reflect.visitor.CtInheritanceScanner.scan(CtInheritanceScanner.java:184)
[INFO] 	at spoon.support.compiler.jdt.JDTCommentBuilder$1.scan(JDTCommentBuilder.java:233)
[INFO] 	at spoon.support.compiler.jdt.JDTCommentBuilder.insertCommentInAST(JDTCommentBuilder.java:433)
[INFO] 	at spoon.support.compiler.jdt.JDTCommentBuilder.buildComment(JDTCommentBuilder.java:147)
[INFO] 	at spoon.support.compiler.jdt.JDTCommentBuilder.build(JDTCommentBuilder.java:111)
[INFO] 	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.lambda$buildModel$2(JDTBasedSpoonCompiler.java:443)
[INFO] 	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.forEachCompilationUnit(JDTBasedSpoonCompiler.java:457)
[INFO] 	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildModel(JDTBasedSpoonCompiler.java:442)
[INFO] 	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:375)
[INFO] 	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:340)
[INFO] 	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:123)
[INFO] 	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:106)
[INFO] 	at spoon.Launcher.buildModel(Launcher.java:772)
[INFO] 	at com.pvsstudio.projects.Module.build(Module.java:283)
[INFO] 	at com.pvsstudio.runner.ModuleWorker.run(ModuleWorker.java:41)
[INFO] 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[INFO] 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[INFO] 	at java.lang.Thread.run(Thread.java:748)

In general spoon 7.2 fails to build model for the following code:

if (false)
    System.out.println("hello");
else if (false);
    // Do nothing...
else
    System.out.println("world");

I think this issue is related to that commit: 7f29a81

So in this PR I provide failing test case and a possible solution (we need to check if then part is null, even if the else part is not null).

@pvojtechovsky
Copy link
Copy Markdown
Collaborator

Thank you Egor!

Could you please simplify the already complex and long conditions. E.g. by creating variables for thenExpression?

I think this issue is related to that commit: 7f29a81

no, it is related to #2733

@pvojtechovsky pvojtechovsky merged commit 7c77b77 into INRIA:master Dec 7, 2018
@pvojtechovsky
Copy link
Copy Markdown
Collaborator

Thank you!

@Egor18 Egor18 deleted the fixNPE branch June 15, 2019 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants