Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Groovy: compiler error: exception in phase 'semantic analysis' in source unit 'xxx.groovy' unexpected NullPointerException #1268

Closed
EKarnerRmd opened this issue Jun 15, 2021 · 6 comments
Assignees
Labels
Milestone

Comments

@EKarnerRmd
Copy link

In Eclipse 2021-03 and Eclipse Groovy Development Tools 4.2.0.v202106140218-e2103 i get this error

eclipse.buildId=4.19.0.I20210303-1800
java.version=15.0.1
java.vendor=AdoptOpenJDK
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=de_AT
Framework arguments: -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product

org.eclipse.jdt.groovy.core
Error
Tue Jun 15 13:38:11 CEST 2021
Groovy compiler error

BUG! exception in phase 'semantic analysis' in source unit '/filepath/filenname.groovy' unexpected NullPointerException
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1135)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:635)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:614)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:591)
at org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.processToPhase(GroovyCompilationUnitDeclaration.java:226)
at org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.resolve(GroovyCompilationUnitDeclaration.java:609)
at org.eclipse.jdt.internal.core.hierarchy.HierarchyResolver.resolve(HierarchyResolver.java:882)
at org.eclipse.jdt.internal.core.hierarchy.IndexBasedHierarchyBuilder.buildForProject(IndexBasedHierarchyBuilder.java:266)
at org.eclipse.jdt.internal.core.hierarchy.IndexBasedHierarchyBuilder.buildFromPotentialSubtypes(IndexBasedHierarchyBuilder.java:377)
at org.eclipse.jdt.internal.core.hierarchy.IndexBasedHierarchyBuilder.build(IndexBasedHierarchyBuilder.java:167)
at org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy.compute(TypeHierarchy.java:323)
at org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy.refresh(TypeHierarchy.java:1319)
at org.eclipse.jdt.internal.core.CreateTypeHierarchyOperation.executeOperation(CreateTypeHierarchyOperation.java:94)
at org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:740)
at org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:806)
at org.eclipse.jdt.internal.core.SourceType.newTypeHierarchy(SourceType.java:950)
at org.eclipse.jdt.internal.core.SourceType.newTypeHierarchy(SourceType.java:907)
at org.eclipse.jdt.internal.ui.javaeditor.codemining.JavaImplementationCodeMining.countTypeImplementations(JavaImplementationCodeMining.java:165)
at org.eclipse.jdt.internal.ui.javaeditor.codemining.JavaImplementationCodeMining.lambda$0(JavaImplementationCodeMining.java:83)
at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1800)
at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1792)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1016)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1665)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1598)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Caused by: java.lang.NullPointerException: Cannot invoke "org.codehaus.groovy.ast.FieldNode.getType()" because the return value of "org.codehaus.groovy.ast.ClassNode.getDeclaredField(String)" is null
at org.codehaus.groovy.classgen.InnerClassVisitor.insertThis0ToSuperCall(InnerClassVisitor.java:309)
at org.codehaus.groovy.classgen.InnerClassVisitor.passThisReference(InnerClassVisitor.java:287)
at org.codehaus.groovy.classgen.InnerClassVisitor.visitConstructorCallExpression(InnerClassVisitor.java:141)
at org.codehaus.groovy.ast.expr.ConstructorCallExpression.visit(ConstructorCallExpression.java:46)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitReturnStatement(CodeVisitorSupport.java:124)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitReturnStatement(ClassCodeVisitorSupport.java:302)
at org.codehaus.groovy.ast.stmt.ReturnStatement.visit(ReturnStatement.java:49)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:86)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:256)
at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:164)
at org.codehaus.groovy.classgen.InnerClassVisitor.visitConstructorOrMethod(InnerClassVisitor.java:110)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:185)
at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1207)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:56)
at org.codehaus.groovy.classgen.InnerClassVisitor.visitClass(InnerClassVisitor.java:80)
at org.codehaus.groovy.control.CompilationUnit$9.call(CompilationUnit.java:222)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1130)
... 25 more

@eric-milles
Copy link
Member

Would it be possible to isolate this error to a small piece of code? It has to do with an inner class somewhere, which could also be a closure or anonymous inner. I'm thinking it's an anon. inner because the stack trace has visitConstructorCallExpression just before the InnerClassVisitor.

Also, what version of the Groovy Compiler are you running?

@EKarnerRmd
Copy link
Author

EKarnerRmd commented Jun 16, 2021

I use Groovy Compiler 2.5.4 and spock-core version 1.3-groovy-2.5.

package at.example

import at.example.service.logging.extractor.LogDataExtractor
import at.example.service.logging.extractor.MessageToLogDataConverter
import at.example.logging.UnicLogConstants
import at.example.logging.LogRecord
import org.apache.cxf.binding.soap.SoapMessage
import org.apache.cxf.message.ExchangeImpl
import org.apache.cxf.message.MessageImpl
import org.aspectj.lang.ProceedingJoinPoint
import org.joda.time.DateTimeUtils
import org.slf4j.MDC
import spock.lang.Specification

class LogDataExtractorTest extends Specification {

  final def RELEASE_NUMBER = "5.18.x"

  final def CURRENT_TIME = 4815162342L

  final def TRANSACTION_ID = UUID.randomUUID().toString();

  final def DEFAULT_MESSAGE_XML = "<request />"

  final def SOAP_ACTION = "checkRequestedServices"

  def soapMessage = setupMessage()

  LogDataExtractor<Message, TestLogRecord> logDataExtractor = new LogDataExtractor<Message, TestLogRecord>(Message) {

    @Override
    protected MessageToLogDataConverter<Message, TestLogRecord> getConverter() {
      return new MessageToLogDataConverter<Message, TestLogRecord>() {
            @Override
            TestLogRecord extractLogDataFromMessage(String messageXml, Message convertedMessage, ProceedingJoinPoint pjp) {
              return new TestLogRecord()
            }
          }
    }
  }

  def setup() {
    logDataExtractor.releaseNumber = RELEASE_NUMBER
    DateTimeUtils.setCurrentMillisFixed(CURRENT_TIME)
    MDC.put(UnicLogConstants.TRANSACTION_ID, TRANSACTION_ID)
  }

  def "Throws ClassCastException if message does not correspond to the extractors type argument"() {
    given:
    InvalidMessage invalidUnmarshalledMessage = new InvalidMessage()

    when:
    logDataExtractor.extractLogRecord(DEFAULT_MESSAGE_XML, invalidUnmarshalledMessage, null)

    then:
    thrown(ClassCastException)
  }

  def "LogRecord contains release number"() {
    given:
    Message unmarshalledMessage = new Message()

    when:
    LogRecord logRecord = logDataExtractor.extractLogRecord(DEFAULT_MESSAGE_XML, unmarshalledMessage, null)

    then:
    logRecord.releaseNumber == RELEASE_NUMBER
  }

  def "LogRecord contains transactionId()"() {
    given:
    Message unmarshalledMessage = new Message()

    when:
    LogRecord logRecord = logDataExtractor.extractLogRecord(DEFAULT_MESSAGE_XML, unmarshalledMessage, null)

    then:
    logRecord.transactionId == TRANSACTION_ID
  }

  SoapMessage setupMessage() {
    SoapMessage message = new SoapMessage(new MessageImpl(exchange: new ExchangeImpl()))
    message.put("SOAPAction", SOAP_ACTION)
    return message
  }

  private class Message {}

  private class InvalidMessage {}

  private class TestLogRecord extends at.example.logging.LogRecord {}
}

@eric-milles
Copy link
Member

The NPE happens when processing "new TestLogRecord()" within the LogDataExtractor AIC. You can work around this by making TestLogRecord a static inner class. Indeed, Message and InvalidMessage could also be static inner classes since they make no reference to outer class members.

@eric-milles eric-milles added this to the v4.2.0 milestone Jun 16, 2021
@eric-milles eric-milles self-assigned this Jun 16, 2021
@EKarnerRmd
Copy link
Author

Hi!

Changig the inner classes helped.
I just want to mention that this NullpointerException was not there with

Groovy Compiler 2.5.14
Eclipe 2020-12 (4.18.0)
Eclipse Groovy Development Tools 4.0.0.v202012311456-e2012-RELEASE

Thanks

@eric-milles
Copy link
Member

I have a fix building. The GROOVY-8104 fix introduced this NPE.

@eric-milles
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants