eclipse-aspectj / aspectj

Other
303 stars 86 forks source link

Binary type being null resulting in a NullPointerException #171

Closed Cibor closed 2 years ago

Cibor commented 2 years ago

Hi,

In our project based on Java 11 I'm getting this error during compilation

Internal compiler error: java.lang.Exception: java.lang.IllegalStateException: Error processing configuration meta-data on com.mycomnpany.PrdbProperties at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:172) [ERROR]

I debugged the compilation and discovered the root cause. In LookupEnvironment there is this piece of code (line 1050) inside the method createBinaryTypeFrom()

    if (cachedType != null && !cachedType.isUnresolvedType()) {
        if (cachedType.isBinaryBinding()) // sanity check... at this point the cache should ONLY contain unresolved types
            return (BinaryTypeBinding) cachedType;
        // it is possible with a large number of source files (exceeding AbstractImageBuilder.MAX_AT_ONCE) that a member type can be in the cache as an UnresolvedType,
        // but because its enclosingType is resolved while its created (call to BinaryTypeBinding constructor), its replaced with a source type
        return null;
    }

In my case cachedType is resolved but it isn't binary (whatever that means). So the method returns a null. The consequence is that after returning from this method to AjLookupEnvironment when this line is executed

        ContextToken tok = CompilationAndWeavingContext.enteringPhase(CompilationAndWeavingContext.WEAVING_INTERTYPE_DECLARATIONS,
                sourceType.sourceName);

the sourceType variable is null so a NullPointerException is thrown.

I am not sure what is happening, especially what a binary type is. Is there a quick workaround that I could apply to my project to make this error go away?

There seems to be nothing particular about the PrdbProperties class. A POJO containing the following annotations

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
kriegaex commented 2 years ago

Thanks for the detailed error description. What would really help the most, is a reproducer. Maybe a little GitHub project, ideally Maven, but whatever helps me to reproduce this is fine.

Cibor commented 2 years ago

For now through trial and error I discovered that it was connected with this annotation

@ConfigurationProperties(prefix = "bts-report.prdb")

Removing it fixed the compilation. Digging further I came accross issue Spring Boot #4825 and found out that in my case indeed adding

<configuration>
  <sources/>
</configuration>

to the Maven config of the aspectj-maven-plugin indeed fixed the problem.

kriegaex commented 2 years ago

Hm, that bug was fixed 5 years ago in JDT Core (the project containing ECJ), and hence it probably was fixed in AspectJ, too, because AJC forks and regularly refreshes JDT Core. I would really need a reproducer in order to assess whether we have a problem in AspectJ or not. Your XML snippet alone does not help much, because I have no idea what you are excluding from where and what the situation is.

I can only speculate, which I hate to do, because I could be completely wrong in assuming that you might be including an aspect designed for Spring AOP into an AspectJ compiler run, maybe leading to it being woven twice (once by AspectJ, then again by Spring AOP during runtime). I really cannot say.

If you would prepare an MCVE, we could both learn something. I would find out if there is an AspectJ problem, you could get an explanation what the real root cause of your problem is and whether your workaround is actually correct or simply sweeping a problem under the rug for now.

kriegaex commented 2 years ago

@Cibor, because you did not respond within one full month after I responded the very same day you created the issue, I am closing this issue as invalid. I will consider reopening it, if you provide the MCVE I requested.

kriegaex commented 9 months ago

Note to myself: This error occurs in the exact same line 172 of RoundDispatcher::handleProcessor as #195, which actually is a problem upstream in ECJ, see https://github.com/eclipse-jdt/eclipse.jdt.core/issues/565. The difference is that the other problem is a StackOverflowError, while here we see an IllegalStateException. So, the issues might be unrelated. But at least, they occur in the in the same part of the JDT Core code and both seem to be related to annotation processing in one way or another.