Java Code Examples for jdk.internal.org.objectweb.asm.MethodVisitor#visitInsn()

The following examples show how to use jdk.internal.org.objectweb.asm.MethodVisitor#visitInsn() . 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: ScriptClassInstrumentor.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void emitStaticInitializer() {
    final String className = scriptClassInfo.getJavaName();
    if (! staticInitFound) {
        // no user written <clinit> and so create one
        final MethodVisitor mv = ClassGenerator.makeStaticInitializer(this);
        mv.visitCode();
        mv.visitInsn(RETURN);
        mv.visitMaxs(Short.MAX_VALUE, 0);
        mv.visitEnd();
    }
    // Now generate $clinit$
    final MethodGenerator mi = ClassGenerator.makeStaticInitializer(this, $CLINIT$);
    ClassGenerator.emitStaticInitPrefix(mi, className, memberCount);
    if (memberCount > 0) {
        for (final MemberInfo memInfo : scriptClassInfo.getMembers()) {
            if (memInfo.isInstanceProperty() || memInfo.isInstanceFunction()) {
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo);
            } else if (memInfo.isInstanceGetter()) {
                final MemberInfo setter = scriptClassInfo.findSetter(memInfo);
                ClassGenerator.linkerAddGetterSetter(mi, className, memInfo, setter);
            }
        }
    }
    ClassGenerator.emitStaticInitSuffix(mi, className);
}
 
Example 2
Source File: ObjectType.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Type ldc(final MethodVisitor method, final Object c) {
    if (c == null) {
        method.visitInsn(ACONST_NULL);
    } else if (c instanceof Undefined) {
        return loadUndefined(method);
    } else if (c instanceof String) {
        method.visitLdcInsn(c);
        return STRING;
    } else if (c instanceof Handle) {
        method.visitLdcInsn(c);
        return Type.typeFor(MethodHandle.class);
    } else {
        throw new UnsupportedOperationException("implementation missing for class " + c.getClass() + " value=" + c);
    }

    return Type.OBJECT;
}
 
Example 3
Source File: TestANewArray.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static byte[] dumpClassFile(int cfv, int testDimension264, String arrayDim) throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;

    classCName = "classCName_" + cfv + "_" + testDimension264;

    cw.visit(cfv, ACC_PUBLIC + ACC_SUPER, classCName, null, "java/lang/Object", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {   // classCName main method
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        mv.visitIntInsn(BIPUSH, 1);
        mv.visitTypeInsn(ANEWARRAY, arrayDim); // Test ANEWARRAY bytecode with various dimensions
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 4
Source File: IntType.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Type neg(final MethodVisitor method, final int programPoint) {
    if(programPoint == INVALID_PROGRAM_POINT) {
        method.visitInsn(INEG);
    } else {
        method.visitInvokeDynamicInsn("ineg", "(I)I", MATHBOOTSTRAP, programPoint);
    }
    return INT;
}
 
Example 5
Source File: IntType.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Type mul(final MethodVisitor method, final int programPoint) {
    if(programPoint == INVALID_PROGRAM_POINT) {
        method.visitInsn(IMUL);
    } else {
        method.visitInvokeDynamicInsn("imul", "(II)I", MATHBOOTSTRAP, programPoint);
    }
    return INT;
}
 
Example 6
Source File: BootstrapMethodErrorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void defineIndyCallingClass(ClassWriter cw) {
    cw.visit(52, ACC_SUPER | ACC_PUBLIC, INDY_CALLER_CLASS_NAME, null, "java/lang/Object", null);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invoke", "()V", null, null);
    mv.visitCode();
    Handle h = new Handle(H_INVOKESTATIC,
                          BOOTSTRAP_METHOD_CLASS_NAME, BOOTSTRAP_METHOD_NAME,
                          BOOTSTRAP_METHOD_DESC, false);
    mv.visitInvokeDynamicInsn(BOOTSTRAP_METHOD_CLASS_NAME, "()V", h);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
}
 
Example 7
Source File: Type.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type aload(final MethodVisitor method) {
    method.visitInsn(IALOAD);
    return INT;
}
 
Example 8
Source File: NumberType.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type rem(final MethodVisitor method, final int programPoint) {
    method.visitInsn(DREM);
    return NUMBER;
}
 
Example 9
Source File: TestAMEnotNPE.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the bytes for a class with name d_name (presumably "D" in some
 * package), extending some class with name sub_what, implementing p.I,
 * and defining two methods m() and m(11args) with access method_acc.
 *
 * @param d_name      Name of class that is defined
 * @param sub_what    Name of class that it extends
 * @param method_acc  Accessibility of method(s) m in defined class.
 * @return
 * @throws Exception
 */
public static byte[] bytesForSomeDsubSomethingSomeAccess
        (String d_name, String sub_what, int method_acc)
        throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    String[] interfaces = {"p/I"};

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, d_name, null, sub_what, interfaces);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, sub_what, "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    // int m() {return 3;}
    {
        mv = cw.visitMethod(method_acc, "m", "()I", null, null);
        mv.visitCode();
        mv.visitLdcInsn(new Integer(3));
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    // int m(11args) {return 3;}
    {
        mv = cw.visitMethod(method_acc, "m", "(BCSIJ"
                + "Ljava/lang/Object;"
                + "Ljava/lang/Object;"
                + "Ljava/lang/Object;"
                + "Ljava/lang/Object;"
                + "Ljava/lang/Object;"
                + "Ljava/lang/Object;"
                + ")I", null, null);
        mv.visitCode();
        mv.visitLdcInsn(new Integer(3));
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 10
Source File: IntType.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type or(final MethodVisitor method) {
    method.visitInsn(IOR);
    return INT;
}
 
Example 11
Source File: Type.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void astore(final MethodVisitor method) {
    method.visitInsn(IASTORE);
}
 
Example 12
Source File: NumberType.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type neg(final MethodVisitor method) {
    method.visitInsn(DNEG);
    return NUMBER;
}
 
Example 13
Source File: BooleanType.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type loadForcedInitializer(final MethodVisitor method) {
    method.visitInsn(ICONST_0);
    return BOOLEAN;
}
 
Example 14
Source File: ProxyGenerator.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Generate the static initializer method for the proxy class.
 */
private void generateStaticInitializer() {

    MethodVisitor mv = visitMethod(Modifier.STATIC, NAME_CLINIT,
            "()V", null, null);
    mv.visitCode();
    Label L_startBlock = new Label();
    Label L_endBlock = new Label();
    Label L_NoMethodHandler = new Label();
    Label L_NoClassHandler = new Label();

    mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_NoMethodHandler,
            JL_NO_SUCH_METHOD_EX);
    mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_NoClassHandler,
            JL_CLASS_NOT_FOUND_EX);

    mv.visitLabel(L_startBlock);
    for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
        for (ProxyMethod pm : sigmethods) {
            pm.codeFieldInitialization(mv, className);
        }
    }
    mv.visitInsn(RETURN);
    mv.visitLabel(L_endBlock);
    // Generate exception handler

    mv.visitLabel(L_NoMethodHandler);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitTypeInsn(Opcodes.NEW, JL_NO_SUCH_METHOD_ERROR);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, JL_THROWABLE,
            "getMessage", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, JL_NO_SUCH_METHOD_ERROR,
            "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);

    mv.visitLabel(L_NoClassHandler);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitTypeInsn(Opcodes.NEW, JL_NO_CLASS_DEF_FOUND_ERROR);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, JL_THROWABLE,
            "getMessage", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, JL_NO_CLASS_DEF_FOUND_ERROR,
            "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);

    // Maxs computed by ClassWriter.COMPUTE_FRAMES, these arguments ignored
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
 
Example 15
Source File: IntType.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type div(final MethodVisitor method) {
    method.visitInsn(IDIV);
    return INT;
}
 
Example 16
Source File: LongType.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type sub(final MethodVisitor method) {
    method.visitInsn(LSUB);
    return LONG;
}
 
Example 17
Source File: IntType.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type sub(final MethodVisitor method) {
    method.visitInsn(ISUB);
    return INT;
}
 
Example 18
Source File: LongType.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Type mul(final MethodVisitor method) {
    method.visitInsn(LMUL);
    return LONG;
}
 
Example 19
Source File: Type.java    From nashorn with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Superclass logic for pop for all types
 *
 * @param method method emitter
 * @param type   type to pop
 */
protected static void pop(final MethodVisitor method, final Type type) {
    method.visitInsn(type.isCategory2() ? POP2 : POP);
}
 
Example 20
Source File: Type.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Superclass logic for pop for all types
 *
 * @param method method emitter
 * @param type   type to pop
 */
protected static void pop(final MethodVisitor method, final Type type) {
    method.visitInsn(type.isCategory2() ? POP2 : POP);
}