jdk.jfr.AnnotationElement Java Examples
The following examples show how to use
jdk.jfr.AnnotationElement.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetadataRepository.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private EventHandler makeHandler(Class<? extends Event> eventClass, List<AnnotationElement> dynamicAnnotations, List<ValueDescriptor> dynamicFields) throws InternalError { SecuritySupport.addHandlerExport(eventClass); PlatformEventType pEventType = (PlatformEventType) TypeLibrary.createType(eventClass, dynamicAnnotations, dynamicFields); EventType eventType = PrivateAccess.getInstance().newEventType(pEventType); EventControl ec = new EventControl(pEventType, eventClass); Class<? extends EventHandler> handlerClass = null; try { String eventHandlerName = EventHandlerCreator.makeEventHandlerName(eventType.getId()); handlerClass = Class.forName(eventHandlerName, false, Event.class.getClassLoader()).asSubclass(EventHandler.class); // Created eagerly on class load, tag as instrumented pEventType.setInstrumented(); Logger.log(JFR_SYSTEM, DEBUG, "Found existing event handler for " + eventType.getName()); } catch (ClassNotFoundException cne) { EventHandlerCreator ehc = new EventHandlerCreator(eventType.getId(), ec.getSettingInfos(), eventType, eventClass); handlerClass = ehc.makeEventHandlerClass(); Logger.log(LogTag.JFR_SYSTEM, DEBUG, "Created event handler for " + eventType.getName()); } EventHandler handler = EventHandlerCreator.instantiateEventHandler(handlerClass, true, eventType, ec); Utils.setHandler(eventClass, handler); return handler; }
Example #2
Source File: TestExperimental.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { EventType t = EventType.getEventType(ExperimentalEvent.class); // @Experimental on event Experimental e = t.getAnnotation(Experimental.class); Asserts.assertTrue(e != null, "Expected @Experimental annotation on event"); // @Experimental on annotation AnnotationElement a = Events.getAnnotationByName(t, ExperimentalAnnotation.class.getName()); e = a.getAnnotation(Experimental.class); Asserts.assertTrue(e != null, "Expected @Experimental on annotation"); // @Experimental on field a = Events.getAnnotation(t.getField("experimentalField"), Experimental.class); Asserts.assertTrue(e != null, "Expected @Experimental on field"); }
Example #3
Source File: TestExperimental.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { EventType t = EventType.getEventType(ExperimentalEvent.class); // @Experimental on event Experimental e = t.getAnnotation(Experimental.class); Asserts.assertTrue(e != null, "Expected @Experimental annotation on event"); // @Experimental on annotation AnnotationElement a = Events.getAnnotationByName(t, ExperimentalAnnotation.class.getName()); e = a.getAnnotation(Experimental.class); Asserts.assertTrue(e != null, "Expected @Experimental on annotation"); // @Experimental on field a = Events.getAnnotation(t.getField("experimentalField"), Experimental.class); Asserts.assertTrue(e != null, "Expected @Experimental on field"); }
Example #4
Source File: TestDescription.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { EventType t = EventType.getEventType(DescriptionEvent.class); // field description AnnotationElement aMax = Events.getAnnotation(t.getField("field"), Description.class); String d = (String) aMax.getValue("value"); Asserts.assertEquals("Field Annotation", d, "Incorrect annotation for field, got '" + d + "'"); // event description d = t.getAnnotation(Description.class).value(); Asserts.assertEquals("Event Annotation", d, "Incorrect annotation for event, got '" + d + "'"); // annotation description AnnotationElement a = Events.getAnnotationByName(t, AnnotationWithDescription.class.getName()); Description de = a.getAnnotation(Description.class); Asserts.assertEquals("Meta Annotation", de.value(), "Incorrect annotation for event, got '" + de.value() + "'"); for (SettingDescriptor v: t.getSettingDescriptors()) { if (v.getName().equals("dummy")) { Description settingDescription = v.getAnnotation(Description.class); Asserts.assertEquals(settingDescription.value(), "Setting description", "Incorrect description for setting"); } } }
Example #5
Source File: MetadataRepository.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
public synchronized EventType register(Class<? extends Event> eventClass, List<AnnotationElement> dynamicAnnotations, List<ValueDescriptor> dynamicFields) { Utils.checkRegisterPermission(); EventHandler handler = getHandler(eventClass); if (handler == null) { handler = makeHandler(eventClass, dynamicAnnotations, dynamicFields); } handler.setRegistered(true); typeLibrary.addType(handler.getPlatformEventType()); if (jvm.isRecording()) { storeDescriptorInJVM(); // needed for emergency dump settingsManager.setEventControl(handler.getEventControl()); settingsManager.updateRetransform(Collections.singletonList((eventClass))); } else { setStaleMetadata(); } return handler.getEventType(); }
Example #6
Source File: TestDescription.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { EventType t = EventType.getEventType(DescriptionEvent.class); // field description AnnotationElement aMax = Events.getAnnotation(t.getField("field"), Description.class); String d = (String) aMax.getValue("value"); Asserts.assertEquals("Field Annotation", d, "Incorrect annotation for field, got '" + d + "'"); // event description d = t.getAnnotation(Description.class).value(); Asserts.assertEquals("Event Annotation", d, "Incorrect annotation for event, got '" + d + "'"); // annotation description AnnotationElement a = Events.getAnnotationByName(t, AnnotationWithDescription.class.getName()); Description de = a.getAnnotation(Description.class); Asserts.assertEquals("Meta Annotation", de.value(), "Incorrect annotation for event, got '" + de.value() + "'"); for (SettingDescriptor v: t.getSettingDescriptors()) { if (v.getName().equals("dummy")) { Description settingDescription = v.getAnnotation(Description.class); Asserts.assertEquals(settingDescription.value(), "Setting description", "Incorrect description for setting"); } } }
Example #7
Source File: TypeLibrary.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
public static Type createAnnotationType(Class<? extends Annotation> a) { if (shouldPersist(a)) { Type type = defineType(a, Type.SUPER_TYPE_ANNOTATION, false); if (type != null) { for (Method method : a.getDeclaredMethods()) { type.add(PrivateAccess.getInstance().newValueDescriptor(method.getReturnType(), method.getName())); } ArrayList<AnnotationElement> aes = new ArrayList<>(); for (Annotation annotation : resolveRepeatedAnnotations(a.getAnnotations())) { AnnotationElement ae = createAnnotation(annotation); if (ae != null) { aes.add(ae); } } aes.trimToSize(); type.setAnnotations(aes); } return getType(a); } return null; }
Example #8
Source File: TypeLibrary.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
private static void addUserFields(Class<?> clazz, Type type, List<ValueDescriptor> dynamicFields) { Map<String, ValueDescriptor> dynamicFieldSet = new HashMap<>(); for (ValueDescriptor dynamicField : dynamicFields) { dynamicFieldSet.put(dynamicField.getName(), dynamicField); } List<Type> newTypes = new ArrayList<>(); for (Field field : Utils.getVisibleEventFields(clazz)) { ValueDescriptor vd = dynamicFieldSet.get(field.getName()); if (vd != null) { if (!vd.getTypeName().equals(field.getType().getName())) { throw new InternalError("Type expected to match for field " + vd.getName() + " expected " + field.getName() + " but got " + vd.getName()); } for (AnnotationElement ae : vd.getAnnotationElements()) { newTypes.add(PrivateAccess.getInstance().getType(ae)); } newTypes.add(PrivateAccess.getInstance().getType(vd)); } else { vd = createField(field); } if (vd != null) { type.add(vd); } } addTypes(newTypes); }
Example #9
Source File: TestName.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { EventType t = EventType.getEventType(NamedEvent.class); ValueDescriptor testField = t.getField("testField"); SettingDescriptor setting = getSetting(t, "name"); AnnotationElement a = Events.getAnnotationByName(t, "com.oracle.TestAnnotation"); // Check that names are overridden Asserts.assertNotNull(testField, "Can't find expected field testField"); Asserts.assertEquals(t.getName(), "com.oracle.TestEvent", "Incorrect name for event"); Asserts.assertEquals(a.getTypeName(), "com.oracle.TestAnnotation", "Incorrect name for annotation"); Asserts.assertEquals(a.getTypeName(), "com.oracle.TestAnnotation", "Incorrect name for annotation"); Asserts.assertEquals(setting.getName(), "name", "Incorrect name for setting"); // Check that @Name is persisted assertAnnotation(t.getAnnotation(Name.class), "@Name should be persisted on event"); assertAnnotation(testField.getAnnotation(Name.class), "@Name should be persisted on field"); assertAnnotation(a.getAnnotation(Name.class), "@Name should be persisted on annotations"); assertAnnotation(setting.getAnnotation(Name.class), "@Name should be persisted on setting"); }
Example #10
Source File: TypeLibrary.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private static void addUserFields(Class<?> clazz, Type type, List<ValueDescriptor> dynamicFields) { Map<String, ValueDescriptor> dynamicFieldSet = new HashMap<>(); for (ValueDescriptor dynamicField : dynamicFields) { dynamicFieldSet.put(dynamicField.getName(), dynamicField); } List<Type> newTypes = new ArrayList<>(); for (Field field : Utils.getVisibleEventFields(clazz)) { ValueDescriptor vd = dynamicFieldSet.get(field.getName()); if (vd != null) { if (!vd.getTypeName().equals(field.getType().getName())) { throw new InternalError("Type expected to match for field " + vd.getName() + " expected " + field.getName() + " but got " + vd.getName()); } for (AnnotationElement ae : vd.getAnnotationElements()) { newTypes.add(PrivateAccess.getInstance().getType(ae)); } newTypes.add(PrivateAccess.getInstance().getType(vd)); } else { vd = createField(field); } if (vd != null) { type.add(vd); } } addTypes(newTypes); }
Example #11
Source File: EventClassBuilder.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private void buildClassInfo() { String internalSuperName = ASMToolkit.getInternalName(Event.class.getName()); String internalClassName = type.getInternalName(); classWriter.visit(52, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, internalClassName, null, internalSuperName, null); for (AnnotationElement a : annotationElements) { String descriptor = ASMToolkit.getDescriptor(a.getTypeName()); AnnotationVisitor av = classWriter.visitAnnotation(descriptor, true); for (ValueDescriptor v : a.getValueDescriptors()) { Object value = a.getValue(v.getName()); String name = v.getName(); if (v.isArray()) { AnnotationVisitor arrayVisitor = av.visitArray(name); Object[] array = (Object[]) value; for (int i = 0; i < array.length; i++) { arrayVisitor.visit(null, array[i]); } arrayVisitor.visitEnd(); } else { av.visit(name, value); } } av.visitEnd(); } }
Example #12
Source File: PrettyWriter.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
private void printAnnotation(AnnotationElement a) throws IOException { StringJoiner sj = new StringJoiner(", "); for (ValueDescriptor v : a.getValueDescriptors()) { StringBuilder sb = new StringBuilder(); Object o = a.getValue(v.getName()); if (o instanceof String) { sb.append("\""); sb.append((String) o); sb.append("\""); } else { sb.append(o); } sj.add(sb.toString()); } print(sj.toString()); }
Example #13
Source File: EventControl.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
EventControl(PlatformEventType eventType) { eventControls.put(Enabled.NAME, defineEnabled(eventType)); if (eventType.hasDuration()) { eventControls.put(Threshold.NAME, defineThreshold(eventType)); } if (eventType.hasStackTrace()) { eventControls.put(StackTrace.NAME, defineStackTrace(eventType)); } if (eventType.hasPeriod()) { eventControls.put(Period.NAME, definePeriod(eventType)); } if (eventType.hasCutoff()) { eventControls.put(Cutoff.NAME, defineCutoff(eventType)); } ArrayList<AnnotationElement> aes = new ArrayList<>(eventType.getAnnotationElements()); remove(eventType, aes, Threshold.class); remove(eventType, aes, Period.class); remove(eventType, aes, Enabled.class); remove(eventType, aes, StackTrace.class); remove(eventType, aes, Cutoff.class); aes.trimToSize(); eventType.setAnnotations(aes); this.type = eventType; this.idName = String.valueOf(eventType.getId()); }
Example #14
Source File: PrettyWriter.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
private void printAnnotations(int commentIndex, List<AnnotationElement> annotations) throws IOException { for (AnnotationElement a : annotations) { printIndent(); print("@"); print(makeSimpleType(a.getTypeName())); List<ValueDescriptor> vs = a.getValueDescriptors(); if (!vs.isEmpty()) { print("("); printAnnotation(a); print(")"); printCommentRef(commentIndex, a.getTypeId()); } else { println(); } } }
Example #15
Source File: TestMetadata.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { EventType t = EventType.getEventType(MetadataEvent.class); ValueDescriptor field = t.getField("field"); Events.hasAnnotation(field, FirstJFRAnnotation.class); Asserts.assertTrue(field.getAnnotation(OtherAnnotation.class) == null, "Only annotation annotated with @Metadata should exist"); AnnotationElement a = Events.getAnnotationByName(t, FirstJFRAnnotation.class.getName()); Asserts.assertTrue(a.getAnnotation(SecondJFRAnnotation.class) != null , "Annotations with @Metadata should be followed for indirect annotations"); }
Example #16
Source File: Events.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public static void assertAnnotation(ValueDescriptor field, Class<? extends java.lang.annotation.Annotation> annotationClass, String value) throws Exception { AnnotationElement a = getAnnotation(field, annotationClass); Object v = a.getValue("value"); if (!v.equals(value)) { throw new Exception("Expected " + annotationClass.getSimpleName() + " on field " + field.getName() + " to have value " + value + ", but got " + v); } }
Example #17
Source File: AnnotationConstruct.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@SuppressWarnings("unchecked") public final <T> T getAnnotation(Class<? extends Annotation> clazz) { AnnotationElement ae = getAnnotationElement(clazz); if (ae != null) { return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[] { clazz }, new AnnotationInvokationHandler(ae)); } return null; }
Example #18
Source File: Events.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static AnnotationElement getAnnotation(ValueDescriptor v, Class<?> clazz) throws Exception { for (AnnotationElement a : v.getAnnotationElements()) { if (a.getTypeName().equals(clazz.getName())) { return a; } } throw new Exception("Could not find annotation " + clazz.getName()); }
Example #19
Source File: Events.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static AnnotationElement getAnnotationByName(EventType t, String name) throws Exception { for (AnnotationElement a : t.getAnnotationElements()) { if (a.getTypeName().equals(name)) { return a; } } throw new Exception("Could not find annotation '" + name + " in type " + t.getName()); }
Example #20
Source File: TestDynamicAnnotations.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public static void testArray() throws Exception { List<AnnotationElement> annotations = new ArrayList<>(); Map<String, Object> values = new HashMap<>(); values.put("stringArray", new String[] {"zero", "one"}); values.put("intArray", new int[] {0, 1}); values.put("longArray", new long[] {0L, 1L}); values.put("floatArray", new float[] {0.0f, 1.0f}); values.put("doubleArray", new double[] {0.0, 1.0}); values.put("booleanArray", new boolean[] {false, true}); values.put("shortArray", new short[] {(short)0, (short)1}); values.put("byteArray", new byte[] {(byte)0, (byte)1}); values.put("charArray", new char[] {'0','1'}); annotations.add(new AnnotationElement(Array.class, values)); EventFactory f = EventFactory.create(annotations, Collections.emptyList()); Array a = f.getEventType().getAnnotation(Array.class); if (a == null) { throw new Exception("Missing array annotation"); } verifyArrayAnnotation(a); System.out.println("Event metadata is correct"); try (Recording r = new Recording()) { r.start(); Event e = f.newEvent(); e.commit(); r.stop(); List<RecordedEvent> events = Events.fromRecording(r); Events.hasEvents(events); RecordedEvent re = events.get(0); Array arrayAnnotation = re.getEventType().getAnnotation(Array.class); if (arrayAnnotation== null) { throw new Exception("Missing array annotation"); } verifyArrayAnnotation(arrayAnnotation); System.out.println("Persisted event metadata is correct"); } }
Example #21
Source File: TestDynamicAnnotations.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void testArray() throws Exception { List<AnnotationElement> annotations = new ArrayList<>(); Map<String, Object> values = new HashMap<>(); values.put("stringArray", new String[] {"zero", "one"}); values.put("intArray", new int[] {0, 1}); values.put("longArray", new long[] {0L, 1L}); values.put("floatArray", new float[] {0.0f, 1.0f}); values.put("doubleArray", new double[] {0.0, 1.0}); values.put("booleanArray", new boolean[] {false, true}); values.put("shortArray", new short[] {(short)0, (short)1}); values.put("byteArray", new byte[] {(byte)0, (byte)1}); values.put("charArray", new char[] {'0','1'}); annotations.add(new AnnotationElement(Array.class, values)); EventFactory f = EventFactory.create(annotations, Collections.emptyList()); Array a = f.getEventType().getAnnotation(Array.class); if (a == null) { throw new Exception("Missing array annotation"); } verifyArrayAnnotation(a); System.out.println("Event metadata is correct"); try (Recording r = new Recording()) { r.start(); Event e = f.newEvent(); e.commit(); r.stop(); List<RecordedEvent> events = Events.fromRecording(r); Events.hasEvents(events); RecordedEvent re = events.get(0); Array arrayAnnotation = re.getEventType().getAnnotation(Array.class); if (arrayAnnotation== null) { throw new Exception("Missing array annotation"); } verifyArrayAnnotation(arrayAnnotation); System.out.println("Persisted event metadata is correct"); } }
Example #22
Source File: TestContentType.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { EventType t = EventType.getEventType(SunnyDay.class); AnnotationElement aMax = Events.getAnnotation(t.getField("max"), Temperature.class); ContentType cMax = aMax.getAnnotation(ContentType.class); if (cMax == null) { throw new Exception("Expected Temperature annotation for field 'max' to have meta annotation ContentType"); } AnnotationElement aHours = Events.getAnnotation(t.getField("hours"), Timespan.class); ContentType cHours = aHours.getAnnotation(ContentType.class); Asserts.assertTrue(cHours != null, "Expected Timespan annotation for field 'hours' to have meta annotation ContentType"); }
Example #23
Source File: TestMetadata.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { EventType t = EventType.getEventType(MetadataEvent.class); ValueDescriptor field = t.getField("field"); Events.hasAnnotation(field, FirstJFRAnnotation.class); Asserts.assertTrue(field.getAnnotation(OtherAnnotation.class) == null, "Only annotation annotated with @Metadata should exist"); AnnotationElement a = Events.getAnnotationByName(t, FirstJFRAnnotation.class.getName()); Asserts.assertTrue(a.getAnnotation(SecondJFRAnnotation.class) != null , "Annotations with @Metadata should be followed for indirect annotations"); }
Example #24
Source File: EventControl.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
static void remove(PlatformEventType type, List<AnnotationElement> aes, Class<? extends java.lang.annotation.Annotation> clazz) { long id = Type.getTypeId(clazz); for (AnnotationElement a : type.getAnnotationElements()) { if (a.getTypeId() == id && a.getTypeName().equals(clazz.getName())) { aes.remove(a); } } }
Example #25
Source File: TestGetAnnotations.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private static void assertAnnotation(List<AnnotationElement> annos, String name, Object expectedValue) { for (AnnotationElement a : annos) { if (a.getTypeName().equals(name)) { Object value = a.getValue("value"); Asserts.assertTrue(Objects.deepEquals(value, expectedValue), "Found annotation " + name + " but value was "+ value +" but expected " + expectedValue); } } }
Example #26
Source File: TestGetAnnotations.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Throwable { EventType type = EventType.getEventType(MyEvent.class); List<AnnotationElement> annos = type.getAnnotationElements(); Asserts.assertEquals(annos.size(), 2, "Wrong number of annotations"); assertAnnotation(annos, "jdk.jfr.Label", MY_LABEL); assertAnnotation(annos, "jdk.jfr.Description", MY_DESCRIPTION); }
Example #27
Source File: TestJavaEvent.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private static void printAnnotation(String indent, AnnotationElement a) { String name = removePackage(a.getTypeName()); if (a.getValues().isEmpty()) { System.out.println(indent + "@" + name); return; } System.out.print(indent + "@" + name + "("); for (Object o : a.getValues()) { printAnnotationValue(o); } System.out.println(")"); }
Example #28
Source File: TypeLibrary.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private static void visitAnnotations(Queue<Type> typeQ, List<AnnotationElement> aes) { Queue<AnnotationElement> aQ = new ArrayDeque<>(aes); Set<AnnotationElement> visited = new HashSet<>(); while (!aQ.isEmpty()) { AnnotationElement ae = aQ.poll(); if (!visited.contains(ae)) { Type ty = PrivateAccess.getInstance().getType(ae); typeQ.add(ty); visited.add(ae); } aQ.addAll(ae.getAnnotationElements()); } }
Example #29
Source File: TestGetAnnotationElements.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private static void assertContainsAnnotation(List<AnnotationElement> aez, Class<?> clz) { for (AnnotationElement ae : aez) { if (ae.getTypeName().equals(clz.getName())) { return; } } Asserts.fail("Class " + clz + " not found in the annotation elements"); }
Example #30
Source File: TestJavaEvent.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
private static void printAnnotation(String indent, AnnotationElement a) { String name = removePackage(a.getTypeName()); if (a.getValues().isEmpty()) { System.out.println(indent + "@" + name); return; } System.out.print(indent + "@" + name + "("); for (Object o : a.getValues()) { printAnnotationValue(o); } System.out.println(")"); }