Java Code Examples for org.objectweb.asm.commons.GeneratorAdapter#loadArgs()

The following examples show how to use org.objectweb.asm.commons.GeneratorAdapter#loadArgs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestCustomFunctions.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public Class<?> createFakeClass() {
  String className = TestCustomFunctions.class.getName() + "$Foo";
  ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
  classWriter.visit(Opcodes.V1_5, ACC_PUBLIC | ACC_SUPER | ACC_FINAL | ACC_SYNTHETIC,
      className.replace('.', '/'), null, Type.getInternalName(Object.class), null);
  
  org.objectweb.asm.commons.Method m = org.objectweb.asm.commons.Method.getMethod("void <init>()");
  GeneratorAdapter constructor = new GeneratorAdapter(ACC_PRIVATE | ACC_SYNTHETIC, m, null, null, classWriter);
  constructor.loadThis();
  constructor.loadArgs();
  constructor.invokeConstructor(Type.getType(Object.class), m);
  constructor.returnValue();
  constructor.endMethod();
  
  GeneratorAdapter gen = new GeneratorAdapter(ACC_STATIC | ACC_PUBLIC | ACC_SYNTHETIC,
      org.objectweb.asm.commons.Method.getMethod("double bar()"), null, null, classWriter);
  gen.push(2.0);
  gen.returnValue();
  gen.endMethod();      
  
  byte[] bc = classWriter.toByteArray();
  return defineClass(className, bc, 0, bc.length);
}
 
Example 2
Source File: AsmDeltaSpikeProxyClassGenerator.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 3
Source File: WeavingClassVisitor.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@RequiresNonNull("type")
private void overrideAndWeaveInheritedMethod(AnalyzedMethod inheritedMethod) {
    String superName = analyzedClass.superName();
    // superName is null only for java.lang.Object which doesn't inherit anything
    // so safe to assume superName not null here
    checkNotNull(superName);
    String[] exceptions = new String[inheritedMethod.exceptions().size()];
    for (int i = 0; i < inheritedMethod.exceptions().size(); i++) {
        exceptions[i] = ClassNames.toInternalName(inheritedMethod.exceptions().get(i));
    }
    List<Advice> advisors = removeSuperseded(inheritedMethod.advisors());
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, inheritedMethod.name(),
            inheritedMethod.getDesc(), inheritedMethod.signature(), exceptions);
    mv = visitMethodWithAdvice(mv, ACC_PUBLIC, inheritedMethod.name(),
            inheritedMethod.getDesc(), advisors);
    checkNotNull(mv);
    GeneratorAdapter mg = new GeneratorAdapter(mv, ACC_PUBLIC, inheritedMethod.name(),
            inheritedMethod.getDesc());
    mg.visitCode();
    mg.loadThis();
    mg.loadArgs();
    Type superType = Type.getObjectType(ClassNames.toInternalName(superName));
    // method is called invokeConstructor, but should really be called invokeSpecial
    Method method = new Method(inheritedMethod.name(), inheritedMethod.getDesc());
    mg.invokeConstructor(superType, method);
    mg.returnValue();
    mg.endMethod();
}
 
Example 4
Source File: PointcutClassVisitor.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void visitEnd() {
    if (onBeforeMethodVisitor != null) {
        int access = onBeforeMethodVisitor.access;
        String name = onBeforeMethodVisitor.name;
        String descriptor = "(Z" + onBeforeMethodVisitor.descriptor.substring(1);
        String signature = onBeforeMethodVisitor.signature;
        String[] exceptions = onBeforeMethodVisitor.exceptions;
        GeneratorAdapter mv = new GeneratorAdapter(
                cw.visitMethod(access, name, descriptor, signature, exceptions), access, name,
                descriptor);
        mv.visitCode();
        mv.visitVarInsn(ILOAD, 0);
        Label endWithDefaultLabel = new Label();
        mv.visitJumpInsn(IFEQ, endWithDefaultLabel);
        mv.loadArgs(1, mv.getArgumentTypes().length - 1);
        mv.visitMethodInsn(INVOKESTATIC, checkNotNull(className), name,
                onBeforeMethodVisitor.descriptor, false);
        mv.returnValue();

        mv.visitLabel(endWithDefaultLabel);
        mv.visitFrame(F_NEW, 0, new Object[0], 0, new Object[0]);
        if (mv.getReturnType().getSort() != Type.VOID) {
            // return value will be ignored when !enabled, but need to return something
            WeavingMethodVisitor.pushDefault(mv, mv.getReturnType());
        }
        mv.returnValue();
        mv.endMethod();
    }
    super.visitEnd();
}