Search in sources :

Example 51 with ClassWriter

use of org.objectweb.asm.ClassWriter in project Lucee by lucee.

the class SourceLastModifiedClassAdapter method setSourceLastModified.

public static byte[] setSourceLastModified(byte[] barr, long lastModified) {
    ClassReader cr = new ClassReader(barr);
    ClassWriter cw = ASMUtil.getClassWriter();
    ClassVisitor ca = new SourceLastModifiedClassAdapter(cw, lastModified);
    cr.accept(ca, 0);
    return cw.toByteArray();
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter)

Example 52 with ClassWriter

use of org.objectweb.asm.ClassWriter in project SpongeCommon by SpongePowered.

the class ClassEventListenerFactory method generateClass.

private static byte[] generateClass(String name, Class<?> handle, Method method, Class<?> eventClass, Class<? extends EventFilter> filter) {
    name = name.replace('.', '/');
    final String handleName = Type.getInternalName(handle);
    final String handleDescriptor = Type.getDescriptor(handle);
    final String filterName = Type.getInternalName(filter);
    String eventDescriptor = "(";
    for (int i = 0; i < method.getParameterCount(); i++) {
        eventDescriptor += Type.getDescriptor(method.getParameterTypes()[i]);
    }
    eventDescriptor += ")V";
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    FieldVisitor fv;
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, BASE_HANDLER, null);
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "FILTER", "L" + filterName + ";", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, filterName);
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, filterName, "<init>", "()V", false);
        mv.visitFieldInsn(PUTSTATIC, name, "FILTER", "L" + filterName + ";");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", '(' + handleDescriptor + ")V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, BASE_HANDLER, "<init>", "(Ljava/lang/Object;)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "handle", HANDLE_METHOD_DESCRIPTOR, null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, name, "FILTER", "L" + filterName + ";");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(EventFilter.class), "filter", FILTER_DESCRIPTOR, true);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 2);
        Label l2 = new Label();
        mv.visitJumpInsn(IFNULL, l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "handle", "Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, handleName);
        for (int i = 0; i < method.getParameterCount(); i++) {
            mv.visitVarInsn(ALOAD, 2);
            mv.visitIntInsn(BIPUSH, i);
            mv.visitInsn(AALOAD);
            Type paramType = Type.getType(method.getParameterTypes()[i]);
            GeneratorUtils.visitUnboxingMethod(mv, paramType);
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, handleName, method.getName(), eventDescriptor, false);
        mv.visitLabel(l2);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Type(org.objectweb.asm.Type) Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 53 with ClassWriter

use of org.objectweb.asm.ClassWriter in project jacoco by jacoco.

the class BootstrapMethodReferenceTest method test.

@Test
public void test() throws Exception {
    final String className = "Example";
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null);
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "run", "()I", null, null);
    mv.visitCode();
    addCauseOfResizeInstructions(mv);
    final MethodType methodType = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class);
    final Handle handle = new Handle(Opcodes.H_INVOKESTATIC, this.getClass().getCanonicalName().replace('.', '/'), "bootstrap", methodType.toMethodDescriptorString(), false);
    mv.visitInvokeDynamicInsn("invoke", "()I", handle);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(1, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] original = cw.toByteArray();
    assertEquals(42, run(className, original));
    final byte[] instrumented = instrumenter.instrument(original, className);
    assertEquals(42, run(className, instrumented));
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandles(java.lang.invoke.MethodHandles) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Handle(org.objectweb.asm.Handle) Test(org.junit.Test)

Example 54 with ClassWriter

use of org.objectweb.asm.ClassWriter in project jacoco by jacoco.

the class ProbeArrayStrategyFactoryTest method testModule.

@Test
public void testModule() {
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V9, Opcodes.ACC_MODULE, "module-info", null, null, null);
    writer.visitModule("module", 0, null).visitEnd();
    writer.visitEnd();
    final IProbeArrayStrategy strategy = ProbeArrayStrategyFactory.createFor(0, new ClassReader(writer.toByteArray()), generator);
    assertEquals(NoneProbeArrayStrategy.class, strategy.getClass());
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter) Test(org.junit.Test)

Example 55 with ClassWriter

use of org.objectweb.asm.ClassWriter in project jacoco by jacoco.

the class ModifiedSystemClassRuntimeTest method createClass.

private static byte[] createClass(final int version) {
    final ClassWriter cw = new ClassWriter(0);
    cw.visit(version, 0, "Foo", null, "java/lang/Object", null);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

ClassWriter (org.objectweb.asm.ClassWriter)502 ClassReader (org.objectweb.asm.ClassReader)285 MethodVisitor (org.objectweb.asm.MethodVisitor)127 Test (org.junit.Test)99 ClassVisitor (org.objectweb.asm.ClassVisitor)88 ClassNode (org.objectweb.asm.tree.ClassNode)70 IOException (java.io.IOException)62 Label (org.objectweb.asm.Label)55 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)52 HashSet (java.util.HashSet)37 Method (java.lang.reflect.Method)36 Type (org.objectweb.asm.Type)33 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 File (java.io.File)30 InvocationTargetException (java.lang.reflect.InvocationTargetException)28 FieldVisitor (org.objectweb.asm.FieldVisitor)28 FileOutputStream (java.io.FileOutputStream)27 InputStream (java.io.InputStream)26 MethodNode (org.objectweb.asm.tree.MethodNode)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23