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

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

Introduction

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

Prototype

public ClassNode() 

Source Link

Document

Constructs a new ClassNode .

Usage

From source file:com.typesafe.path_hole.PathHole.java

License:Open Source License

public static Object[] getMethodNode(Class<?> clazz, String methodName, String methodDescription) {

    ClassReader cr = new ClassReader(getByteCode(clazz));
    ClassNode classNode = new ClassNode();
    cr.accept(classNode, 0);/*from  w w  w .j av  a 2 s.c  o m*/

    for (Object mnRaw : classNode.methods) {
        MethodNode mn = (MethodNode) mnRaw;
        if (mn.name.equals(methodName) && mn.desc.equals(methodDescription)) {
            return new Object[] { classNode, mn };
        }

    }
    throw new IllegalArgumentException("No method found for the provided params [clazz=" + clazz.getName()
            + ", methodName=" + methodName + ", desc=" + methodDescription);
}

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;
    }//ww  w  . j av a 2 s . co 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.AccessTransformer.java

License:Open Source License

private static void processJar(File inFile, File outFile, AccessTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;/*ww w  .  j a  v  a 2s.  c  om*/
    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 {
        if (outJar != null) {
            try {
                outJar.close();
            } catch (IOException e) {
            }
        }

        if (inJar != null) {
            try {
                inJar.close();
            } catch (IOException e) {
            }
        }
    }
}

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

License:Open Source License

private String getFieldType(String owner, String name) {
    if (fieldDescriptions.containsKey(owner)) {
        return fieldDescriptions.get(owner).get(name);
    }//www .  j a va  2s .c  o  m
    synchronized (fieldDescriptions) {
        try {
            byte[] classBytes = classLoader.getClassBytes(owner);
            if (classBytes == null) {
                return null;
            }
            ClassReader cr = new ClassReader(classBytes);
            ClassNode classNode = new ClassNode();
            cr.accept(classNode, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
            Map<String, String> resMap = Maps.newHashMap();
            for (FieldNode fieldNode : (List<FieldNode>) classNode.fields) {
                resMap.put(fieldNode.name, fieldNode.desc);
            }
            fieldDescriptions.put(owner, resMap);
            return resMap.get(name);
        } catch (IOException e) {
            FMLLog.log(Level.SEVERE, e, "A critical exception occured reading a class file %s", owner);
        }
        return null;
    }
}

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  w  w w. j  a  v a 2s . c  o  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.MarkerTransformer.java

License:Open Source License

private static void processJar(File inFile, File outFile, MarkerTransformer[] transformers) throws IOException {
    ZipInputStream inJar = null;/*w w  w .  jav a 2  s. c  om*/
    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 (MarkerTransformer trans : transformers) {
                    entryData = trans.transform(name, name, entryData);
                }
            }

            ZipEntry newEntry = new ZipEntry(entryName);
            outJar.putNextEntry(newEntry);
            outJar.write(entryData);
        }
    } finally {
        if (outJar != null) {
            try {
                outJar.close();
            } catch (IOException e) {
            }
        }

        if (inJar != null) {
            try {
                inJar.close();
            } catch (IOException e) {
            }
        }
    }
}

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);/*from w  ww  . ja va2 s  .co 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

private static ClassNode getClassNode(byte[] data) {
    ClassReader reader = new ClassReader(data);
    ClassNode classNode = new ClassNode();
    reader.accept(classNode, 0);/*w  ww  .  ja  v  a  2  s.  c o  m*/
    return classNode;
}

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;
    }/*w w  w  .j  av  a 2  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.analysis.ParsedJar.java

License:Open Source License

public ParsedJar(JarFile jar) throws IOException {
    try {/*from www  .  j  av a 2  s.c  o m*/
        // get the jar entries that correspond to classes
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            // is this a class file?
            if (entry.getName().endsWith(".class")) {
                try (InputStream input = new BufferedInputStream(jar.getInputStream(entry))) {
                    // read the ClassNode from the jar
                    ClassReader reader = new ClassReader(input);
                    ClassNode node = new ClassNode();
                    reader.accept(node, 0);
                    String path = entry.getName().substring(0, entry.getName().length() - ".class".length());
                    nodes.put(path, node);
                }
            }
        }
    } finally {
        jar.close();
    }
}