Java Code Examples for jdk.internal.org.objectweb.asm.MethodVisitor

The following examples show how to use jdk.internal.org.objectweb.asm.MethodVisitor. These examples are extracted from open source projects. 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
public static byte[] dumpT2() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T2", null, "p1/T1", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T1", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "m", "()I", null, null);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 2
Source Project: nashorn   Source File: NullVisitor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public MethodVisitor visitMethod(
    final int access,
    final String name,
    final String desc,
    final String signature,
    final String[] exceptions) {
    return new MethodVisitor(Opcodes.ASM4) {
        @Override
        public AnnotationVisitor visitAnnotationDefault() {
            return new NullAnnotationVisitor();
        }

        @Override
        public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
            return new NullAnnotationVisitor();
        }
    };
}
 
Example 3
Source Project: openjdk-8   Source File: FrameNode.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Makes the given visitor visit this stack map frame.
 *
 * @param mv
 *            a method visitor.
 */
@Override
public void accept(final MethodVisitor mv) {
    switch (type) {
    case Opcodes.F_NEW:
    case Opcodes.F_FULL:
        mv.visitFrame(type, local.size(), asArray(local), stack.size(),
                asArray(stack));
        break;
    case Opcodes.F_APPEND:
        mv.visitFrame(type, local.size(), asArray(local), 0, null);
        break;
    case Opcodes.F_CHOP:
        mv.visitFrame(type, local.size(), null, 0, null);
        break;
    case Opcodes.F_SAME:
        mv.visitFrame(type, 0, null, 0, null);
        break;
    case Opcodes.F_SAME1:
        mv.visitFrame(type, 0, null, 1, asArray(stack));
        break;
    }
}
 
Example 4
Source Project: hottub   Source File: Type.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Type dup(final MethodVisitor method, final Type type, final int depth) {
    final boolean       cat2 = type.isCategory2();

    switch (depth) {
    case 0:
        method.visitInsn(cat2 ? DUP2 : DUP);
        break;
    case 1:
        method.visitInsn(cat2 ? DUP2_X1 : DUP_X1);
        break;
    case 2:
        method.visitInsn(cat2 ? DUP2_X2 : DUP_X2);
        break;
    default:
        return null; //invalid depth
    }

    return type;
}
 
Example 5
@Override
public void accept(final MethodVisitor mv) {
    int[] keys = new int[this.keys.size()];
    for (int i = 0; i < keys.length; ++i) {
        keys[i] = this.keys.get(i).intValue();
    }
    Label[] labels = new Label[this.labels.size()];
    for (int i = 0; i < labels.length; ++i) {
        labels[i] = this.labels.get(i).getLabel();
    }
    mv.visitLookupSwitchInsn(dflt.getLabel(), keys, labels);
    acceptAnnotations(mv);
}
 
Example 6
Source Project: openjdk-8   Source File: ClassGenerator.java    License: GNU General Public License v2.0 5 votes vote down vote up
static MethodGenerator makeConstructor(final ClassVisitor cv) {
    final int access = 0;
    final String name = INIT;
    final String desc = DEFAULT_INIT_DESC;
    final MethodVisitor mv = cv.visitMethod(access, name, desc, null, null);
    return new MethodGenerator(mv, access, name, desc);
}
 
Example 7
Source Project: TencentKona-8   Source File: ClassEmitter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add a new method to the class, representing a rest-of version of the
 * function node.
 *
 * @param functionNode the function node to generate a method for
 *
 * @return method emitter to use for weaving this method
 */
MethodEmitter restOfMethod(final FunctionNode functionNode) {
    methodCount++;
    methodNames.add(functionNode.getName());
    final MethodVisitor mv = cw.visitMethod(
        ACC_PUBLIC | ACC_STATIC,
        functionNode.getName(),
        Type.getMethodDescriptor(functionNode.getReturnType().getTypeClass(), RewriteException.class),
        null,
        null);

    return new MethodEmitter(this, mv, functionNode);
}
 
Example 8
Source Project: openjdk-jdk8u   Source File: IntType.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Type div(final MethodVisitor method, final int programPoint) {
    if (programPoint == INVALID_PROGRAM_POINT) {
        JSType.DIV_ZERO.invoke(method);
    } else {
        method.visitInvokeDynamicInsn("idiv", "(II)I", MATHBOOTSTRAP, programPoint);
    }
    return INT;
}
 
Example 9
private byte[] loadClassData(String name) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    switch (name) {
        case INTERFACE_NAME:
            cw.visit(V1_8, ACC_ABSTRACT | ACC_INTERFACE | ACC_PUBLIC, INTERFACE_NAME, null, "java/lang/Object", null);
            {
                mv = cw.visitMethod(ACC_PRIVATE, "privInstance", "()I", null, null);
                mv.visitCode();
                mv.visitLdcInsn(EXPECTED);
                mv.visitInsn(IRETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        case CLASS_NAME:
            cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME, null, "java/lang/Object", new String[]{INTERFACE_NAME});
            {
                mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
                mv.visitCode();
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
                mv.visitInsn(RETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        default:
            break;
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 10
Source Project: jdk8u-jdk   Source File: BoundMethodHandle.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static void emitPushFields(String types, String className, MethodVisitor mv) {
    for (int i = 0; i < types.length(); ++i) {
        char tc = types.charAt(i);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, className, makeFieldName(types, i), typeSig(tc));
    }
}
 
Example 11
Source Project: TencentKona-8   Source File: BooleanType.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Type add(final MethodVisitor method, final int programPoint) {
    // Adding booleans in JavaScript is perfectly valid, they add as if false=0 and true=1
    if(programPoint == INVALID_PROGRAM_POINT) {
        method.visitInsn(IADD);
    } else {
        method.visitInvokeDynamicInsn("iadd", "(II)I", MATHBOOTSTRAP, programPoint);
    }
    return INT;
}
 
Example 12
Source Project: jdk8u_jdk   Source File: BoundMethodHandle.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static void emitPushFields(String types, String className, MethodVisitor mv) {
    for (int i = 0; i < types.length(); ++i) {
        char tc = types.charAt(i);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, className, makeFieldName(types, i), typeSig(tc));
    }
}
 
Example 13
Source Project: openjdk-jdk9   Source File: ClassGenerator.java    License: GNU General Public License v2.0 5 votes vote down vote up
static MethodGenerator makeConstructor(final ClassVisitor cv) {
    final int access = 0;
    final String name = INIT;
    final String desc = DEFAULT_INIT_DESC;
    final MethodVisitor mv = cv.visitMethod(access, name, desc, null, null);
    return new MethodGenerator(mv, access, name, desc);
}
 
Example 14
Source Project: Bytecoder   Source File: InsnList.java    License: Apache License 2.0 5 votes vote down vote up
/**
  * Makes the given visitor visit all the instructions in this list.
  *
  * @param methodVisitor the method visitor that must visit the instructions.
  */
public void accept(final MethodVisitor methodVisitor) {
    AbstractInsnNode currentInsn = firstInsn;
    while (currentInsn != null) {
        currentInsn.accept(methodVisitor);
        currentInsn = currentInsn.nextInsn;
    }
}
 
Example 15
Source Project: hottub   Source File: IntType.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Type add(final MethodVisitor method, final int programPoint) {
    if(programPoint == INVALID_PROGRAM_POINT) {
        method.visitInsn(IADD);
    } else {
        method.visitInvokeDynamicInsn("iadd", "(II)I", MATHBOOTSTRAP, programPoint);
    }
    return INT;
}
 
Example 16
Source Project: jdk8u-jdk   Source File: InsnList.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Makes the given visitor visit all of the instructions in this list.
 *
 * @param mv
 *            the method visitor that must visit the instructions.
 */
public void accept(final MethodVisitor mv) {
    AbstractInsnNode insn = first;
    while (insn != null) {
        insn.accept(mv);
        insn = insn.next;
    }
}
 
Example 17
Source Project: Bytecoder   Source File: AddressVarHandleGenerator.java    License: Apache License 2.0 5 votes vote down vote up
void addCarrierAccessor(BinderClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_FINAL, "carrier", "()Ljava/lang/Class;", null, null);
    mv.visitCode();
    mv.visitLdcInsn(cw.makeConstantPoolPatch(carrier));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Class.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 18
Source Project: nashorn   Source File: LocalVariablesSorter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new {@link LocalVariablesSorter}.
 *
 * @param api the ASM API version implemented by this visitor. Must be one
 *        of {@link Opcodes#ASM4}.
 * @param access access flags of the adapted method.
 * @param desc the method's descriptor (see {@link Type Type}).
 * @param mv the method visitor to which this adapter delegates calls.
 */
protected LocalVariablesSorter(
    final int api,
    final int access,
    final String desc,
    final MethodVisitor mv)
{
    super(api, mv);
    Type[] args = Type.getArgumentTypes(desc);
    nextLocal = (Opcodes.ACC_STATIC & access) == 0 ? 1 : 0;
    for (int i = 0; i < args.length; i++) {
        nextLocal += args[i].getSize();
    }
    firstLocal = nextLocal;
}
 
Example 19
private static byte[] loadC(boolean redefine) {
    ClassWriter cw = new ClassWriter(0);

    cw.visit(52, ACC_SUPER | ACC_PUBLIC, "C", null, "java/lang/Object", null);
    {
        MethodVisitor mv;

        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "m", "()V", null, null);
        mv.visitCode();

        // First time we run we will:
        // 1) Cache resolution errors
        // 2) Redefine the class / method
        // 3) Try to read the resolution errors that were cached
        //
        // The redefined method will never run, throw error to be sure
        if (redefine) {
            createThrowRuntimeExceptionCode(mv, "The redefined method was called");
        } else {
            createMethodBody(mv);
        }
        mv.visitMaxs(3, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 20
Source Project: TencentKona-8   Source File: LongType.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Type add(final MethodVisitor method, final int programPoint) {
    if(programPoint == INVALID_PROGRAM_POINT) {
        method.visitInsn(LADD);
    } else {
        method.visitInvokeDynamicInsn("ladd", "(JJ)J", MATHBOOTSTRAP, programPoint);
    }
    return LONG;
}
 
Example 21
@Override
public void accept(final MethodVisitor mv) {
    int[] keys = new int[this.keys.size()];
    for (int i = 0; i < keys.length; ++i) {
        keys[i] = this.keys.get(i).intValue();
    }
    Label[] labels = new Label[this.labels.size()];
    for (int i = 0; i < labels.length; ++i) {
        labels[i] = this.labels.get(i).getLabel();
    }
    mv.visitLookupSwitchInsn(dflt.getLabel(), keys, labels);
    acceptAnnotations(mv);
}
 
Example 22
Source Project: hottub   Source File: ArrayType.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type load(final MethodVisitor method, final int slot) {
    assert slot != -1;
    method.visitVarInsn(ALOAD, slot);
    return this;
}
 
Example 23
Source Project: TencentKona-8   Source File: Type.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type ldc(final MethodVisitor method, final Object c) {
    throw new UnsupportedOperationException("ldc " + c);
}
 
Example 24
Source Project: jdk8u60   Source File: TryCatchBlockSorter.java    License: GNU General Public License v2.0 4 votes vote down vote up
public TryCatchBlockSorter(final MethodVisitor mv, final int access,
        final String name, final String desc, final String signature,
        final String[] exceptions) {
    this(Opcodes.ASM5, mv, access, name, desc, signature, exceptions);
}
 
Example 25
Source Project: openjdk-jdk9   Source File: MethodRemapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
protected MethodRemapper(final int api, final MethodVisitor mv,
        final Remapper remapper) {
    super(api, mv);
    this.remapper = remapper;
}
 
Example 26
Source Project: hottub   Source File: NumberType.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type loadUndefined(final MethodVisitor method) {
    method.visitLdcInsn(UNDEFINED_DOUBLE);
    return NUMBER;
}
 
Example 27
Source Project: TencentKona-8   Source File: Type.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type loadUndefined(final MethodVisitor method) {
    throw new UnsupportedOperationException("load undefined");
}
 
Example 28
Source Project: jdk8u60   Source File: Type.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type newarray(final MethodVisitor method) {
    method.visitIntInsn(NEWARRAY, T_DOUBLE);
    return this;
}
 
Example 29
Source Project: openjdk-jdk8u   Source File: IntType.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type loadUndefined(final MethodVisitor method) {
    method.visitLdcInsn(UNDEFINED_INT);
    return INT;
}
 
Example 30
@Override
public void _return(final MethodVisitor method) {
    method.visitInsn(LRETURN);
}