-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
DevBeanInfo.java
208 lines (183 loc) · 6.75 KB
/
DevBeanInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package io.quarkus.arc.deployment.devconsole;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.Type;
import io.quarkus.arc.deployment.CompletedApplicationClassPredicateBuildItem;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.arc.processor.InterceptorInfo;
public class DevBeanInfo implements Comparable<DevBeanInfo> {
public static DevBeanInfo from(BeanInfo bean, CompletedApplicationClassPredicateBuildItem predicate) {
Set<Name> qualifiers = new HashSet<>();
for (AnnotationInstance qualifier : bean.getQualifiers()) {
qualifiers.add(Name.from(qualifier));
}
Set<Name> types = new HashSet<>();
for (Type beanType : bean.getTypes()) {
types.add(Name.from(beanType));
}
Name scope = Name.from(bean.getScope().getDotName());
Name providerType = Name.from(bean.getProviderType());
List<String> interceptors;
List<InterceptorInfo> boundInterceptors = bean.getBoundInterceptors();
if (boundInterceptors.isEmpty()) {
interceptors = List.of();
} else {
interceptors = new ArrayList<>();
for (InterceptorInfo interceptor : boundInterceptors) {
interceptors.add(interceptor.getIdentifier());
}
}
if (bean.getTarget().isPresent()) {
AnnotationTarget target = bean.getTarget().get();
DevBeanKind kind;
String memberName;
boolean isApplicationBean;
Name declaringClass;
if (target.kind() == Kind.METHOD) {
MethodInfo method = target.asMethod();
memberName = method.name();
kind = DevBeanKind.METHOD;
isApplicationBean = predicate.test(bean.getDeclaringBean().getBeanClass());
declaringClass = Name.from(bean.getDeclaringBean().getBeanClass());
} else if (target.kind() == Kind.FIELD) {
FieldInfo field = target.asField();
memberName = field.name();
kind = DevBeanKind.FIELD;
isApplicationBean = predicate.test(bean.getDeclaringBean().getBeanClass());
declaringClass = Name.from(bean.getDeclaringBean().getBeanClass());
} else if (target.kind() == Kind.CLASS) {
ClassInfo clazz = target.asClass();
kind = DevBeanKind.CLASS;
memberName = null;
isApplicationBean = predicate.test(clazz.name());
declaringClass = null;
} else {
throw new IllegalArgumentException("Invalid annotation target: " + target);
}
return new DevBeanInfo(bean.getIdentifier(), kind, isApplicationBean, providerType, memberName, types, qualifiers,
scope, declaringClass,
interceptors);
} else {
// Synthetic bean
return new DevBeanInfo(bean.getIdentifier(), DevBeanKind.SYNTHETIC, false, providerType, null, types, qualifiers,
scope, null,
interceptors);
}
}
public DevBeanInfo(String id, DevBeanKind kind, boolean isApplicationBean, Name providerType, String memberName,
Set<Name> types,
Set<Name> qualifiers, Name scope, Name declaringClass, List<String> boundInterceptors) {
this.id = id;
this.kind = kind;
this.isApplicationBean = isApplicationBean;
this.providerType = providerType;
this.memberName = memberName;
this.types = types;
this.qualifiers = qualifiers;
this.scope = scope;
this.declaringClass = declaringClass;
this.interceptors = boundInterceptors;
}
private final String id;
private final DevBeanKind kind;
private final boolean isApplicationBean;
private final Name providerType;
private final String memberName;
private final Set<Name> types;
private final Set<Name> qualifiers;
private final Name scope;
private final Name declaringClass;
private final List<String> interceptors;
public String getId() {
return id;
}
public DevBeanKind getKind() {
return kind;
}
public Name getScope() {
return scope;
}
public Set<Name> getQualifiers() {
return qualifiers;
}
public Set<Name> getNonDefaultQualifiers() {
Set<Name> nonDefault = new HashSet<>();
String atDefault = DotNames.DEFAULT.toString();
String atAny = DotNames.ANY.toString();
for (Name qualifier : qualifiers) {
if (qualifier.toString().endsWith(atDefault) || qualifier.toString().endsWith(atAny)) {
continue;
}
nonDefault.add(qualifier);
}
return nonDefault;
}
public Set<Name> getTypes() {
return types;
}
public Name getProviderType() {
return providerType;
}
public String getMemberName() {
return memberName;
}
public boolean isApplicationBean() {
return isApplicationBean;
}
public Name getDeclaringClass() {
return declaringClass;
}
public List<String> getInterceptors() {
return interceptors;
}
public String getDescription() {
switch (kind) {
case CLASS:
return providerType.toString();
case FIELD:
return declaringClass.toString() + "#" + memberName;
case METHOD:
return declaringClass.toString() + "#" + memberName + "()";
case SYNTHETIC:
return "Synthetic: " + providerType.toString();
default:
return providerType.toString();
}
}
@Override
public int compareTo(DevBeanInfo o) {
// Application beans should go first
if (isApplicationBean == o.isApplicationBean) {
return providerType.compareTo(o.providerType);
}
return isApplicationBean ? -1 : 1;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
DevBeanInfo other = (DevBeanInfo) obj;
return Objects.equals(id, other.id);
}
}