Search in sources :

Example 16 with DirectClassFile

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

use of com.android.dx.cf.direct.DirectClassFile in project buck by facebook.

the class DotDumper method run.

private void run() {
    ByteArray ba = new ByteArray(bytes);
    /*
         * First, parse the file completely, so we can safely refer to
         * attributes, etc.
         */
    classFile = new DirectClassFile(ba, filePath, strictParse);
    classFile.setAttributeFactory(StdAttributeFactory.THE_ONE);
    // Force parsing to happen.
    classFile.getMagic();
    // Next, reparse it and observe the process.
    DirectClassFile liveCf = new DirectClassFile(ba, filePath, strictParse);
    liveCf.setAttributeFactory(StdAttributeFactory.THE_ONE);
    liveCf.setObserver(this);
    // Force parsing to happen.
    liveCf.getMagic();
}
Also used : DirectClassFile(com.android.dx.cf.direct.DirectClassFile) ByteArray(com.android.dx.util.ByteArray)

Example 18 with DirectClassFile

use of com.android.dx.cf.direct.DirectClassFile in project buck by facebook.

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)) {
            if (CLASS_TO_CHECK != null && name.contains(CLASS_TO_CHECK)) {
                found();
            }
            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.android.dx.cf.direct.DirectClassFile) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 19 with DirectClassFile

use of com.android.dx.cf.direct.DirectClassFile in project buck by facebook.

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, byteArrayOutputStream, readBuffer);
                byteArrayOutputStream.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.android.dx.cf.direct.DirectClassFile) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 20 with DirectClassFile

use of com.android.dx.cf.direct.DirectClassFile in project dex2jar by pxb1988.

the class TestUtils method translateAndCheck.

public static byte[] translateAndCheck(DexFileNode fileNode, DexClassNode clzNode) throws AnalyzerException, IllegalAccessException {
    // 1. convert to .class
    Dex2Asm dex2Asm = new Dex2Asm() {

        @Override
        public void convertCode(DexMethodNode methodNode, MethodVisitor mv) {
            try {
                super.convertCode(methodNode, mv);
            } catch (Exception ex) {
                BaksmaliDumper d = new BaksmaliDumper();
                try {
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.err, "UTF-8"));
                    d.baksmaliMethod(methodNode, out);
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                throw new DexException(ex, "fail convert code %s", methodNode.method);
            }
        }
    };
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ClassVisitorFactory cvf = new ClassVisitorFactory() {

        @Override
        public ClassVisitor create(String classInternalName) {
            return cw;
        }
    };
    if (fileNode != null) {
        dex2Asm.convertClass(clzNode, cvf, fileNode);
    } else {
        dex2Asm.convertClass(clzNode, cvf);
    }
    byte[] data = cw.toByteArray();
    // 2. verify .class
    ClassReader cr = new ClassReader(data);
    TestUtils.verify(cr);
    // 3. convert back to dex
    CfOptions cfOptions = new CfOptions();
    cfOptions.strictNameCheck = false;
    DexOptions dexOptions = new DexOptions();
    DirectClassFile dcf = new DirectClassFile(data, clzNode.className.substring(1, clzNode.className.length() - 1) + ".class", true);
    dcf.setAttributeFactory(new StdAttributeFactory());
    com.android.dx.dex.file.DexFile dxFile = new com.android.dx.dex.file.DexFile(dexOptions);
    CfTranslator.translate(dcf, data, cfOptions, dexOptions, dxFile);
    return data;
}
Also used : DexException(com.googlecode.d2j.DexException) BaksmaliDumper(com.googlecode.d2j.smali.BaksmaliDumper) StdAttributeFactory(com.android.dx.cf.direct.StdAttributeFactory) MethodVisitor(org.objectweb.asm.MethodVisitor) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) Dex2Asm(com.googlecode.d2j.dex.Dex2Asm) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) DexOptions(com.android.dx.dex.DexOptions) AnalyzerException(org.objectweb.asm.tree.analysis.AnalyzerException) DexException(com.googlecode.d2j.DexException) ZipException(java.util.zip.ZipException) ClassWriter(org.objectweb.asm.ClassWriter) DirectClassFile(com.android.dx.cf.direct.DirectClassFile) ClassReader(org.objectweb.asm.ClassReader) ClassVisitorFactory(com.googlecode.d2j.dex.ClassVisitorFactory) CfOptions(com.android.dx.dex.cf.CfOptions)

Aggregations

DirectClassFile (com.android.dx.cf.direct.DirectClassFile)20 FileNotFoundException (java.io.FileNotFoundException)11 IOException (java.io.IOException)6 ByteArray (com.android.dx.util.ByteArray)5 CstType (com.android.dx.rop.cst.CstType)4 StdTypeList (com.android.dx.rop.type.StdTypeList)3 TypeList (com.android.dx.rop.type.TypeList)3 InputStream (java.io.InputStream)3 ZipEntry (java.util.zip.ZipEntry)3 File (java.io.File)2 BaseAnnotations (com.android.dx.cf.attrib.BaseAnnotations)1 ClassPathOpener (com.android.dx.cf.direct.ClassPathOpener)1 StdAttributeFactory (com.android.dx.cf.direct.StdAttributeFactory)1 Attribute (com.android.dx.cf.iface.Attribute)1 AttributeList (com.android.dx.cf.iface.AttributeList)1 DexOptions (com.android.dx.dex.DexOptions)1 CfOptions (com.android.dx.dex.cf.CfOptions)1 DexFile (com.android.dx.dex.file.DexFile)1 DexException (com.googlecode.d2j.DexException)1 ClassVisitorFactory (com.googlecode.d2j.dex.ClassVisitorFactory)1