50113
|
1 |
/*
|
58863
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
50113
|
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 |
|
58863
|
26 |
package jdk.jfr.internal.consumer;
|
50113
|
27 |
|
58863
|
28 |
import jdk.jfr.consumer.RecordedClass;
|
|
29 |
import jdk.jfr.consumer.RecordedClassLoader;
|
|
30 |
import jdk.jfr.consumer.RecordedFrame;
|
|
31 |
import jdk.jfr.consumer.RecordedMethod;
|
|
32 |
import jdk.jfr.consumer.RecordedStackTrace;
|
|
33 |
import jdk.jfr.consumer.RecordedThread;
|
|
34 |
import jdk.jfr.consumer.RecordedThreadGroup;
|
50113
|
35 |
import jdk.jfr.internal.Type;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Abstract factory for creating specialized types
|
|
39 |
*/
|
58863
|
40 |
public abstract class ObjectFactory<T> {
|
|
41 |
private static final JdkJfrConsumer PRIVATE_ACCESS = JdkJfrConsumer.instance();
|
50113
|
42 |
|
58863
|
43 |
private final static String TYPE_PREFIX_VERSION_1 = "com.oracle.jfr.types.";
|
|
44 |
private final static String TYPE_PREFIX_VERSION_2 = Type.TYPES_PREFIX;
|
|
45 |
public final static String STACK_FRAME_VERSION_1 = TYPE_PREFIX_VERSION_1 + "StackFrame";
|
|
46 |
public final static String STACK_FRAME_VERSION_2 = TYPE_PREFIX_VERSION_2 + "StackFrame";
|
50113
|
47 |
|
58863
|
48 |
static ObjectFactory<?> create(Type type, TimeConverter timeConverter) {
|
50113
|
49 |
switch (type.getName()) {
|
|
50 |
case "java.lang.Thread":
|
58863
|
51 |
return createThreadFactory(type, timeConverter);
|
50113
|
52 |
case TYPE_PREFIX_VERSION_1 + "StackFrame":
|
|
53 |
case TYPE_PREFIX_VERSION_2 + "StackFrame":
|
58863
|
54 |
return createFrameFactory(type, timeConverter);
|
50113
|
55 |
case TYPE_PREFIX_VERSION_1 + "Method":
|
|
56 |
case TYPE_PREFIX_VERSION_2 + "Method":
|
58863
|
57 |
return createMethodFactory(type, timeConverter);
|
50113
|
58 |
case TYPE_PREFIX_VERSION_1 + "ThreadGroup":
|
|
59 |
case TYPE_PREFIX_VERSION_2 + "ThreadGroup":
|
58863
|
60 |
return createdThreadGroupFactory(type, timeConverter);
|
50113
|
61 |
case TYPE_PREFIX_VERSION_1 + "StackTrace":
|
|
62 |
case TYPE_PREFIX_VERSION_2 + "StackTrace":
|
58863
|
63 |
return createStackTraceFactory(type, timeConverter);
|
50113
|
64 |
case TYPE_PREFIX_VERSION_1 + "ClassLoader":
|
|
65 |
case TYPE_PREFIX_VERSION_2 + "ClassLoader":
|
58863
|
66 |
return createClassLoaderFactory(type, timeConverter);
|
50113
|
67 |
case "java.lang.Class":
|
58863
|
68 |
return createClassFactory(type, timeConverter);
|
50113
|
69 |
}
|
|
70 |
return null;
|
|
71 |
}
|
|
72 |
|
58863
|
73 |
private static ObjectFactory<RecordedClass> createClassFactory(Type type, TimeConverter timeConverter) {
|
|
74 |
return new ObjectFactory<RecordedClass>(type, timeConverter) {
|
|
75 |
@Override
|
|
76 |
RecordedClass createTyped(ObjectContext objectContext, long id, Object[] values) {
|
|
77 |
return PRIVATE_ACCESS.newRecordedClass(objectContext, id, values);
|
|
78 |
}
|
|
79 |
};
|
|
80 |
}
|
|
81 |
|
|
82 |
private static ObjectFactory<?> createClassLoaderFactory(Type type, TimeConverter timeConverter) {
|
|
83 |
return new ObjectFactory<RecordedClassLoader>(type, timeConverter) {
|
|
84 |
@Override
|
|
85 |
RecordedClassLoader createTyped(ObjectContext objectContext, long id, Object[] values) {
|
|
86 |
return PRIVATE_ACCESS.newRecordedClassLoader(objectContext, id, values);
|
|
87 |
}
|
|
88 |
};
|
|
89 |
}
|
|
90 |
|
|
91 |
private static ObjectFactory<RecordedStackTrace> createStackTraceFactory(Type type, TimeConverter timeConverter) {
|
|
92 |
return new ObjectFactory<RecordedStackTrace>(type, timeConverter) {
|
|
93 |
@Override
|
|
94 |
RecordedStackTrace createTyped(ObjectContext objectContext, long id, Object[] values) {
|
|
95 |
return PRIVATE_ACCESS.newRecordedStackTrace(objectContext, values);
|
|
96 |
}
|
|
97 |
};
|
|
98 |
}
|
50113
|
99 |
|
58863
|
100 |
private static ObjectFactory<RecordedThreadGroup> createdThreadGroupFactory(Type type, TimeConverter timeConverter) {
|
|
101 |
return new ObjectFactory<RecordedThreadGroup>(type, timeConverter) {
|
|
102 |
@Override
|
|
103 |
RecordedThreadGroup createTyped(ObjectContext objectContext, long id, Object[] values) {
|
|
104 |
return PRIVATE_ACCESS.newRecordedThreadGroup(objectContext, values);
|
|
105 |
}
|
|
106 |
};
|
|
107 |
}
|
|
108 |
|
|
109 |
private static ObjectFactory<RecordedMethod> createMethodFactory(Type type, TimeConverter timeConverter) {
|
|
110 |
return new ObjectFactory<RecordedMethod>(type, timeConverter) {
|
|
111 |
@Override
|
|
112 |
RecordedMethod createTyped(ObjectContext objectContext, long id, Object[] values) {
|
|
113 |
return PRIVATE_ACCESS.newRecordedMethod(objectContext, values);
|
|
114 |
}
|
|
115 |
};
|
|
116 |
}
|
|
117 |
|
|
118 |
private static ObjectFactory<RecordedFrame> createFrameFactory(Type type, TimeConverter timeConverter) {
|
|
119 |
return new ObjectFactory<RecordedFrame>(type, timeConverter) {
|
|
120 |
@Override
|
|
121 |
RecordedFrame createTyped(ObjectContext objectContext, long id, Object[] values) {
|
|
122 |
return PRIVATE_ACCESS.newRecordedFrame(objectContext, values);
|
|
123 |
}
|
|
124 |
};
|
|
125 |
}
|
|
126 |
|
|
127 |
private static ObjectFactory<RecordedThread> createThreadFactory(Type type, TimeConverter timeConverter) {
|
|
128 |
return new ObjectFactory<RecordedThread>(type, timeConverter) {
|
|
129 |
@Override
|
|
130 |
RecordedThread createTyped(ObjectContext objectContext, long id, Object[] values) {
|
|
131 |
return PRIVATE_ACCESS.newRecordedThread(objectContext, id, values);
|
|
132 |
}
|
|
133 |
};
|
|
134 |
}
|
|
135 |
|
|
136 |
private final ObjectContext objectContext;
|
|
137 |
|
|
138 |
private ObjectFactory(Type type, TimeConverter timeConverter) {
|
|
139 |
this.objectContext = new ObjectContext(null, type.getFields(), timeConverter);
|
50113
|
140 |
}
|
|
141 |
|
|
142 |
T createObject(long id, Object value) {
|
|
143 |
if (value == null) {
|
|
144 |
return null;
|
|
145 |
}
|
|
146 |
if (value instanceof Object[]) {
|
58863
|
147 |
return createTyped(objectContext, id, (Object[]) value);
|
50113
|
148 |
}
|
58863
|
149 |
throw new InternalError("Object factory must have struct type. Type was " + value.getClass().getName());
|
50113
|
150 |
}
|
|
151 |
|
58863
|
152 |
abstract T createTyped(ObjectContext objectContextm, long id, Object[] values);
|
50113
|
153 |
}
|