Search in sources :

Example 61 with ClassReader

use of org.objectweb.asm.ClassReader in project semantic-versioning by jeluard.

the class JarDiff method loadClasses.

/**
     * Load all the classes from the specified URL and store information
     * about them in the specified map.
     * This currently only works for jar files, <b>not</b> directories
     * which contain classes in subdirectories or in the current directory.
     *
     * @param infoMap the map to store the ClassInfo in.
     * @param file the jarfile to load classes from.
     * @throws IOException if there is an IOException reading info about a
     *                     class.
     */
private void loadClasses(Map infoMap, File file) throws DiffException {
    try {
        JarFile jar = new JarFile(file);
        Enumeration e = jar.entries();
        while (e.hasMoreElements()) {
            JarEntry entry = (JarEntry) e.nextElement();
            String name = entry.getName();
            if (!entry.isDirectory() && name.endsWith(".class")) {
                ClassReader reader = new ClassReader(jar.getInputStream(entry));
                ClassInfo ci = loadClassInfo(reader);
                infoMap.put(ci.getName(), ci);
            }
        }
    } catch (IOException ioe) {
        throw new DiffException(ioe);
    }
}
Also used : ClassReader(org.objectweb.asm.ClassReader) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 62 with ClassReader

use of org.objectweb.asm.ClassReader in project jphp by jphp-compiler.

the class ClassStmtCompiler method writeCopiedMethod.

protected void writeCopiedMethod(ClassStmtToken.Alias alias, String methodName, ClassEntity trait) {
    final MethodEntity methodEntity = fetchClassAndCheck(alias.getTrait()).findMethod(methodName.toLowerCase());
    String name = alias.getName();
    if (name == null)
        name = methodName;
    MethodEntity origin = entity.findMethod(name.toLowerCase());
    if (origin != null) {
        if (origin.getClazz() == entity) {
            if (origin.getTrait() != null) {
                compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_TRAIT_METHOD_COLLISION.fetch(methodName, trait.getName(), origin.getTrait().getName(), entity.getName()));
            }
            return;
        }
    }
    if (methodEntity == null) {
        compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_METHOD_NOT_FOUND.fetch(alias.getTrait(), methodName));
        return;
    }
    MethodEntity dup = methodEntity.duplicateForInject();
    dup.setClazz(entity);
    dup.setTrait(trait);
    if (alias.getName() != null)
        dup.setName(alias.getName());
    if (alias.getModifier() != null)
        dup.setModifier(alias.getModifier());
    MethodNodeImpl methodNode = MethodNodeImpl.duplicate(methodEntity.getAdditionalData("methodNode", MethodNode.class, new Function<MethodNode>() {

        @Override
        public MethodNode call() {
            ClassNode classNode = methodEntity.getClazz().getAdditionalData("classNode", ClassNode.class, new Function<ClassNode>() {

                @Override
                public ClassNode call() {
                    ClassReader classReader;
                    if (methodEntity.getClazz().getData() != null)
                        classReader = new ClassReader(methodEntity.getClazz().getData());
                    else {
                        try {
                            classReader = new ClassReader(methodEntity.getClazz().getName());
                        } catch (IOException e) {
                            throw new CriticalException(e);
                        }
                    }
                    ClassNode classNode = new ClassNode();
                    classReader.accept(classNode, 0);
                    return classNode;
                }
            });
            for (Object m : classNode.methods) {
                MethodNode method = (MethodNode) m;
                if (method.name.equals(methodEntity.getInternalName())) {
                    return method;
                }
            }
            throw new CriticalException("Cannot find MethodNode for method - " + methodEntity.getName() + "(" + methodEntity.getSignatureString(true) + ")");
        }
    }));
    if (origin != null) {
        dup.setPrototype(origin);
    }
    dup.setInternalName(dup.getName() + "$" + entity.nextMethodIndex());
    methodNode.name = dup.getInternalName();
    ClassEntity.SignatureResult result = entity.addMethod(dup, null);
    result.check(compiler.getEnvironment());
    node.methods.add(methodNode);
}
Also used : MethodNodeImpl(org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl) IOException(java.io.IOException) CriticalException(php.runtime.exceptions.CriticalException) Function(php.runtime.common.Function) ClassReader(org.objectweb.asm.ClassReader) BaseObject(php.runtime.lang.BaseObject)

Example 63 with ClassReader

use of org.objectweb.asm.ClassReader in project MinecraftForge by MinecraftForge.

the class AccessTransformer method processJar.

private static void processJar(File inFile, File outFile, AccessTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;
    ZipOutputStream outJar = null;
    try {
        try {
            inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open input file: " + e.getMessage());
        }
        try {
            outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e) {
            throw new FileNotFoundException("Could not open output file: " + e.getMessage());
        }
        ZipEntry entry;
        while ((entry = inJar.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                outJar.putNextEntry(entry);
                continue;
            }
            byte[] data = new byte[4096];
            ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();
            int len;
            do {
                len = inJar.read(data);
                if (len > 0) {
                    entryBuffer.write(data, 0, len);
                }
            } while (len != -1);
            byte[] entryData = entryBuffer.toByteArray();
            String entryName = entry.getName();
            if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
                ClassNode cls = new ClassNode();
                ClassReader rdr = new ClassReader(entryData);
                rdr.accept(cls, 0);
                String name = cls.name.replace('/', '.').replace('\\', '.');
                for (AccessTransformer trans : transformers) {
                    entryData = trans.transform(name, name, entryData);
                }
            }
            ZipEntry newEntry = new ZipEntry(entryName);
            outJar.putNextEntry(newEntry);
            outJar.write(entryData);
        }
    } finally {
        IOUtils.closeQuietly(outJar);
        IOUtils.closeQuietly(inJar);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) BufferedOutputStream(java.io.BufferedOutputStream)

Example 64 with ClassReader

use of org.objectweb.asm.ClassReader in project MinecraftForge by MinecraftForge.

the class BlamingTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    ClassReader classReader = new ClassReader(bytes);
    VersionVisitor visitor = new VersionVisitor();
    classReader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG);
    return bytes;
}
Also used : ClassReader(org.objectweb.asm.ClassReader)

Example 65 with ClassReader

use of org.objectweb.asm.ClassReader in project MinecraftForge by MinecraftForge.

the class DeobfuscationTransformer method transform.

// COMPUTE_FRAMES causes classes to be loaded, which could cause issues if the classes do not exist.
// However in testing this has not happened. {As we run post SideTransformer}
// If reported we need to add a custom implementation of ClassWriter.getCommonSuperClass
// that does not cause class loading.
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    ClassReader classReader = new ClassReader(bytes);
    ClassWriter classWriter = new ClassWriter(WRITER_FLAGS);
    RemappingClassAdapter remapAdapter = new FMLRemappingAdapter(classWriter);
    classReader.accept(remapAdapter, READER_FLAGS);
    return classWriter.toByteArray();
}
Also used : RemappingClassAdapter(org.objectweb.asm.commons.RemappingClassAdapter) FMLRemappingAdapter(net.minecraftforge.fml.common.asm.transformers.deobf.FMLRemappingAdapter) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

ClassReader (org.objectweb.asm.ClassReader)449 ClassWriter (org.objectweb.asm.ClassWriter)187 Test (org.junit.Test)134 IOException (java.io.IOException)78 InputStream (java.io.InputStream)76 TreeMap (java.util.TreeMap)59 ClassNode (org.objectweb.asm.tree.ClassNode)58 ClassVisitor (org.objectweb.asm.ClassVisitor)54 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)53 HashSet (java.util.HashSet)39 ZipEntry (java.util.zip.ZipEntry)34 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 ZipFile (java.util.zip.ZipFile)29 InvocationTargetException (java.lang.reflect.InvocationTargetException)26 Method (java.lang.reflect.Method)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23 InnerClass (com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass)23 PrintWriter (java.io.PrintWriter)23 MethodVisitor (org.objectweb.asm.MethodVisitor)23 MethodNode (org.objectweb.asm.tree.MethodNode)21