Skip to content

Commit

Permalink
Merge pull request #318 from Ladicek/annotation-equivalence
Browse files Browse the repository at this point in the history
Establish the notion of annotation instance equivalence
  • Loading branch information
Ladicek authored Jun 22, 2023
2 parents 0c3bc13 + 6473e08 commit ad1785b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 9 deletions.
29 changes: 25 additions & 4 deletions core/src/main/java/org/jboss/jandex/AnnotationInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,17 +426,17 @@ public String toString() {
return toString(true);
}

// runtime visibility is ignored for the purpose of equality and hash code, because
// the annotation type identity (the name) already includes that information
// runtime visibility is ignored for the purpose of equality, hash code and equivalence,
// because the annotation type identity (the name) already includes that information

/**
* Returns whether this annotation instance is equal to another instance.
* Two annotation instances are equal if their names and values of their members are equal,
* and they share the exact same {@code AnnotationTarget} instance. The latter restriction
* may be softened in future versions.
*
* @param o the annotation instance to compare to.
* @return true if equal, false if not
* @param o the annotation instance to compare to
* @return {@code true} if equal, {@code false} if not
*
* @see Object#equals(Object)
*/
Expand Down Expand Up @@ -465,4 +465,25 @@ public int hashCode() {

return result;
}

/**
* Returns whether this annotation instance is equivalent to the {@code other} annotation
* instance. Two annotation instances are equivalent if their names and members are equal.
* No attention is paid to the annotation target.
*
* @param other the annotation instance to compare to
* @return {@code true} if equivalent, {@code false} if not
*
* @see #equals(Object)
*/
public boolean equivalentTo(AnnotationInstance other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}

return name.equals(other.name) && Arrays.equals(values, other.values);
}
}
1 change: 0 additions & 1 deletion core/src/main/java/org/jboss/jandex/FieldInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.jboss.jandex;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/jboss/jandex/MethodInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.jboss.jandex;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/jboss/jandex/ParameterizedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.jboss.jandex;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/jboss/jandex/TypeVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.jboss.jandex.test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.Index;
import org.jboss.jandex.test.util.IndexingUtil;
import org.junit.jupiter.api.Test;

public class AnnotationInstanceTest {
@MyAnnotation("foo")
static class Foo {
}

@MyAnnotation("foo")
static class Foo2 {
}

@MyAnnotation("bar")
static class Bar {
}

@Test
public void equalityEquivalence() throws IOException {
Index index = Index.of(Foo.class, Foo2.class, Bar.class);
testEqualityEquivalence(index);
testEqualityEquivalence(IndexingUtil.roundtrip(index));
}

private void testEqualityEquivalence(Index index) {
AnnotationInstance foo = index.getClassByName(Foo.class).declaredAnnotation(MyAnnotation.DOT_NAME);
AnnotationInstance foo2 = index.getClassByName(Foo2.class).declaredAnnotation(MyAnnotation.DOT_NAME);
AnnotationInstance bar = index.getClassByName(Bar.class).declaredAnnotation(MyAnnotation.DOT_NAME);

assertNotNull(foo);
assertNotNull(foo2);
assertNotNull(bar);

assertNotEquals(foo, foo2);
assertNotEquals(foo, bar);
assertNotEquals(foo2, bar);

assertTrue(foo.equivalentTo(foo2));
assertFalse(foo.equivalentTo(bar));
assertFalse(foo2.equivalentTo(bar));
}
}

0 comments on commit ad1785b

Please sign in to comment.