1 /*
 2  * Copyright (c) 2016, 2023, 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 package jdk.jfr.internal;
26 
27 import java.util.HashMap;
28 import java.util.Map;
29 
30 import jdk.jfr.Event;
31 import jdk.jfr.events.DeserializationEvent;
32 import jdk.jfr.events.ErrorThrownEvent;
33 import jdk.jfr.events.ExceptionStatisticsEvent;
34 import jdk.jfr.events.ExceptionThrownEvent;
35 import jdk.jfr.events.ProcessStartEvent;
36 import jdk.jfr.events.SecurityPropertyModificationEvent;
37 import jdk.jfr.events.SecurityProviderServiceEvent;
38 import jdk.jfr.events.SerializationMisdeclarationEvent;
39 import jdk.jfr.events.SocketReadEvent;
40 import jdk.jfr.events.SocketWriteEvent;
41 import jdk.jfr.events.TLSHandshakeEvent;
42 import jdk.jfr.events.ThreadSleepEvent;
43 import jdk.jfr.events.VirtualThreadEndEvent;
44 import jdk.jfr.events.VirtualThreadPinnedEvent;
45 import jdk.jfr.events.VirtualThreadStartEvent;
46 import jdk.jfr.events.VirtualThreadSubmitFailedEvent;
47 import jdk.jfr.events.X509CertificateEvent;
48 import jdk.jfr.events.X509ValidationEvent;
49 
50 public final class MirrorEvents {
51     private static final Class<?>[] mirrorEventClasses = {
52         DeserializationEvent.class,
53         ProcessStartEvent.class,
54         SecurityPropertyModificationEvent.class,
55         SecurityProviderServiceEvent.class,
56         SerializationMisdeclarationEvent.class,
57         SocketReadEvent.class,
58         SocketWriteEvent.class,
59         ThreadSleepEvent.class,
60         TLSHandshakeEvent.class,
61         VirtualThreadStartEvent.class,
62         VirtualThreadEndEvent.class,
63         VirtualThreadPinnedEvent.class,
64         VirtualThreadSubmitFailedEvent.class,
65         X509CertificateEvent.class,
66         X509ValidationEvent.class,
67         ErrorThrownEvent.class,
68         ExceptionStatisticsEvent.class,
69         ExceptionThrownEvent.class,
70     };
71 
72     private static final Map<String, Class<? extends Event>> mirrorLookup = createLookup();
73 
74     public static Class<? extends Event> find(String name) {
75         // When <clinit> of this class is executed it may lead
76         // to a JVM up call and invocation of this method before
77         // the mirrorLookup field has been set. This is fine,
78         // mirrors should not be instrumented.
79         if (mirrorLookup != null) {
80             return mirrorLookup.get(name);
81         }
82         return null;
83     }
84 
85     @SuppressWarnings("unchecked")
86     private static Map<String, Class<? extends Event>> createLookup() {
87         Map<String, Class<? extends Event>> mirrors = new HashMap<>();
88         for (Class<?> eventClass : mirrorEventClasses) {
89             MirrorEvent me = eventClass.getAnnotation(MirrorEvent.class);
90             if (me == null) {
91                 throw new InternalError("Mirror class must have annotation " + MirrorEvent.class.getName());
92             }
93             String fullName = me.module() + ":" + me.className();
94             mirrors.put(fullName, (Class<? extends Event>) eventClass);
95         }
96         return mirrors;
97     }
98 }