Search in sources :

Example 6 with AnnotationsAttribute

use of javassist.bytecode.AnnotationsAttribute in project hibernate-orm by hibernate.

the class FieldWriter method addAnnotations.

/* --- */
private static void addAnnotations(FieldInfo fieldInfo, Class<?>[] annotations) {
    AnnotationsAttribute annotationsAttribute = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);
    if (annotationsAttribute == null) {
        annotationsAttribute = new AnnotationsAttribute(fieldInfo.getConstPool(), AnnotationsAttribute.visibleTag);
        fieldInfo.addAttribute(annotationsAttribute);
    }
    for (Class<?> annotation : annotations) {
        annotationsAttribute.addAnnotation(new Annotation(annotation.getName(), fieldInfo.getConstPool()));
    }
}
Also used : AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) Annotation(javassist.bytecode.annotation.Annotation)

Example 7 with AnnotationsAttribute

use of javassist.bytecode.AnnotationsAttribute in project dubbo by alibaba.

the class JValidator method getMethodParameterBean.

private static Object getMethodParameterBean(Class<?> clazz, Method method, Object[] args) {
    if (!hasConstraintParameter(method)) {
        return null;
    }
    try {
        String upperName = toUpperMethoName(method.getName());
        String parameterSimpleName = upperName + "Parameter";
        String parameterClassName = clazz.getName() + "_" + parameterSimpleName;
        Class<?> parameterClass;
        try {
            parameterClass = (Class<?>) Class.forName(parameterClassName, true, clazz.getClassLoader());
        } catch (ClassNotFoundException e) {
            ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader());
            CtClass ctClass = pool.makeClass(parameterClassName);
            ClassFile classFile = ctClass.getClassFile();
            classFile.setVersionToJava5();
            ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName)));
            // parameter fields
            Class<?>[] parameterTypes = method.getParameterTypes();
            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            for (int i = 0; i < parameterTypes.length; i++) {
                Class<?> type = parameterTypes[i];
                Annotation[] annotations = parameterAnnotations[i];
                AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag);
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType().isAnnotationPresent(Constraint.class)) {
                        javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation(classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName()));
                        Method[] members = annotation.annotationType().getMethods();
                        for (Method member : members) {
                            if (Modifier.isPublic(member.getModifiers()) && member.getParameterTypes().length == 0 && member.getDeclaringClass() == annotation.annotationType()) {
                                Object value = member.invoke(annotation, new Object[0]);
                                if (value != null && !value.equals(member.getDefaultValue())) {
                                    MemberValue memberValue = createMemberValue(classFile.getConstPool(), pool.get(member.getReturnType().getName()), value);
                                    ja.addMemberValue(member.getName(), memberValue);
                                }
                            }
                        }
                        attribute.addAnnotation(ja);
                    }
                }
                String fieldName = method.getName() + "Argument" + i;
                CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName));
                ctField.getFieldInfo().addAttribute(attribute);
                ctClass.addField(ctField);
            }
            parameterClass = ctClass.toClass();
        }
        Object parameterBean = parameterClass.newInstance();
        for (int i = 0; i < args.length; i++) {
            Field field = parameterClass.getField(method.getName() + "Argument" + i);
            field.set(parameterBean, args[i]);
        }
        return parameterBean;
    } catch (Throwable e) {
        logger.warn(e.getMessage(), e);
        return null;
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) Constraint(javax.validation.Constraint) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ClassPool(javassist.ClassPool) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Constraint(javax.validation.Constraint) CtClass(javassist.CtClass) Field(java.lang.reflect.Field) CtField(javassist.CtField) MemberValue(javassist.bytecode.annotation.MemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) CtField(javassist.CtField) CtClass(javassist.CtClass)

Aggregations

AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)7 CtClass (javassist.CtClass)4 Annotation (java.lang.annotation.Annotation)2 Annotation (javassist.bytecode.annotation.Annotation)2 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)2 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ClassPool (javassist.ClassPool)1 CtField (javassist.CtField)1 ClassFile (javassist.bytecode.ClassFile)1 ConstPool (javassist.bytecode.ConstPool)1 ArrayMemberValue (javassist.bytecode.annotation.ArrayMemberValue)1 BooleanMemberValue (javassist.bytecode.annotation.BooleanMemberValue)1 ByteMemberValue (javassist.bytecode.annotation.ByteMemberValue)1 CharMemberValue (javassist.bytecode.annotation.CharMemberValue)1 ClassMemberValue (javassist.bytecode.annotation.ClassMemberValue)1 DoubleMemberValue (javassist.bytecode.annotation.DoubleMemberValue)1 FloatMemberValue (javassist.bytecode.annotation.FloatMemberValue)1 IntegerMemberValue (javassist.bytecode.annotation.IntegerMemberValue)1