Search in sources :

Example 41 with Method

use of org.objectweb.asm.commons.Method in project Payara by payara.

the class EjbOptionalIntfGenerator method generateNonAccessibleMethod.

private static void generateNonAccessibleMethod(ClassVisitor cv, java.lang.reflect.Method m) {
    String methodName = m.getName();
    Type returnType = Type.getReturnType(m);
    Type[] argTypes = Type.getArgumentTypes(m);
    Method asmMethod = new Method(methodName, returnType, argTypes);
    // Only called for non-static Protected or Package access
    int access = ACC_PUBLIC;
    GeneratorAdapter mg = new GeneratorAdapter(access, asmMethod, null, getExceptionTypes(m), cv);
    mg.throwException(Type.getType(javax.ejb.EJBException.class), "Illegal non-business method access on no-interface view");
    mg.returnValue();
    mg.endMethod();
}
Also used : GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 42 with Method

use of org.objectweb.asm.commons.Method in project jacoco by jacoco.

the class RuntimeTestBase method generateAndInstantiateClass.

/**
 * Creates a new class with the given id, loads this class and instantiates
 * it. The constructor of the generated class will request the probe array
 * from the runtime under test.
 */
private ITarget generateAndInstantiateClass(int classid) throws InstantiationException, IllegalAccessException {
    final String className = "org/jacoco/test/targets/RuntimeTestTarget_" + classid;
    Type classType = Type.getObjectType(className);
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", new String[] { Type.getInternalName(ITarget.class) });
    writer.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC, null, null);
    // Constructor
    GeneratorAdapter gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "<init>", "()V");
    gen.visitCode();
    gen.loadThis();
    gen.invokeConstructor(Type.getType(Object.class), new Method("<init>", "()V"));
    gen.loadThis();
    final int size = runtime.generateDataAccessor(classid, className, 2, gen);
    gen.putStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(size + 1, 0);
    gen.visitEnd();
    // get()
    gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "get", "()[Z", null, new String[0]), Opcodes.ACC_PUBLIC, "get", "()[Z");
    gen.visitCode();
    gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(1, 0);
    gen.visitEnd();
    // a()
    gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "a", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "a", "()V");
    gen.visitCode();
    gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.push(0);
    gen.push(1);
    gen.arrayStore(Type.BOOLEAN_TYPE);
    gen.returnValue();
    gen.visitMaxs(3, 0);
    gen.visitEnd();
    // b()
    gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "b", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "b", "()V");
    gen.visitCode();
    gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.push(1);
    gen.push(1);
    gen.arrayStore(Type.BOOLEAN_TYPE);
    gen.returnValue();
    gen.visitMaxs(3, 0);
    gen.visitEnd();
    writer.visitEnd();
    final TargetLoader loader = new TargetLoader();
    return (ITarget) loader.add(className.replace('/', '.'), writer.toByteArray()).newInstance();
}
Also used : Type(org.objectweb.asm.Type) TargetLoader(org.jacoco.core.test.TargetLoader) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter)

Example 43 with Method

use of org.objectweb.asm.commons.Method in project jacoco by jacoco.

the class OfflineInstrumentationAccessGeneratorTest method generateAndInstantiateClass.

/**
 * Creates a new class with the given id, loads this class and instantiates
 * it. The constructor of the generated class will request the probe array
 * from the access generator under test.
 */
private ITarget generateAndInstantiateClass(int classid) throws InstantiationException, IllegalAccessException {
    final String className = "org/jacoco/test/targets/RuntimeTestTarget_" + classid;
    Type classType = Type.getObjectType(className);
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", new String[] { Type.getInternalName(ITarget.class) });
    writer.visitField(InstrSupport.DATAFIELD_ACC, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC, null, null);
    // Constructor
    GeneratorAdapter gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]), Opcodes.ACC_PUBLIC, "<init>", "()V");
    gen.visitCode();
    gen.loadThis();
    gen.invokeConstructor(Type.getType(Object.class), new Method("<init>", "()V"));
    gen.loadThis();
    final int size = generator.generateDataAccessor(classid, className, 2, gen);
    gen.putStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(size + 1, 0);
    gen.visitEnd();
    // get()
    gen = new GeneratorAdapter(writer.visitMethod(Opcodes.ACC_PUBLIC, "get", "()[Z", null, new String[0]), Opcodes.ACC_PUBLIC, "get", "()[Z");
    gen.visitCode();
    gen.getStatic(classType, InstrSupport.DATAFIELD_NAME, Type.getObjectType(InstrSupport.DATAFIELD_DESC));
    gen.returnValue();
    gen.visitMaxs(1, 0);
    gen.visitEnd();
    writer.visitEnd();
    final TargetLoader loader = new TargetLoader();
    return (ITarget) loader.add(className.replace('/', '.'), writer.toByteArray()).newInstance();
}
Also used : Type(org.objectweb.asm.Type) TargetLoader(org.jacoco.core.test.TargetLoader) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter)

Example 44 with Method

use of org.objectweb.asm.commons.Method in project MinecraftForge by MinecraftForge.

the class ASMTransformerWrapper method makeWrapper.

private static byte[] makeWrapper(String fileName) {
    if (!wrapperModMap.containsKey(fileName) || !wrapperParentMap.containsKey(fileName) || !fileName.endsWith(".class")) {
        throw new IllegalArgumentException("makeWrapper called with strange argument: " + fileName);
    }
    String name = fileName.substring(0, fileName.length() - ".class".length());
    try {
        Type wrapper = Type.getType(TransformerWrapper.class);
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, name, null, wrapper.getInternalName(), null);
        Method m = Method.getMethod("void <init> ()");
        GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, writer);
        mg.loadThis();
        mg.invokeConstructor(wrapper, m);
        mg.returnValue();
        mg.endMethod();
        m = Method.getMethod("java.lang.String getParentClass ()");
        mg = new GeneratorAdapter(Opcodes.ACC_PROTECTED, m, null, null, writer);
        mg.push(wrapperParentMap.get(fileName));
        mg.returnValue();
        mg.endMethod();
        m = Method.getMethod("java.lang.String getCoreMod ()");
        mg = new GeneratorAdapter(Opcodes.ACC_PROTECTED, m, null, null, writer);
        mg.push(wrapperModMap.get(fileName));
        mg.returnValue();
        mg.endMethod();
        writer.visitEnd();
        return writer.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Type(org.objectweb.asm.Type) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 45 with Method

use of org.objectweb.asm.commons.Method in project deltaspike by apache.

the class AsmProxyClassGenerator method defineSuperAccessorMethod.

private static void defineSuperAccessorMethod(ClassWriter cw, java.lang.reflect.Method method, Type superType, String superAccessorMethodSuffix) {
    Method originalAsmMethod = Method.getMethod(method);
    Method newAsmMethod = new Method(method.getName() + superAccessorMethodSuffix, originalAsmMethod.getReturnType(), originalAsmMethod.getArgumentTypes());
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, newAsmMethod, null, null, cw);
    mg.visitCode();
    // call super method
    mg.loadThis();
    mg.loadArgs();
    mg.visitMethodInsn(Opcodes.INVOKESPECIAL, superType.getInternalName(), method.getName(), Type.getMethodDescriptor(method), false);
    mg.returnValue();
    // finish the method
    mg.endMethod();
    mg.visitMaxs(10, 10);
    mg.visitEnd();
}
Also used : GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Aggregations

Method (org.objectweb.asm.commons.Method)78 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)58 Type (org.objectweb.asm.Type)38 ClassWriter (org.objectweb.asm.ClassWriter)14 Label (org.objectweb.asm.Label)14 MethodVisitor (org.objectweb.asm.MethodVisitor)9 IOException (java.io.IOException)7 ClassReader (org.objectweb.asm.ClassReader)7 TypeInfo (com.google.template.soy.jbcsrc.restricted.TypeInfo)6 MethodNode (org.objectweb.asm.tree.MethodNode)6 ExprString (lucee.transformer.expression.ExprString)5 LitString (lucee.transformer.expression.literal.LitString)5 ClassVisitor (org.objectweb.asm.ClassVisitor)5 Schema (co.cask.cdap.api.data.schema.Schema)4 ArrayList (java.util.ArrayList)4 File (java.io.File)3 Set (java.util.Set)3 Nullable (javax.annotation.Nullable)3 ExprDouble (lucee.transformer.expression.ExprDouble)3 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)2