Search in sources :

Example 1 with DescriptorKey

use of javax.management.DescriptorKey in project jdk8u_jdk by JetBrains.

the class Introspector method descriptorForAnnotations.

public static Descriptor descriptorForAnnotations(Annotation[] annots) {
    if (annots.length == 0)
        return ImmutableDescriptor.EMPTY_DESCRIPTOR;
    Map<String, Object> descriptorMap = new HashMap<String, Object>();
    for (Annotation a : annots) {
        Class<? extends Annotation> c = a.annotationType();
        Method[] elements = c.getMethods();
        boolean packageAccess = false;
        for (Method element : elements) {
            DescriptorKey key = element.getAnnotation(DescriptorKey.class);
            if (key != null) {
                String name = key.value();
                Object value;
                try {
                    // Avoid checking access more than once per annotation
                    if (!packageAccess) {
                        ReflectUtil.checkPackageAccess(c);
                        packageAccess = true;
                    }
                    value = MethodUtil.invoke(element, a, null);
                } catch (RuntimeException e) {
                    //
                    throw e;
                } catch (Exception e) {
                    // we don't expect this
                    throw new UndeclaredThrowableException(e);
                }
                value = annotationToField(value);
                Object oldValue = descriptorMap.put(name, value);
                if (oldValue != null && !equals(oldValue, value)) {
                    final String msg = "Inconsistent values for descriptor field " + name + " from annotations: " + value + " :: " + oldValue;
                    throw new IllegalArgumentException(msg);
                }
            }
        }
    }
    if (descriptorMap.isEmpty())
        return ImmutableDescriptor.EMPTY_DESCRIPTOR;
    else
        return new ImmutableDescriptor(descriptorMap);
}
Also used : ImmutableDescriptor(javax.management.ImmutableDescriptor) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) DescriptorKey(javax.management.DescriptorKey) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) AttributeNotFoundException(javax.management.AttributeNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException)

Aggregations

Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 HashMap (java.util.HashMap)1 WeakHashMap (java.util.WeakHashMap)1 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 DescriptorKey (javax.management.DescriptorKey)1 ImmutableDescriptor (javax.management.ImmutableDescriptor)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1