Java Code Examples for jdk.internal.org.objectweb.asm.ClassWriter#visit()

The following examples show how to use jdk.internal.org.objectweb.asm.ClassWriter#visit() . 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: GetModuleTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Exercise Class::getModule on VM anonymous classes
 */
@Test(dataProvider = "hostclasses")
public void testGetModuleOnVMAnonymousClass(Class<?> hostClass, String ignore) {

    // choose a class name in the same package as the host class
    String prefix = hostClass.getPackageName();
    if (prefix.length() > 0)
        prefix = prefix.replace('.', '/') + "/";
    String className = prefix + "Anon";

    // create the class
    String superName = "java/lang/Object";
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
                                     + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
             className, null, superName, null);
    byte[] classBytes = cw.toByteArray();
    int cpPoolSize = constantPoolSize(classBytes);
    Class<?> anonClass
        = U.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);

    assertTrue(anonClass.getModule() == hostClass.getModule());
}
 
Example 2
Source File: CallSiteDepContextTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static byte[] getClassFile(String suffix) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    cw.visit(52, ACC_PUBLIC | ACC_SUPER, CLASS_NAME + suffix, null, "java/lang/Object", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, METHOD_NAME, TYPE.toMethodDescriptorString(), null, null);
        mv.visitCode();
        Handle bsm = new Handle(H_INVOKESTATIC,
                CallSiteDepContextTest.class.getName().replace(".", "/"),
                "bootstrap",
                bsmMH.type().toMethodDescriptorString());
        mv.visitInvokeDynamicInsn("methodName", TYPE.toMethodDescriptorString(), bsm);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 3
Source File: UnsupportedClassFileVersion.java    From openjdk-jdk8u 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 4
Source File: TestAMEnotNPE.java    From TencentKona-8 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 5
Source File: LambdaAsm.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
static void verifyASM() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_8, ACC_PUBLIC, "X", null, "java/lang/Object", null);
    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "foo",
            "()V", null, null);
    mv.visitMaxs(2, 1);
    mv.visitMethodInsn(INVOKESTATIC,
            "java/util/function/Function.class",
            "identity", "()Ljava/util/function/Function;", true);
    mv.visitInsn(RETURN);
    cw.visitEnd();
    byte[] carray = cw.toByteArray();
    // for debugging
    // write((new File("X.class")).toPath(), carray, CREATE, TRUNCATE_EXISTING);

    // verify using javap/classfile reader
    ClassFile cf = ClassFile.read(new ByteArrayInputStream(carray));
    int mcount = checkMethod(cf, "foo");
    if (mcount < 1) {
        throw new RuntimeException("unexpected method count, expected 1" +
                "but got " + mcount);
    }
}
 
Example 6
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 7
Source File: LambdaStackTrace.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] generateMaker() {
    // interface Maker {
    //   Object make();
    // }
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "Maker", null, "java/lang/Object", null);
    cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
            "()Ljava/lang/Object;", null, null);
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 8
Source File: BootstrapMethodErrorTest.java    From openjdk-jdk8u 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 9
Source File: LambdaStackTrace.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] generateMaker() {
    // interface Maker {
    //   Object make();
    // }
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "Maker", null, "java/lang/Object", null);
    cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
            "()Ljava/lang/Object;", null, null);
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 10
Source File: TestMultiANewArray.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void writeClassFile(int cfv) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(cfv, 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();

    mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
    mv.visitCode();
    mv.visitInsn(ICONST_1);
    mv.visitInsn(ICONST_2);
    mv.visitMultiANewArrayInsn("[I", 2);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    cw.visitEnd();

    try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {
         fos.write(cw.toByteArray());
    }
}
 
Example 11
Source File: LambdaStackTrace.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] generateStringMaker() {
    // interface StringMaker extends Maker {
    //   String make();
    // }
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "StringMaker", null, "java/lang/Object", new String[]{"Maker"});
    cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
            "()Ljava/lang/String;", null, null);
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 12
Source File: RedefineRunningMethodsWithResolutionErrors.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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 13
Source File: TestMultiANewArray.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void writeClassFile(int cfv) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(cfv, 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();

    mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
    mv.visitCode();
    mv.visitInsn(ICONST_1);
    mv.visitInsn(ICONST_2);
    mv.visitMultiANewArrayInsn("[I", 2);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    cw.visitEnd();

    try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {
         fos.write(cw.toByteArray());
    }
}
 
Example 14
Source File: LambdaStackTrace.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] generateMaker() {
    // interface Maker {
    //   Object make();
    // }
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "Maker", null, "java/lang/Object", null);
    cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
            "()Ljava/lang/Object;", null, null);
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 15
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 16
Source File: TestOSRWithNonEmptyStack.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] generateTestClass() {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    cw.visit(TestOSRWithNonEmptyStack.CLASS_FILE_VERSION, ACC_PUBLIC,
            TestOSRWithNonEmptyStack.CLASS_NAME, null, "java/lang/Object",
            null);

    TestOSRWithNonEmptyStack.generateConstructor(cw);
    TestOSRWithNonEmptyStack.generateTestMethod(cw);

    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 17
Source File: LambdaStackTrace.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] generateMaker() {
    // interface Maker {
    //   Object make();
    // }
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "Maker", null, "java/lang/Object", null);
    cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
            "()Ljava/lang/Object;", null, null);
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 18
Source File: LambdaStackTrace.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] generateStringMaker() {
    // interface StringMaker extends Maker {
    //   String make();
    // }
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "StringMaker", null, "java/lang/Object", new String[]{"Maker"});
    cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
            "()Ljava/lang/String;", null, null);
    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 19
Source File: ClassConstruct.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Base constructor for building a Class or Interface
 * @param name Name of Class/Interface, including package name
 * @param extending Name of extending Class if any
 * @param access Access for Class/Interface
 * @param classFileVersion Class file version
 * @param interfaces Interface implemented
 */
public ClassConstruct(String name,
                      String extending,
                      int access,
                      int classFileVersion,
                      int index,
                      String... interfaces) {
    this.name = name;
    isInterface = (access & Opcodes.ACC_INTERFACE) == Opcodes.ACC_INTERFACE;
    cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(classFileVersion, access, name, null, extending, interfaces == null ?  new String[] { } : interfaces);
    this.index = index;
}
 
Example 20
Source File: LambdaStackTrace.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] generateMaker() {
    // interface Maker {
    //   Object make();
    // }
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_7, ACC_INTERFACE | ACC_ABSTRACT, "Maker", null, "java/lang/Object", null);
    cw.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "make",
            "()Ljava/lang/Object;", null, null);
    cw.visitEnd();
    return cw.toByteArray();
}