Search in sources :

Example 1 with DirectClassFile

use of com.taobao.android.dx.cf.direct.DirectClassFile 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 DirectClassFile

use of com.taobao.android.dx.cf.direct.DirectClassFile in project atlas by alibaba.

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.taobao.android.dx.cf.direct.DirectClassFile) ByteArray(com.taobao.android.dx.util.ByteArray)

Example 3 with DirectClassFile

use of com.taobao.android.dx.cf.direct.DirectClassFile in project atlas by alibaba.

the class ClassReferenceListBuilder method addClassWithHierachy.

private void addClassWithHierachy(String classBinaryName) {
    if (classNames.contains(classBinaryName)) {
        return;
    }
    try {
        DirectClassFile classFile = path.getClass(classBinaryName + CLASS_EXTENSION);
        classNames.add(classBinaryName);
        CstType superClass = classFile.getSuperclass();
        if (superClass != null) {
            addClassWithHierachy(superClass.getClassType().getClassName());
        }
        TypeList interfaceList = classFile.getInterfaces();
        int interfaceNumber = interfaceList.size();
        for (int i = 0; i < interfaceNumber; i++) {
            addClassWithHierachy(interfaceList.getType(i).getClassName());
        }
    } catch (FileNotFoundException e) {
    // Ignore: The referenced type is not in the path it must be part of the libraries.
    }
}
Also used : DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) CstType(com.taobao.android.dx.rop.cst.CstType) FileNotFoundException(java.io.FileNotFoundException) TypeList(com.taobao.android.dx.rop.type.TypeList)

Example 4 with DirectClassFile

use of com.taobao.android.dx.cf.direct.DirectClassFile in project atlas by alibaba.

the class ClassReferenceListBuilder method addRoots.

/**
     * @param jarOfRoots Archive containing the class files resulting of the tracing, typically
     * this is the result of running ProGuard.
     */
public void addRoots(ZipFile jarOfRoots) throws IOException {
    // keep roots
    for (Enumeration<? extends ZipEntry> entries = jarOfRoots.entries(); entries.hasMoreElements(); ) {
        ZipEntry entry = entries.nextElement();
        String name = entry.getName();
        if (name.endsWith(CLASS_EXTENSION)) {
            classNames.add(name.substring(0, name.length() - CLASS_EXTENSION.length()));
        }
    }
    // keep direct references of roots (+ direct references hierarchy)
    for (Enumeration<? extends ZipEntry> entries = jarOfRoots.entries(); entries.hasMoreElements(); ) {
        ZipEntry entry = entries.nextElement();
        String name = entry.getName();
        if (name.endsWith(CLASS_EXTENSION)) {
            DirectClassFile classFile;
            try {
                classFile = path.getClass(name);
            } catch (FileNotFoundException e) {
                throw new IOException("Class " + name + " is missing form original class path " + path, e);
            }
            addDependencies(classFile.getConstantPool());
        }
    }
}
Also used : DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 5 with DirectClassFile

use of com.taobao.android.dx.cf.direct.DirectClassFile in project atlas by alibaba.

the class Path method getClass.

synchronized DirectClassFile getClass(String path) throws FileNotFoundException {
    DirectClassFile classFile = null;
    for (ClassPathElement element : elements) {
        try {
            InputStream in = element.open(path);
            try {
                byte[] bytes = readStream(in, baos, readBuffer);
                baos.reset();
                classFile = new DirectClassFile(bytes, path, false);
                classFile.setAttributeFactory(StdAttributeFactory.THE_ONE);
                break;
            } finally {
                in.close();
            }
        } catch (IOException e) {
        // search next element
        }
    }
    if (classFile == null) {
        throw new FileNotFoundException("File \"" + path + "\" not found");
    }
    return classFile;
}
Also used : DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Aggregations

DirectClassFile (com.taobao.android.dx.cf.direct.DirectClassFile)8 ByteArray (com.taobao.android.dx.util.ByteArray)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)2 BaseAnnotations (com.taobao.android.dx.cf.attrib.BaseAnnotations)1 ClassPathOpener (com.taobao.android.dx.cf.direct.ClassPathOpener)1 Attribute (com.taobao.android.dx.cf.iface.Attribute)1 AttributeList (com.taobao.android.dx.cf.iface.AttributeList)1 ParseException (com.taobao.android.dx.cf.iface.ParseException)1 ClassDefItem (com.taobao.android.dx.dex.file.ClassDefItem)1 DexFile (com.taobao.android.dx.dex.file.DexFile)1 CstType (com.taobao.android.dx.rop.cst.CstType)1 TypeList (com.taobao.android.dx.rop.type.TypeList)1 File (java.io.File)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1