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:org.sonar.java.bytecode.loader.SquidClassLoaderTest.java

License:Open Source License

@Test
public void test_loading_java11_class() throws Exception {
    SquidClassLoader classLoader = new SquidClassLoader(
            Collections.singletonList(new File("src/test/files/bytecode/java11/bin")));
    byte[] bytes = classLoader.getBytesForClass("org.foo.A");
    assertThat(bytes).isNotNull();/*from  www  . ja  v  a 2s .  com*/
    ClassReader cr = new ClassReader(bytes);
    ClassNode classNode = new ClassNode();
    cr.accept(classNode, 0);
    assertThat(classNode.version).isEqualTo(Opcodes.V11);
    classLoader.close();
}

From source file:org.spongepowered.asm.mixin.transformer.TreeInfo.java

License:MIT License

/**
 * @param classBytes/*  w w w  .  j  av  a 2s .c  om*/
 * @param flags
 * @return ASM Tree view of the specified class 
 */
static ClassNode getClassNode(byte[] classBytes, int flags) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(classBytes);
    classReader.accept(classNode, flags);
    return classNode;
}

From source file:org.spongepowered.asm.transformers.TreeTransformer.java

License:MIT License

/**
 * @param basicClass Original bytecode/*from  w  ww .  j  a v  a  2 s .c  o m*/
 * @param cacheReader True to cache the classReader instance for use when
 *      writing the generated ClassNode later
 * @return tree
 */
protected final ClassNode readClass(byte[] basicClass, boolean cacheReader) {
    ClassReader classReader = new ClassReader(basicClass);
    if (cacheReader) {
        this.classReader = classReader;
    }

    ClassNode classNode = new ClassNode();
    classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
    return classNode;
}

From source file:org.spongepowered.clean.plugin.PluginScanner.java

License:MIT License

@SuppressWarnings("unchecked")
private PluginCandidate scanClassFile(InputStream in) throws IOException {
    ClassReader reader = new ClassReader(in);
    ClassNode cn = new ClassNode();

    reader.accept(cn, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    AnnotationNode plugin = null;//from   w w w.  j a  v  a2s.  c  o m
    boolean isAsync = false;
    if (cn.visibleTypeAnnotations != null) {
        for (TypeAnnotationNode an : (List<TypeAnnotationNode>) cn.visibleTypeAnnotations) {
            if ("Lorg/spongepowered/api/plugin/Plugin;".equals(an.desc)) {
                plugin = an;
            } else if ("Lorg/spongepowered/api/plugin/AsyncPlugin;".equals(an.desc)) {
                isAsync = true;
            }
        }
    }
    if (cn.visibleAnnotations != null) {
        for (AnnotationNode an : (List<AnnotationNode>) cn.visibleAnnotations) {
            if ("Lorg/spongepowered/api/plugin/Plugin;".equals(an.desc)) {
                plugin = an;
            } else if ("Lorg/spongepowered/api/plugin/AsyncPlugin;".equals(an.desc)) {
                isAsync = true;
            }
        }
    }
    if (plugin == null) {
        return null;
    }
    Map<String, Object> values = new HashMap<>();
    for (int i = 0; i < plugin.values.size(); i += 2) {
        String key = (String) plugin.values.get(i);
        values.put(key, plugin.values.get(i + 1));
    }
    String id = (String) values.get("id");
    if (!ID_PATTERN.matcher(id).matches()) {
        logger.error("Skipping plugin with invalid plugin ID '{}' from {}. " + ID_WARNING, id);
        return null;
    }
    if (!isAsync) {
        logger.warn(
                "Plugin {} does not support CleanSponge's async capabilities. Missing @AsyncPlugin annotation on main plugin class.",
                id);
        return null;
    }
    return new PluginCandidate(cn.name.replace('/', '.'), values);
}

From source file:org.spongepowered.despector.ast.io.DirectoryWalker.java

License:Open Source License

private void visit(File file, SourceSet src) throws IOException {
    if (file.isDirectory()) {
        for (File f : file.listFiles()) {
            visit(f, src);//from w  w w  .  j av a  2 s .co m
        }
    } else {
        if (file.getName().endsWith(".class")) {
            ClassReader reader = new ClassReader(new FileInputStream(file));
            ClassNode cn = new ClassNode();
            reader.accept(cn, 0);
            SingularClassLoader.instance.load(cn, src);
        }
    }
}

From source file:org.spongepowered.despector.ast.io.JarWalker.java

License:Open Source License

private void scanClassFile(JarInputStream input, SourceSet src) throws IOException {
    ClassReader reader = new ClassReader(input);
    ClassNode cn = new ClassNode();
    reader.accept(cn, 0);//from   ww w.ja  v a  2  s. c  o m
    for (String ex : EXCLUDES) {
        if (cn.name.startsWith(ex)) {
            return;
        }
    }
    SingularClassLoader.instance.load(cn, src);
}

From source file:org.spongepowered.despector.ast.io.SingularClassLoader.java

License:Open Source License

public TypeEntry load(Path file) throws IOException {
    ClassReader reader = new ClassReader(new FileInputStream(file.toFile()));
    ClassNode cn = new ClassNode();

    reader.accept(cn, 0);/* w w  w . j ava  2 s.  c o  m*/
    return load(cn, null);
}

From source file:org.spongepowered.despector.ast.io.SingularClassLoader.java

License:Open Source License

public TypeEntry load(Path file, SourceSet src) throws IOException {
    ClassReader reader = new ClassReader(new FileInputStream(file.toFile()));
    ClassNode cn = new ClassNode();

    reader.accept(cn, 0);//from   w w  w  . j  a v  a  2s.  com
    return load(cn, src);
}

From source file:org.spongepowered.granite.launch.transformers.AccessTransformer.java

License:MIT License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null || !this.modifiers.containsKey(transformedName)) {
        return bytes;
    }//  w ww  .  j a  va2s  . c o  m

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

    for (Modifier m : this.modifiers.get(transformedName)) {
        if (m.isClass) { // Class
            classNode.access = m.transform(classNode.access);
        } else if (m.desc == null) { // Field
            for (FieldNode fieldNode : classNode.fields) {
                if (m.wildcard || fieldNode.name.equals(m.name)) {
                    fieldNode.access = m.transform(fieldNode.access);
                    if (!m.wildcard) {
                        break;
                    }
                }
            }
        } else {
            List<MethodNode> overridable = null;

            for (MethodNode methodNode : classNode.methods) {
                if (m.wildcard || (methodNode.name.equals(m.name) && methodNode.desc.equals(m.desc))) {
                    boolean wasPrivate = (methodNode.access & ACC_PRIVATE) != 0;
                    methodNode.access = m.transform(methodNode.access);

                    // Constructors always use INVOKESPECIAL
                    // if we changed from private to something else we need to replace all INVOKESPECIAL calls to this method with INVOKEVIRTUAL
                    // so that overridden methods will be called. Only need to scan this class, because obviously the method was private.
                    if (!methodNode.name.equals("<init>") && wasPrivate
                            && (methodNode.access & ACC_PRIVATE) == 0) {
                        if (overridable == null) {
                            overridable = new ArrayList<>(3);
                        }

                        overridable.add(methodNode);
                    }

                    if (!m.wildcard) {
                        break;
                    }
                }
            }

            if (overridable != null) {
                for (MethodNode methodNode : classNode.methods) {
                    for (Iterator<AbstractInsnNode> itr = methodNode.instructions.iterator(); itr.hasNext();) {
                        AbstractInsnNode insn = itr.next();
                        if (insn.getOpcode() == INVOKESPECIAL) {
                            MethodInsnNode mInsn = (MethodInsnNode) insn;
                            for (MethodNode replace : overridable) {
                                if (replace.name.equals(mInsn.name) && replace.desc.equals(mInsn.desc)) {
                                    mInsn.setOpcode(INVOKEVIRTUAL);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

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

From source file:org.spongepowered.lantern.launch.AccessTransformer.java

License:MIT License

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null || !this.modifiers.containsKey(transformedName)) {
        return bytes;
    }// w  ww.j a v  a  2s. c  om

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

    for (Modifier m : this.modifiers.get(transformedName)) {
        if (m.isClass) { // Class
            classNode.access = m.transform(classNode.access);
        } else if (m.desc == null) { // Field
            for (FieldNode fieldNode : classNode.fields) {
                if (m.wildcard || fieldNode.name.equals(m.name)) {
                    fieldNode.access = m.transform(fieldNode.access);
                    if (!m.wildcard) {
                        break;
                    }
                }
            }
        } else {
            List<MethodNode> overridable = null;

            for (MethodNode methodNode : classNode.methods) {
                if (m.wildcard || (methodNode.name.equals(m.name) && methodNode.desc.equals(m.desc))) {
                    boolean wasPrivate = (methodNode.access & ACC_PRIVATE) != 0;
                    methodNode.access = m.transform(methodNode.access);

                    // Constructors always use INVOKESPECIAL
                    // if we changed from private to something else we need to replace all INVOKESPECIAL calls to this method with INVOKEVIRTUAL
                    // so that overridden methods will be called. Only need to scan this class, because obviously the method was private.
                    if (!methodNode.name.equals("<init>") && wasPrivate
                            && (methodNode.access & ACC_PRIVATE) == 0) {
                        if (overridable == null) {
                            overridable = Lists.newArrayListWithExpectedSize(3);
                        }

                        overridable.add(methodNode);
                    }

                    if (!m.wildcard) {
                        break;
                    }
                }
            }

            if (overridable != null) {
                for (MethodNode methodNode : classNode.methods) {
                    for (Iterator<AbstractInsnNode> itr = methodNode.instructions.iterator(); itr.hasNext();) {
                        AbstractInsnNode insn = itr.next();
                        if (insn.getOpcode() == INVOKESPECIAL) {
                            MethodInsnNode mInsn = (MethodInsnNode) insn;
                            for (MethodNode replace : overridable) {
                                if (replace.name.equals(mInsn.name) && replace.desc.equals(mInsn.desc)) {
                                    mInsn.setOpcode(INVOKEVIRTUAL);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

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