| 1 |
/* |
| 2 |
* JBoss, Home of Professional Open Source. |
| 3 |
* |
| 4 |
* Copyright 2012 Red Hat, Inc. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.jboss.classfilewriter.annotations; |
| 19 |
|
| 20 |
import java.lang.annotation.Annotation; |
| 21 |
import java.lang.reflect.Array; |
| 22 |
import java.lang.reflect.InvocationTargetException; |
| 23 |
import java.lang.reflect.Method; |
| 24 |
import java.security.AccessController; |
| 25 |
import java.security.PrivilegedActionException; |
| 26 |
import java.security.PrivilegedExceptionAction; |
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
import org.jboss.classfilewriter.constpool.ConstPool; |
| 31 |
|
| 32 |
/** |
| 33 |
* Utility class that can be used to contruct annotations and annotation attributes from java {@link Annotation} instances |
| 34 |
* |
| 35 |
* @author Stuart Douglas |
| 36 |
*/ |
| 37 |
public class AnnotationBuilder { |
| 38 |
|
| 39 |
public static ClassAnnotation createAnnotation(ConstPool constPool, final Annotation annotation) { |
| 40 |
Class<? extends Annotation> annotationType = annotation.annotationType(); |
| 41 |
List<AnnotationValue> values = new ArrayList<AnnotationValue>(); |
| 42 |
try { |
| 43 |
for (final Method m : annotationType.getDeclaredMethods()) { |
| 44 |
Object value = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { |
| 45 |
@Override |
| 46 |
public Object run() throws InvocationTargetException, IllegalAccessException { |
| 47 |
m.setAccessible(true); |
| 48 |
return m.invoke(annotation); |
| 49 |
} |
| 50 |
}); |
| 51 |
|
| 52 |
values.add(createValue(constPool, m.getName(), value)); |
| 53 |
} |
| 54 |
} catch (IllegalArgumentException e) { |
| 55 |
throw new RuntimeException(e); |
| 56 |
} catch (PrivilegedActionException e) { |
| 57 |
throw new RuntimeException(e); |
| 58 |
} |
| 59 |
return new ClassAnnotation(constPool, annotationType.getName(), values); |
| 60 |
} |
| 61 |
|
| 62 |
public static AnnotationValue createValue(ConstPool constPool, String name, Object value) { |
| 63 |
Class<?> type = value.getClass(); |
| 64 |
if (type == String.class) { |
| 65 |
return new StringAnnotationValue(constPool, name, (String) value); |
| 66 |
} else if (type == int.class || type == Integer.class) { |
| 67 |
return new IntAnnotationValue(constPool, name, (Integer) value); |
| 68 |
} else if (type == short.class || type == Short.class) { |
| 69 |
return new ShortAnnotationValue(constPool, name, (Short) value); |
| 70 |
} else if (type == byte.class || type == Byte.class) { |
| 71 |
return new ByteAnnotationValue(constPool, name, (Byte) value); |
| 72 |
} else if (type == boolean.class || type == Boolean.class) { |
| 73 |
return new BooleanAnnotationValue(constPool, name, (Boolean) value); |
| 74 |
} else if (type == char.class || type == Character.class) { |
| 75 |
return new CharAnnotationValue(constPool, name, (Character) value); |
| 76 |
} else if (type == long.class || type == Long.class) { |
| 77 |
return new LongAnnotationValue(constPool, name, (Long) value); |
| 78 |
} else if (type == float.class || type == Float.class) { |
| 79 |
return new FloatAnnotationValue(constPool, name, (Float) value); |
| 80 |
} else if (type == double.class || type == Double.class) { |
| 81 |
return new DoubleAnnotationValue(constPool, name, (Double) value); |
| 82 |
} else if (type == Class.class) { |
| 83 |
return new ClassAnnotationValue(constPool, name, (Class<?>) value); |
| 84 |
} else if (type.isEnum() || (type.getEnclosingClass() != null && type.getEnclosingClass().isEnum())) { |
| 85 |
return new EnumAnnotationValue(constPool, name, (Enum<?>) value); |
| 86 |
} else if (value instanceof Annotation) { |
| 87 |
return new AnnotationAnnotationValue(constPool, name, createAnnotation(constPool, (Annotation) value)); |
| 88 |
} else if (type.isArray()) { |
| 89 |
int length = Array.getLength(value); |
| 90 |
List<AnnotationValue> values = new ArrayList<AnnotationValue>(); |
| 91 |
for (int i = 0; i < length; ++i) { |
| 92 |
values.add(createValue(constPool, null, Array.get(value, i))); |
| 93 |
} |
| 94 |
return new ArrayAnnotationValue(constPool, name, values); |
| 95 |
} |
| 96 |
throw new RuntimeException("Invalid type for annotation value. Type: " + type + " Value: " + value); |
| 97 |
} |
| 98 |
|
| 99 |
private AnnotationBuilder() { |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
} |