Example usage for org.aspectj.apache.bcel.classfile ConstantClass getNameIndex

List of usage examples for org.aspectj.apache.bcel.classfile ConstantClass getNameIndex

Introduction

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

Prototype

public final int getNameIndex() 

Source Link

Usage

From source file:br.jabuti.lookup.java.bytecode.ClassClosure.java

License:Open Source License

public String[] accessedClasses(JavaClass javaClass) {
    Vector interestedClasses = new Vector();
    ConstantPool cp = javaClass.getConstantPool();
    Constant[] ct = cp.getConstantPool();

    for (int i = 0; i < ct.length; i++) {
        if (ct[i] instanceof ConstantClass) {
            ConstantClass cc = (ConstantClass) ct[i];
            // System.out.println("accessed: " + cc);
            ConstantUtf8 cutf = (ConstantUtf8) cp.getConstant(cc.getNameIndex());
            String t = cutf.getBytes();

            if (t.charAt(0) != '[') {
                interestedClasses.add(toPoint(t));
            }/*from  w  ww . j a va 2s  . c o m*/
        }
    }
    return (String[]) interestedClasses.toArray(new String[0]);
}

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

License:Open Source License

protected JavaClass transform(JavaClass clazz) throws MixerException {
    // empty collection of anonymous inner classes
    anonymousInners = new Vector<String>();

    oldOuterClassName = Tools.getOuterClass(clazz, oldClassName);
    oldSuperclassName = clazz.getSuperclassName();

    // create a copy as work base
    JavaClass newClass = clazz.copy();/*  w w  w .j  a  v  a2 s.  c om*/

    // find indices of class and super class name
    int classNameIndex = newClass.getClassNameIndex(), superclassNameIndex = newClass.getSuperclassNameIndex();
    ConstantClass cc = (ConstantClass) newClass.getConstantPool().getConstant(classNameIndex),
            csc = (ConstantClass) newClass.getConstantPool().getConstant(superclassNameIndex);
    classNameIndex = cc.getNameIndex();
    superclassNameIndex = csc.getNameIndex();

    // Set new class & superclass name
    newClass.getConstantPool().setConstant(superclassNameIndex, new ConstantUtf8(newSuperclassName));
    newClass.getConstantPool().setConstant(classNameIndex, new ConstantUtf8(newClassName));

    // visit fields, methods and local variables to replace type references
    new DescendingVisitor(newClass, this).visit();

    // Delete all inner class references 
    Attribute[] atts = newClass.getAttributes();
    Vector<Attribute> v = new Vector<Attribute>();
    for (int i = 0; i < atts.length; i++) {
        Attribute attribute = atts[i];
        if (attribute.getTag() == Constants.ATTR_INNER_CLASSES) {
            InnerClasses ic = (InnerClasses) attribute;
            ic.setInnerClasses(new InnerClass[0]);
            ic.setLength(2);

        }
        v.add(attribute);
    }
    atts = v.toArray(new Attribute[v.size()]);
    newClass.setAttributes(atts);

    newClass = removeFactoryMethods(newClass);

    // Mixin classes must be abstract, because they do not implement factory methods,
    // (if the class is final => it was a anonymous inner class)
    if (!newClass.isFinal()) {
        newClass.setAccessFlags(newClass.getAccessFlags() | Constants.ACC_ABSTRACT);
    }

    // take a look at all methodrefs
    modifyMethodAndFieldRefs(newClass);

    // return generated class
    return newClass;
}

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