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

The following examples show how to use jdk.internal.org.objectweb.asm.MethodVisitor#visitEnd() . 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: UnsupportedClassFileVersion.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void writeClassFile() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(99, ACC_PUBLIC + ACC_SUPER, "ClassFile", 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();
    cw.visitEnd();

    try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {
         fos.write(cw.toByteArray());
    }
}
 
Example 2
Source File: AddressVarHandleGenerator.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
void addConstructor(BinderClassWriter cw) {
    MethodType constrType = MethodType.methodType(void.class, VarForm.class, boolean.class, long.class, long.class, long.class, long[].class);
    MethodVisitor mv = cw.visitMethod(0, "<init>", constrType.toMethodDescriptorString(), null, null);
    mv.visitCode();
    //super call
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(VarForm.class));
    mv.visitVarInsn(ILOAD, 2);
    mv.visitVarInsn(LLOAD, 3);
    mv.visitVarInsn(LLOAD, 5);
    mv.visitVarInsn(LLOAD, 7);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(BASE_CLASS), "<init>",
            MethodType.methodType(void.class, VarForm.class, boolean.class, long.class, long.class, long.class).toMethodDescriptorString(), false);
    //init dimensions
    for (int i = 0 ; i < dimensions ; i++) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 9);
        mv.visitLdcInsn(i);
        mv.visitInsn(LALOAD);
        mv.visitFieldInsn(PUTFIELD, implClassName, "dim" + i, "J");
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 3
Source File: TestAMEnotNPE.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The bytes for D, a NOT abstract class extending abstract class C without
 * supplying an implementation for abstract method m. There is a default
 * method in the interface I, but it should lose to the abstract class.
 *
 * @return
 * @throws Exception
 */
public static byte[] bytesForD() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/D", null, "p/C", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p/C", "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 4
Source File: UnsupportedClassFileVersion.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void writeClassFile() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(99, ACC_PUBLIC + ACC_SUPER, "ClassFile", 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();
    cw.visitEnd();

    try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {
         fos.write(cw.toByteArray());
    }
}
 
Example 5
Source File: TestAMEnotNPE.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The bytes for D, a NOT abstract class extending abstract class C without
 * supplying an implementation for abstract method m. There is a default
 * method in the interface I, but it should lose to the abstract class.
 *
 * @return
 * @throws Exception
 */
public static byte[] bytesForD() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/D", null, "p/C", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p/C", "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 6
Source File: TestAMEnotNPE.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The bytes for D, a NOT abstract class extending abstract class C without
 * supplying an implementation for abstract method m. There is a default
 * method in the interface I, but it should lose to the abstract class.
 *
 * @return
 * @throws Exception
 */
public static byte[] bytesForD() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/D", null, "p/C", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p/C", "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
Example 7
Source File: UnloadClass.java    From LearningOfThinkInJava with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws NoSuchMethodException,SecurityException,
        IllegalAccessException,IllegalArgumentException,InvocationTargetException{
    ClassWriter cw=new ClassWriter(ClassWriter.COMPUTE_MAXS|ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_7,ACC_PUBLIC,"Example",null,"java/lang/Object",null);
    MethodVisitor mw=cw.visitMethod(ACC_PUBLIC,"<init>","()V",null,null);
    mw.visitVarInsn(ALOAD,0);
    mw.visitMethodInsn(INVOKESPECIAL,"java/lang/Object","<init>","()V");
    mw.visitInsn(RETURN);
    mw.visitMaxs(0,0);
    mw.visitEnd();
    mw=cw.visitMethod(ACC_PUBLIC+ACC_STATIC,"main","([Ljava/lang/String;)V",null,null);
    mw.visitFieldInsn(GETSTATIC,"java/lang/System","out","Ljava/io/PrintStream;");
    mw.visitLdcInsn("Hello world!");
    mw.visitMethodInsn(INVOKEVIRTUAL,"java/io/PrintStream","println","(Ljava/lang/String;)V");
    mw.visitInsn(RETURN);
    mw.visitMaxs(0,0);
    mw.visitEnd();
    byte[] code=cw.toByteArray();
    for(int i=0;i<1;i++){
        ClassLoader loader=UnloadClass.class.getClassLoader();
        Method m=ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
        m.setAccessible(true);
        m.invoke(loader,"Example",code,0,code.length);
        m.setAccessible(false);
        System.gc();
    }

}
 
Example 8
Source File: DefineClassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a class file with the given class name. The class implements Runnable
 * with a run method to invokestatic the given targetClass/targetMethod.
 */
byte[] generateRunner(String className,
                      String targetClass,
                      String targetMethod) throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
                                     + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_9,
            ACC_PUBLIC + ACC_SUPER,
            className.replace(".", "/"),
            null,
            "java/lang/Object",
            new String[] { "java/lang/Runnable" });

    // <init>
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    // run()
    String tc = targetClass.replace(".", "/");
    mv = cw.visitMethod(ACC_PUBLIC, "run", "()V", null, null);
    mv.visitMethodInsn(INVOKESTATIC, tc, targetMethod, "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 9
Source File: TestPrivateInterfaceMethodReflect.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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 File: TestPrivateInterfaceMethodReflect.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 11
Source File: TestPrivateInterfaceMethodReflect.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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 12
Source File: EventHandlerCreator.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void buildConstructor() {
    MethodVisitor mv = classWriter.visitMethod(Opcodes.ACC_PRIVATE, METHOD_EVENT_HANDLER_CONSTRUCTOR.getName(), makeConstructorDescriptor(settingInfos), null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0); // this
    mv.visitVarInsn(Opcodes.ILOAD, 1); // registered
    mv.visitVarInsn(Opcodes.ALOAD, 2); // event type
    mv.visitVarInsn(Opcodes.ALOAD, 3); // event control
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(EventHandler.class), METHOD_EVENT_HANDLER_CONSTRUCTOR.getName(), METHOD_EVENT_HANDLER_CONSTRUCTOR.getDescriptor(), false);
    for (SettingInfo si : settingInfos) {
        mv.visitVarInsn(Opcodes.ALOAD, 0); // this
        mv.visitVarInsn(Opcodes.ALOAD, si.index + 4); // Setting Control
        mv.visitFieldInsn(Opcodes.PUTFIELD, internalClassName, si.fieldName, TYPE_SETTING_CONTROL.getDescriptor());
    }
    // initialized string field writers
    int fieldIndex = 0;
    for (FieldInfo field : fields) {
        if (field.isString()) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(EventHandler.class), "createStringFieldWriter", "()" + TYPE_STRING_POOL.getDescriptor(), false);
            mv.visitFieldInsn(Opcodes.PUTFIELD, internalClassName, FIELD_PREFIX_STRING_POOL + fieldIndex, TYPE_STRING_POOL.getDescriptor());
        }
        fieldIndex++;
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 13
Source File: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate bytecode for SystemModules::targets method
 */
private void genTargetsMethod() {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC+ACC_STATIC,
                                      "targets",
                                      "()" + MODULE_TARGET_ARRAY_SIGNATURE,
                                      "()" + MODULE_TARGET_ARRAY_SIGNATURE,
                                      null);
    mv.visitCode();
    pushInt(mv, moduleInfos.size());
    mv.visitTypeInsn(ANEWARRAY, MODULE_TARGET_CLASSNAME);
    mv.visitVarInsn(ASTORE, MT_VAR);

    for (int index=0; index < moduleInfos.size(); index++) {
        ModuleInfo minfo = moduleInfos.get(index);
        if (minfo.target() != null && !minfo.dropModuleTarget) {
            mv.visitVarInsn(ALOAD, MT_VAR);
            pushInt(mv, index);

            // new ModuleTarget(String, String)
            mv.visitTypeInsn(NEW, MODULE_TARGET_CLASSNAME);
            mv.visitInsn(DUP);
            mv.visitLdcInsn(minfo.target().targetPlatform());
            mv.visitMethodInsn(INVOKESPECIAL, MODULE_TARGET_CLASSNAME,
                "<init>", "(Ljava/lang/String;)V", false);

            mv.visitInsn(AASTORE);
        }
    }

    mv.visitVarInsn(ALOAD, MT_VAR);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 14
Source File: EventHandlerProxyCreator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void buildDurationMethod() {
    MethodVisitor mv = classWriter.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, METHOD_DURATION.getName(), METHOD_DURATION.getDescriptor(), null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.LLOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(EventHandler.class), METHOD_DURATION.getName(), METHOD_DURATION.getDescriptor(), false);
    mv.visitInsn(Opcodes.LRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 15
Source File: BootstrapMethodErrorTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private byte[] loadClassData(String name) throws Exception {
  ClassWriter cw = new ClassWriter(0);
  MethodVisitor mv;

  if (name.equals("C")) {
    cw.visit(52, ACC_SUPER | ACC_PUBLIC, "C", null, "java/lang/Object", null);
    {
      mv = cw.visitMethod(ACC_PRIVATE | ACC_STATIC, "m", "()V", null, null);
      mv.visitCode();
      mv.visitInsn(RETURN);
      mv.visitMaxs(0, 1);
      mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
  } else if (name.equals("Exec")) {
    cw.visit(52, ACC_SUPER | ACC_PUBLIC, "Exec", null, "java/lang/Object", null);
    {
      mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invokeRef", "()V", null, null);
      mv.visitCode();
      Handle h = new Handle(H_INVOKESTATIC, "C", "m", "()V");
      mv.visitInvokeDynamicInsn("C", "()V", h);
      mv.visitInsn(RETURN);
      mv.visitMaxs(0, 0);
      mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
  }
  return null;
}
 
Example 16
Source File: EventHandlerCreator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void buildConstructor() {
    MethodVisitor mv = classWriter.visitMethod(Opcodes.ACC_PRIVATE, METHOD_EVENT_HANDLER_CONSTRUCTOR.getName(), makeConstructorDescriptor(settingInfos), null, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0); // this
    mv.visitVarInsn(Opcodes.ILOAD, 1); // registered
    mv.visitVarInsn(Opcodes.ALOAD, 2); // event type
    mv.visitVarInsn(Opcodes.ALOAD, 3); // event control
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(eventHandlerProxy), METHOD_EVENT_HANDLER_CONSTRUCTOR.getName(), METHOD_EVENT_HANDLER_CONSTRUCTOR.getDescriptor(), false);
    for (SettingInfo si : settingInfos) {
        mv.visitVarInsn(Opcodes.ALOAD, 0); // this
        mv.visitVarInsn(Opcodes.ALOAD, si.index + 4); // Setting Control
        mv.visitFieldInsn(Opcodes.PUTFIELD, internalClassName, si.fieldName, TYPE_SETTING_CONTROL.getDescriptor());
    }
    // initialized string field writers
    int fieldIndex = 0;
    for (FieldInfo field : fields) {
        if (field.isString()) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(eventHandlerProxy), "createStringFieldWriter", "()" + TYPE_STRING_POOL.getDescriptor(), false);
            mv.visitFieldInsn(Opcodes.PUTFIELD, internalClassName, FIELD_PREFIX_STRING_POOL + fieldIndex, TYPE_STRING_POOL.getDescriptor());
        }
        fieldIndex++;
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 17
Source File: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate bytecode for SystemModules::methodResoultions method
 */
private void genModuleResolutionsMethod() {
    MethodVisitor mresmv =
        cw.visitMethod(ACC_PUBLIC+ACC_STATIC,
                       "moduleResolutions",
                       "()" + MODULE_RESOLUTIONS_ARRAY_SIGNATURE,
                       "()" + MODULE_RESOLUTIONS_ARRAY_SIGNATURE,
                       null);
    mresmv.visitCode();
    pushInt(mresmv, moduleInfos.size());
    mresmv.visitTypeInsn(ANEWARRAY, MODULE_RESOLUTION_CLASSNAME);
    mresmv.visitVarInsn(ASTORE, 0);

    for (int index=0; index < moduleInfos.size(); index++) {
        ModuleInfo minfo = moduleInfos.get(index);
        if (minfo.moduleResolution() != null) {
            mresmv.visitVarInsn(ALOAD, 0);
            pushInt(mresmv, index);
            mresmv.visitTypeInsn(NEW, MODULE_RESOLUTION_CLASSNAME);
            mresmv.visitInsn(DUP);
            mresmv.visitLdcInsn(minfo.moduleResolution().value());
            mresmv.visitMethodInsn(INVOKESPECIAL,
                                   MODULE_RESOLUTION_CLASSNAME,
                                   "<init>",
                                   "(I)V", false);
            mresmv.visitInsn(AASTORE);
        }
    }
    mresmv.visitVarInsn(ALOAD, 0);
    mresmv.visitInsn(ARETURN);
    mresmv.visitMaxs(0, 0);
    mresmv.visitEnd();
}
 
Example 18
Source File: TestAMEnotNPE.java    From hottub 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 19
Source File: BadProvidersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test a service provider that defines more than one no-args
 * public static "provider" method.
 */
@Test(expectedExceptions = ServiceConfigurationError.class)
public void testWithTwoFactoryMethods() throws Exception {
    Path mods = compileTest(TEST1_MODULE);

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
                                     + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_9,
            ACC_PUBLIC + ACC_SUPER,
            "p/ProviderFactory",
            null,
            "java/lang/Object",
            null);

    // public static p.Service provider()
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC,
            "provider",
            "()Lp/Service;",
            null,
            null);
    mv.visitTypeInsn(NEW, "p/ProviderFactory$1");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL,
            "p/ProviderFactory$1",
            "<init>", "()V",
            false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    // public static p.ProviderFactory$1 provider()
    mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC,
            "provider",
            "()Lp/ProviderFactory$1;",
            null,
            null);
    mv.visitTypeInsn(NEW, "p/ProviderFactory$1");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL,
            "p/ProviderFactory$1",
            "<init>",
            "()V",
            false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    cw.visitEnd();

    // write the class bytes into the compiled module directory
    Path classFile = mods.resolve(TEST1_MODULE)
            .resolve("p")
            .resolve("ProviderFactory.class");
    Files.write(classFile, cw.toByteArray());

    // load providers and instantiate each one
    loadProviders(mods, TEST1_MODULE).forEach(Provider::get);
}
 
Example 20
Source File: FinalStatic.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private byte[] loadClassData(String name) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    switch (name) {
       case CLASS_NAME_A:
            cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME_A, 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");
                mv.visitInsn(RETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();

                mv = cw.visitMethod(ACC_FINAL | ACC_STATIC, "m", "()I", null, null);
                mv.visitCode();
                mv.visitLdcInsn(FAILED);
                mv.visitInsn(IRETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        case CLASS_NAME_B:
            cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME_B, null, CLASS_NAME_A, null);
            {
                mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
                mv.visitCode();
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESPECIAL, CLASS_NAME_A, "<init>", "()V");
                mv.visitInsn(RETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();

                mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
                mv.visitCode();
                mv.visitLdcInsn(EXPECTED);
                mv.visitInsn(IRETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();

            }
            break;
        default:
            break;
    }
    cw.visitEnd();

    return cw.toByteArray();
}