Search in sources :

Example 1 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project pinpoint by naver.

the class JavaAssistUtils method lookupLocalVariableAttribute.

/**
     * get LocalVariableAttribute
     *
     * @param method
     * @return null if the class is not compiled with debug option
     */
public static LocalVariableAttribute lookupLocalVariableAttribute(CtBehavior method) {
    if (method == null) {
        throw new NullPointerException("method must not be null");
    }
    MethodInfo methodInfo = method.getMethodInfo2();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    if (codeAttribute == null) {
        return null;
    }
    AttributeInfo localVariableTable = codeAttribute.getAttribute(LocalVariableAttribute.tag);
    LocalVariableAttribute local = (LocalVariableAttribute) localVariableTable;
    return local;
}
Also used : AttributeInfo(javassist.bytecode.AttributeInfo) CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute)

Example 2 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project fakereplace by fakereplace.

the class FieldReplacementTransformer method copyFieldAttributes.

private static void copyFieldAttributes(FieldInfo oldField, FieldInfo newField) {
    AnnotationsAttribute annotations = (AnnotationsAttribute) oldField.getAttribute(AnnotationsAttribute.visibleTag);
    SignatureAttribute sigAt = (SignatureAttribute) oldField.getAttribute(SignatureAttribute.tag);
    if (annotations != null) {
        AttributeInfo newAnnotations = annotations.copy(newField.getConstPool(), Collections.EMPTY_MAP);
        newField.addAttribute(newAnnotations);
    }
    if (sigAt != null) {
        AttributeInfo newAnnotations = sigAt.copy(newField.getConstPool(), Collections.EMPTY_MAP);
        newField.addAttribute(newAnnotations);
    }
}
Also used : SignatureAttribute(javassist.bytecode.SignatureAttribute) AttributeInfo(javassist.bytecode.AttributeInfo) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute)

Example 3 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project fakereplace by fakereplace.

the class MethodReplacementTransformer method copyMethodAttributes.

public static void copyMethodAttributes(MethodInfo oldMethod, MethodInfo newMethod) {
    AnnotationsAttribute annotations = (AnnotationsAttribute) oldMethod.getAttribute(AnnotationsAttribute.visibleTag);
    ParameterAnnotationsAttribute pannotations = (ParameterAnnotationsAttribute) oldMethod.getAttribute(ParameterAnnotationsAttribute.visibleTag);
    ExceptionsAttribute exAt = (ExceptionsAttribute) oldMethod.getAttribute(ExceptionsAttribute.tag);
    SignatureAttribute sigAt = (SignatureAttribute) oldMethod.getAttribute(SignatureAttribute.tag);
    if (annotations != null) {
        AttributeInfo newAnnotations = annotations.copy(newMethod.getConstPool(), Collections.EMPTY_MAP);
        newMethod.addAttribute(newAnnotations);
    }
    if (pannotations != null) {
        AttributeInfo newAnnotations = pannotations.copy(newMethod.getConstPool(), Collections.EMPTY_MAP);
        newMethod.addAttribute(newAnnotations);
    }
    if (sigAt != null) {
        AttributeInfo newAnnotations = sigAt.copy(newMethod.getConstPool(), Collections.EMPTY_MAP);
        newMethod.addAttribute(newAnnotations);
    }
    if (exAt != null) {
        AttributeInfo newAnnotations = exAt.copy(newMethod.getConstPool(), Collections.EMPTY_MAP);
        newMethod.addAttribute(newAnnotations);
    }
}
Also used : SignatureAttribute(javassist.bytecode.SignatureAttribute) AttributeInfo(javassist.bytecode.AttributeInfo) ExceptionsAttribute(javassist.bytecode.ExceptionsAttribute) ParameterAnnotationsAttribute(javassist.bytecode.ParameterAnnotationsAttribute) ParameterAnnotationsAttribute(javassist.bytecode.ParameterAnnotationsAttribute) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute)

Example 4 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project fakereplace by fakereplace.

the class FinalMethodManipulator method transformClass.

public boolean transformClass(ClassFile file, ClassLoader loader, boolean modifiableClass, final Set<MethodInfo> modifiedMethods) {
    if (!modifiableClass) {
        return false;
    }
    boolean modified = false;
    for (Object i : file.getMethods()) {
        MethodInfo m = (MethodInfo) i;
        if ((m.getAccessFlags() & AccessFlag.FINAL) != 0) {
            m.setAccessFlags(m.getAccessFlags() & ~AccessFlag.FINAL);
            // ClassDataStore.addFinalMethod(file.getName(), m.getName(),
            // m.getDescriptor());
            AnnotationsAttribute at = (AnnotationsAttribute) m.getAttribute(AnnotationsAttribute.visibleTag);
            if (at == null) {
                at = new AnnotationsAttribute(file.getConstPool(), AnnotationsAttribute.visibleTag);
                m.addAttribute(at);
            }
            at.addAnnotation(new Annotation(ModifiedMethod.class.getName(), file.getConstPool()));
            m.addAttribute(new AttributeInfo(file.getConstPool(), Constants.FINAL_METHOD_ATTRIBUTE, new byte[0]));
            modified = true;
        }
    }
    return modified;
}
Also used : AttributeInfo(javassist.bytecode.AttributeInfo) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) MethodInfo(javassist.bytecode.MethodInfo) Annotation(javassist.bytecode.annotation.Annotation)

Example 5 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project powermock by powermock.

the class ClassFinalModifierMockTransformer method transform.

@Override
public CtClass transform(final CtClass clazz) {
    if (clazz.isInterface()) {
        return clazz;
    }
    if (getStrategy() != INST_REDEFINE) {
        if (Modifier.isFinal(clazz.getModifiers())) {
            clazz.setModifiers(clazz.getModifiers() ^ Modifier.FINAL);
        }
        ClassFile classFile = clazz.getClassFile2();
        AttributeInfo attribute = classFile.getAttribute(InnerClassesAttribute.tag);
        if (attribute != null && attribute instanceof InnerClassesAttribute) {
            InnerClassesAttribute ica = (InnerClassesAttribute) attribute;
            String name = classFile.getName();
            int n = ica.tableLength();
            for (int i = 0; i < n; ++i) {
                if (name.equals(ica.innerClass(i))) {
                    int accessFlags = ica.accessFlags(i);
                    if (Modifier.isFinal(accessFlags)) {
                        ica.setAccessFlags(i, accessFlags ^ Modifier.FINAL);
                    }
                }
            }
        }
    }
    return clazz;
}
Also used : AttributeInfo(javassist.bytecode.AttributeInfo) ClassFile(javassist.bytecode.ClassFile) InnerClassesAttribute(javassist.bytecode.InnerClassesAttribute)

Aggregations

AttributeInfo (javassist.bytecode.AttributeInfo)9 ClassFile (javassist.bytecode.ClassFile)4 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)3 MethodInfo (javassist.bytecode.MethodInfo)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 IOException (java.io.IOException)2 InnerClassesAttribute (javassist.bytecode.InnerClassesAttribute)2 SignatureAttribute (javassist.bytecode.SignatureAttribute)2 LambdaMetafactory (java.lang.invoke.LambdaMetafactory)1 BootstrapMethodsAttribute (javassist.bytecode.BootstrapMethodsAttribute)1 Bytecode (javassist.bytecode.Bytecode)1 CodeAttribute (javassist.bytecode.CodeAttribute)1 ExceptionsAttribute (javassist.bytecode.ExceptionsAttribute)1 LocalVariableAttribute (javassist.bytecode.LocalVariableAttribute)1 ParameterAnnotationsAttribute (javassist.bytecode.ParameterAnnotationsAttribute)1 Annotation (javassist.bytecode.annotation.Annotation)1 BaseClassData (org.fakereplace.data.BaseClassData)1 MethodData (org.fakereplace.data.MethodData)1