Skip to content
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

add an equivalence proxy for AnnotationInstance #341

Merged
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
7 changes: 7 additions & 0 deletions core/src/main/java/org/jboss/jandex/AnnotationInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,11 @@ public int equivalenceHashCode() {
result = 31 * result + Arrays.hashCode(values);
return result;
}

/**
* Returns an {@linkplain AnnotationInstanceEquivalenceProxy equivalence proxy} for this annotation instance.
*/
public AnnotationInstanceEquivalenceProxy createEquivalenceProxy() {
return new AnnotationInstanceEquivalenceProxy(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.jboss.jandex;

import java.util.Objects;

/**
* Holds an {@link AnnotationInstance} and implements equality and hash code as equivalence.
* <p>
* When using equivalence proxies, it is usually a mistake to obtain
* the {@linkplain AnnotationInstance#target() target} of the delegate annotation instance.
* <p>
* <b>Thread-Safety</b>
* </p>
* This class is immutable and can be shared between threads without safe
* publication.
*
* @see AnnotationInstance#equivalentTo(AnnotationInstance)
* @see AnnotationInstance#equivalenceHashCode()
*/
public final class AnnotationInstanceEquivalenceProxy {
private final AnnotationInstance annotation;

AnnotationInstanceEquivalenceProxy(AnnotationInstance annotation) {
this.annotation = Objects.requireNonNull(annotation);
}

public AnnotationInstance get() {
return annotation;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
return annotation.equivalentTo(((AnnotationInstanceEquivalenceProxy) o).annotation);
}

@Override
public int hashCode() {
return annotation.equivalenceHashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationInstanceEquivalenceProxy;
import org.jboss.jandex.Index;
import org.jboss.jandex.test.util.IndexingUtil;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -51,5 +52,19 @@ private void testEqualityEquivalence(Index index) {
assertFalse(foo2.equivalentTo(bar));

assertEquals(foo.equivalenceHashCode(), foo2.equivalenceHashCode());

AnnotationInstanceEquivalenceProxy fooEquiv = foo.createEquivalenceProxy();
AnnotationInstanceEquivalenceProxy foo2Equiv = foo2.createEquivalenceProxy();
AnnotationInstanceEquivalenceProxy barEquiv = bar.createEquivalenceProxy();

assertNotNull(fooEquiv);
assertNotNull(foo2Equiv);
assertNotNull(barEquiv);

assertEquals(fooEquiv, foo2Equiv);
assertNotEquals(fooEquiv, barEquiv);
assertNotEquals(foo2Equiv, barEquiv);

assertEquals(fooEquiv.hashCode(), foo2Equiv.hashCode());
}
}
Loading