author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 52850 | f527b24990d7 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jfr.internal; |
|
27 |
||
28 |
import java.util.ArrayList; |
|
29 |
import java.util.Collection; |
|
52850 | 30 |
import java.util.Collections; |
50113 | 31 |
import java.util.HashMap; |
32 |
import java.util.List; |
|
33 |
import java.util.Map; |
|
34 |
import java.util.Objects; |
|
35 |
||
36 |
import jdk.jfr.AnnotationElement; |
|
37 |
import jdk.jfr.Event; |
|
38 |
import jdk.jfr.SettingControl; |
|
39 |
import jdk.jfr.ValueDescriptor; |
|
40 |
||
41 |
/** |
|
42 |
* Internal data structure that describes a type, |
|
43 |
* |
|
44 |
* Used to create event types, value descriptor and annotations. |
|
45 |
* |
|
46 |
*/ |
|
47 |
public class Type implements Comparable<Type> { |
|
48 |
public static final String SUPER_TYPE_ANNOTATION = java.lang.annotation.Annotation.class.getName(); |
|
49 |
public static final String SUPER_TYPE_SETTING = SettingControl.class.getName(); |
|
50 |
public static final String SUPER_TYPE_EVENT = Event.class.getName(); |
|
51 |
public static final String EVENT_NAME_PREFIX = "jdk."; |
|
52 |
public static final String TYPES_PREFIX = "jdk.types."; |
|
53 |
public static final String SETTINGS_PREFIX = "jdk.settings."; |
|
54 |
||
55 |
||
56 |
// Initialization of known types |
|
57 |
private final static Map<Type, Class<?>> knownTypes = new HashMap<>(); |
|
58 |
static final Type BOOLEAN = register(boolean.class, new Type("boolean", null, 4)); |
|
59 |
static final Type CHAR = register(char.class, new Type("char", null, 5)); |
|
60 |
static final Type FLOAT = register(float.class, new Type("float", null, 6)); |
|
61 |
static final Type DOUBLE = register(double.class, new Type("double", null, 7)); |
|
62 |
static final Type BYTE = register(byte.class, new Type("byte", null, 8)); |
|
63 |
static final Type SHORT = register(short.class, new Type("short", null, 9)); |
|
64 |
static final Type INT = register(int.class, new Type("int", null, 10)); |
|
65 |
static final Type LONG = register(long.class, new Type("long", null, 11)); |
|
66 |
static final Type CLASS = register(Class.class, new Type("java.lang.Class", null, 20)); |
|
67 |
static final Type STRING = register(String.class, new Type("java.lang.String", null, 21)); |
|
68 |
static final Type THREAD = register(Thread.class, new Type("java.lang.Thread", null, 22)); |
|
69 |
static final Type STACK_TRACE = register(null, new Type(TYPES_PREFIX + "StackTrace", null, 23)); |
|
70 |
||
71 |
private final AnnotationConstruct annos = new AnnotationConstruct(); |
|
72 |
private final String name; |
|
73 |
private final String superType; |
|
74 |
private final boolean constantPool; |
|
52850 | 75 |
private List<ValueDescriptor> fields = new ArrayList<>(); |
50113 | 76 |
private Boolean simpleType; // calculated lazy |
77 |
private boolean remove = true; |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
78 |
private long id; |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
79 |
|
50113 | 80 |
/** |
81 |
* Creates a type |
|
82 |
* |
|
83 |
* @param javaTypeName i.e "java.lang.String" |
|
84 |
* @param superType i.e "java.lang.Annotation" |
|
85 |
* @param id the class id that represents the class in the JVM |
|
86 |
* |
|
87 |
*/ |
|
88 |
public Type(String javaTypeName, String superType, long typeId) { |
|
89 |
this(javaTypeName, superType, typeId, false); |
|
90 |
} |
|
91 |
||
92 |
Type(String javaTypeName, String superType, long typeId, boolean contantPool) { |
|
93 |
this(javaTypeName, superType, typeId, contantPool, null); |
|
94 |
} |
|
95 |
||
96 |
Type(String javaTypeName, String superType, long typeId, boolean contantPool, Boolean simpleType) { |
|
97 |
Objects.requireNonNull(javaTypeName); |
|
98 |
||
99 |
if (!isValidJavaIdentifier(javaTypeName)) { |
|
100 |
throw new IllegalArgumentException(javaTypeName + " is not a valid Java identifier"); |
|
101 |
} |
|
102 |
this.constantPool = contantPool; |
|
103 |
this.superType = superType; |
|
104 |
this.name = javaTypeName; |
|
105 |
this.id = typeId; |
|
106 |
this.simpleType = simpleType; |
|
107 |
} |
|
108 |
||
109 |
static boolean isDefinedByJVM(long id) { |
|
110 |
return id < JVM.RESERVED_CLASS_ID_LIMIT; |
|
111 |
} |
|
112 |
||
113 |
public static long getTypeId(Class<?> clazz) { |
|
114 |
Type type = Type.getKnownType(clazz); |
|
115 |
return type == null ? JVM.getJVM().getTypeId(clazz) : type.getId(); |
|
116 |
} |
|
117 |
||
118 |
static Collection<Type> getKnownTypes() { |
|
119 |
return knownTypes.keySet(); |
|
120 |
} |
|
121 |
||
122 |
public static boolean isValidJavaIdentifier(String identifier) { |
|
123 |
if (identifier.isEmpty()) { |
|
124 |
return false; |
|
125 |
} |
|
126 |
if (!Character.isJavaIdentifierStart(identifier.charAt(0))) { |
|
127 |
return false; |
|
128 |
} |
|
129 |
for (int i = 1; i < identifier.length(); i++) { |
|
130 |
char c = identifier.charAt(i); |
|
131 |
if (c != '.') { |
|
132 |
if (!Character.isJavaIdentifierPart(c)) { |
|
133 |
return false; |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
return true; |
|
138 |
} |
|
139 |
||
140 |
public static boolean isValidJavaFieldType(String name) { |
|
141 |
for (Map.Entry<Type, Class<?>> entry : knownTypes.entrySet()) { |
|
142 |
Class<?> clazz = entry.getValue(); |
|
143 |
if (clazz != null && name.equals(clazz.getName())) { |
|
144 |
return true; |
|
145 |
} |
|
146 |
} |
|
147 |
return false; |
|
148 |
} |
|
149 |
||
150 |
public static Type getKnownType(String typeName) { |
|
151 |
for (Type type : knownTypes.keySet()) { |
|
152 |
if (type.getName().equals(typeName)) { |
|
153 |
return type; |
|
154 |
} |
|
155 |
} |
|
156 |
return null; |
|
157 |
} |
|
158 |
||
159 |
static boolean isKnownType(Class<?> type) { |
|
160 |
if (type.isPrimitive()) { |
|
161 |
return true; |
|
162 |
} |
|
163 |
if (type.equals(Class.class) || type.equals(Thread.class) || type.equals(String.class)) { |
|
164 |
return true; |
|
165 |
} |
|
166 |
return false; |
|
167 |
} |
|
168 |
||
169 |
public static Type getKnownType(Class<?> clazz) { |
|
170 |
for (Map.Entry<Type, Class<?>> entry : knownTypes.entrySet()) { |
|
171 |
if (clazz != null && clazz.equals(entry.getValue())) { |
|
172 |
return entry.getKey(); |
|
173 |
} |
|
174 |
} |
|
175 |
return null; |
|
176 |
} |
|
177 |
||
178 |
public String getName() { |
|
179 |
return name; |
|
180 |
} |
|
181 |
||
182 |
public String getLogName() { |
|
183 |
return getName() + "(" + getId() + ")"; |
|
184 |
} |
|
185 |
||
186 |
public List<ValueDescriptor> getFields() { |
|
52850 | 187 |
if (fields instanceof ArrayList) { |
188 |
((ArrayList<ValueDescriptor>) fields).trimToSize(); |
|
189 |
fields = Collections.unmodifiableList(fields); |
|
190 |
} |
|
50113 | 191 |
return fields; |
192 |
} |
|
193 |
||
194 |
public boolean isSimpleType() { |
|
195 |
if (simpleType == null) { |
|
196 |
simpleType = calculateSimpleType(); |
|
197 |
} |
|
198 |
return simpleType.booleanValue(); |
|
199 |
} |
|
200 |
||
201 |
private boolean calculateSimpleType() { |
|
202 |
if (fields.size() != 1) { |
|
203 |
return false; |
|
204 |
} |
|
205 |
// annotation, settings and event can never be simple types |
|
206 |
return superType == null; |
|
207 |
} |
|
208 |
||
209 |
public boolean isDefinedByJVM() { |
|
210 |
return id < JVM.RESERVED_CLASS_ID_LIMIT; |
|
211 |
} |
|
212 |
||
213 |
private static Type register(Class<?> clazz, Type type) { |
|
214 |
knownTypes.put(type, clazz); |
|
215 |
return type; |
|
216 |
} |
|
217 |
||
218 |
public void add(ValueDescriptor valueDescriptor) { |
|
219 |
Objects.requireNonNull(valueDescriptor); |
|
220 |
fields.add(valueDescriptor); |
|
221 |
} |
|
222 |
||
223 |
void trimFields() { |
|
52850 | 224 |
getFields(); |
50113 | 225 |
} |
226 |
||
227 |
void setAnnotations(List<AnnotationElement> annotations) { |
|
228 |
annos.setAnnotationElements(annotations); |
|
229 |
} |
|
230 |
||
231 |
public String getSuperType() { |
|
232 |
return superType; |
|
233 |
} |
|
234 |
||
235 |
public long getId() { |
|
236 |
return id; |
|
237 |
} |
|
238 |
||
239 |
public boolean isConstantPool() { |
|
240 |
return constantPool; |
|
241 |
} |
|
242 |
||
243 |
public String getLabel() { |
|
244 |
return annos.getLabel(); |
|
245 |
} |
|
246 |
||
247 |
public List<AnnotationElement> getAnnotationElements() { |
|
248 |
return annos.getUnmodifiableAnnotationElements(); |
|
249 |
} |
|
250 |
||
251 |
public <T> T getAnnotation(Class<? extends java.lang.annotation.Annotation> clazz) { |
|
252 |
return annos.getAnnotation(clazz); |
|
253 |
} |
|
254 |
||
255 |
public String getDescription() { |
|
256 |
return annos.getDescription(); |
|
257 |
} |
|
258 |
||
259 |
@Override |
|
260 |
public int hashCode() { |
|
261 |
return Long.hashCode(id); |
|
262 |
} |
|
263 |
||
264 |
@Override |
|
265 |
public boolean equals(Object object) { |
|
266 |
if (object instanceof Type) { |
|
267 |
Type that = (Type) object; |
|
268 |
return that.id == this.id; |
|
269 |
} |
|
270 |
return false; |
|
271 |
} |
|
272 |
||
273 |
@Override |
|
274 |
public int compareTo(Type that) { |
|
275 |
return Long.compare(this.id, that.id); |
|
276 |
} |
|
277 |
||
278 |
void log(String action, LogTag logTag, LogLevel level) { |
|
52413 | 279 |
if (Logger.shouldLog(logTag, level) && !isSimpleType()) { |
50113 | 280 |
Logger.log(logTag, LogLevel.TRACE, action + " " + typeText() + " " + getLogName() + " {"); |
281 |
for (ValueDescriptor v : getFields()) { |
|
282 |
String array = v.isArray() ? "[]" : ""; |
|
283 |
Logger.log(logTag, LogLevel.TRACE, " " + v.getTypeName() + array + " " + v.getName() + ";"); |
|
284 |
} |
|
285 |
Logger.log(logTag, LogLevel.TRACE, "}"); |
|
286 |
} else { |
|
52413 | 287 |
if (Logger.shouldLog(logTag, LogLevel.INFO) && !isSimpleType()) { |
50113 | 288 |
Logger.log(logTag, LogLevel.INFO, action + " " + typeText() + " " + getLogName()); |
289 |
} |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
private String typeText() { |
|
294 |
if (this instanceof PlatformEventType) { |
|
295 |
return "event type"; |
|
296 |
} |
|
297 |
if (Type.SUPER_TYPE_SETTING.equals(superType)) { |
|
298 |
return "setting type"; |
|
299 |
} |
|
300 |
if (Type.SUPER_TYPE_ANNOTATION.equals(superType)) { |
|
301 |
return "annotation type"; |
|
302 |
} |
|
303 |
return "type"; |
|
304 |
} |
|
305 |
||
306 |
@Override |
|
307 |
public String toString() { |
|
308 |
StringBuilder sb = new StringBuilder(); |
|
309 |
sb.append(getLogName()); |
|
310 |
if (!getFields().isEmpty()) { |
|
311 |
sb.append(" {\n"); |
|
312 |
for (ValueDescriptor td : getFields()) { |
|
313 |
sb.append(" type=" + td.getTypeName() + "(" + td.getTypeId() + ") name=" + td.getName() + "\n"); |
|
314 |
} |
|
315 |
sb.append("}\n"); |
|
316 |
} |
|
317 |
return sb.toString(); |
|
318 |
} |
|
319 |
||
320 |
public void setRemove(boolean remove) { |
|
321 |
this.remove = remove; |
|
322 |
} |
|
323 |
||
324 |
public boolean getRemove() { |
|
325 |
return remove; |
|
326 |
} |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
327 |
|
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
328 |
public void setId(long id) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
329 |
this.id = id; |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
330 |
} |
50113 | 331 |
} |