1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.di;
20
21 import java.lang.reflect.ParameterizedType;
22 import java.lang.reflect.Type;
23 import java.util.Objects;
24
25 import org.apache.maven.api.annotations.Nullable;
26 import org.apache.maven.di.impl.ReflectionUtils;
27 import org.apache.maven.di.impl.Types;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public abstract class Key<T> {
52 private final Type type;
53 private final @Nullable Object qualifier;
54
55 private int hash;
56
57 protected Key() {
58 this(null);
59 }
60
61 protected Key(@Nullable Object qualifier) {
62 this.type = Types.simplifyType(getTypeParameter());
63 this.qualifier = qualifier;
64 }
65
66 protected Key(Type type, @Nullable Object qualifier) {
67 this.type = Types.simplifyType(type);
68 this.qualifier = qualifier;
69 }
70
71 static final class KeyImpl<T> extends Key<T> {
72 KeyImpl(Type type, Object qualifier) {
73 super(type, qualifier);
74 }
75 }
76
77
78
79
80
81
82
83
84
85 public static <T> Key<T> of(Class<T> type) {
86 return new KeyImpl<>(type, null);
87 }
88
89
90
91
92
93
94
95
96
97
98 public static <T> Key<T> of(Class<T> type, @Nullable Object qualifier) {
99 return new KeyImpl<>(type, qualifier);
100 }
101
102 public static <T> Key<T> ofType(Type type) {
103 return new KeyImpl<>(type, null);
104 }
105
106 public static <T> Key<T> ofType(Type type, @Nullable Object qualifier) {
107 return new KeyImpl<>(type, qualifier);
108 }
109
110 private Type getTypeParameter() {
111
112 Type typeArgument = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
113 Object outerInstance = ReflectionUtils.getOuterClassInstance(this);
114
115 return outerInstance != null
116 ? Types.bind(typeArgument, Types.getAllTypeBindings(outerInstance.getClass()))
117 : typeArgument;
118 }
119
120
121
122
123
124
125
126
127 public Type getType() {
128 return type;
129 }
130
131
132
133
134
135 @SuppressWarnings("unchecked")
136 public Class<T> getRawType() {
137 return (Class<T>) Types.getRawType(type);
138 }
139
140
141
142
143
144
145 public <U> Key<U> getTypeParameter(int index) {
146 if (type instanceof ParameterizedType parameterizedType) {
147 return new KeyImpl<>(parameterizedType.getActualTypeArguments()[index], null);
148 }
149 throw new IllegalStateException("Expected type from key " + getDisplayString() + " to be parameterized");
150 }
151
152
153
154
155
156
157 public @Nullable Object getQualifier() {
158 return qualifier;
159 }
160
161
162
163
164
165 public String getDisplayString() {
166 StringBuilder result = new StringBuilder();
167 if (qualifier instanceof String s) {
168 if (s.isEmpty()) {
169 result.append("@Named ");
170 } else {
171 result.append("@Named(\"").append(s).append("\") ");
172 }
173 } else if (qualifier != null) {
174 ReflectionUtils.getDisplayString(result, qualifier);
175 result.append(" ");
176 }
177 result.append(ReflectionUtils.getDisplayName(type));
178 return result.toString();
179 }
180
181 @Override
182 public boolean equals(Object o) {
183 if (this == o) {
184 return true;
185 }
186 if (o instanceof Key<?> that) {
187 return type.equals(that.type) && Objects.equals(qualifier, that.qualifier);
188 } else {
189 return false;
190 }
191 }
192
193 @Override
194 public int hashCode() {
195 int hashCode = hash;
196 if (hashCode == 0) {
197 hash = 31 * type.hashCode() + (qualifier == null ? 0 : qualifier.hashCode());
198 }
199 return hash;
200 }
201
202 @Override
203 public String toString() {
204 return getDisplayString();
205 }
206 }