Skip to content

Commit 63160cc

Browse files
committed
fix SimpleAnnotation equals and hashCode
1 parent 230a838 commit 63160cc

File tree

9 files changed

+32
-20
lines changed

9 files changed

+32
-20
lines changed

compiler/src/main/java/io/jbock/simple/processor/ContextComponent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.jbock.simple.processor.binding.InjectBindingFactory;
66
import io.jbock.simple.processor.binding.KeyFactory;
77
import io.jbock.simple.processor.graph.TopologicalSorter;
8+
import io.jbock.simple.processor.util.SafeElements;
89
import io.jbock.simple.processor.util.TypeTool;
910
import io.jbock.simple.processor.writing.Generator;
1011

@@ -16,18 +17,21 @@ interface Factory {
1617
ContextComponent create(
1718
ComponentElement component,
1819
TypeTool tool,
20+
SafeElements elements,
1921
InjectBindingFactory injectBindingFactory,
2022
KeyFactory keyFactory);
2123
}
2224

2325
static ContextComponent create(
2426
ComponentElement component,
2527
TypeTool tool,
28+
SafeElements elements,
2629
InjectBindingFactory injectBindingFactory,
2730
KeyFactory keyFactory) {
2831
return ContextComponent_Impl.factory().create(
2932
component,
3033
tool,
34+
elements,
3135
injectBindingFactory,
3236
keyFactory);
3337
}

compiler/src/main/java/io/jbock/simple/processor/binding/KeyFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public KeyFactory(TypeTool tool) {
3535
private Optional<SimpleAnnotation> getQualifier(Element element) {
3636
List<SimpleAnnotation> qualifiers = element.getAnnotationMirrors().stream()
3737
.filter(this::hasQualifierAnnotation)
38-
.map(mirror -> SimpleAnnotation.create(mirror, tool.elements()))
38+
.map(mirror -> SimpleAnnotation.create(mirror, tool.elements(), tool.types()))
3939
.collect(Collectors.toList());
4040
if (qualifiers.isEmpty()) {
4141
return Optional.empty();

compiler/src/main/java/io/jbock/simple/processor/graph/GraphFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ private Optional<Binding> getBinding(DependencyRequest request) {
4545
return result;
4646
}
4747
result = component.parameterBinding(key)
48-
.or(() -> injectBindingFactory.binding(key))
4948
.or(() -> Optional.ofNullable(component.providesBindings().get(key)))
49+
.or(() -> injectBindingFactory.binding(key))
5050
.or(() -> providerBinding(key));
5151
if (result.isPresent()) {
5252
bindingCache.put(key, result);
@@ -62,8 +62,8 @@ private Optional<Binding> providerBinding(Key key) {
6262
ProviderType provider = providerType.orElseThrow();
6363
Key innerKey = key.changeType(provider.innerType());
6464
return component.parameterBinding(innerKey)
65-
.or(() -> injectBindingFactory.binding(innerKey))
6665
.or(() -> Optional.ofNullable(component.providesBindings().get(innerKey)))
66+
.or(() -> injectBindingFactory.binding(innerKey))
6767
.map(b -> new ProviderBinding(key, b, provider));
6868
}
6969

compiler/src/main/java/io/jbock/simple/processor/step/ComponentStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private void process(TypeElement typeElement) {
8989
}
9090
}
9191
ContextComponent componentComponent = ContextComponent.create(
92-
component, tool, injectBindingFactory, keyFactory);
92+
component, tool, tool.elements(), injectBindingFactory, keyFactory);
9393
Generator generator = componentComponent.generator();
9494
List<Binding> sorted = componentComponent.topologicalSorter().sortedBindings();
9595
TypeSpec typeSpec = generator.generate(sorted);

compiler/src/main/java/io/jbock/simple/processor/util/SimpleAnnotation.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.jbock.simple.processor.util;
22

3+
import io.jbock.javapoet.TypeName;
4+
35
import javax.lang.model.element.AnnotationMirror;
46
import javax.lang.model.element.AnnotationValue;
57
import javax.lang.model.type.DeclaredType;
@@ -12,27 +14,35 @@
1214
public final class SimpleAnnotation {
1315

1416
private final DeclaredType annotationType;
17+
private final TypeName typeName;
1518
private final List<AnnotationValue> values;
19+
private final SafeTypes types;
1620

1721
private SimpleAnnotation(
1822
DeclaredType annotationType,
19-
List<AnnotationValue> values) {
23+
List<AnnotationValue> values,
24+
SafeTypes types) {
2025
this.annotationType = annotationType;
2126
this.values = values;
27+
this.types = types;
28+
this.typeName = TypeName.get(annotationType);
2229
}
2330

24-
public static SimpleAnnotation create(AnnotationMirror mirror, SafeElements elements) {
31+
public static SimpleAnnotation create(
32+
AnnotationMirror mirror,
33+
SafeElements elements,
34+
SafeTypes types) {
2535
DeclaredType annotationType = mirror.getAnnotationType();
2636
List<AnnotationValue> values = new ArrayList<>(elements.getElementValuesWithDefaults(mirror).values());
27-
return new SimpleAnnotation(annotationType, values);
37+
return new SimpleAnnotation(annotationType, values, types);
2838
}
2939

3040
@Override
3141
public boolean equals(Object o) {
3242
if (this == o) return true;
3343
if (o == null || getClass() != o.getClass()) return false;
3444
SimpleAnnotation that = (SimpleAnnotation) o;
35-
if (!annotationType.equals(that.annotationType)) {
45+
if (!types.isSameType(annotationType, that.annotationType)) {
3646
return false;
3747
}
3848
if (values.size() != that.values.size()) {
@@ -49,7 +59,7 @@ public boolean equals(Object o) {
4959
@Override
5060
public int hashCode() {
5161
int[] result = new int[values.size() + 1];
52-
result[0] = Objects.hashCode(annotationType);
62+
result[0] = Objects.hashCode(typeName); //avoid TypeMirror hashCode
5363
for (int i = 0; i < values.size(); i++) {
5464
result[i + 1] = Objects.hashCode(values.get(i).getValue());
5565
}

simple-component/src/main/java/io/jbock/simple/Component.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package io.jbock.simple;
22

3-
import java.lang.annotation.Documented;
43
import java.lang.annotation.Retention;
54
import java.lang.annotation.Target;
65

76
import static java.lang.annotation.ElementType.TYPE;
8-
import static java.lang.annotation.RetentionPolicy.SOURCE;
7+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
98

109
/**
1110
* Annotates an interface for which a dependency-injected
@@ -28,7 +27,7 @@
2827
* </ul>
2928
*/
3029
@Target(TYPE)
31-
@Retention(SOURCE)
30+
@Retention(RUNTIME)
3231
public @interface Component {
3332

3433
/**
@@ -47,9 +46,8 @@
4746
* <li>The method parameters bind the instance passed for that parameter within the component.
4847
* </ul>
4948
*/
50-
@Retention(SOURCE)
5149
@Target(TYPE)
52-
@Documented
50+
@Retention(RUNTIME)
5351
@interface Factory {
5452
}
5553
}

simple-component/src/main/java/io/jbock/simple/Inject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
import static java.lang.annotation.ElementType.CONSTRUCTOR;
77
import static java.lang.annotation.ElementType.METHOD;
8-
import static java.lang.annotation.RetentionPolicy.SOURCE;
8+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
99

1010
/**
1111
* Identifies injectable constructors and <em>static</em> methods.
1212
*/
1313
@Target({METHOD, CONSTRUCTOR})
14-
@Retention(SOURCE)
14+
@Retention(RUNTIME)
1515
public @interface Inject {
1616
}

simple-component/src/main/java/io/jbock/simple/Named.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121

2222
import static java.lang.annotation.ElementType.METHOD;
2323
import static java.lang.annotation.ElementType.PARAMETER;
24-
import static java.lang.annotation.RetentionPolicy.SOURCE;
24+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
2525

2626
/**
2727
* String-based qualifier.
2828
*/
2929
@Qualifier
3030
@Target({METHOD, PARAMETER})
31-
@Retention(SOURCE)
31+
@Retention(RUNTIME)
3232
public @interface Named {
3333

3434
String value();

simple-component/src/main/java/io/jbock/simple/Provides.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.lang.annotation.Target;
55

66
import static java.lang.annotation.ElementType.METHOD;
7-
import static java.lang.annotation.RetentionPolicy.SOURCE;
7+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
88

99
/**
1010
* Annotates <em>static</em> methods of a {@linkplain Component component}
@@ -16,6 +16,6 @@
1616
* implementation will pass dependencies to the method as parameters.
1717
*/
1818
@Target(METHOD)
19-
@Retention(SOURCE)
19+
@Retention(RUNTIME)
2020
public @interface Provides {
2121
}

0 commit comments

Comments
 (0)