Skip to content

Commit

Permalink
#394 AnnotationClassFilter no longer uses ctClass.getClassFile().getA…
Browse files Browse the repository at this point in the history
…ttributes() as this may throw a RuntimeException when CtClass is frozen
  • Loading branch information
siom79 committed Apr 21, 2024
1 parent 369daac commit 02d833e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

import java.util.List;

import static japicmp.test.util.Helper.getArchive;
import static japicmp.test.util.Helper.getJApiClass;
import static japicmp.test.util.Helper.getJApiMethod;
import static japicmp.test.util.Helper.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Test to ensure that annotation-based includes and excludes are working.
Expand All @@ -46,6 +44,8 @@ public void detectChangeOnAnnotatedClass() {
JarArchiveComparator jarArchiveComparator = new JarArchiveComparator(options);
final List<JApiClass> jApiClasses = jarArchiveComparator.compare(getArchive("japicmp-test-v1.jar"), getArchive("japicmp-test-v2.jar"));
Assert.assertEquals(3, jApiClasses.size());
Assert.assertTrue(jApiClasses.stream().anyMatch(jApiClass -> jApiClass.getFullyQualifiedName().contains(AnnotatedClass.class.getSimpleName())));
Assert.assertTrue(jApiClasses.stream().anyMatch(jApiClass -> jApiClass.getFullyQualifiedName().contains(ClassWithInnerClass.class.getSimpleName())));
Assert.assertEquals(JApiChangeStatus.MODIFIED, jApiClasses.get(0).getChangeStatus());
}

Expand Down
19 changes: 8 additions & 11 deletions japicmp/src/main/java/japicmp/filter/AnnotationClassFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import javassist.CtClass;
import javassist.NotFoundException;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -16,20 +15,18 @@ public AnnotationClassFilter(String filterString) {

@Override
public boolean matches(CtClass ctClass) {
List attributes = ctClass.getClassFile().getAttributes();
boolean hasAnnotation = hasAnnotation(attributes);
if (!hasAnnotation) {
try {
boolean matches = ctClass.hasAnnotation(annotationClassName);
if (!matches) {
try {
CtClass declaringClass = ctClass.getDeclaringClass();
if (declaringClass != null) {
attributes = declaringClass.getClassFile().getAttributes();
hasAnnotation = hasAnnotation(attributes);
matches = declaringClass.hasAnnotation(annotationClassName);
}
} catch (NotFoundException e) {
} catch (NotFoundException e) {
LOGGER.log(Level.FINE, "Failed to load class '" + ctClass.getName() + "': " + e.getLocalizedMessage(), e);
}
}
return hasAnnotation;
}
}
return matches;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package japicmp.filter;

import japicmp.util.CtClassBuilder;
import javassist.ClassPool;
import javassist.CtClass;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class AnnotationClassFilterTest {

@Test
public void testMatchesAnnotation() {
AnnotationClassFilter annotationClassFilter = new AnnotationClassFilter("@japicmp.Exclude");
CtClass ctClass = CtClassBuilder.create()
.name("japicmp.ExcludeMe")
.withAnnotation("japicmp.Exclude")
.addToClassPool(new ClassPool());

boolean matches = annotationClassFilter.matches(ctClass);

assertThat(matches, is(true));
}

@Test
public void testMatchesNotAnnotation() {
AnnotationClassFilter annotationClassFilter = new AnnotationClassFilter("@japicmp.Exclude");
CtClass ctClass = CtClassBuilder.create()
.name("japicmp.ExcludeMeNot")
.addToClassPool(new ClassPool());
ctClass.defrost();

boolean matches = annotationClassFilter.matches(ctClass);

assertThat(matches, is(false));
}
}

0 comments on commit 02d833e

Please sign in to comment.