eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
161 stars 130 forks source link

JDT Core throws NullPointerException: Cannot invoke "org.eclipse.jdt.internal.compiler.lookup.TypeBinding.leafComponentType()" because "this.resolvedType" is null #2399

Closed r0texx closed 5 months ago

r0texx commented 5 months ago

Hello,

I'm trying to build a source, but JDT Core throws an exception:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.eclipse.jdt.internal.compiler.lookup.TypeBinding.leafComponentType()" because "this.resolvedType" is null
    at org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference.updateWithAnnotations(ParameterizedQualifiedTypeReference.java:549)
    at org.eclipse.jdt.internal.compiler.ast.TypeReference.updateParameterizedTypeWithAnnotations(TypeReference.java:679)
    at org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference.updateWithAnnotations(ParameterizedQualifiedTypeReference.java:547)
    at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.updateWithAnnotations(TypeDeclaration.java:1938)
    at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.updateSupertypesWithAnnotations(TypeDeclaration.java:1920)
    at org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope.integrateAnnotationsInHierarchy(CompilationUnitScope.java:418)
    at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment$CompleteTypeBindingsSteps.perform(LookupEnvironment.java:176)
    at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.completeTypeBindings(LookupEnvironment.java:546)
    at org.eclipse.jdt.core.dom.CompilationUnitResolver.beginToCompile(CompilationUnitResolver.java:275)
    at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:1107)
    at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java:739)
    at org.eclipse.jdt.core.dom.ASTParser.createASTs(ASTParser.java:1049)

For the following test source (save it to a directory and change the ROOT variable):

package com.test;

public class TestClass implements TestClass.Missing1<TestClass.Missing2<TestClass.Missing3>> {
}

I use the following code to parse it using JDT Core:

package main;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.FileASTRequestor;

public class PoCMain {
    private static final File ROOT = new File("PATH_WHERE_YOU_UNPACK_com.test.TestClass");

    public static void main(String[] args) throws Throwable {
        List<String> files = findSources(ROOT, ".java");
        getParser().createASTs(files.toArray(new String[files.size()]), null, new String[0], new FileASTRequestor() {}, null);
    }

    private static ASTParser getParser() {
        ASTParser astParser = ASTParser.newParser(AST.JLS21);
        astParser.setResolveBindings(true);
        astParser.setStatementsRecovery(true);
        astParser.setBindingsRecovery(true);
        astParser.setKind(ASTParser.K_COMPILATION_UNIT);
        astParser.setEnvironment(new String[0], new String[0], null, false);

        Map<String, String> options = JavaCore.getDefaultOptions();
        options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_21);
        options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_21);
        options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_21);
        astParser.setCompilerOptions(options);
        return astParser;
    }

    private static List<String> findSources(File dir, String ext) {
        List<String> paths = new ArrayList<>();
        for (File child : dir.listFiles()) {
            if (child.isFile()) {
                if (child.getName().endsWith(ext)) {
                    paths.add(child.getAbsolutePath());
                }
            } else {
                paths.addAll(findSources(child, ext));
            }
        }

        return paths;
    }
}
srikanth-sankaran commented 5 months ago

@stephan-herrmann : Just seeing integrateAnnotationsInHierarchy on the stack trace, assigning this to you. If your analysis finds the trouble originating elsewhere reassign suitably,

stephan-herrmann commented 5 months ago

Same root cause as #2386

stephan-herrmann commented 5 months ago

Resolved via #2407, although in the end I added two individual null checks, one for this issue and one for #2386.

I also checked the direct vicinity and found no other potential NPE.

Thanks, @r0texx for reporting both cases.

Actually I didn't need the PoCMain, since regular compilation sufficed for reproducing NPE.

thomas2123 commented 1 month ago

I have a similar problem with eclipse for Java, I tried a lot of things, in the end what worked I rolled back to 2023-12 image

stephan-herrmann commented 1 month ago

@thomas2123 this bug has been resolved for 2024-06. In fact what you show concerns a different code location. So, if you can provide additional information (ideally a reproducing sample project, minimally the stack trace of that error), then please file a new issue. Thanks.