Example usage for org.aspectj.apache.bcel.generic Type getArgumentTypes

List of usage examples for org.aspectj.apache.bcel.generic Type getArgumentTypes

Introduction

In this page you can find the example usage for org.aspectj.apache.bcel.generic Type getArgumentTypes.

Prototype


public static Type[] getArgumentTypes(String signature) 

Source Link

Document

Convert arguments of a method (signature) to an array of Type objects.

Usage

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

License:Open Source License

/**
 * Checks method references to super-constructors and outerclass methods
 * and modifies them to refer to the correct new outer classes. 
 *//*from   w  ww .  j a  v  a2s .co  m*/
protected void modifyMethodAndFieldRefs(JavaClass clazz) {

    ConstantPool cp = clazz.getConstantPool();
    for (int i = 1; i < cp.getLength(); i++) {
        Constant c = cp.getConstant(i);

        if (c == null)
            continue;

        if (c.getTag() == Constants.CONSTANT_Fieldref) {
            ConstantFieldref fieldRef = (ConstantFieldref) c;

            String targetClass = fieldRef.getClass(cp);
            if (Tools.sameClass(targetClass, oldOuterClassName)) {
                int classIndex = fieldRef.getClassIndex();
                ConstantClass cc = (ConstantClass) cp.getConstant(classIndex);
                int nameIndex = cc.getNameIndex();
                cp.setConstant(nameIndex, new ConstantUtf8(newOuterClassName));
            }

        } else if (c.getTag() == Constants.CONSTANT_Methodref) {
            ConstantMethodref mr = (ConstantMethodref) c;
            String targetClassName = mr.getClass(cp);

            int nameAndTypeIndex = mr.getNameAndTypeIndex();
            ConstantNameAndType nat = ((ConstantNameAndType) cp.getConstant(nameAndTypeIndex));
            String methodName = nat.getName(cp);

            // check for innerclass construction
            if (Tools.isPrefix(oldClassName, targetClassName)) {
                String innerIdent = targetClassName.substring(oldClassName.length() + 1);
                String newInnerName = newClassName + "$" + innerIdent;

                try {
                    Integer.parseInt(innerIdent);
                } catch (Exception e) {
                    // not an anonymous inner class
                    continue;
                }

                // Change target class to new inner class
                int targetClassIndex = mr.getClassIndex();
                int targetNameIndex = ((ConstantClass) cp.getConstant(targetClassIndex)).getNameIndex();
                cp.setConstant(targetNameIndex, new ConstantUtf8(newInnerName));

                // Change argument class to new class
                Type[] args = Type.getArgumentTypes(nat.getSignature(cp));
                for (int j = 0; j < args.length; j++) {
                    Type arg = args[j];
                    String signature = arg.getSignature();
                    String argumentType = arg.toString();
                    if (Tools.sameClass(argumentType, oldClassName)) {
                        args[j] = Type.getType("L" + newClassName + ";");
                    }
                }
                String signature = Type.getMethodSignature(Type.getReturnType(nat.getSignature(cp)), args);
                int signatureIndex = nat.getSignatureIndex();
                cp.setConstant(signatureIndex, new ConstantUtf8(signature));
            }

            // Check for superconstructor calls with otuer class parameter
            if (Tools.sameClass(targetClassName, newSuperclassName)) {
                if (methodName.equals("<init>")) {
                    Type[] args = Type.getArgumentTypes(nat.getSignature(cp));
                    if (args.length == 1) {
                        String argumentType = args[0].toString();
                        // if parameter is of old super-outer-class type, set new signature
                        if (Tools.sameClass(argumentType, outerOfOldSuper)) {
                            cp.setConstant(nat.getSignatureIndex(),
                                    new ConstantUtf8("(L" + outerOfNewSuper + ";)V"));
                        }
                    }
                }
            }

            // check whether its a call to our old outer class
            if (Tools.isPrefix(targetClassName, oldOuterClassName)) {
                //               String newTargetClass = Tools.getNewOuterName(
                //                                    oldClassName,
                //                                    targetClassName,
                //                                    outerClasses);
                int classIndex = mr.getClassIndex();
                ConstantClass cc = (ConstantClass) cp.getConstant(classIndex);
                int nameIndex = cc.getNameIndex();
                cp.setConstant(nameIndex, new ConstantUtf8(newOuterClassName));
            }
        }
    }
}

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));
    }/* w w w .j  a v a 2 s . 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));
    }
}