Skip to content

fix: Removed Nullpointer exception when a class is extended and the type is unresolved #773

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

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
@@ -0,0 +1,7 @@
package org.springframework.sbm.java.exceptions;

public class UnresolvedTypeException extends RuntimeException {
public UnresolvedTypeException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.springframework.sbm.java.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionContext;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.java.ChangeType;
Expand All @@ -27,6 +26,7 @@
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import org.springframework.sbm.java.api.*;
import org.springframework.sbm.java.exceptions.UnresolvedTypeException;
import org.springframework.sbm.java.filter.JavaSourceListFilter;
import org.springframework.sbm.java.refactoring.JavaGlobalRefactoring;
import org.springframework.sbm.project.resource.ProjectResourceSet;
Expand Down Expand Up @@ -143,28 +143,29 @@ public List<? extends JavaSource> findClassesUsingType(String fqName) {
}

private boolean hasTypeImplementing(J.ClassDeclaration c, String type) {
try {
return c.getImplements() != null &&
c.getImplements()
.stream()
.anyMatch(intaface -> {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(
intaface.getType()
return c.getImplements() != null &&
c.getImplements()
.stream()
.anyMatch(intaface -> {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(
intaface.getType()
);
if (fullyQualified == null) {

throw new UnresolvedTypeException(
String.format("Could not calculate if class '%s' implements an " +
"interface compatible to '%s'. Type of interface '%s' could not" +
" be resolved and was '%s'", c.getType(), type, intaface,
intaface.getType().toString())
);
if(fullyQualified == null && J.Identifier.class.isInstance(intaface)) {
log.error(String.format("Could not calculate if class '%s' implements an interface compatible to '%s'. Type of interface '%s' could not be resolved and was '%s'", c, type, intaface, intaface.getType().toString()));
return false;
}
return fullyQualified
.getFullyQualifiedName()
.equals(type);

}
);
return fullyQualified
.getFullyQualifiedName()
.equals(type);
}
);

} catch(Exception e) {
log.error("", e);
return false;
}
}

private Type getTypeForClassDecl(J.ClassDeclaration c) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
*/
package org.springframework.sbm.java.impl;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.java.api.JavaSourceAndType;
import org.springframework.sbm.java.api.ProjectJavaSources;
import org.springframework.sbm.java.exceptions.UnresolvedTypeException;
import org.springframework.sbm.project.resource.TestProjectContext;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* @author Fabian Krüger
Expand Down Expand Up @@ -94,5 +98,24 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
assertThat(typesImplementingInterface.get(0).getType().getFullyQualifiedName()).isEqualTo("com.example.TheClass");
}

@Test
void whenClassInheritsParameterizedInterfaceButNoResolvedType() {
@Language("java") String sourceCode =
"""
package a.b.c;
import a.b.c.K;

interface SomeClass extends K<String, String> {
}
""";

ProjectJavaSources javaSource = TestProjectContext.buildProjectContext()
.withJavaSources(sourceCode)
.build()
.getProjectJavaSources();

}
assertThrows(UnresolvedTypeException.class, () ->
javaSource.findTypesImplementing("a.b.c.K"));

}
}