Skip to content

Commit

Permalink
Use the @inherited annotation for @UNMETERED
Browse files Browse the repository at this point in the history
  • Loading branch information
blerer committed Jul 13, 2023
1 parent c9ba44c commit 9349d21
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
23 changes: 8 additions & 15 deletions src/org/github/jamm/Filters.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,23 @@ public boolean ignore(Class<?> cls, Field field) {

@Override
public boolean ignore(Class<?> cls) {
return isUnmeteredAnnotationPresent(cls);
// The @Inherited annotation only causes annotations to be inherited from superclasses. Therefore we need to check the interfaces manually
return cls != null && (cls.isAnnotationPresent(Unmetered.class) || isAnnotationPresentOnInterfaces(cls));
}

/**
* Checks if the specified class or one of its parents is annotated with {@code Unmetered}
*
* @param cls the class to check
* @return {@code true} if the specified class or one of its parents is annotated with {@code Unmetered}, {@code false} otherwise.
* Checks if any of the implemented interfaces has the {@code @Unmetered} annotation
* @param cls the class for which the interfaces must be checked
* @return {@code true} if any of the interfaces is annotated with {@code @Unmetered}. {@code false} otherwise.
*/
private boolean isUnmeteredAnnotationPresent(Class<?> cls) {

if (cls == null)
return false;

if (cls.isAnnotationPresent(Unmetered.class))
return true;

private boolean isAnnotationPresentOnInterfaces(Class<?> cls) {
Class<?>[] interfaces = cls.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (isUnmeteredAnnotationPresent(cls.getInterfaces()[i]))
if (cls.getInterfaces()[i].isAnnotationPresent(Unmetered.class))
return true;
}

return isUnmeteredAnnotationPresent(cls.getSuperclass());
return false;
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/org/github/jamm/Unmetered.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.github.jamm;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -10,6 +11,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
@Inherited
public @interface Unmetered {

}
49 changes: 43 additions & 6 deletions test/org/github/jamm/MemoryMeterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,21 @@ public void testUnmeteredTypeAnnotation() {
MemoryMeter meter = MemoryMeter.builder().withGuessing(guess).build();

String s = "test";
assertEquals(0, meter.measureDeep(new WithTypeAnnotation(s)));
assertEquals(0, meter.measureDeep(new ClassWithAnnotation(s)));
}

@Test
public void testUnmeteredAnnotationOnParent() {
MemoryMeter meter = MemoryMeter.builder().withGuessing(guess).build();

String s = "test";
assertEquals(0, meter.measureDeep(new WithParentWithAnnotation(s)));
assertEquals(0, meter.measureDeep(new WithParentWithAnnotation("test")));
}

@Test
public void testUnmeteredAnnotationOnImplementedInteface() {
MemoryMeter meter = MemoryMeter.builder().withGuessing(guess).build();

assertEquals(0, meter.measureDeep(new WithAnnotatedInterface("test")));
}

@Test
Expand All @@ -197,6 +203,17 @@ public void testUnmeteredAnnotationOnFieldInterface() {
assertEquals(withoutSize, withSize);
}

@Test
public void testUnmeteredAnnotationOnFieldImplementedInterface() {
MemoryMeter meter = MemoryMeter.builder().withGuessing(guess).build();

long withoutSize = meter.measureDeep(new WithFieldTypeWithAnnotatedInterface(null));

WithAnnotatedInterface field = new WithAnnotatedInterface("test");
long withSize = meter.measureDeep(new WithFieldTypeWithAnnotatedInterface(field));
assertEquals(withoutSize, withSize);
}

@SuppressWarnings("unused")
private static class WithoutAnnotationField {
private String s;
Expand All @@ -218,15 +235,15 @@ public WithAnnotationField(String s) {

@Unmetered
@SuppressWarnings("unused")
private static class WithTypeAnnotation {
private static class ClassWithAnnotation {
private String s;

public WithTypeAnnotation(String s) {
public ClassWithAnnotation(String s) {
this.s = s;
}
}

private static class WithParentWithAnnotation extends WithTypeAnnotation {
private static class WithParentWithAnnotation extends ClassWithAnnotation {

public WithParentWithAnnotation(String s) {
super(s);
Expand Down Expand Up @@ -258,6 +275,26 @@ public WithFieldAnnotatedInterface(AnnotatedInterface field) {
}
}

private static class WithAnnotatedInterface implements AnnotatedInterface {

private String s;

public WithAnnotatedInterface(String s) {
this.s = s;
}
}

@SuppressWarnings("unused")
private static class WithFieldTypeWithAnnotatedInterface {

private final WithAnnotatedInterface field;

public WithFieldTypeWithAnnotatedInterface(WithAnnotatedInterface field) {
this.field = field;
}
}


@Test
public void testMeasureWithLambdaField() {
MemoryMeter meter = MemoryMeter.builder().withGuessing(guess).build();
Expand Down

0 comments on commit 9349d21

Please sign in to comment.