Search in sources :

Example 6 with DuplicateMemberException

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

the class Transformer method addStaticConstructorForInstrumentation.

private static void addStaticConstructorForInstrumentation(ClassFile file) {
    try {
        MethodInfo m = new MethodInfo(file.getConstPool(), "<clinit>", "()V");
        m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC);
        Bytecode b = new Bytecode(file.getConstPool());
        b.add(Opcode.RETURN);
        m.setCodeAttribute(b.toCodeAttribute());
        file.addMethod(m);
    } catch (DuplicateMemberException e) {
    // e.printStackTrace();
    }
}
Also used : DuplicateMemberException(javassist.bytecode.DuplicateMemberException) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode)

Example 7 with DuplicateMemberException

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

the class Transformer method addConstructorForInstrumentation.

private void addConstructorForInstrumentation(ClassFile file) {
    MethodInfo ret = new MethodInfo(file.getConstPool(), "<init>", Constants.ADDED_CONSTRUCTOR_DESCRIPTOR);
    Bytecode code = new Bytecode(file.getConstPool());
    // if the class does not have a constructor return
    if (!ManipulationUtils.addBogusConstructorCall(file, code)) {
        return;
    }
    CodeAttribute ca = code.toCodeAttribute();
    ca.setMaxLocals(4);
    ret.setCodeAttribute(ca);
    ret.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.SYNTHETIC);
    try {
        ca.computeMaxStack();
        file.addMethod(ret);
    } catch (DuplicateMemberException e) {
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DuplicateMemberException(javassist.bytecode.DuplicateMemberException) CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) DuplicateMemberException(javassist.bytecode.DuplicateMemberException)

Example 8 with DuplicateMemberException

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

the class Transformer method addAbstractMethodForInstrumentation.

/**
 * Adds a method to a class that re can redefine when the class is reloaded
 */
private void addAbstractMethodForInstrumentation(ClassFile file) {
    try {
        MethodInfo m = new MethodInfo(file.getConstPool(), Constants.ADDED_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
        m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.ABSTRACT | AccessFlag.SYNTHETIC);
        file.addMethod(m);
    } catch (DuplicateMemberException e) {
    // e.printStackTrace();
    }
}
Also used : DuplicateMemberException(javassist.bytecode.DuplicateMemberException) MethodInfo(javassist.bytecode.MethodInfo)

Example 9 with DuplicateMemberException

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

the class Transformer method addMethodForInstrumentation.

/**
 * Adds a method to a class that re can redefine when the class is reloaded
 */
private void addMethodForInstrumentation(ClassFile file) {
    try {
        MethodInfo m = new MethodInfo(file.getConstPool(), Constants.ADDED_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
        m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.SYNTHETIC);
        Bytecode b = new Bytecode(file.getConstPool(), 5, 3);
        if (BuiltinClassData.skipInstrumentation(file.getSuperclass())) {
            b.addNew(NoSuchMethodError.class.getName());
            b.add(Opcode.DUP);
            b.addInvokespecial(NoSuchMethodError.class.getName(), "<init>", "()V");
            b.add(Opcode.ATHROW);
        } else {
            // delegate to the parent class
            b.add(Bytecode.ALOAD_0);
            b.add(Bytecode.ILOAD_1);
            b.add(Bytecode.ALOAD_2);
            b.addInvokespecial(file.getSuperclass(), Constants.ADDED_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
            b.add(Bytecode.ARETURN);
        }
        CodeAttribute ca = b.toCodeAttribute();
        m.setCodeAttribute(ca);
        file.addMethod(m);
    } catch (DuplicateMemberException e) {
    // e.printStackTrace();
    }
    try {
        MethodInfo m = new MethodInfo(file.getConstPool(), Constants.ADDED_STATIC_METHOD_NAME, Constants.ADDED_METHOD_DESCRIPTOR);
        m.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC | AccessFlag.SYNTHETIC);
        Bytecode b = new Bytecode(file.getConstPool(), 5, 3);
        b.addNew(NoSuchMethodError.class.getName());
        b.add(Opcode.DUP);
        b.addInvokespecial(NoSuchMethodError.class.getName(), "<init>", "()V");
        b.add(Opcode.ATHROW);
        CodeAttribute ca = b.toCodeAttribute();
        m.setCodeAttribute(ca);
        file.addMethod(m);
    } catch (DuplicateMemberException e) {
    // e.printStackTrace();
    }
}
Also used : DuplicateMemberException(javassist.bytecode.DuplicateMemberException) CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode)

Example 10 with DuplicateMemberException

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

the class MethodReplacementTransformer method createRemovedMethod.

private static MethodInfo createRemovedMethod(ClassFile file, MethodData md, Class<?> oldClass, Set<MethodData> methodsToRemove) {
    if (md.getMethodName().equals("<clinit>")) {
        // if the static constructor is removed it gets added later on
        return null;
    // in the process
    }
    // load up the existing method object
    MethodInfo m = new MethodInfo(file.getConstPool(), md.getMethodName(), md.getDescriptor());
    m.setAccessFlags(md.getAccessFlags());
    // put the old annotations on the class
    if (md.getMethodName().equals("<init>")) {
        Constructor<?> meth;
        try {
            meth = md.getConstructor(oldClass);
        } catch (Exception e) {
            throw new RuntimeException("Error accessing existing constructor via reflection in not found", e);
        }
        m.addAttribute(AnnotationReplacer.duplicateAnnotationsAttribute(file.getConstPool(), meth));
    } else {
        Method meth;
        try {
            meth = md.getMethod(oldClass);
        } catch (Exception e) {
            throw new RuntimeException("Error accessing existing method via reflection in not found", e);
        }
        m.addAttribute(AnnotationReplacer.duplicateAnnotationsAttribute(file.getConstPool(), meth));
    }
    Bytecode b = new Bytecode(file.getConstPool(), 5, 3);
    b.addNew("java.lang.NoSuchMethodError");
    b.add(Opcode.DUP);
    b.addInvokespecial("java.lang.NoSuchMethodError", "<init>", "()V");
    b.add(Bytecode.ATHROW);
    CodeAttribute ca = b.toCodeAttribute();
    m.setCodeAttribute(ca);
    try {
        ca.computeMaxStack();
        file.addMethod(m);
    } catch (DuplicateMemberException e) {
        logger.error("Duplicate error", e);
    } catch (BadBytecode e) {
        logger.error("Bad bytecode", e);
    }
    methodsToRemove.add(md);
    return m;
}
Also used : DuplicateMemberException(javassist.bytecode.DuplicateMemberException) CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) Method(java.lang.reflect.Method) DuplicateMemberException(javassist.bytecode.DuplicateMemberException) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) IOException(java.io.IOException) BadBytecode(javassist.bytecode.BadBytecode)

Aggregations

DuplicateMemberException (javassist.bytecode.DuplicateMemberException)12 MethodInfo (javassist.bytecode.MethodInfo)9 BadBytecode (javassist.bytecode.BadBytecode)8 Bytecode (javassist.bytecode.Bytecode)8 IOException (java.io.IOException)6 CodeAttribute (javassist.bytecode.CodeAttribute)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 ClassFile (javassist.bytecode.ClassFile)3 FieldInfo (javassist.bytecode.FieldInfo)3 Method (java.lang.reflect.Method)2 HashSet (java.util.HashSet)2 BaseClassData (org.fakereplace.data.BaseClassData)2 Field (java.lang.reflect.Field)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 ClassPool (javassist.ClassPool)1 CtClass (javassist.CtClass)1 NotFoundException (javassist.NotFoundException)1