diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/java/exceptions/UnresolvedTypeException.java b/components/sbm-core/src/main/java/org/springframework/sbm/java/exceptions/UnresolvedTypeException.java new file mode 100644 index 000000000..cfbd8ae81 --- /dev/null +++ b/components/sbm-core/src/main/java/org/springframework/sbm/java/exceptions/UnresolvedTypeException.java @@ -0,0 +1,7 @@ +package org.springframework.sbm.java.exceptions; + +public class UnresolvedTypeException extends RuntimeException { + public UnresolvedTypeException(String message) { + super(message); + } +} diff --git a/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/ProjectJavaSourcesImpl.java b/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/ProjectJavaSourcesImpl.java index 76e4d1743..e6b659fc1 100644 --- a/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/ProjectJavaSourcesImpl.java +++ b/components/sbm-core/src/main/java/org/springframework/sbm/java/impl/ProjectJavaSourcesImpl.java @@ -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; @@ -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; @@ -143,28 +143,29 @@ public List 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) { diff --git a/components/sbm-core/src/test/java/org/springframework/sbm/java/impl/ProjectJavaSourcesImplTest.java b/components/sbm-core/src/test/java/org/springframework/sbm/java/impl/ProjectJavaSourcesImplTest.java index 5d3b01079..7feb8591d 100644 --- a/components/sbm-core/src/test/java/org/springframework/sbm/java/impl/ProjectJavaSourcesImplTest.java +++ b/components/sbm-core/src/test/java/org/springframework/sbm/java/impl/ProjectJavaSourcesImplTest.java @@ -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 @@ -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 { + } + """; + + ProjectJavaSources javaSource = TestProjectContext.buildProjectContext() + .withJavaSources(sourceCode) + .build() + .getProjectJavaSources(); -} \ No newline at end of file + assertThrows(UnresolvedTypeException.class, () -> + javaSource.findTypesImplementing("a.b.c.K")); + + } +}