Search in sources :

Example 1 with MemberValue

use of javassist.bytecode.annotation.MemberValue 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

Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ClassPool (javassist.ClassPool)1 CtClass (javassist.CtClass)1 CtField (javassist.CtField)1 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 ClassFile (javassist.bytecode.ClassFile)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 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)1 FloatMemberValue (javassist.bytecode.annotation.FloatMemberValue)1 IntegerMemberValue (javassist.bytecode.annotation.IntegerMemberValue)1 LongMemberValue (javassist.bytecode.annotation.LongMemberValue)1 MemberValue (javassist.bytecode.annotation.MemberValue)1 ShortMemberValue (javassist.bytecode.annotation.ShortMemberValue)1