Search in sources :

Example 26 with ByteArray

use of com.android.dx.util.ByteArray in project buck by facebook.

the class AnnotationLister method process.

/** Processes based on configuration specified in constructor. */
void process() {
    for (String path : args.files) {
        ClassPathOpener opener;
        opener = new ClassPathOpener(path, true, new ClassPathOpener.Consumer() {

            public boolean processFileBytes(String name, long lastModified, byte[] bytes) {
                if (!name.endsWith(".class")) {
                    return true;
                }
                ByteArray ba = new ByteArray(bytes);
                DirectClassFile cf = new DirectClassFile(ba, name, true);
                cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
                AttributeList attributes = cf.getAttributes();
                Attribute att;
                String cfClassName = cf.getThisClass().getClassType().getClassName();
                if (cfClassName.endsWith(PACKAGE_INFO)) {
                    att = attributes.findFirst(AttRuntimeInvisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitPackageAnnotation(cf, ann);
                    }
                    att = attributes.findFirst(AttRuntimeVisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitPackageAnnotation(cf, ann);
                    }
                } else if (isMatchingInnerClass(cfClassName) || isMatchingPackage(cfClassName)) {
                    printMatch(cf);
                } else {
                    att = attributes.findFirst(AttRuntimeInvisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitClassAnnotation(cf, ann);
                    }
                    att = attributes.findFirst(AttRuntimeVisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitClassAnnotation(cf, ann);
                    }
                }
                return true;
            }

            public void onException(Exception ex) {
                throw new RuntimeException(ex);
            }

            public void onProcessArchiveStart(File file) {
            }
        });
        opener.process();
    }
}
Also used : ClassPathOpener(com.android.dx.cf.direct.ClassPathOpener) DirectClassFile(com.android.dx.cf.direct.DirectClassFile) Attribute(com.android.dx.cf.iface.Attribute) AttributeList(com.android.dx.cf.iface.AttributeList) BaseAnnotations(com.android.dx.cf.attrib.BaseAnnotations) ByteArray(com.android.dx.util.ByteArray) File(java.io.File) DirectClassFile(com.android.dx.cf.direct.DirectClassFile)

Example 27 with ByteArray

use of com.android.dx.util.ByteArray in project buck by facebook.

the class BlockDumper method ropDump.

/**
     * Does a registerizing dump.
     *
     * @param meth {@code non-null;} method data to dump
     */
private void ropDump(ConcreteMethod meth) {
    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    BytecodeArray code = meth.getCode();
    ByteArray bytes = code.getBytes();
    RopMethod rmeth = Ropper.convert(meth, advice, classFile.getMethods());
    StringBuffer sb = new StringBuffer(2000);
    if (optimize) {
        boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
        int paramWidth = computeParamWidth(meth, isStatic);
        rmeth = Optimizer.optimize(rmeth, paramWidth, isStatic, true, advice);
    }
    BasicBlockList blocks = rmeth.getBlocks();
    int[] order = blocks.getLabelsInOrder();
    sb.append("first " + Hex.u2(rmeth.getFirstLabel()) + "\n");
    for (int label : order) {
        BasicBlock bb = blocks.get(blocks.indexOfLabel(label));
        sb.append("block ");
        sb.append(Hex.u2(label));
        sb.append("\n");
        IntList preds = rmeth.labelToPredecessors(label);
        int psz = preds.size();
        for (int i = 0; i < psz; i++) {
            sb.append("  pred ");
            sb.append(Hex.u2(preds.get(i)));
            sb.append("\n");
        }
        InsnList il = bb.getInsns();
        int ilsz = il.size();
        for (int i = 0; i < ilsz; i++) {
            Insn one = il.get(i);
            sb.append("  ");
            sb.append(il.get(i).toHuman());
            sb.append("\n");
        }
        IntList successors = bb.getSuccessors();
        int ssz = successors.size();
        if (ssz == 0) {
            sb.append("  returns\n");
        } else {
            int primary = bb.getPrimarySuccessor();
            for (int i = 0; i < ssz; i++) {
                int succ = successors.get(i);
                sb.append("  next ");
                sb.append(Hex.u2(succ));
                if ((ssz != 1) && (succ == primary)) {
                    sb.append(" *");
                }
                sb.append("\n");
            }
        }
    }
    suppressDump = false;
    setAt(bytes, 0);
    parsed(bytes, 0, bytes.size(), sb.toString());
    suppressDump = true;
}
Also used : BytecodeArray(com.android.dx.cf.code.BytecodeArray) Insn(com.android.dx.rop.code.Insn) RopMethod(com.android.dx.rop.code.RopMethod) BasicBlock(com.android.dx.rop.code.BasicBlock) DexTranslationAdvice(com.android.dx.rop.code.DexTranslationAdvice) TranslationAdvice(com.android.dx.rop.code.TranslationAdvice) InsnList(com.android.dx.rop.code.InsnList) IntList(com.android.dx.util.IntList) ByteArray(com.android.dx.util.ByteArray) BasicBlockList(com.android.dx.rop.code.BasicBlockList)

Example 28 with ByteArray

use of com.android.dx.util.ByteArray in project buck by facebook.

the class ClassDumper method dump.

/**
     * Does the dumping.
     */
public void dump() {
    byte[] bytes = getBytes();
    ByteArray ba = new ByteArray(bytes);
    DirectClassFile cf = new DirectClassFile(ba, getFilePath(), getStrictParse());
    cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
    cf.setObserver(this);
    // Force parsing to happen.
    cf.getMagic();
    int at = getAt();
    if (at != bytes.length) {
        parsed(ba, at, bytes.length - at, "<extra data at end of file>");
    }
}
Also used : DirectClassFile(com.android.dx.cf.direct.DirectClassFile) ByteArray(com.android.dx.util.ByteArray)

Example 29 with ByteArray

use of com.android.dx.util.ByteArray in project buck by facebook.

the class StdAttributeFactory method exceptions.

/**
     * Parses an {@code Exceptions} attribute.
     */
private Attribute exceptions(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 2) {
        return throwSeverelyTruncated();
    }
    ByteArray bytes = cf.getBytes();
    // number_of_exceptions
    int count = bytes.getUnsignedShort(offset);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "number_of_exceptions: " + Hex.u2(count));
    }
    offset += 2;
    length -= 2;
    if (length != (count * 2)) {
        throwBadLength((count * 2) + 2);
    }
    TypeList list = cf.makeTypeList(offset, count);
    return new AttExceptions(list);
}
Also used : AttExceptions(com.android.dx.cf.attrib.AttExceptions) ByteArray(com.android.dx.util.ByteArray) TypeList(com.android.dx.rop.type.TypeList)

Example 30 with ByteArray

use of com.android.dx.util.ByteArray in project buck by facebook.

the class StdAttributeFactory method localVariableTypeTable.

/**
     * Parses a {@code LocalVariableTypeTable} attribute.
     */
private Attribute localVariableTypeTable(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length < 2) {
        return throwSeverelyTruncated();
    }
    ByteArray bytes = cf.getBytes();
    int count = bytes.getUnsignedShort(offset);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "local_variable_type_table_length: " + Hex.u2(count));
    }
    LocalVariableList list = parseLocalVariables(bytes.slice(offset + 2, offset + length), cf.getConstantPool(), observer, count, true);
    return new AttLocalVariableTypeTable(list);
}
Also used : LocalVariableList(com.android.dx.cf.code.LocalVariableList) AttLocalVariableTypeTable(com.android.dx.cf.attrib.AttLocalVariableTypeTable) ByteArray(com.android.dx.util.ByteArray)

Aggregations

ByteArray (com.android.dx.util.ByteArray)42 ConstantPool (com.android.dx.rop.cst.ConstantPool)18 CstString (com.android.dx.rop.cst.CstString)15 Attribute (com.android.dx.cf.iface.Attribute)14 ParseException (com.android.dx.cf.iface.ParseException)8 CstType (com.android.dx.rop.cst.CstType)7 LocalVariableList (com.android.dx.cf.code.LocalVariableList)6 DirectClassFile (com.android.dx.cf.direct.DirectClassFile)5 BytecodeArray (com.android.dx.cf.code.BytecodeArray)4 StdAttributeList (com.android.dx.cf.iface.StdAttributeList)4 CstNat (com.android.dx.rop.cst.CstNat)4 ByteCatchList (com.android.dx.cf.code.ByteCatchList)3 AttCode (com.android.dx.cf.attrib.AttCode)2 AttConstantValue (com.android.dx.cf.attrib.AttConstantValue)2 AttEnclosingMethod (com.android.dx.cf.attrib.AttEnclosingMethod)2 AttExceptions (com.android.dx.cf.attrib.AttExceptions)2 AttInnerClasses (com.android.dx.cf.attrib.AttInnerClasses)2 AttLineNumberTable (com.android.dx.cf.attrib.AttLineNumberTable)2 AttLocalVariableTable (com.android.dx.cf.attrib.AttLocalVariableTable)2 AttLocalVariableTypeTable (com.android.dx.cf.attrib.AttLocalVariableTypeTable)2