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(final int api) 

Source Link

Document

Constructs a new ClassNode .

Usage

From source file:com.dragome.methodlogger.serverside.MethodLoggerBytecodeTransformer.java

License:Apache License

public byte[] transform(String className, byte[] bytecode) {
    if (requiresTransformation(className)) {
        ClassNode classNode = new ClassNode(Opcodes.ASM5);
        MethodLoggerAdapter invokeDynamicConverter = new MethodLoggerAdapter(classNode);
        new ClassReader(bytecode).accept(invokeDynamicConverter, ClassReader.EXPAND_FRAMES);
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(cw);/*w  w  w. ja v  a 2  s . c o  m*/
        return cw.toByteArray();
    } else
        return bytecode;
}

From source file:com.facebook.buck.android.dalvik.firstorder.FirstOrderTest.java

License:Apache License

private static ClassNode loadClassNode(Class<?> input) {
    try {// w w w .j  a v  a2s  .  c  o  m
        ClassReader reader = new ClassReader(input.getName());
        ClassNode node = new ClassNode(Opcodes.ASM4);
        reader.accept(node, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        return node;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.facebook.buck.dalvik.firstorder.FirstOrderTest.java

License:Apache License

private static ClassNode loadClassNode(Class<?> input) {
    try {/*w  w  w .j  a v a2s . c o m*/
        ClassReader reader = new ClassReader(input.getName());
        ClassNode node = new ClassNode(Opcodes.ASM4);
        reader.accept(node, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        return node;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.facebook.buck.jvm.java.abi.DescriptorAndSignatureFactoryTestBase.java

License:Apache License

private ClassNode getClassNode(String classBinaryName) throws IOException {
    ClassNode classNode = new ClassNode(Opcodes.ASM5);
    testCompiler.getClasses().acceptClassVisitor(classBinaryName, 0, classNode);
    return classNode;
}

From source file:com.facebook.buck.jvm.java.abi.StubJarClassEntry.java

License:Apache License

@Nullable
public static StubJarClassEntry of(LibraryReader input, Path path, boolean sourceAbiCompatible)
        throws IOException {
    ClassNode stub = new ClassNode(Opcodes.ASM5);

    // As we read the class in, we create a partial stub that removes non-ABI methods and fields
    // but leaves the entire InnerClasses table. We record all classes that are referenced from
    // ABI methods and fields, and will use that information later to filter the InnerClasses table.
    ClassReferenceTracker referenceTracker = new ClassReferenceTracker(stub);
    ClassVisitor firstLevelFiltering = new AbiFilteringClassVisitor(referenceTracker);

    // If we want ABIs that are compatible with those generated from source, we add a visitor
    // at the very start of the chain which transforms the event stream coming out of `ClassNode`
    // to look like what ClassVisitorDriverFromElement would have produced.
    if (sourceAbiCompatible) {
        firstLevelFiltering = new SourceAbiCompatibleVisitor(firstLevelFiltering);
    }//  ww w.ja  v a 2  s  .  c  o m
    input.visitClass(path, firstLevelFiltering);

    // The synthetic package-info class is how package annotations are recorded; that one is
    // actually used by the compiler
    if (!isAnonymousOrLocalOrSyntheticClass(stub) || stub.name.endsWith("/package-info")) {
        return new StubJarClassEntry(path, stub, referenceTracker.getReferencedClassNames());
    }

    return null;
}

From source file:com.github.jonathanxd.codeapi.test.asm.LocalsReuseTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*www  .  ja  v a  2 s  .c o  m*/
public void test() {
    ClassDeclaration declaration = ClassDeclarationBuilder.builder().withModifiers(CodeModifier.PUBLIC)
            .withQualifiedName("codeapi.LocalsReuse")
            .withBody(CodeAPI.sourceOfParts(MethodDeclarationBuilder.builder()
                    .withModifiers(CodeModifier.PUBLIC).withName("test").withReturnType(Types.INT)
                    .withParameters(CodeAPI.parameter(Types.BOOLEAN, "bool"))
                    .withBody(CodeAPI.sourceOfParts(CodeAPI.ifStatement(
                            CodeAPI.checkTrue(CodeAPI.accessLocalVariable(Types.BOOLEAN, "bool")),
                            CodeAPI.sourceOfParts(VariableFactory.variable(Types.INT, "i", Literals.INT(10)),
                                    CodeAPI.returnValue(Types.INT,
                                            CodeAPI.accessLocalVariable(Types.INT, "i"))),
                            CodeAPI.sourceOfParts(VariableFactory.variable(Types.INT, "i", Literals.INT(17)),
                                    CodeAPI.returnValue(Types.INT,
                                            CodeAPI.accessLocalVariable(Types.INT, "i"))))))
                    .build()

            )).build();

    BytecodeGenerator bytecodeGenerator = new BytecodeGenerator();

    bytecodeGenerator.getOptions().set(BytecodeOptions.VISIT_LINES, VisitLineType.FOLLOW_CODE_SOURCE);

    BytecodeClass[] gen = bytecodeGenerator.gen(declaration);

    ResultSaver.save(this.getClass(), gen);

    ClassReader cr = new ClassReader(gen[0].getBytecode());

    ClassNode cn = new ClassNode(Opcodes.ASM5);
    cr.accept(cn, 0);

    MethodNode node = ((List<MethodNode>) cn.methods).stream()
            .filter(methodNode -> methodNode.name.equals("test")).findFirst()
            .orElseThrow(NullPointerException::new);

    Assert.assertEquals("Locals reuse", 3, node.maxLocals);
}

From source file:com.lodgon.parboiled.transform.AsmTestUtils.java

License:Apache License

public static void verifyIntegrity(String classInternalName, byte[] classCode) {
    checkArgNotNull(classCode, "classCode");
    ClassNode generatedClassNode = new ClassNode(Opcodes.ASM4);
    ClassReader classReader = new ClassReader(classCode);
    classReader.accept(generatedClassNode, 0);

    for (Object methodObj : generatedClassNode.methods) {
        verifyMethodIntegrity(classInternalName, (MethodNode) methodObj);
    }/*from  www  . jav a2 s .  c  om*/
}

From source file:com.navercorp.pinpoint.profiler.instrument.lambda.LambdaFactoryTest.java

License:Apache License

@Test
public void transformTest() throws IOException, NoSuchMethodException, IllegalAccessException,
        InstantiationException, InvocationTargetException {

    ClassLoader classLoader = this.getClass().getClassLoader();
    String name = JavaAssistUtils.javaClassNameToJvmResourceName(UnsafeClassMock.class.getName());
    InputStream resourceStream = ClassLoader.getSystemResourceAsStream(name);
    byte[] bytes = IOUtils.toByteArray(resourceStream);
    logger.info("dump-------");
    ByteCodeDumper.dumpByteCode(bytes);/*from  ww w. j  av a 2 s.c  om*/

    ClassReader reader = new ClassReader(bytes, 0, bytes.length);
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    String delegateClassName = "com/navercorp/pinpoint/profiler/instrument/lambda/mock/DefineAnonymousClassDelegator";
    String delegateMethod = "delegate";
    MethodInstReplacer replacer = new MethodInstReplacer(writer, "test",
            "com/navercorp/pinpoint/profiler/instrument/lambda/mock/UnsafeMock", "defineAnonymousClass",
            delegateClassName, delegateMethod);

    renameClass(reader, replacer);

    byte[] bytes1 = writer.toByteArray();
    ClassReader newReader1 = new ClassReader(bytes1);
    ClassNode node = new ClassNode(ASMVersion.VERSION);
    newReader1.accept(node, 0);

    logger.debug("dump-------");
    ByteCodeDumper.dumpByteCode(bytes1);

    Class<?> delegatorClazz = DefineClassUtils.defineClass(classLoader,
            JavaAssistUtils.jvmNameToJavaName(node.name), bytes1);
    logger.debug("class:{}", delegatorClazz);
    Object o = delegatorClazz.getDeclaredConstructor().newInstance();
    Method test = o.getClass().getMethod("test");
    Object invoke = test.invoke(o);

    Assert.assertEquals(DefineAnonymousClassDelegator.count, 1);

}

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);/*www.j  a v a  2s.co m*/
        return cw.toByteArray();
    }

    return bytes;
}

From source file:de.loskutov.bco.asm.DecompilerHelper.java

License:Open Source License

public static DecompiledClass getDecompiledClass(final InputStream is, DecompilerOptions options)
        throws IOException, UnsupportedClassVersionError {
    ClassReader cr = new ClassReader(is);
    ClassNode cn = new ClassNode(Opcodes.ASM5);
    int crFlags = 0;
    if (options.modes.get(BCOConstants.F_EXPAND_STACKMAP)) {
        crFlags |= ClassReader.EXPAND_FRAMES;
    }/*from  w  ww. j av  a2 s .  co  m*/
    cr.accept(cn, crFlags);
    ICommentedClassVisitor printer;
    if (options.modes.get(BCOConstants.F_SHOW_ASMIFIER_CODE)) {
        printer = new CommentedASMifierClassVisitor(cn, options);
    } else {
        printer = new CommentedClassVisitor(cn, options);
    }
    TraceClassVisitor dcv = new TraceClassVisitor(null, (Printer) printer, null);
    cn.accept(dcv);
    return getResult(printer, options, cn);
}