Search in sources :

Example 1 with Attribute

use of com.taobao.android.dx.cf.iface.Attribute in project atlas by alibaba.

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.taobao.android.dx.cf.direct.ClassPathOpener) DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) Attribute(com.taobao.android.dx.cf.iface.Attribute) AttributeList(com.taobao.android.dx.cf.iface.AttributeList) BaseAnnotations(com.taobao.android.dx.cf.attrib.BaseAnnotations) ByteArray(com.taobao.android.dx.util.ByteArray) DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) File(java.io.File)

Example 2 with Attribute

use of com.taobao.android.dx.cf.iface.Attribute in project atlas by alibaba.

the class StdAttributeFactory method constantValue.

/**
 * Parses a {@code ConstantValue} attribute.
 */
private Attribute constantValue(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length != 2) {
        return throwBadLength(2);
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    int idx = bytes.getUnsignedShort(offset);
    TypedConstant cst = (TypedConstant) pool.get(idx);
    Attribute result = new AttConstantValue(cst);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "value: " + cst);
    }
    return result;
}
Also used : TypedConstant(com.taobao.android.dx.rop.cst.TypedConstant) Attribute(com.taobao.android.dx.cf.iface.Attribute) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) ByteArray(com.taobao.android.dx.util.ByteArray) AttConstantValue(com.taobao.android.dx.cf.attrib.AttConstantValue)

Example 3 with Attribute

use of com.taobao.android.dx.cf.iface.Attribute in project atlas by alibaba.

the class StdAttributeFactory method sourceFile.

/**
 * Parses a {@code SourceFile} attribute.
 */
private Attribute sourceFile(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length != 2) {
        throwBadLength(2);
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    int idx = bytes.getUnsignedShort(offset);
    CstString cst = (CstString) pool.get(idx);
    Attribute result = new AttSourceFile(cst);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "source: " + cst);
    }
    return result;
}
Also used : Attribute(com.taobao.android.dx.cf.iface.Attribute) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) CstString(com.taobao.android.dx.rop.cst.CstString) AttSourceFile(com.taobao.android.dx.cf.attrib.AttSourceFile) ByteArray(com.taobao.android.dx.util.ByteArray)

Example 4 with Attribute

use of com.taobao.android.dx.cf.iface.Attribute in project atlas by alibaba.

the class StdAttributeFactory method enclosingMethod.

/**
 * Parses an {@code EnclosingMethod} attribute.
 */
private Attribute enclosingMethod(DirectClassFile cf, int offset, int length, ParseObserver observer) {
    if (length != 4) {
        throwBadLength(4);
    }
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    int idx = bytes.getUnsignedShort(offset);
    CstType type = (CstType) pool.get(idx);
    idx = bytes.getUnsignedShort(offset + 2);
    CstNat method = (CstNat) pool.get0Ok(idx);
    Attribute result = new AttEnclosingMethod(type, method);
    if (observer != null) {
        observer.parsed(bytes, offset, 2, "class: " + type);
        observer.parsed(bytes, offset + 2, 2, "method: " + DirectClassFile.stringOrNone(method));
    }
    return result;
}
Also used : CstNat(com.taobao.android.dx.rop.cst.CstNat) AttEnclosingMethod(com.taobao.android.dx.cf.attrib.AttEnclosingMethod) Attribute(com.taobao.android.dx.cf.iface.Attribute) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) CstType(com.taobao.android.dx.rop.cst.CstType) ByteArray(com.taobao.android.dx.util.ByteArray)

Example 5 with Attribute

use of com.taobao.android.dx.cf.iface.Attribute in project atlas by alibaba.

the class AttributeFactory method parse0.

/**
 * Parses attribute content. The base class implements this by constructing
 * an instance of {@link RawAttribute}. Subclasses are expected to
 * override this to do something better in most cases.
 *
 * @param cf {@code non-null;} class file to parse from
 * @param context context to parse in; one of the {@code CTX_*}
 * constants
 * @param name {@code non-null;} the attribute name
 * @param offset offset into {@code bytes} to start parsing at; this
 * is the offset to the start of attribute data, not to the header
 * @param length the length of the attribute data
 * @param observer {@code null-ok;} parse observer to report to, if any
 * @return {@code non-null;} an appropriately-constructed {@link Attribute}
 */
protected Attribute parse0(DirectClassFile cf, int context, String name, int offset, int length, ParseObserver observer) {
    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    Attribute result = new RawAttribute(name, bytes, offset, length, pool);
    if (observer != null) {
        observer.parsed(bytes, offset, length, "attribute data");
    }
    return result;
}
Also used : RawAttribute(com.taobao.android.dx.cf.attrib.RawAttribute) Attribute(com.taobao.android.dx.cf.iface.Attribute) ConstantPool(com.taobao.android.dx.rop.cst.ConstantPool) RawAttribute(com.taobao.android.dx.cf.attrib.RawAttribute) ByteArray(com.taobao.android.dx.util.ByteArray)

Aggregations

Attribute (com.taobao.android.dx.cf.iface.Attribute)8 ByteArray (com.taobao.android.dx.util.ByteArray)7 ConstantPool (com.taobao.android.dx.rop.cst.ConstantPool)5 AttSourceFile (com.taobao.android.dx.cf.attrib.AttSourceFile)2 AttributeList (com.taobao.android.dx.cf.iface.AttributeList)2 CstString (com.taobao.android.dx.rop.cst.CstString)2 AttConstantValue (com.taobao.android.dx.cf.attrib.AttConstantValue)1 AttEnclosingMethod (com.taobao.android.dx.cf.attrib.AttEnclosingMethod)1 AttSignature (com.taobao.android.dx.cf.attrib.AttSignature)1 BaseAnnotations (com.taobao.android.dx.cf.attrib.BaseAnnotations)1 RawAttribute (com.taobao.android.dx.cf.attrib.RawAttribute)1 ClassPathOpener (com.taobao.android.dx.cf.direct.ClassPathOpener)1 DirectClassFile (com.taobao.android.dx.cf.direct.DirectClassFile)1 ParseException (com.taobao.android.dx.cf.iface.ParseException)1 StdAttributeList (com.taobao.android.dx.cf.iface.StdAttributeList)1 CstNat (com.taobao.android.dx.rop.cst.CstNat)1 CstType (com.taobao.android.dx.rop.cst.CstType)1 TypedConstant (com.taobao.android.dx.rop.cst.TypedConstant)1 File (java.io.File)1