author | egahlin |
Mon, 05 Nov 2018 14:40:16 +0100 | |
changeset 52413 | 6372f5af9612 |
parent 52334 | a181612f0715 |
child 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; |
|
30 |
import java.util.HashMap; |
|
31 |
import java.util.List; |
|
32 |
import java.util.Map; |
|
33 |
import java.util.Objects; |
|
34 |
||
35 |
import jdk.jfr.AnnotationElement; |
|
36 |
import jdk.jfr.Event; |
|
37 |
import jdk.jfr.SettingControl; |
|
38 |
import jdk.jfr.ValueDescriptor; |
|
39 |
||
40 |
/** |
|
41 |
* Internal data structure that describes a type, |
|
42 |
* |
|
43 |
* Used to create event types, value descriptor and annotations. |
|
44 |
* |
|
45 |
*/ |
|
46 |
public class Type implements Comparable<Type> { |
|
47 |
public static final String SUPER_TYPE_ANNOTATION = java.lang.annotation.Annotation.class.getName(); |
|
48 |
public static final String SUPER_TYPE_SETTING = SettingControl.class.getName(); |
|
49 |
public static final String SUPER_TYPE_EVENT = Event.class.getName(); |
|
50 |
public static final String EVENT_NAME_PREFIX = "jdk."; |
|
51 |
public static final String TYPES_PREFIX = "jdk.types."; |
|
52 |
public static final String SETTINGS_PREFIX = "jdk.settings."; |
|
53 |
||
54 |
||
55 |
// Initialization of known types |
|
56 |
private final static Map<Type, Class<?>> knownTypes = new HashMap<>(); |
|
57 |
static final Type BOOLEAN = register(boolean.class, new Type("boolean", null, 4)); |
|
58 |
static final Type CHAR = register(char.class, new Type("char", null, 5)); |
|
59 |
static final Type FLOAT = register(float.class, new Type("float", null, 6)); |
|
60 |
static final Type DOUBLE = register(double.class, new Type("double", null, 7)); |
|
61 |
static final Type BYTE = register(byte.class, new Type("byte", null, 8)); |
|
62 |
static final Type SHORT = register(short.class, new Type("short", null, 9)); |
|
63 |
static final Type INT = register(int.class, new Type("int", null, 10)); |
|
64 |
static final Type LONG = register(long.class, new Type("long", null, 11)); |
|
65 |
static final Type CLASS = register(Class.class, new Type("java.lang.Class", null, 20)); |
|
66 |
static final Type STRING = register(String.class, new Type("java.lang.String", null, 21)); |
|
67 |
static final Type THREAD = register(Thread.class, new Type("java.lang.Thread", null, 22)); |
|
68 |
static final Type STACK_TRACE = register(null, new Type(TYPES_PREFIX + "StackTrace", null, 23)); |
|
69 |
||
70 |
private final AnnotationConstruct annos = new AnnotationConstruct(); |
|
71 |
private final String name; |
|
72 |
private final String superType; |
|
73 |
private final boolean constantPool; |
|
74 |
private final ArrayList<ValueDescriptor> fields = new ArrayList<>(); |
|
75 |
private Boolean simpleType; // calculated lazy |
|
76 |
private boolean remove = true; |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
77 |
private long id; |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
78 |
|
50113 | 79 |
/** |
80 |
* Creates a type |
|
81 |
* |
|
82 |
* @param javaTypeName i.e "java.lang.String" |
|
83 |
* @param superType i.e "java.lang.Annotation" |
|
84 |
* @param id the class id that represents the class in the JVM |
|
85 |
* |
|
86 |
*/ |
|
87 |
public Type(String javaTypeName, String superType, long typeId) { |
|
88 |
this(javaTypeName, superType, typeId, false); |
|
89 |
} |
|
90 |
||
91 |
Type(String javaTypeName, String superType, long typeId, boolean contantPool) { |
|
92 |
this(javaTypeName, superType, typeId, contantPool, null); |
|
93 |
} |
|
94 |
||
95 |
Type(String javaTypeName, String superType, long typeId, boolean contantPool, Boolean simpleType) { |
|
96 |
Objects.requireNonNull(javaTypeName); |
|
97 |
||
98 |
if (!isValidJavaIdentifier(javaTypeName)) { |
|
99 |
throw new IllegalArgumentException(javaTypeName + " is not a valid Java identifier"); |
|
100 |
} |
|
101 |
this.constantPool = contantPool; |
|
102 |
this.superType = superType; |
|
103 |
this.name = javaTypeName; |
|
104 |
this.id = typeId; |
|
105 |
this.simpleType = simpleType; |
|
106 |
} |
|
107 |
||
108 |
static boolean isDefinedByJVM(long id) { |
|
109 |
return id < JVM.RESERVED_CLASS_ID_LIMIT; |
|
110 |
} |
|
111 |
||
112 |
public static long getTypeId(Class<?> clazz) { |
|
113 |
Type type = Type.getKnownType(clazz); |
|
114 |
return type == null ? JVM.getJVM().getTypeId(clazz) : type.getId(); |
|
115 |
} |
|
116 |
||
117 |
static Collection<Type> getKnownTypes() { |
|
118 |
return knownTypes.keySet(); |
|
119 |
} |
|
120 |
||
121 |
public static boolean isValidJavaIdentifier(String identifier) { |
|
122 |
if (identifier.isEmpty()) { |
|
123 |
return false; |
|
124 |
} |
|
125 |
if (!Character.isJavaIdentifierStart(identifier.charAt(0))) { |
|
126 |
return false; |
|
127 |
} |
|
128 |
for (int i = 1; i < identifier.length(); i++) { |
|
129 |
char c = identifier.charAt(i); |
|
130 |
if (c != '.') { |
|
131 |
if (!Character.isJavaIdentifierPart(c)) { |
|
132 |
return false; |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
return true; |
|
137 |
} |
|
138 |
||
139 |
public static boolean isValidJavaFieldType(String name) { |
|
140 |
for (Map.Entry<Type, Class<?>> entry : knownTypes.entrySet()) { |
|
141 |
Class<?> clazz = entry.getValue(); |
|
142 |
if (clazz != null && name.equals(clazz.getName())) { |
|
143 |
return true; |
|
144 |
} |
|
145 |
} |
|
146 |
return false; |
|
147 |
} |
|
148 |
||
149 |
public static Type getKnownType(String typeName) { |
|
150 |
for (Type type : knownTypes.keySet()) { |
|
151 |
if (type.getName().equals(typeName)) { |
|
152 |
return type; |
|
153 |
} |
|
154 |
} |
|
155 |
return null; |
|
156 |
} |
|
157 |
||
158 |
static boolean isKnownType(Class<?> type) { |
|
159 |
if (type.isPrimitive()) { |
|
160 |
return true; |
|
161 |
} |
|
162 |
if (type.equals(Class.class) || type.equals(Thread.class) || type.equals(String.class)) { |
|
163 |
return true; |
|
164 |
} |
|
165 |
return false; |
|
166 |
} |
|
167 |
||
168 |
public static Type getKnownType(Class<?> clazz) { |
|
169 |
for (Map.Entry<Type, Class<?>> entry : knownTypes.entrySet()) { |
|
170 |
if (clazz != null && clazz.equals(entry.getValue())) { |
|
171 |
return entry.getKey(); |
|
172 |
} |
|
173 |
} |
|
174 |
return null; |
|
175 |
} |
|
176 |
||
177 |
public String getName() { |
|
178 |
return name; |
|
179 |
} |
|
180 |
||
181 |
public String getLogName() { |
|
182 |
return getName() + "(" + getId() + ")"; |
|
183 |
} |
|
184 |
||
185 |
public List<ValueDescriptor> getFields() { |
|
186 |
return fields; |
|
187 |
} |
|
188 |
||
189 |
public boolean isSimpleType() { |
|
190 |
if (simpleType == null) { |
|
191 |
simpleType = calculateSimpleType(); |
|
192 |
} |
|
193 |
return simpleType.booleanValue(); |
|
194 |
} |
|
195 |
||
196 |
private boolean calculateSimpleType() { |
|
197 |
if (fields.size() != 1) { |
|
198 |
return false; |
|
199 |
} |
|
200 |
// annotation, settings and event can never be simple types |
|
201 |
return superType == null; |
|
202 |
} |
|
203 |
||
204 |
public boolean isDefinedByJVM() { |
|
205 |
return id < JVM.RESERVED_CLASS_ID_LIMIT; |
|
206 |
} |
|
207 |
||
208 |
private static Type register(Class<?> clazz, Type type) { |
|
209 |
knownTypes.put(type, clazz); |
|
210 |
return type; |
|
211 |
} |
|
212 |
||
213 |
public void add(ValueDescriptor valueDescriptor) { |
|
214 |
Objects.requireNonNull(valueDescriptor); |
|
215 |
fields.add(valueDescriptor); |
|
216 |
} |
|
217 |
||
218 |
void trimFields() { |
|
219 |
fields.trimToSize(); |
|
220 |
} |
|
221 |
||
222 |
void setAnnotations(List<AnnotationElement> annotations) { |
|
223 |
annos.setAnnotationElements(annotations); |
|
224 |
} |
|
225 |
||
226 |
public String getSuperType() { |
|
227 |
return superType; |
|
228 |
} |
|
229 |
||
230 |
public long getId() { |
|
231 |
return id; |
|
232 |
} |
|
233 |
||
234 |
public boolean isConstantPool() { |
|
235 |
return constantPool; |
|
236 |
} |
|
237 |
||
238 |
public String getLabel() { |
|
239 |
return annos.getLabel(); |
|
240 |
} |
|
241 |
||
242 |
public List<AnnotationElement> getAnnotationElements() { |
|
243 |
return annos.getUnmodifiableAnnotationElements(); |
|
244 |
} |
|
245 |
||
246 |
public <T> T getAnnotation(Class<? extends java.lang.annotation.Annotation> clazz) { |
|
247 |
return annos.getAnnotation(clazz); |
|
248 |
} |
|
249 |
||
250 |
public String getDescription() { |
|
251 |
return annos.getDescription(); |
|
252 |
} |
|
253 |
||
254 |
@Override |
|
255 |
public int hashCode() { |
|
256 |
return Long.hashCode(id); |
|
257 |
} |
|
258 |
||
259 |
@Override |
|
260 |
public boolean equals(Object object) { |
|
261 |
if (object instanceof Type) { |
|
262 |
Type that = (Type) object; |
|
263 |
return that.id == this.id; |
|
264 |
} |
|
265 |
return false; |
|
266 |
} |
|
267 |
||
268 |
@Override |
|
269 |
public int compareTo(Type that) { |
|
270 |
return Long.compare(this.id, that.id); |
|
271 |
} |
|
272 |
||
273 |
void log(String action, LogTag logTag, LogLevel level) { |
|
52413 | 274 |
if (Logger.shouldLog(logTag, level) && !isSimpleType()) { |
50113 | 275 |
Logger.log(logTag, LogLevel.TRACE, action + " " + typeText() + " " + getLogName() + " {"); |
276 |
for (ValueDescriptor v : getFields()) { |
|
277 |
String array = v.isArray() ? "[]" : ""; |
|
278 |
Logger.log(logTag, LogLevel.TRACE, " " + v.getTypeName() + array + " " + v.getName() + ";"); |
|
279 |
} |
|
280 |
Logger.log(logTag, LogLevel.TRACE, "}"); |
|
281 |
} else { |
|
52413 | 282 |
if (Logger.shouldLog(logTag, LogLevel.INFO) && !isSimpleType()) { |
50113 | 283 |
Logger.log(logTag, LogLevel.INFO, action + " " + typeText() + " " + getLogName()); |
284 |
} |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
private String typeText() { |
|
289 |
if (this instanceof PlatformEventType) { |
|
290 |
return "event type"; |
|
291 |
} |
|
292 |
if (Type.SUPER_TYPE_SETTING.equals(superType)) { |
|
293 |
return "setting type"; |
|
294 |
} |
|
295 |
if (Type.SUPER_TYPE_ANNOTATION.equals(superType)) { |
|
296 |
return "annotation type"; |
|
297 |
} |
|
298 |
return "type"; |
|
299 |
} |
|
300 |
||
301 |
@Override |
|
302 |
public String toString() { |
|
303 |
StringBuilder sb = new StringBuilder(); |
|
304 |
sb.append(getLogName()); |
|
305 |
if (!getFields().isEmpty()) { |
|
306 |
sb.append(" {\n"); |
|
307 |
for (ValueDescriptor td : getFields()) { |
|
308 |
sb.append(" type=" + td.getTypeName() + "(" + td.getTypeId() + ") name=" + td.getName() + "\n"); |
|
309 |
} |
|
310 |
sb.append("}\n"); |
|
311 |
} |
|
312 |
return sb.toString(); |
|
313 |
} |
|
314 |
||
315 |
public void setRemove(boolean remove) { |
|
316 |
this.remove = remove; |
|
317 |
} |
|
318 |
||
319 |
public boolean getRemove() { |
|
320 |
return remove; |
|
321 |
} |
|
52334
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
322 |
|
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
323 |
public void setId(long id) { |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
324 |
this.id = id; |
a181612f0715
8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents:
50113
diff
changeset
|
325 |
} |
50113 | 326 |
} |