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

fix: Fix crash on null type binding in nested type during search #3961

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ static TypeBinding searchTypeBinding(String qualifiedName, CompilationUnitDeclar
for (CompilationUnitDeclaration unitToProcess : unitsToProcess) {
if (unitToProcess.types != null) {
for (TypeDeclaration type : unitToProcess.types) {
if (type.binding != null
&& qualifiedName.equals(CharOperation.toString(type.binding.compoundName))) {
if (qualifiedNameEqualsBindingCompoundName(qualifiedName, type)) {
return type.binding;
}
if (type.memberTypes != null) {
for (TypeDeclaration memberType : type.memberTypes) {
if (qualifiedName.equals(CharOperation.toString(memberType.binding.compoundName))) {
if (qualifiedNameEqualsBindingCompoundName(qualifiedName, memberType)) {
return type.binding;
}
}
Expand All @@ -92,6 +91,13 @@ static TypeBinding searchTypeBinding(String qualifiedName, CompilationUnitDeclar
return null;
}

/**
* Compare the qualified name to the type binding's compound name.
*/
private static boolean qualifiedNameEqualsBindingCompoundName(String qualifiedName, TypeDeclaration type) {
return type.binding != null && qualifiedName.equals(CharOperation.toString(type.binding.compoundName));
}

/**
* Searches a type declared in imports.
*
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/model/TypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void testMultiClassEnableWithStaticFieldAccessToNestedType() {
CtModel model = launcher.buildModel();

Set<String> expectedTypeNames = new HashSet<>(
Arrays.asList("duplicates.Duplicate", "duplicates.Main", "duplicates.WithNestedEnum"));
Arrays.asList("AlsoWithNestedEnum", "duplicates.Duplicate", "duplicates.Main", "duplicates.WithNestedEnum"));
Set<String> typeNames = model.getAllTypes().stream().map(CtType::getQualifiedName).collect(Collectors.toSet());

assertEquals(expectedTypeNames, typeNames);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;

/**
* This class contains a nested type, is duplicated across the two modules in this project, and
* starts with an A, which places it at the top of the compilation units as these are sorted
* lexicographically for each module by JDT. At the time of writing this test file, this causes
* the nested type's type binding to be checked in
* {@link spoon.support.compiler.jdt.JDTTreeBuilderQuery#searchTypeBinding(String, CompilationUnitDeclaration[])},
* which if done without a null check causes a crash due to the type duplication making the binding
* null in one case.
*/
public class AlsoWithNestedEnum {
public enum SomeEnum {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* See the comment in ../moduleA/duplicates/AlsoWithNestedEnum.java.
*/
public class AlsoWithNestedEnum {
public enum SomeEnum {

}
}