package org.glassfish.gmbal.impl;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.management.Descriptor;
import org.glassfish.gmbal.DescriptorKey ;
import org.glassfish.gmbal.DescriptorFields ;
public class DescriptorIntrospector {
private DescriptorIntrospector() {
}
public static Descriptor descriptorForElement(
final ManagedObjectManagerInternal mom, final AnnotatedElement elmt) {
if (elmt == null) {
return DescriptorUtility.EMPTY_DESCRIPTOR;
}
Collection<Annotation> annots = null ;
if (mom == null) {
annots = Arrays.asList( elmt.getAnnotations() ) ;
} else {
annots = mom.getAnnotations( elmt );
}
return descriptorForAnnotations(annots);
}
private static Descriptor descriptorForAnnotations(Collection<Annotation> annots) {
if (annots.isEmpty()) {
return DescriptorUtility.EMPTY_DESCRIPTOR;
}
Map<String, Object> descriptorMap = new HashMap<String, Object>();
for (Annotation a : annots) {
if (a instanceof DescriptorFields) {
addDescriptorFieldsToMap(descriptorMap,
(DescriptorFields) a);
}
addAnnotationFieldsToMap(descriptorMap, a);
}
if (descriptorMap.isEmpty()) {
return DescriptorUtility.EMPTY_DESCRIPTOR;
} else {
return DescriptorUtility.makeDescriptor(descriptorMap);
}
}
private static void addDescriptorFieldsToMap(
Map<String, Object> descriptorMap, DescriptorFields df) {
for (String field : df.value()) {
int eq = field.indexOf('=');
if (eq < 0) {
throw Exceptions.self.excForAddDescriptorFieldsToMap( field ) ;
}
String name = field.substring(0, eq);
String value = field.substring(eq + 1);
addToMap(descriptorMap, name, value);
}
}
private static void addAnnotationFieldsToMap(
Map<String, Object> descriptorMap, Annotation a) {
Class<? extends Annotation> c = a.annotationType();
Method[] elements = c.getMethods();
for (Method element : elements) {
DescriptorKey key = element.getAnnotation(DescriptorKey.class);
if (key != null) {
String name = key.value();
Object value;
try {
value = element.invoke(a);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw Exceptions.self.excForAddAnnotationFieldsToMap( e ) ;
}
if (!key.omitIfDefault() ||
!equals(value, element.getDefaultValue())) {
value = annotationToField(value);
addToMap(descriptorMap, name, value);
}
}
}
}
private static void addToMap(
Map<String, Object> descriptorMap, String name, Object value) {
Object oldValue = descriptorMap.put(name, value);
if (oldValue != null && !equals(oldValue, value)) {
throw Exceptions.self.excForAddToMap( name, value, oldValue ) ;
}
}
private static Object annotationToField(Object x) {
if (x == null) {
return null;
}
if (x instanceof Number || x instanceof String ||
x instanceof Character || x instanceof Boolean ||
x instanceof String[]) {
return x;
}
Class<?> c = x.getClass();
if (c.isArray()) {
if (c.getComponentType().isPrimitive()) {
return x;
}
Object[] xx = (Object[]) x;
String[] ss = new String[xx.length];
for (int i = 0; i < xx.length; i++) {
ss[i] = (String) annotationToField(xx[i]);
}
return ss;
}
if (x instanceof Class) {
return ((Class<?>) x).getName();
}
if (x instanceof Enum) {
return ((Enum) x).name();
}
if (Proxy.isProxyClass(c)) {
c = c.getInterfaces()[0];
}
throw Exceptions.self.excForAnnotationToField( c.getName() ) ;
}
private static boolean equals(Object x, Object y) {
return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
}
}