Java Code Examples for org.objectweb.asm.ClassWriter#visitInnerClass()

The following examples show how to use org.objectweb.asm.ClassWriter#visitInnerClass() . 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: InterfaceFieldClassGeneratorTest.java    From AVM with MIT License 6 votes vote down vote up
public static byte[] getNestedInterfaceCalledFIELDSLevelOne() {
    ClassWriter classWriter = new ClassWriter(0);
    FieldVisitor fieldVisitor;

    classWriter.visit(V10, ACC_ABSTRACT | ACC_INTERFACE, "NestedInterfaces$FIELDS", null, "java/lang/Object", null);

    classWriter.visitSource("NestedInterfaces.java", null);

    classWriter.visitInnerClass("NestedInterfaces$FIELDS", "NestedInterfaces", "FIELDS", ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

    classWriter.visitInnerClass("NestedInterfaces$FIELDS$FIELDS", "NestedInterfaces$FIELDS", "FIELDS", ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

    {
        fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC, "a", "I", null, Integer.valueOf(101));
        fieldVisitor.visitEnd();
    }
    classWriter.visitEnd();

    return classWriter.toByteArray();
}
 
Example 2
Source File: UnbalancedMonitorsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static byte[] generateClass() {

        ClassWriter cw = new ClassWriter(0);

        cw.visit(52, ACC_SUPER | ACC_PUBLIC, INNER_CLASS_NAME_INTERNAL, null, "java/lang/Object", null);

        cw.visitSource("UnbalancedMonitorsTest.java", null);

        cw.visitInnerClass(INNER_CLASS_NAME_INTERNAL, CLASS_NAME_INTERNAL, "UnbalancedMonitors", ACC_STATIC);

        visitConstructor(cw);
        visitWrongOrder(cw);
        visitBlockStructured(cw, true, false);
        visitBlockStructured(cw, true, true);
        visitBlockStructured(cw, false, false);
        visitBlockStructured(cw, false, true);
        cw.visitEnd();

        return cw.toByteArray();
    }
 
Example 3
Source File: ClassReaderTest.java    From turbine with Apache License 2.0 6 votes vote down vote up
@Test
public void innerClass() {
  ClassWriter cw = new ClassWriter(0);
  cw.visit(52, Opcodes.ACC_SUPER, "test/Hello$Inner", null, "java/lang/Object", null);
  cw.visitInnerClass(
      "test/Hello$Inner", "test/Hello", "Inner", Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE);
  cw.visitInnerClass("test/Hello$Inner$InnerMost", "test/Hello$Inner", "InnerMost", 0);
  byte[] bytes = cw.toByteArray();

  ClassFile classFile = com.google.turbine.bytecode.ClassReader.read(null, bytes);

  assertThat(classFile.innerClasses()).hasSize(2);

  ClassFile.InnerClass a = classFile.innerClasses().get(0);
  assertThat(a.access()).isEqualTo(TurbineFlag.ACC_STATIC | TurbineFlag.ACC_PRIVATE);
  assertThat(a.innerName()).isEqualTo("Inner");
  assertThat(a.innerClass()).isEqualTo("test/Hello$Inner");
  assertThat(a.outerClass()).isEqualTo("test/Hello");

  ClassFile.InnerClass b = classFile.innerClasses().get(1);
  assertThat(b.innerName()).isEqualTo("InnerMost");
  assertThat(b.innerClass()).isEqualTo("test/Hello$Inner$InnerMost");
  assertThat(b.outerClass()).isEqualTo("test/Hello$Inner");
}
 
Example 4
Source File: RClassGenerator.java    From bazel with Apache License 2.0 6 votes vote down vote up
private String writeInnerClassHeader(
    String fullyQualifiedOuterClass, String innerClass, ClassWriter innerClassWriter) {
  String fullyQualifiedInnerClass = fullyQualifiedOuterClass + "$" + innerClass;
  innerClassWriter.visit(
      JAVA_VERSION,
      Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER,
      fullyQualifiedInnerClass,
      null, /* signature */
      SUPER_CLASS,
      null /* interfaces */);
  innerClassWriter.visitSource(SdkConstants.FN_RESOURCE_CLASS, null);
  writeConstructor(innerClassWriter);
  innerClassWriter.visitInnerClass(
      fullyQualifiedInnerClass,
      fullyQualifiedOuterClass,
      innerClass,
      Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC);
  return fullyQualifiedInnerClass;
}
 
Example 5
Source File: InterfaceFieldClassGeneratorTest.java    From AVM with MIT License 5 votes vote down vote up
public static byte[] getInnerFILEDSInterfaceBytes() {
    ClassWriter classWriter = new ClassWriter(0);
    MethodVisitor methodVisitor;

    classWriter.visit(V10, ACC_PUBLIC | ACC_SUPER, "NestedInterfaces", null, "java/lang/Object", null);

    classWriter.visitSource("NestedInterfaces.java", null);

    classWriter.visitInnerClass("NestedInterfaces$FIELDS", "NestedInterfaces", "FIELDS", ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

    classWriter.visitInnerClass("NestedInterfaces$FIELDS$FIELDS", "NestedInterfaces$FIELDS", "FIELDS", ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

    {
        methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        methodVisitor.visitCode();
        Label label0 = new Label();
        methodVisitor.visitLabel(label0);
        methodVisitor.visitLineNumber(3, label0);
        methodVisitor.visitVarInsn(ALOAD, 0);
        methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        methodVisitor.visitInsn(RETURN);
        methodVisitor.visitMaxs(1, 1);
        methodVisitor.visitEnd();
    }
    classWriter.visitEnd();

    return classWriter.toByteArray();
}
 
Example 6
Source File: InterfaceFieldClassGeneratorTest.java    From AVM with MIT License 5 votes vote down vote up
public static byte[] getNestedInterfaceCalledFIELDSLevelTwo() {

        ClassWriter classWriter = new ClassWriter(0);
        FieldVisitor fieldVisitor;
        MethodVisitor methodVisitor;

        classWriter.visit(V10, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "NestedInterfaces$FIELDS$FIELDS", null, "java/lang/Object", null);

        classWriter.visitSource("NestedInterfaces.java", null);

        classWriter.visitInnerClass("NestedInterfaces$FIELDS", "NestedInterfaces", "FIELDS", ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

        classWriter.visitInnerClass("NestedInterfaces$FIELDS$FIELDS", "NestedInterfaces$FIELDS", "FIELDS", ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

        {
            fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC, "b", "I", null, null);
            fieldVisitor.visitEnd();
        }
        {
            methodVisitor = classWriter.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
            methodVisitor.visitCode();
            Label label0 = new Label();
            methodVisitor.visitLabel(label0);
            methodVisitor.visitLineNumber(7, label0);
            methodVisitor.visitTypeInsn(NEW, "java/lang/Object");
            methodVisitor.visitInsn(DUP);
            methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
            methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "hashCode", "()I", false);
            methodVisitor.visitFieldInsn(PUTSTATIC, "NestedInterfaces$FIELDS$FIELDS", "b", "I");
            methodVisitor.visitInsn(RETURN);
            methodVisitor.visitMaxs(2, 0);
            methodVisitor.visitEnd();
        }
        classWriter.visitEnd();

        return classWriter.toByteArray();
    }
 
Example 7
Source File: Bug62060793TestDataGenerator.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static byte[] createInterface() {
  ClassWriter cw = new ClassWriter(0);
  MethodVisitor mv;

  cw.visit(
      V1_8,
      ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE,
      INTERFACE_TYPE_NAME,
      null,
      "java/lang/Object",
      null);

  cw.visitInnerClass(
      INTERFACE_TYPE_NAME,
      CLASS_NAME,
      "Interface",
      ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

  {
    mv =
        cw.visitMethod(
            ACC_PUBLIC | ACC_ABSTRACT,
            "call",
            "(Ljava/lang/String;)Ljava/lang/String;",
            null,
            null);
    mv.visitParameter("input", 0);
    mv.visitEnd();
  }
  cw.visitEnd();

  return cw.toByteArray();
}
 
Example 8
Source File: ConstantPoolSubstitutionsTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static byte[] generateClass() {

        ClassWriter cw = new ClassWriter(0);
        MethodVisitor mv;

        cw.visit(52, ACC_SUPER, PACKAGE_NAME_INTERNAL + "/ConstantPoolTest", null, "java/lang/Object", null);
        cw.visitInnerClass(PACKAGE_NAME_INTERNAL + "/ConstantPoolTest", PACKAGE_NAME_INTERNAL + "/ConstantPoolSubstitutionsTests", "ConstantPoolTest",
                        ACC_STATIC);
        String constantPool = Java8OrEarlier ? "sun/reflect/ConstantPool" : "jdk/internal/reflect/ConstantPool";

        mv = cw.visitMethod(0, "<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, "getSize", "(Ljava/lang/Object;)I", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(CHECKCAST, constantPool);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getSize", "()I", false);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(1, 3);
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getIntAt", "(Ljava/lang/Object;)I", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(CHECKCAST, constantPool);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ICONST_0);
        mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getIntAt", "(I)I", false);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 3);
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getLongAt", "(Ljava/lang/Object;)J", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(CHECKCAST, constantPool);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ICONST_0);
        mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getLongAt", "(I)J", false);
        mv.visitInsn(LRETURN);
        mv.visitMaxs(2, 3);
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getFloatAt", "(Ljava/lang/Object;)F", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(CHECKCAST, constantPool);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ICONST_0);
        mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getFloatAt", "(I)F", false);
        mv.visitInsn(FRETURN);
        mv.visitMaxs(2, 3);
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getDoubleAt", "(Ljava/lang/Object;)D", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(CHECKCAST, constantPool);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ICONST_0);
        mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getDoubleAt", "(I)D", false);
        mv.visitInsn(DRETURN);
        mv.visitMaxs(2, 3);
        mv.visitEnd();

        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "getUTF8At", "(Ljava/lang/Object;)Ljava/lang/String;", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(CHECKCAST, constantPool);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ICONST_0);
        mv.visitMethodInsn(INVOKEVIRTUAL, constantPool, "getUTF8At", "(I)Ljava/lang/String;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 3);
        mv.visitEnd();
        cw.visitEnd();

        return cw.toByteArray();
    }
 
Example 9
Source File: ClientServiceFactory.java    From thorntail with Apache License 2.0 4 votes vote down vote up
static byte[] createImpl(String implName, ClassInfo classInfo) {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER,
             implName.replace('.', '/'),
             null,
             "java/lang/Object",
             new String[]{classInfo.name().toString().replace('.', '/')}
    );

    int lastDot = implName.lastIndexOf('.');
    String simpleName = implName.substring(lastDot + 1);

    cw.visitSource(simpleName + ".java", null);
    {
        av0 = cw.visitAnnotation("Ljavax/enterprise/context/ApplicationScoped;", true);
        av0.visitEnd();
    }
    cw.visitInnerClass("javax/ws/rs/client/Invocation$Builder", "javax/ws/rs/client/Invocation", "Builder", ACC_PUBLIC + ACC_STATIC + ACC_ABSTRACT + ACC_INTERFACE);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(14, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(15, l1);
        mv.visitInsn(RETURN);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this",
                              buildTypeDef(implName),
                              null,
                              l0,
                              l2,
                              0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }

    List<AnnotationInstance> annotations = classInfo.annotations().get(DotName.createSimple("org.wildfly.swarm.client.jaxrs.Service"));
    String baseUrl = (String) annotations.get(0).value("baseUrl").value();
    int lineNum = 18;

    classInfo.asClass().methods()
            .stream()
            .forEachOrdered(method -> {
                createMethod(cw, implName, classInfo.name().toString(), method, lineNum, baseUrl);
            });
    cw.visitEnd();

    return cw.toByteArray();
}