Example usage for org.objectweb.asm.tree ClassNode accept

List of usage examples for org.objectweb.asm.tree ClassNode accept

Introduction

In this page you can find the example usage for org.objectweb.asm.tree ClassNode accept.

Prototype

public void accept(final ClassVisitor classVisitor) 

Source Link

Document

Makes the given class visitor visit this class.

Usage

From source file:cpw.mods.fml.common.asm.ASMTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if ("net.minecraft.src.Block".equals(name)) {
        ClassReader cr = new ClassReader(bytes);
        ClassNode cn = new ClassNode(Opcodes.ASM4);
        cr.accept(cn, ClassReader.EXPAND_FRAMES);
        cn.interfaces.add(Type.getInternalName(BlockProxy.class));
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        cn.accept(cw);
        return cw.toByteArray();
    }/*from w ww  .  j  a va2s  . c o  m*/

    return bytes;
}

From source file:cpw.mods.fml.common.asm.transformers.AccessTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null) {
        return null;
    }//from w w  w  .ja  v a  2 s .c o m
    boolean makeAllPublic = FMLDeobfuscatingRemapper.INSTANCE.isRemappedClass(name);

    if (DEBUG) {
        FMLRelaunchLog.fine("Considering all methods and fields on %s (%s): %b\n", name, transformedName,
                makeAllPublic);
    }
    if (!makeAllPublic && !modifiers.containsKey(name)) {
        return bytes;
    }

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    if (makeAllPublic) {
        // class
        Modifier m = new Modifier();
        m.targetAccess = ACC_PUBLIC;
        m.modifyClassVisibility = true;
        modifiers.put(name, m);
        // fields
        m = new Modifier();
        m.targetAccess = ACC_PUBLIC;
        m.name = "*";
        modifiers.put(name, m);
        // methods
        m = new Modifier();
        m.targetAccess = ACC_PUBLIC;
        m.name = "*";
        m.desc = "<dummy>";
        modifiers.put(name, m);
        if (DEBUG) {
            System.out.printf("Injected all public modifiers for %s (%s)\n", name, transformedName);
        }
    }

    Collection<Modifier> mods = modifiers.get(name);
    for (Modifier m : mods) {
        if (m.modifyClassVisibility) {
            classNode.access = getFixedAccess(classNode.access, m);
            if (DEBUG) {
                System.out.println(String.format("Class: %s %s -> %s", name, toBinary(m.oldAccess),
                        toBinary(m.newAccess)));
            }
            continue;
        }
        if (m.desc.isEmpty()) {
            for (FieldNode n : classNode.fields) {
                if (n.name.equals(m.name) || m.name.equals("*")) {
                    n.access = getFixedAccess(n.access, m);
                    if (DEBUG) {
                        System.out.println(String.format("Field: %s.%s %s -> %s", name, n.name,
                                toBinary(m.oldAccess), toBinary(m.newAccess)));
                    }

                    if (!m.name.equals("*")) {
                        break;
                    }
                }
            }
        } else {
            for (MethodNode n : classNode.methods) {
                if ((n.name.equals(m.name) && n.desc.equals(m.desc)) || m.name.equals("*")) {
                    n.access = getFixedAccess(n.access, m);
                    if (DEBUG) {
                        System.out.println(String.format("Method: %s.%s%s %s -> %s", name, n.name, n.desc,
                                toBinary(m.oldAccess), toBinary(m.newAccess)));
                    }

                    if (!m.name.equals("*")) {
                        break;
                    }
                }
            }
        }
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:cpw.mods.fml.common.asm.transformers.MarkerTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null) {
        return null;
    }//from   ww  w .j a  v a  2 s  .  co m
    if (!markers.containsKey(name)) {
        return bytes;
    }

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    for (String marker : markers.get(name)) {
        classNode.interfaces.add(marker);
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:cpw.mods.fml.common.asm.transformers.MCPMerger.java

License:Open Source License

private static void copyClass(ZipFile inJar, ZipEntry entry, ZipOutputStream outJar, ZipOutputStream outJar2,
        boolean isClientOnly) throws IOException {
    ClassReader reader = new ClassReader(readEntry(inJar, entry));
    ClassNode classNode = new ClassNode();

    reader.accept(classNode, 0);// w  w  w .  ja v  a2 s  . c o m

    if (!dontAnnotate.contains(classNode.name)) {
        if (classNode.visibleAnnotations == null)
            classNode.visibleAnnotations = new ArrayList<AnnotationNode>();
        classNode.visibleAnnotations.add(getSideAnn(isClientOnly));
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    byte[] data = writer.toByteArray();

    ZipEntry newEntry = new ZipEntry(entry.getName());
    if (outJar != null) {
        outJar.putNextEntry(newEntry);
        outJar.write(data);
    }
    if (outJar2 != null) {
        outJar2.putNextEntry(newEntry);
        outJar2.write(data);
    }
}

From source file:cpw.mods.fml.common.asm.transformers.MCPMerger.java

License:Open Source License

public static byte[] processClass(byte[] cIn, byte[] sIn, ClassInfo info) {
    ClassNode cClassNode = getClassNode(cIn);
    ClassNode sClassNode = getClassNode(sIn);

    processFields(cClassNode, sClassNode, info);
    processMethods(cClassNode, sClassNode, info);

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cClassNode.accept(writer);
    return writer.toByteArray();
}

From source file:cpw.mods.fml.common.asm.transformers.SideTransformer.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null) {
        return null;
    }/*from w  w w . j  av a2  s  .c  o m*/

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    if (remove((List<AnnotationNode>) classNode.visibleAnnotations, SIDE)) {
        if (DEBUG) {
            System.out.println(
                    String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
        }
        throw new RuntimeException(
                String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
    }

    Iterator<FieldNode> fields = classNode.fields.iterator();
    while (fields.hasNext()) {
        FieldNode field = fields.next();
        if (remove((List<AnnotationNode>) field.visibleAnnotations, SIDE)) {
            if (DEBUG) {
                System.out.println(String.format("Removing Field: %s.%s", classNode.name, field.name));
            }
            fields.remove();
        }
    }
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while (methods.hasNext()) {
        MethodNode method = methods.next();
        if (remove((List<AnnotationNode>) method.visibleAnnotations, SIDE)) {
            if (DEBUG) {
                System.out.println(
                        String.format("Removing Method: %s.%s%s", classNode.name, method.name, method.desc));
            }
            methods.remove();
        }
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}

From source file:cuchaz.enigma.CompiledSourceTypeLoader.java

License:Open Source License

private byte[] loadType(String className) {
    ClassEntry entry = new ClassEntry(className);

    // find the class in the jar
    ClassNode node = findClassNode(entry);
    if (node == null) {
        // couldn't find it
        return null;
    }/*from   ww  w  .  j  a v a2s.co m*/

    removeRedundantClassCalls(node);

    ClassWriter writer = new ClassWriter(0);

    ClassVisitor visitor = writer;
    for (Function<ClassVisitor, ClassVisitor> visitorFunction : this.visitors) {
        visitor = visitorFunction.apply(visitor);
    }

    node.accept(visitor);

    // we have a transformed class!
    return writer.toByteArray();
}

From source file:cuchaz.enigma.TranslatingTypeLoader.java

License:Open Source License

@Override
public String transformInto(ClassNode node, ClassWriter writer) {
    node.accept(
            new TranslationClassVisitor(deobfuscatingTranslator, jarIndex, entryPool, Opcodes.ASM5, writer));
    return deobfuscatingTranslator.getTranslatedClass(new ClassEntry(node.name)).getName();
}

From source file:customskinloader.modfix.ModFixTransformer.java

License:GNU General Public License

@Override
public byte[] transform(String className, String transformedName, byte[] bytes) {
    if (!map.containsKey(className))
        return bytes;
    //FMLRelaunchLog.info("[ModFixTransformer] !!! "+className);
    Map<String, IMethodTransformer> transMap = map.get(className);
    ClassReader cr = new ClassReader(bytes);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);/*  w ww  . ja v  a2s.c om*/

    List<MethodNode> ml = new ArrayList<MethodNode>();
    ml.addAll(cn.methods);
    for (MethodNode mn : ml) {
        String methodName = mn.name;
        String methodDesc = mn.desc;
        //FMLRelaunchLog.info("[ModFixTransformer] ! "+methodName+" "+methodDesc);
        if (transMap.containsKey(methodName + methodDesc)) {
            try {
                FMLRelaunchLog.info("[ModFixTransformer] Transforming method %s in class %s",
                        methodName + methodDesc, className);
                transMap.get(methodName + methodDesc).transform(mn);
                FMLRelaunchLog.info("[ModFixTransformer] Successfully transformed method %s in class %s",
                        methodName + methodDesc, className);
            } catch (Exception e) {
                FMLRelaunchLog.warning(
                        "[ModFixTransformer] An error happened when transforming method %s in class %s. The whole class was not modified.",
                        methodName + methodDesc, className);
                e.printStackTrace();
                return bytes;
            }
        }
    }

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cn.accept(cw);
    return cw.toByteArray();
}

From source file:de.enough.polish.libraryprocessor.ImportLibraryProcessor.java

License:Open Source License

public void processLibrary(File baseDir, String[] relativeClassPaths, Device device, Locale locale,
        Environment env) throws IOException {
    //boolean isSlash = File.separatorChar == '/';
    ImportConversionMap renamingMap = new ImportConversionMap();
    addImportConversions(renamingMap, device, locale, env);

    ArrayList classes = new ArrayList();
    for (int i = 0; i < relativeClassPaths.length; i++) {
        // Cut off file extension.
        String classPath = relativeClassPaths[i];
        String className = classPath.substring(0, classPath.length() - ".class".length());

        // Add class name to known classes.
        classes.add(className);//from  w  w w  . j a v a2 s  .  c  o m
    }
    System.out.println("Converting imports in " + classes.size() + " classes from " + baseDir.getPath());

    // Create classloader for the classesDir.
    DirClassLoader loader = DirClassLoader.createClassLoader(baseDir);

    ASMClassLoader asmLoader = new ASMClassLoader(loader);
    Iterator classesIt = classes.iterator();

    while (classesIt.hasNext()) {
        String className = (String) classesIt.next();

        try {
            ClassNode classNode = asmLoader.loadClass(className);
            ClassWriter writer = new ClassWriter(0);
            ClassRenamingClassVisitor visitor = new ClassRenamingClassVisitor(writer, renamingMap);
            classNode.accept(visitor);

            writeClass(baseDir, className, writer.toByteArray(), renamingMap);
        } catch (ClassNotFoundException e) {
            System.out.println("Error loading class " + className);
        } catch (IOException e) {
            throw new BuildException(e);
        } catch (Exception e) {
            System.out.println("Error loading class " + className + ": " + e.toString());
            e.printStackTrace();
            throw new IOException(e.toString());
        }
    }
}