Search in sources :

Example 31 with ClassFile

use of javassist.bytecode.ClassFile in project audit4j-core by audit4j.

the class AnnotationDB method scanFields.

protected void scanFields(ClassFile cf) {
    List<ClassFile> fields = cf.getFields();
    if (fields == null)
        return;
    for (Object obj : fields) {
        FieldInfo field = (FieldInfo) obj;
        AnnotationsAttribute visible = (AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.visibleTag);
        AnnotationsAttribute invisible = (AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.invisibleTag);
        if (visible != null)
            populate(visible.getAnnotations(), cf.getName());
        if (invisible != null)
            populate(invisible.getAnnotations(), cf.getName());
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) ParameterAnnotationsAttribute(javassist.bytecode.ParameterAnnotationsAttribute) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) FieldInfo(javassist.bytecode.FieldInfo)

Example 32 with ClassFile

use of javassist.bytecode.ClassFile in project audit4j-core by audit4j.

the class AnnotationDB method scanMethods.

/**
 * Scanns both the method and its parameters for annotations.
 *
 * @param cf
 */
protected void scanMethods(ClassFile cf) {
    List<ClassFile> methods = cf.getMethods();
    if (methods == null)
        return;
    for (Object obj : methods) {
        MethodInfo method = (MethodInfo) obj;
        if (scanMethodAnnotations) {
            AnnotationsAttribute visible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.visibleTag);
            AnnotationsAttribute invisible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.invisibleTag);
            if (visible != null)
                populate(visible.getAnnotations(), cf.getName());
            if (invisible != null)
                populate(invisible.getAnnotations(), cf.getName());
        }
        if (scanParameterAnnotations) {
            ParameterAnnotationsAttribute paramsVisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.visibleTag);
            ParameterAnnotationsAttribute paramsInvisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.invisibleTag);
            if (paramsVisible != null && paramsVisible.getAnnotations() != null) {
                for (Annotation[] anns : paramsVisible.getAnnotations()) {
                    populate(anns, cf.getName());
                }
            }
            if (paramsInvisible != null && paramsInvisible.getAnnotations() != null) {
                for (Annotation[] anns : paramsInvisible.getAnnotations()) {
                    populate(anns, cf.getName());
                }
            }
        }
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) ParameterAnnotationsAttribute(javassist.bytecode.ParameterAnnotationsAttribute) ParameterAnnotationsAttribute(javassist.bytecode.ParameterAnnotationsAttribute) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) MethodInfo(javassist.bytecode.MethodInfo) Annotation(javassist.bytecode.annotation.Annotation)

Example 33 with ClassFile

use of javassist.bytecode.ClassFile in project coprhd-controller by CoprHD.

the class DbSchemaChanger method removeAnnotation.

/**
 * remove an annotation from a method
 *
 * @param methodName the method to which the annotation to be removed
 * @param annotationName the annotation name, it should be a full name
 */
public DbSchemaChanger removeAnnotation(String methodName, String annotationName) throws Exception {
    // looking for the method to apply the annotation on
    CtMethod methodDescriptor = cc.getDeclaredMethod(methodName);
    // create the annotation
    ClassFile ccFile = cc.getClassFile();
    ccFile.setVersionToJava5();
    ConstPool constpool = ccFile.getConstPool();
    MethodInfo minfo = methodDescriptor.getMethodInfo();
    AnnotationsAttribute attr = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.visibleTag);
    Annotation[] annotations = attr.getAnnotations();
    List<Annotation> list = new ArrayList();
    for (Annotation annotation : annotations) {
        if (!annotation.getTypeName().equals(annotationName)) {
            list.add(annotation);
        }
    }
    Annotation[] newAnnotations = list.toArray(new Annotation[0]);
    attr.setAnnotations(newAnnotations);
    minfo.addAttribute(attr);
    return this;
}
Also used : ConstPool(javassist.bytecode.ConstPool) ClassFile(javassist.bytecode.ClassFile) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ArrayList(java.util.ArrayList) MethodInfo(javassist.bytecode.MethodInfo) CtMethod(javassist.CtMethod) Annotation(javassist.bytecode.annotation.Annotation)

Example 34 with ClassFile

use of javassist.bytecode.ClassFile 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 parameterClassName = generateMethodParameterClassName(clazz, method);
        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 (null != value) {
                                    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(clazz.getClassLoader(), null);
        }
        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)

Example 35 with ClassFile

use of javassist.bytecode.ClassFile in project openj9 by eclipse.

the class DummyClassGenerator method modifyInvokerOperand.

/*
	 * This method is to workaround a limitation of ASM which prevents
	 * generating class file where invokeinterface and invoke[static|special]
	 * bytecodes share same constant pool entry.
	 * 
	 * To overcome this limitation, after the ASM has generated class data, the
	 * operand of invoke[static|special] bytecodes is modified to be same as the
	 * operand of invokeinterface bytecode. It uses javassist APIs to make this
	 * modification.
	 */
protected byte[] modifyInvokerOperand() throws Exception {
    int cpIndex = 0;
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    ClassFile classFile = new ClassFile(new DataInputStream(bais));
    String[] sharedInvokers = (String[]) characteristics.get(SHARED_INVOKERS);
    /* Record operand of invokeinterface in invokeInterface() */
    for (int i = 0; i < sharedInvokers.length; i++) {
        String methodName = sharedInvokers[i];
        if (methodName.equals(INVOKE_INTERFACE)) {
            MethodInfo methodInfo = classFile.getMethod(methodName);
            CodeIterator ci = methodInfo.getCodeAttribute().iterator();
            while (ci.hasNext()) {
                int bcIndex = ci.next();
                int bc = ci.byteAt(bcIndex);
                if (bc == Opcode.INVOKEINTERFACE) {
                    /* found invokeinterfce bytecode */
                    cpIndex = ci.s16bitAt(bcIndex + 1);
                    break;
                }
            }
        }
    }
    /*
		 * Modify operand of invokestatic/invokespecial to cpIndex recorded
		 * above
		 */
    for (int i = 0; i < sharedInvokers.length; i++) {
        String methodName = sharedInvokers[i];
        if (methodName.equals(INVOKE_SPECIAL) || (methodName.equals(INVOKE_STATIC))) {
            MethodInfo methodInfo = classFile.getMethod(methodName);
            CodeIterator ci = methodInfo.getCodeAttribute().iterator();
            while (ci.hasNext()) {
                int bcIndex = ci.next();
                int bc = ci.byteAt(bcIndex);
                if ((bc == Opcode.INVOKESPECIAL) || (bc == Opcode.INVOKESTATIC)) {
                    /*
						 * found invokespecial or invokestatic bytecode; update
						 * the operand constant pool index
						 */
                    ci.write16bit(cpIndex, bcIndex + 1);
                }
            }
        }
    }
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    DataOutputStream dataStream = new DataOutputStream(byteStream);
    classFile.write(dataStream);
    return byteStream.toByteArray();
}
Also used : ClassFile(javassist.bytecode.ClassFile) ByteArrayInputStream(java.io.ByteArrayInputStream) CodeIterator(javassist.bytecode.CodeIterator) DataOutputStream(java.io.DataOutputStream) MethodInfo(javassist.bytecode.MethodInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Aggregations

ClassFile (javassist.bytecode.ClassFile)51 DataInputStream (java.io.DataInputStream)15 ClassPool (javassist.ClassPool)15 DataOutputStream (java.io.DataOutputStream)14 CtClass (javassist.CtClass)14 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)14 ConstPool (javassist.bytecode.ConstPool)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 IOException (java.io.IOException)11 MethodInfo (javassist.bytecode.MethodInfo)11 CtField (javassist.CtField)10 NotFoundException (javassist.NotFoundException)9 Annotation (javassist.bytecode.annotation.Annotation)9 Test (org.junit.Test)9 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)8 HashSet (java.util.HashSet)7 CtMethod (javassist.CtMethod)7 FieldInfo (javassist.bytecode.FieldInfo)7 Bytecode (javassist.bytecode.Bytecode)5