Example usage for org.aspectj.apache.bcel.classfile Method getConstantPool

List of usage examples for org.aspectj.apache.bcel.classfile Method getConstantPool

Introduction

In this page you can find the example usage for org.aspectj.apache.bcel.classfile Method getConstantPool.

Prototype

public final ConstantPool getConstantPool() 

Source Link

Usage

From source file:org.caesarj.mixer.intern.ClassModifyingVisitor.java

License:Open Source License

public void visitMethod(Method method) {
    final ConstantPool cp = method.getConstantPool();
    // we search for outer-class-access functions, which
    // are static, have exactly one argument of this class' type and
    // return an instance of the outer class' type
    if (method.isStatic() && method.getName().startsWith("access$")) {
        String returnType = Type.getReturnType(method.getSignature()).toString();

        if (!Tools.sameClass(returnType, oldOuterClassName))
            return;
        Type[] argTypes = Type.getArgumentTypes(method.getSignature());
        if (argTypes.length != 1)
            return;

        // construct the new signature & use it to overwrite the old one
        String newSignature = "(L" + newClassName + ";)L" + newOuterClassName + ";";
        int index = method.getSignatureIndex();
        cp.setConstant(index, new ConstantUtf8(newSignature));
    }/*from  www . ja v a2s  . c  om*/
    // and we check for constructors 
    else if (method.getName().equals("<init>")) {
        Type[] argTypes = Type.getArgumentTypes(method.getSignature());
        for (int argIdx = 0; argIdx < argTypes.length; argIdx++) {
            // modify the signature if neccessary
            if (Tools.sameClass(argTypes[argIdx].toString(), oldOuterClassName)) {
                // construct the new signature and use it to overwrite the old one
                argTypes[argIdx] = Type.getType("L" + newOuterClassName + ";");
            }
        }
        // construct new signature
        String signature = Type.getMethodSignature(method.getReturnType(), argTypes);
        int signatureIndex = method.getSignatureIndex();
        cp.setConstant(signatureIndex, new ConstantUtf8(signature));
    }
}