Skip to content

[apex] Update Apex compiler - #489

Merged
jsotuyod merged 6 commits into
pmd:masterfrom
adangel:apex-update
Aug 12, 2017
Merged

jsotuyod merged 6 commits into
pmd:masterfrom
adangel:apex-update

Conversation

@adangel

@adangel adangel commented Jul 7, 2017 •

Copy link
Copy Markdown
Member

Since a year ago, the Salesforce IDE uses a new version of the jorje compiler jars, see commit 3083815933....
I tried to update the PMD integration. After adjusting to the API changes (e.g. new/renamed/deleted AST node types), I'm running into some problems:

The new compiler seem to have a bug when parsing the following Apex snippet, which contains SOQL:

public class Foo {
public List<SObject> test1() {
       return Database.query("Select Id from Account LIMIT 100");
    }
}

The stacktrace is this:

java.lang.NullPointerException
	at apex.jorje.semantic.ast.AstNodeFactory.createExpressions(AstNodeFactory.java:443)
	at apex.jorje.semantic.ast.expression.MethodCallExpression.<init>(MethodCallExpression.java:95)
	at apex.jorje.semantic.ast.AstNodeFactory$2._case(AstNodeFactory.java:249)
	at apex.jorje.semantic.ast.AstNodeFactory$2._case(AstNodeFactory.java:186)
	at apex.jorje.data.ast.Expr$MethodCallExpr.match(Expr.java:1275)
	at apex.jorje.semantic.ast.AstNodeFactory.create(AstNodeFactory.java:185)
	at apex.jorje.semantic.ast.statement.ReturnStatement.lambda$new$0(ReturnStatement.java:50)
	at java.util.Optional.map(Optional.java:215)
	at apex.jorje.semantic.ast.statement.ReturnStatement.<init>(ReturnStatement.java:50)
	at apex.jorje.semantic.ast.AstNodeFactory$1._case(AstNodeFactory.java:77)
	at apex.jorje.semantic.ast.AstNodeFactory$1._case(AstNodeFactory.java:60)
	at apex.jorje.data.ast.Stmnt$ReturnStmnt.match(Stmnt.java:546)
	at apex.jorje.semantic.ast.AstNodeFactory.create(AstNodeFactory.java:59)
	at apex.jorje.semantic.ast.AstNodeFactory.lambda$createStatements$2(AstNodeFactory.java:450)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
	at apex.jorje.semantic.ast.AstNodeFactory.createStatements(AstNodeFactory.java:451)
	at apex.jorje.semantic.ast.statement.BlockStatement$Builder$1._case(BlockStatement.java:266)
	at apex.jorje.semantic.ast.statement.BlockStatement$Builder$1._case(BlockStatement.java:263)
	at apex.jorje.data.ast.Stmnt$BlockStmnt.match(Stmnt.java:418)
	at apex.jorje.semantic.ast.statement.BlockStatement$Builder.lambda$setStmnt$1(BlockStatement.java:262)
	at apex.jorje.semantic.ast.statement.BlockStatement.<init>(BlockStatement.java:69)
	at apex.jorje.semantic.ast.statement.BlockStatement.<init>(BlockStatement.java:48)
	at apex.jorje.semantic.ast.statement.BlockStatement$Builder.build(BlockStatement.java:292)
	at apex.jorje.semantic.ast.AstNodeFactory$1._case(AstNodeFactory.java:67)
	at apex.jorje.semantic.ast.AstNodeFactory$1._case(AstNodeFactory.java:60)
	at apex.jorje.data.ast.Stmnt$BlockStmnt.match(Stmnt.java:418)
	at apex.jorje.semantic.ast.AstNodeFactory.create(AstNodeFactory.java:59)
	at apex.jorje.semantic.ast.member.MethodFactory.lambda$createUserMethod$0(MethodFactory.java:71)
	at java.util.Optional.map(Optional.java:215)
	at apex.jorje.semantic.ast.member.MethodFactory.createUserMethod(MethodFactory.java:71)
	at apex.jorje.semantic.ast.member.MethodFactory.createUserMethod(MethodFactory.java:51)
	at apex.jorje.semantic.ast.compilation.UserClassMembers$Builder._case(UserClassMembers.java:308)
	at apex.jorje.data.ast.BlockMember$MethodMember._switch(BlockMember.java:374)
	at apex.jorje.semantic.ast.compilation.UserClassMembers.create(UserClassMembers.java:191)
	at apex.jorje.semantic.ast.compilation.UserClass.<init>(UserClass.java:88)
	at apex.jorje.semantic.ast.AstNodeFactory$3._case(AstNodeFactory.java:392)
	at apex.jorje.semantic.ast.AstNodeFactory$3._case(AstNodeFactory.java:374)
	at apex.jorje.data.ast.CompilationUnit$ClassDeclUnit.match(CompilationUnit.java:371)
	at apex.jorje.semantic.ast.AstNodeFactory.create(AstNodeFactory.java:373)
	at apex.jorje.semantic.compiler.CodeUnit.parse(CodeUnit.java:85)
	at apex.jorje.semantic.compiler.CompilerStage$1.invoke(CompilerStage.java:38)
	at apex.jorje.semantic.compiler.ApexCompiler.processNext(ApexCompiler.java:164)
	at apex.jorje.semantic.compiler.ApexCompiler.compileInternal(ApexCompiler.java:98)
	at apex.jorje.semantic.compiler.ApexCompiler.compile(ApexCompiler.java:86)
	at net.sourceforge.pmd.lang.apex.ast.CompilerService.compile(CompilerService.java:93)
	at net.sourceforge.pmd.lang.apex.ast.CompilerService.visitAstsFromStrings(CompilerService.java:87)
	at net.sourceforge.pmd.lang.apex.ast.CompilerService.visitAstFromString(CompilerService.java:75)
	at net.sourceforge.pmd.lang.apex.ast.ApexParser.parseApex(ApexParser.java:42)
	at net.sourceforge.pmd.lang.apex.ast.ApexParser.parse(ApexParser.java:51)
	at net.sourceforge.pmd.lang.apex.ast.ApexCompilerSoqlTest.testSoqlCompilation(ApexCompilerSoqlTest.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

I've created the test class ApexCompilerSoqlTest, which tries to parse/compile this code snippet.
Only parsing works without an error (see ApexLexerTest where I lex and parse the snippet).

Before submitting a PR, please check that:

  • The PR is submitted against master. The PMD team will merge back to support branches as needed.
  • mvn test passes.
  • mvn checkstyle:check passes. Check this for more info

adangel added 2 commits July 7, 2017 10:54
There are some AST changes:
* no DottedExpression anymore
* NewNameValueObjectExpression has been renamed to NewKeyValueObjectExpression
* no TestNode anymore
* new types: CastExpression, ConstructorPreamble, IllegalStoreExpression,
  MethodBlockStatement, Modifier, MultiStatement, NestedExpression,
  NestedSToreExpression, StatementExecuted
The compiler seems to have a problem with Soql expressions
See ApexCompilerSoqlTest
@adangel adangel added an:enhancement An improvement on existing features / rules is:WIP For PRs that are not fully ready, or issues that are actively being tackled labels Jul 7, 2017
@adangel

adangel commented Jul 7, 2017

Copy link
Copy Markdown
Member Author

@sergeygorbaty @up2go-rsoesemann
Could you have a look at this? Any ideas, why the jorje compiler would fail on the given code snippet?

@adangel

adangel commented Jul 8, 2017

Copy link
Copy Markdown
Member Author

I've verified the same code now with Salesforce IDE and it fails with the same NullPointer. The effect is, that the Outline View is not updated anymore with the changes made to the class. You can see the stacktrace of the exception in the Error Log View of eclipse.

@jsotuyod

jsotuyod commented Jul 8, 2017

Copy link
Copy Markdown
Member

Ok, seems this is not going anywhere for the time being. Did you report this issue back to them? https://github.com/forcedotcom/idecore/issues

@rsoesemann

Copy link
Copy Markdown
Member

Sorry @adangel for not responding. I have create an issue here https://github.com/forcedotcom/idecore/issues/227 and will moderate when answers com in.

@adangel

adangel commented Aug 2, 2017

Copy link
Copy Markdown
Member Author

@up2go-rsoesemann Thanks for creating the issue upstream!

@rsoesemann

Copy link
Copy Markdown
Member

@adangel it fails to parse your snippet because its invalid Apex. It took me a while to see it. But Apex uses single quotes instead of double quotes.

Replace

public class Foo {
public List<SObject> test1() {
       return Database.query("Select Id from Account LIMIT 100");
    }
}

with


public class Foo {
public List<SObject> test1() {
       return Database.query('Select Id from Account LIMIT 100');
    }
}

and it should work.

@adangel

adangel commented Aug 7, 2017

Copy link
Copy Markdown
Member Author

@up2go-rsoesemann
OK, good to know. Thanks for the clarification. I'll verify this, once I'm back. Btw: I took this snippet from one of test codes for the apex rules...

Note: the test case in VariableNamingConvetion with a
custom annotation type is not relevant
for apex, since there are no custom annotations.
The new parser seems to always return a BlockStatement, regardless
whether it's a real block or just an expression statement.
@adangel

adangel commented Aug 12, 2017

Copy link
Copy Markdown
Member Author

@up2go-rsoesemann It works now. Using the single quotes indeed fixed it. I've deleted one test case, which created a custom annotation type, which is not possible with Apex, or is it?
There were also slight differences in how the new parser builds the AST: Previously BlockStatement was created only when it really was a block (e.g. using curly braces), but now always BlockStatement is used (instead of ExpressionStatement). This made is much more difficult to fix the braces ruleset, but it seems to be working now.

@adangel adangel removed the is:WIP For PRs that are not fully ready, or issues that are actively being tackled label Aug 12, 2017
@adangel adangel added this to the 6.0.0 milestone Aug 12, 2017
@adangel adangel changed the title [apex] Update apex compiler [apex] Update Apex compiler Aug 12, 2017
@jsotuyod jsotuyod self-assigned this Aug 12, 2017
@jsotuyod
jsotuyod merged commit 58e52a5 into pmd:master Aug 12, 2017
@jsotuyod

Copy link
Copy Markdown
Member

Merged! Amazing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

an:enhancement An improvement on existing features / rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants