FileDocCategorySizeDatePackage
RenameClassAdapter.javaAPI DocAndroid 1.5 API14709Wed May 06 22:42:02 BST 2009com.android.tools.layoutlib.create

RenameClassAdapter

public class RenameClassAdapter extends org.objectweb.asm.ClassAdapter
This class visitor renames a class from a given old name to a given new name. The class visitor will also rename all inner classes and references in the methods.

For inner classes, this handles only the case where the outer class name changes. The inner class name should remain the same.

Fields Summary
private final String
mOldName
private final String
mNewName
private String
mOldBase
private String
mNewBase
Constructors Summary
public RenameClassAdapter(org.objectweb.asm.ClassWriter cv, String oldName, String newName)
Creates a class visitor that renames a class from a given old name to a given new name. The class visitor will also rename all inner classes and references in the methods. The names must be full qualified internal ASM names (e.g. com/blah/MyClass$InnerClass).

        super(cv);
        mOldBase = mOldName = oldName;
        mNewBase = mNewName = newName;
        
        int pos = mOldName.indexOf('$");
        if (pos > 0) {
            mOldBase = mOldName.substring(0, pos);
        }
        pos = mNewName.indexOf('$");
        if (pos > 0) {
            mNewBase = mNewName.substring(0, pos);
        }
        
        assert (mOldBase == null && mNewBase == null) || (mOldBase != null && mNewBase != null);
    
Methods Summary
java.lang.StringrenameFieldSignature(java.lang.String sig)
Renames the FieldTypeSignature handled by ClassVisitor.visitField or MethodVisitor.visitLocalVariable.

        if (sig == null) {
            return null;
        }
        SignatureReader reader = new SignatureReader(sig);
        SignatureWriter writer = new SignatureWriter();
        reader.acceptType(new RenameSignatureAdapter(writer));
        sig = writer.toString();
        return sig;
    
java.lang.StringrenameInternalType(java.lang.String type)
Renames an internal type name, e.g. "com.package.MyClass". If the type doesn't need to be renamed, returns the input string as-is.

The internal type of some of the MethodVisitor turns out to be a type descriptor sometimes so descriptors are renamed too.

        if (type == null) {
            return null;
        }

        if (type.equals(mOldName)) {
            return mNewName;
        }

        if (mOldBase != mOldName && type.equals(mOldBase)) {
            return mNewBase;
        }
    
        int pos = type.indexOf('$");
        if (pos == mOldBase.length() && type.startsWith(mOldBase)) {
            return mNewBase + type.substring(pos);
        }

        // The internal type of some of the MethodVisitor turns out to be a type
        // descriptor sometimes. This is the case with visitTypeInsn(type) and
        // visitMethodInsn(owner). We try to detect it and adjust it here.
        if (type.indexOf(';") > 0) {
            type = renameTypeDesc(type);
        }

        return type;
    
java.lang.StringrenameMethodDesc(java.lang.String desc)
Renames a method descriptor, i.e. applies renameType to all arguments and to the return value.

        if (desc == null) {
            return null;
        }

        Type[] args = Type.getArgumentTypes(desc);

        StringBuilder sb = new StringBuilder("(");
        for (Type arg : args) {
            String name = renameType(arg);
            sb.append(name);
        }
        sb.append(')");
        
        Type ret = Type.getReturnType(desc);
        String name = renameType(ret);
        sb.append(name);

        return sb.toString();
    
java.lang.StringrenameType(org.objectweb.asm.Type type)
Renames an object type, e.g. "Lcom.package.MyClass;" or an array type that has an object element, e.g. "[Lcom.package.MyClass;" If the type doesn't need to be renamed, returns the internal name of the input type.

        if (type == null) {
            return null;
        }

        if (type.getSort() == Type.OBJECT) {
            String in = type.getInternalName();
            return "L" + renameInternalType(in) + ";";
        } else if (type.getSort() == Type.ARRAY) {
            StringBuilder sb = new StringBuilder();
            for (int n = type.getDimensions(); n > 0; n--) {
                sb.append('[");
            }
            sb.append(renameType(type.getElementType()));
            return sb.toString();
        }
        return type.getDescriptor();
    
org.objectweb.asm.TyperenameTypeAsType(org.objectweb.asm.Type type)
Renames an object type, e.g. "Lcom.package.MyClass;" or an array type that has an object element, e.g. "[Lcom.package.MyClass;". This is like renameType() except that it returns a Type object. If the type doesn't need to be renamed, returns the input type object.

        if (type == null) {
            return null;
        }

        if (type.getSort() == Type.OBJECT) {
            String in = type.getInternalName();
            String newIn = renameInternalType(in);
            if (newIn != in) {
                return Type.getType("L" + newIn + ";");
            }
        } else if (type.getSort() == Type.ARRAY) {
            StringBuilder sb = new StringBuilder();
            for (int n = type.getDimensions(); n > 0; n--) {
                sb.append('[");
            }
            sb.append(renameType(type.getElementType()));
            return Type.getType(sb.toString());
        }
        return type;
    
java.lang.StringrenameTypeDesc(java.lang.String desc)
Renames a type descriptor, e.g. "Lcom.package.MyClass;" If the type doesn't need to be renamed, returns the input string as-is.

        if (desc == null) {
            return null;
        }

        return renameType(Type.getType(desc));
    
java.lang.StringrenameTypeSignature(java.lang.String sig)
Renames the ClassSignature handled by ClassVisitor.visit or the MethodTypeSignature handled by ClassVisitor.visitMethod.

        if (sig == null) {
            return null;
        }
        SignatureReader reader = new SignatureReader(sig);
        SignatureWriter writer = new SignatureWriter();
        reader.accept(new RenameSignatureAdapter(writer));
        sig = writer.toString();
        return sig;
    
public voidvisit(int version, int access, java.lang.String name, java.lang.String signature, java.lang.String superName, java.lang.String[] interfaces)

        name = renameInternalType(name);
        superName = renameInternalType(superName);
        signature = renameTypeSignature(signature);
        
        super.visit(version, access, name, signature, superName, interfaces);
    
public org.objectweb.asm.AnnotationVisitorvisitAnnotation(java.lang.String desc, boolean visible)

        desc = renameTypeDesc(desc);
        return super.visitAnnotation(desc, visible);
    
public org.objectweb.asm.FieldVisitorvisitField(int access, java.lang.String name, java.lang.String desc, java.lang.String signature, java.lang.Object value)

        desc = renameTypeDesc(desc);
        signature = renameFieldSignature(signature);
        return super.visitField(access, name, desc, signature, value);
    
public voidvisitInnerClass(java.lang.String name, java.lang.String outerName, java.lang.String innerName, int access)

        assert outerName.equals(mOldName);
        outerName = renameInternalType(outerName);
        name = outerName + "$" + innerName;
        super.visitInnerClass(name, outerName, innerName, access);
    
public org.objectweb.asm.MethodVisitorvisitMethod(int access, java.lang.String name, java.lang.String desc, java.lang.String signature, java.lang.String[] exceptions)

        desc = renameMethodDesc(desc);
        signature = renameTypeSignature(signature);
        MethodVisitor mw = super.visitMethod(access, name, desc, signature, exceptions);
        return new RenameMethodAdapter(mw);