src/jdk.jfr/share/classes/jdk/jfr/internal/AnnotationConstruct.java
changeset 50113 caf115bb98ad
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     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.lang.annotation.Annotation;
       
    29 import java.lang.reflect.InvocationHandler;
       
    30 import java.lang.reflect.Method;
       
    31 import java.lang.reflect.Proxy;
       
    32 import java.util.Collections;
       
    33 import java.util.List;
       
    34 
       
    35 import jdk.jfr.AnnotationElement;
       
    36 import jdk.jfr.Description;
       
    37 import jdk.jfr.Label;
       
    38 import jdk.jfr.Unsigned;
       
    39 
       
    40 public final class AnnotationConstruct {
       
    41 
       
    42     private static final class AnnotationInvokationHandler implements InvocationHandler {
       
    43 
       
    44         private final AnnotationElement annotationElement;
       
    45 
       
    46         AnnotationInvokationHandler(AnnotationElement a) {
       
    47             this.annotationElement = a;
       
    48         }
       
    49 
       
    50         @Override
       
    51         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
       
    52             String methodName = method.getName();
       
    53             int parameters = method.getTypeParameters().length;
       
    54             if (parameters == 0 && annotationElement.hasValue(methodName)) {
       
    55                 return annotationElement.getValue(methodName);
       
    56             }
       
    57             throw new UnsupportedOperationException("Flight Recorder proxy only supports members declared in annotation interfaces, i.e. not toString, equals etc.");
       
    58         }
       
    59     }
       
    60 
       
    61     private List<AnnotationElement> annotationElements = Collections.emptyList();
       
    62     private byte unsignedFlag = -1;
       
    63     public AnnotationConstruct(List<AnnotationElement> ann) {
       
    64         this.annotationElements = ann;
       
    65     }
       
    66 
       
    67     public AnnotationConstruct() {
       
    68     }
       
    69 
       
    70     public void setAnnotationElements(List<AnnotationElement> elements) {
       
    71         annotationElements = Utils.smallUnmodifiable(elements);
       
    72     }
       
    73 
       
    74     public String getLabel() {
       
    75         Label label = getAnnotation(Label.class);
       
    76         if (label == null) {
       
    77             return null;
       
    78         }
       
    79         return label.value();
       
    80     }
       
    81 
       
    82     public String getDescription() {
       
    83         Description description = getAnnotation(Description.class);
       
    84         if (description == null) {
       
    85             return null;
       
    86         }
       
    87         return description.value();
       
    88     }
       
    89 
       
    90     @SuppressWarnings("unchecked")
       
    91     public final <T> T getAnnotation(Class<? extends Annotation> clazz) {
       
    92         AnnotationElement ae = getAnnotationElement(clazz);
       
    93         if (ae != null) {
       
    94             return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[] { clazz }, new AnnotationInvokationHandler(ae));
       
    95         }
       
    96         return null;
       
    97     }
       
    98 
       
    99     public List<AnnotationElement> getUnmodifiableAnnotationElements() {
       
   100         return annotationElements;
       
   101     }
       
   102 
       
   103     // package private
       
   104     boolean remove(AnnotationElement annotation) {
       
   105         return annotationElements.remove(annotation);
       
   106     }
       
   107 
       
   108     private AnnotationElement getAnnotationElement(Class<? extends Annotation> clazz) {
       
   109         // if multiple annotation elements with the same name exists, prioritize
       
   110         // the one with the same id. Note, id alone is not a guarantee, since it
       
   111         // may differ between JVM instances.
       
   112         long id = Type.getTypeId(clazz);
       
   113         String className = clazz.getName();
       
   114         for (AnnotationElement a : getUnmodifiableAnnotationElements()) {
       
   115             if (a.getTypeId() == id && a.getTypeName().equals(className)) {
       
   116                 return a;
       
   117             }
       
   118         }
       
   119         for (AnnotationElement a : getUnmodifiableAnnotationElements()) {
       
   120             if (a.getTypeName().equals(className)) {
       
   121                 return a;
       
   122             }
       
   123         }
       
   124         return null;
       
   125     }
       
   126 
       
   127     public boolean hasUnsigned() {
       
   128         // Must be initialized lazily since some annotation elements
       
   129         // are added after construction
       
   130         if (unsignedFlag < 0) {
       
   131             Unsigned unsigned = getAnnotation(Unsigned.class);
       
   132             unsignedFlag = (byte) (unsigned == null ? 0 :1);
       
   133         }
       
   134         return unsignedFlag == (byte)1 ? true : false;
       
   135     }
       
   136 }