Index: maven-commandline-plugin/src/java/org/codelutin/util/AnnotationConverter.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/util/AnnotationConverter.java:1.1 --- /dev/null Sun Dec 2 05:08:09 2007 +++ maven-commandline-plugin/src/java/org/codelutin/util/AnnotationConverter.java Sun Dec 2 05:08:00 2007 @@ -0,0 +1,173 @@ +/** + * ##% Copyright (C) 2002, 2007 Code Lutin This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. ##%* + */ +package org.codelutin.util; + +import javassist.ClassPool; +import javassist.CtClass; +import javassist.NotFoundException; +import javassist.bytecode.AnnotationsAttribute; +import javassist.bytecode.ConstPool; +import javassist.bytecode.annotation.*; +import static org.codelutin.i18n.I18n._; + +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.List; + + +/** + * This class converts a java Annotation to a javassist Annotation. + * + * @author chemit + */ + +public class AnnotationConverter { + + /** list of method names to skip from annotation parsing. */ + private static final List skippedMethods = Arrays.asList("hashCode", + "toString", "annotationType", "wait", "notify", "notifyAll"); + + /** + * + * @param annotation java annotation to translate + * @param constPool the const pool to use for constant symbols + * @param classPool the class pool to use for getting class + * @return the javassist annotation translated + * @throws NotFoundException if some element was not found + * @throws InvocationTargetException reflection error + * @throws IllegalAccessException reflection error + */ + public static Annotation convert(java.lang.annotation.Annotation annotation, + ConstPool constPool, + ClassPool classPool) + throws NotFoundException, InvocationTargetException, IllegalAccessException { + if (annotation == null) { + throw new NullPointerException(_("lutinutil.parser.annotationConvert.unfound.annotation")); + } + if (classPool == null) { + throw new NullPointerException(_("lutinutil.parser.annotationConvert.unfound.classpool")); + } + if (constPool == null) { + throw new NullPointerException(_("lutinutil.parser.annotationConvert.unfound.constpool")); + } + + Annotation result; + + CtClass ctClazz = classPool.get(annotation.annotationType().getName()); + result = new Annotation(constPool, ctClazz); + + for (java.lang.reflect.Method method : annotation.annotationType().getMethods()) { + Class type = method.getReturnType(); + String name = method.getName(); + if (!java.lang.reflect.Modifier.isPublic(method.getModifiers()) || + method.getParameterTypes().length > 0 || + type.equals(Void.class) || + skippedMethods.contains(name)) { + continue; + } + MemberValue memberValue; + Object value = method.invoke(annotation); + memberValue = newMemberValue(constPool, type, value, classPool); + result.addMemberValue(name, memberValue); + } + return result; + } + + private static MemberValue newMemberValue(ConstPool constPool, + Class type, + Object value, ClassPool classPool) + throws InvocationTargetException, IllegalAccessException, NotFoundException { + + if (type == Byte.class || type == byte.class) { + ByteMemberValue result = new ByteMemberValue((Byte) value, constPool); + result.setValue((Byte) value); + return result; + } + if (type == Short.class || type == short.class) { + ShortMemberValue result = new ShortMemberValue(constPool); + result.setValue((Short) value); + return result; + } + if (type == Integer.class || type == int.class) { + IntegerMemberValue result = new IntegerMemberValue(constPool); + result.setValue((Integer) value); + return result; + } + if (type == Float.class || type == float.class) { + FloatMemberValue result = new FloatMemberValue(constPool); + result.setValue((Float) value); + return result; + } + if (type == Double.class || type == double.class) { + DoubleMemberValue result = new DoubleMemberValue(constPool); + result.setValue((Double) value); + return result; + } + if (type == Boolean.class || type == boolean.class) { + BooleanMemberValue result = new BooleanMemberValue(constPool); + result.setValue((Boolean) value); + return result; + } + if (type == Character.class || type == char.class) { + CharMemberValue result = new CharMemberValue((Character) value, constPool); + result.setValue((Character) value); + return result; + } + if (type == String.class) { + StringMemberValue result = new StringMemberValue(constPool); + result.setValue((String) value); + return result; + } + if (type.isEnum()) { + EnumMemberValue result = new EnumMemberValue(constPool); + result.setType(type.getName()); + result.setValue(((Enum) value).name()); + return result; + } + if (type == Class.class) { + ClassMemberValue result = new ClassMemberValue(constPool); + if (value != null) { + result.setValue(((Class) value).getName()); + } + return result; + } + if (type.isAnnotation()) { + AnnotationMemberValue result = new AnnotationMemberValue(constPool); + AnnotationsAttribute attribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); + Annotation annotation = convert((java.lang.annotation.Annotation) value, constPool, classPool); + attribute.setAnnotation(annotation); + result.setValue(annotation); + return result; + } + if (type.isArray()) { + ArrayMemberValue result = new ArrayMemberValue(constPool); + Object[] values = (Object[]) value; + + MemberValue[] memberValues = new MemberValue[values.length]; + Class typeA = type.getComponentType(); + for (int i = 0; i < values.length; i++) { + Object o = values[i]; + if (o == null) { + continue; + } + MemberValue arrayMemberValue = newMemberValue(constPool, typeA, o, classPool); + memberValues[i] = arrayMemberValue; + } + result.setValue(memberValues); + + return result; + } + throw new IllegalArgumentException(_("lutinutil.parser.annotationConvert.unfound.membervalue", type, value)); + } + +}