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.heliosdecompiler.helios.LoadedFile.java

License:Apache License

public ClassNode getClassNode(String name) {
    if (!this.isPath) {
        return this.classes.get(name);
    } else {//from   w  ww.j  ava2 s.  c om
        if (!this.classes.containsKey(name)) {
            try {
                ClassReader classReader = new ClassReader(new ByteArrayInputStream(files.get(name)));
                ClassNode classNode = new ClassNode();
                classReader.accept(classNode, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG);

                // Store by ClassNode name
                classes.put(classNode.name, classNode);
                // Also store by path
                classes.put(name, classNode);
            } catch (Exception ignored) { //Malformed class
            }
        }
        return this.classes.get(name);
    }
}

From source file:com.insightfullogic.stripper.Runner.java

License:Open Source License

static void rewrite(final String classFile, final ClassRules rules) {
    try {/*from w w w .  j a v a2s . c  om*/
        // Read
        final ClassReader cr = new ClassReader(classFile);
        final ClassNode cls = new ClassNode();
        cr.accept(cls, 0);

        rules.strip(cls);

        // Write
        final ClassWriter cw = new ClassWriter(0);
        cls.accept(cw);
        try (FileOutputStream out = new FileOutputStream(classFile)) {
            out.write(cw.toByteArray());
        }
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.insightfullogic.stripper.TestClassRules.java

License:Open Source License

@Test
public void factoryLoadsAndRuns() throws IOException {
    final ClassNode example = new ClassNode();
    final ClassReader cr = new ClassReader("com.insightfullogic.stripper.ExampleClass");
    cr.accept(example, 0);//from   w ww. j a  v  a2s .  c o m

    final ClassRules rules = ClassRules.compileFrom("src/test/resources/example-rules.py");
    assertEquals(3, rules.predicates.size());

    final MethodNameRegex name = (MethodNameRegex) rules.predicates.get(0);
    assertEquals("toString", name.regex.pattern());

    final ReturnTypeName ret = (ReturnTypeName) rules.predicates.get(1);
    assertEquals("I", ret.typeName);

    final ArgumentTypeName arg = (ArgumentTypeName) rules.predicates.get(2);
    assertEquals("I", arg.typeName);
    assertEquals(0, arg.argNumber);

    rules.strip(example);

    assertEquals(2, example.methods.size());
    final MethodNode meth = (MethodNode) example.methods.get(1);
    assertEquals("takeIntFalsePositive", meth.name);
}

From source file:com.jcwhatever.nucleus.localizer.LanguageGenerator.java

License:MIT License

private List<LiteralInfo> getStringLiterals(File file) throws IOException {

    System.out.println("Opening jar file: " + file.getAbsolutePath());

    JarFile jarFile = new JarFile(file);
    Enumeration<JarEntry> entries = jarFile.entries();
    List<LiteralInfo> results = new ArrayList<>(50);

    Deque<ClassNode> parseQueue = new ArrayDeque<>(10);
    Map<String, AnnotationInfo> annotations = new HashMap<>(10);

    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();

        if (entry.isDirectory() || !entry.getName().endsWith(".class"))
            continue;

        InputStream stream = jarFile.getInputStream(entry);
        ClassReader reader = new ClassReader(stream);

        ClassNode classNode = new ClassNode();
        reader.accept(classNode, 0);//from  www.  java  2  s  . c  o  m

        // check if class is an annotation
        if (!classNode.interfaces.isEmpty()
                && classNode.interfaces.contains("java/lang/annotation/Annotation")) {

            // get annotation first so they can be applied in classes
            AnnotationInfo info = parseAnnotation(classNode);
            if (info == null)
                continue;

            annotations.put(info.className, info);
        } else {
            // set aside non-annotation classes for now
            parseQueue.addLast(classNode);
        }

        stream.close();
    }

    // parse classes
    while (!parseQueue.isEmpty()) {
        ClassNode node = parseQueue.removeFirst();
        results.addAll(parseClass(node, annotations));
    }

    jarFile.close();

    return results;
}

From source file:com.khubla.simpleioc.classlibrary.ClassLibrary.java

License:Open Source License

/**
 * find all the classes in a jar file// w  ww  .ja va  2  s.c  o m
 */
private List<Class<?>> crackJar(String jarfile) throws Exception {
    try {
        /*
         * the ret
         */
        final List<Class<?>> ret = new ArrayList<Class<?>>();
        /*
         * the jar
         */
        final FileInputStream fis = new FileInputStream(jarfile);
        final ZipInputStream zip_inputstream = new ZipInputStream(fis);
        ZipEntry current_zip_entry = null;
        while ((current_zip_entry = zip_inputstream.getNextEntry()) != null) {
            if (current_zip_entry.getName().endsWith(".class")) {
                if (current_zip_entry.getSize() > 0) {
                    final ClassNode classNode = new ClassNode();
                    final ClassReader cr = new ClassReader(zip_inputstream);
                    cr.accept(classNode, 0);
                    if (annotated(classNode)) {
                        ret.add(Class.forName(classNode.name.replaceAll("/", ".")));
                        log.debug("Found " + classNode.name + " in " + jarfile);
                    }
                }
            }
        }
        zip_inputstream.close();
        fis.close();
        return ret;
    } catch (final Throwable e) {
        throw new Exception("Exception in crackJar for jar '" + jarfile + "'", e);
    }
}

From source file:com.khubla.simpleioc.classlibrary.ClassLibrary.java

License:Open Source License

/**
 * scan a given directory/*  www . j  a  v  a 2 s.c  o  m*/
 */
private List<Class<?>> scan(File file, String concatenatedName) throws Exception {
    try {
        log.debug("scanning directory'" + file.getAbsolutePath() + "'");
        /*
         * collection of all classes
         */
        final List<Class<?>> ret = new ArrayList<Class<?>>();
        if (file.isDirectory()) {
            final File[] files = file.listFiles();
            if (null != files) {
                for (final File f : files) {
                    if (false == f.isHidden()) {
                        if (f.isDirectory()) {
                            ret.addAll(scan(f, concatenatedName + f.getName() + "."));
                        } else {
                            ret.addAll(scan(f, concatenatedName + f.getName()));
                        }
                    }
                }
            }
        } else {
            /*
             * its a file, could be a class file or a jar file, or neither
             */
            if (file.getName().endsWith(CLASS)) {
                final ClassNode classNode = new ClassNode();
                final ClassReader cr = new ClassReader(new FileInputStream(file.getAbsolutePath()));
                cr.accept(classNode, 0);
                if (annotated(classNode)) {
                    ret.add(Class.forName(classNode.name.replaceAll("/", ".")));
                    log.debug("adding " + classNode.name + " from " + file.getAbsolutePath());
                }
            } else if (file.getName().endsWith(JAR)) {
                ret.addAll(crackJar(file.getAbsolutePath()));
            }
        }
        /*
         * done
         */
        return ret;
    } catch (final Exception e) {
        throw new Exception("Exception in scan", e);
    }
}

From source file:com.licel.jcardsim.utils.JavaCardApiProcessor.java

License:Apache License

public static void proxyClass(File buildDir, String proxyClassFile, String targetClassFile,
        boolean skipConstructor) throws IOException {
    File proxyFile = new File(buildDir, proxyClassFile.replace(".", File.separator) + ".class");
    FileInputStream fProxyClass = new FileInputStream(proxyFile);
    FileInputStream fTargetClass = new FileInputStream(
            new File(buildDir, targetClassFile.replace(".", File.separator) + ".class"));
    ClassReader crProxy = new ClassReader(fProxyClass);
    ClassNode cnProxy = new ClassNode();
    crProxy.accept(cnProxy, 0);//w w  w . ja  va 2  s . c o m
    ClassReader crTarget = new ClassReader(fTargetClass);
    ClassNode cnTarget = new ClassNode();
    crTarget.accept(cnTarget, 0);

    ClassNode cnProxyRemapped = new ClassNode();
    HashMap<String, String> map = new HashMap();
    map.put(cnProxy.name, cnTarget.name);
    // inner classes
    for (int i = 0; i < 10; i++) {
        map.put(cnProxy.name + "$1", cnTarget.name + "$1");
    }
    RemappingClassAdapter ra = new RemappingClassAdapter(cnProxyRemapped, new SimpleRemapper(map));
    cnProxy.accept(ra);

    ClassWriter cw = new ClassWriter(crTarget, 0);
    MergeAdapter ma = new MergeAdapter(cw, cnProxyRemapped, skipConstructor);
    cnTarget.accept(ma);
    fProxyClass.close();
    fTargetClass.close();
    FileOutputStream fos = new FileOutputStream(
            new File(buildDir, targetClassFile.replace(".", File.separator) + ".class"));
    fos.write(cw.toByteArray());
    fos.close();
    // remove proxy class
    proxyFile.delete();
}

From source file:com.licel.jcardsim.utils.JavaCardApiProcessor.java

License:Apache License

public static void copyClass(File buildDir, String proxyClassFile, String targetClassName, Map map)
        throws IOException {
    File sourceFile = new File(buildDir, proxyClassFile.replace(".", File.separator) + ".class");
    FileInputStream fProxyClass = new FileInputStream(sourceFile);
    ClassReader crProxy = new ClassReader(fProxyClass);
    ClassNode cnProxy = new ClassNode();
    crProxy.accept(cnProxy, 0);//  w  w w .  jav  a  2 s .  c  o m

    ClassWriter cw = new ClassWriter(0);
    map.put(cnProxy.name, targetClassName.replace(".", "/"));
    RemappingClassAdapter ra = new RemappingClassAdapter(cw, new SimpleRemapper(map));
    cnProxy.accept(ra);

    fProxyClass.close();
    FileOutputStream fos = new FileOutputStream(
            new File(buildDir, targetClassName.replace(".", File.separator) + ".class"));
    fos.write(cw.toByteArray());
    fos.close();

    // remove source class
    sourceFile.delete();
}

From source file:com.licel.jcardsim.utils.JavaCardApiProcessor.java

License:Apache License

public static void proxyExceptionClass(File buildDir, String targetClassName) throws IOException {
    FileInputStream fTargetClass = new FileInputStream(
            new File(buildDir, targetClassName.replace(".", File.separator) + ".class"));
    ClassReader crTarget = new ClassReader(fTargetClass);
    ClassNode cnTarget = new ClassNode();
    crTarget.accept(cnTarget, 0);//ww  w  .j av a  2  s  . c o  m
    ClassWriter cw = new ClassWriter(0);
    ExceptionClassProxy ecc = new ExceptionClassProxy(cw, cnTarget.version, cnTarget.name, cnTarget.superName);
    cnTarget.accept(ecc);

    fTargetClass.close();
    FileOutputStream fos = new FileOutputStream(
            new File(buildDir, targetClassName.replace(".", File.separator) + ".class"));
    fos.write(cw.toByteArray());
    fos.close();

}

From source file:com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtilTest.java

License:Open Source License

private ClassNode _loadClass(InputStream is) {
    ClassReader classReader = null;/*from w  w w. j  a v a 2  s .  c  o m*/

    try {
        classReader = new ClassReader(is);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }

    ClassNode classNode = new ClassNode();

    classReader.accept(classNode, 0);

    return classNode;
}