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