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

The following examples show how to use 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: DynamicEntityGenerator.java    From we-cmdb with Apache License 2.0 6 votes vote down vote up
private static void writeClass(String classDesc, String tabelName, String className, ClassWriter classWriter) {
    AnnotationVisitor annotationVisitor0;
    classWriter.visit(V1_8, ACC_PUBLIC | ACC_SUPER, classDesc, null, "java/lang/Object", new String[] { "java/io/Serializable" });

    annotationVisitor0 = classWriter.visitAnnotation("Ljavax/persistence/Entity;", true);
    annotationVisitor0.visitEnd();

    annotationVisitor0 = classWriter.visitAnnotation("Ljavax/persistence/Table;", true);
    annotationVisitor0.visit("name", tabelName);
    annotationVisitor0.visitEnd();

    annotationVisitor0 = classWriter.visitAnnotation("Ljavax/persistence/NamedQuery;", true);
    annotationVisitor0.visit("name", className + ".findAll");
    annotationVisitor0.visit("query", "SELECT a FROM " + className + " a");
    annotationVisitor0.visitEnd();
}
 
Example 2
Source File: TestCustomFunctions.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public Class<?> createFakeClass() {
  String className = TestCustomFunctions.class.getName() + "$Foo";
  ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
  classWriter.visit(Opcodes.V1_5, ACC_PUBLIC | ACC_SUPER | ACC_FINAL | ACC_SYNTHETIC,
      className.replace('.', '/'), null, Type.getInternalName(Object.class), null);
  
  org.objectweb.asm.commons.Method m = org.objectweb.asm.commons.Method.getMethod("void <init>()");
  GeneratorAdapter constructor = new GeneratorAdapter(ACC_PRIVATE | ACC_SYNTHETIC, m, null, null, classWriter);
  constructor.loadThis();
  constructor.loadArgs();
  constructor.invokeConstructor(Type.getType(Object.class), m);
  constructor.returnValue();
  constructor.endMethod();
  
  GeneratorAdapter gen = new GeneratorAdapter(ACC_STATIC | ACC_PUBLIC | ACC_SYNTHETIC,
      org.objectweb.asm.commons.Method.getMethod("double bar()"), null, null, classWriter);
  gen.push(2.0);
  gen.returnValue();
  gen.endMethod();      
  
  byte[] bc = classWriter.toByteArray();
  return defineClass(className, bc, 0, bc.length);
}
 
Example 3
Source File: WriteStyleablesProcessor.java    From shrinker with Apache License 2.0 6 votes vote down vote up
@Override
public void proceed() {
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    writer.visit(Opcodes.V1_6,
            Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_SUPER,
            RSymbols.R_STYLEABLES_CLASS_NAME,
            null,
            "java/lang/Object",
            null);
    for (String name : symbols.getStyleables().keySet()) {
        writer.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, name, "[I", null, null);
    }

    writeClinit(writer);
    writer.visitEnd();
    byte[] bytes = writer.toByteArray();
    try {
        if (!dir.isDirectory() && !dir.mkdirs()) {
            throw new RuntimeException("Cannot mkdir " + dir);
        }
        Files.write(dir.toPath().resolve(RSymbols.R_STYLEABLES_CLASS_NAME + ".class"), bytes);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 4
Source File: CreateTest.java    From AVM with MIT License 6 votes vote down vote up
private byte[] getByteCodeForClassWithoutSuperName() {
    ClassWriter classWriter = new ClassWriter(0);
    MethodVisitor methodVisitor;

    classWriter.visit(V10, ACC_PUBLIC | ACC_SUPER, "b/Main", null, null, null);

    classWriter.visitSource("Main.java", null);
    {
        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 5
Source File: AsmTest.java    From caravan with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
  ClassWriter cw = new ClassWriter(0);
  cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "HelloGen", null, "java/lang/Object", null);

  FieldVisitor fv = cw.visitField(1, "_key", Type.getDescriptor(String.class), null, null);
  fv.visitEnd();

  fv = cw.visitField(1, "_value", Type.getDescriptor(InnerClass.class), null, null);
  fv.visitEnd();

  Class<?> clazz = new DynamicClassLoader().defineClass("HelloGen", cw.toByteArray());
  for(Field f : clazz.getDeclaredFields()) {
    System.out.println(f.getName());
  }
}
 
Example 6
Source File: ClassRefInterfaceGennerator.java    From Concurnas with MIT License 6 votes vote down vote up
private static byte[] create(String name, LinkedHashSet<String> constructors, String superclass) throws Exception {

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

		cw.visit(V1_8, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, name, null, "java/lang/Object", superclass==null?null:new String[]{FuncType.classRefIfacePrefix  +superclass +  FuncType.classRefIfacePostfix });
		for(String desc: constructors){
			
			desc = desc.substring(0, desc.lastIndexOf("V")) + "Ljava/lang/Object;";
			
			mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "apply", desc, null, null);
			mv.visitEnd();
		}
		cw.visitEnd();

		return cw.toByteArray();
	}
 
Example 7
Source File: MyClassLoaderTest.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
/**
 * Create simple stub bytecode for a class with only a no-arg constructor.
 *
 * @param baseName      the base class name for the new class
 * @param superClass    the superclass from which the new class should extend
 * @return              the bytecode for a new class
 */
private static byte[] generateTestClassByteCode(ClassName baseName, Class<?> superClass) {
    final String tmpClassName = baseName.getSlashedTemplate();
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    String superClassName = superClass.getName().replace(".", "/");

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER + ACC_FINAL, tmpClassName, null, superClassName, null);
    cw.visitSource(baseName.getSourceFilename(), null);

    // default (no-arg) constructor:
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, superClassName, "<init>", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    cw.visitEnd();
    return cw.toByteArray();
}
 
Example 8
Source File: MainTest.java    From turbine with Apache License 2.0 6 votes vote down vote up
public static byte[] dump() {
  ClassWriter classWriter = new ClassWriter(0);
  classWriter.visit(
      Opcodes.V1_8,
      Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER,
      "g/Gen",
      null,
      "java/lang/Object",
      null);
  {
    MethodVisitor methodVisitor =
        classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    methodVisitor.visitCode();
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(
        Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(1, 1);
    methodVisitor.visitEnd();
  }
  classWriter.visitEnd();
  return classWriter.toByteArray();
}
 
Example 9
Source File: AdviceGenerator.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@RequiresNonNull("methodMetaInternalName")
private LazyDefinedClass generateMethodMetaClass(InstrumentationConfig config) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, methodMetaInternalName, null, "java/lang/Object",
            null);
    cw.visitField(ACC_PRIVATE + ACC_FINAL, "messageTemplate",
            "Lorg/glowroot/agent/bytecode/api/MessageTemplate;", null, null).visitEnd();
    if (!config.transactionNameTemplate().isEmpty()) {
        cw.visitField(ACC_PRIVATE + ACC_FINAL, "transactionNameTemplate",
                "Lorg/glowroot/agent/bytecode/api/MessageTemplate;", null, null).visitEnd();
    }
    if (!config.transactionUserTemplate().isEmpty()) {
        cw.visitField(ACC_PRIVATE + ACC_FINAL, "transactionUserTemplate",
                "Lorg/glowroot/agent/bytecode/api/MessageTemplate;", null, null).visitEnd();
    }
    for (int i = 0; i < config.transactionAttributeTemplates().size(); i++) {
        cw.visitField(ACC_PRIVATE + ACC_FINAL, "transactionAttributeTemplate" + i,
                "Lorg/glowroot/agent/bytecode/api/MessageTemplate;", null, null).visitEnd();
    }
    generateMethodMetaConstructor(cw);
    generateMethodMetaGetter(cw, "messageTemplate", "getMessageTemplate");
    if (!config.transactionNameTemplate().isEmpty()) {
        generateMethodMetaGetter(cw, "transactionNameTemplate", "getTransactionNameTemplate");
    }
    if (!config.transactionUserTemplate().isEmpty()) {
        generateMethodMetaGetter(cw, "transactionUserTemplate", "getTransactionUserTemplate");
    }
    for (int i = 0; i < config.transactionAttributeTemplates().size(); i++) {
        generateMethodMetaGetter(cw, "transactionAttributeTemplate" + i,
                "getTransactionAttributeTemplate" + i);
    }
    cw.visitEnd();
    return ImmutableLazyDefinedClass.builder()
            .type(Type.getObjectType(methodMetaInternalName))
            .bytes(cw.toByteArray())
            .build();
}
 
Example 10
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 11
Source File: ClassGenerator.java    From android-perftracking with MIT License 5 votes vote down vote up
/**
 * Creates AppPerformanceConfig.class file with a static boolean field AppPerformanceConfig#enabled in it.
 *
 * @param value Initializes the AppPerformanceConfig#enabled with value.
 * @return byte array of the class.
 */
public static byte[] generateConfigClass(boolean value) {

  ClassWriter cw = new ClassWriter(0);
  cw.visit(V1_6, ACC_FINAL + ACC_SUPER,
      "com/rakuten/tech/mobile/perf/runtime/internal/AppPerformanceConfig", null,
      "java/lang/Object", null);
  cw.visitField(ACC_PUBLIC + ACC_STATIC, "enabled", "Z", null, value);
  cw.visitEnd();
  return cw.toByteArray();
}
 
Example 12
Source File: AdviceGenerator.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private LazyDefinedClass generate() {
    LazyDefinedClass methodMetaClass = null;
    if (methodMetaInternalName != null) {
        methodMetaClass = generateMethodMetaClass(config);
    }
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    String[] interfaces = null;
    if (!config.enabledProperty().isEmpty() || !config.traceEntryEnabledProperty().isEmpty()) {
        interfaces = new String[] {"org/glowroot/agent/plugin/api/config/ConfigListener"};
    }
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, adviceInternalName, null, "java/lang/Object",
            interfaces);
    addClassAnnotation(cw);
    addStaticFields(cw);
    addStaticInitializer(cw);
    boolean checkNotInTransaction = config.isTransaction()
            && config.alreadyInTransactionBehavior() == AlreadyInTransactionBehavior.DO_NOTHING;
    boolean checkPropertyNotEnabled = pluginId != null && !config.enabledProperty().isEmpty();
    addIsEnabledMethodIfNeeded(cw, checkNotInTransaction, checkPropertyNotEnabled);
    if (config.isTraceEntryOrGreater()) {
        // methodMetaInternalName is non-null when entry or greater
        checkNotNull(methodMetaInternalName);
        addOnBeforeMethod(cw);
        addOnThrowMethod(cw);
        addOnReturnMethod(cw);
    } else if (config.captureKind() == CaptureKind.TIMER) {
        addOnBeforeMethodTimerOnly(cw);
        addOnAfterMethodTimerOnly(cw);
    } else {
        addOnBeforeMethodOther(cw);
    }
    cw.visitEnd();
    ImmutableLazyDefinedClass.Builder builder = ImmutableLazyDefinedClass.builder()
            .type(Type.getObjectType(adviceInternalName))
            .bytes(cw.toByteArray());
    if (methodMetaClass != null) {
        builder.addDependencies(methodMetaClass);
    }
    return builder.build();
}
 
Example 13
Source File: AbstractMethodInvoker.java    From spring-boot-netty with MIT License 5 votes vote down vote up
final <T extends InvokerBase> T buildInvoker(final Class<?> parent, final Method targetMethod, final Method invokerMethod,
                                             final boolean sendResult, final MethodPrefixAssembler prefixAssembler) {

    final String packageName = parent.getPackage().getName();
    final String invokerClassName = packageName + ".DynamicInvoker$$GeneratedClass$$" + COUNTER.getAndIncrement();

    final String invokerInternalName = invokerClassName.replace('.', '/');
    final String parentInternalName = Type.getInternalName(parent);

    final ClassWriter cw = new ClassWriter(COMPUTE_MAXS);
    cw.visit(V1_8, ACC_SUPER | ACC_PUBLIC, invokerInternalName, null, parentInternalName, EMPTY_INTERFACES);

    final Type invokerType = Type.getObjectType(invokerInternalName);
    final String invokerDescriptor = invokerType.getDescriptor();

    assembleConstructor(invokerType, parentInternalName, cw);
    assembleMethodHandleField(cw, targetMethod, invokerInternalName);
    assembleInvokerMethod(cw, invokerDescriptor, invokerInternalName, invokerMethod, targetMethod, sendResult, prefixAssembler);

    cw.visitEnd();

    final byte[] bytecode = cw.toByteArray();
    final Class<T> cl = CLASS_LOADER.load(bytecode, invokerClassName);

    try {
        return cl.getDeclaredConstructor().newInstance();
    } catch (final IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 14
Source File: AsmBackedEmptyScriptGenerator.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private <T extends Script> Class<? extends T> generateEmptyScriptClass(Class<T> type) {
    ClassWriter visitor = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    String typeName = type.getName() + "_Decorated";
    Type generatedType = Type.getType("L" + typeName.replaceAll("\\.", "/") + ";");
    Type superclassType = Type.getType(type);
    visitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, generatedType.getInternalName(), null,
            superclassType.getInternalName(), new String[0]);

    // Constructor

    String constructorDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, new Type[0]);
    MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", constructorDescriptor, null,
            new String[0]);
    methodVisitor.visitCode();

    // super()
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>",
            constructorDescriptor);

    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    // run() method

    String runDesciptor = Type.getMethodDescriptor(Type.getType(Object.class), new Type[0]);
    methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "run", runDesciptor, null, new String[0]);
    methodVisitor.visitCode();

    // return null
    methodVisitor.visitInsn(Opcodes.ACONST_NULL);

    methodVisitor.visitInsn(Opcodes.ARETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();

    visitor.visitEnd();

    byte[] bytecode = visitor.toByteArray();
    JavaMethod<ClassLoader, Class> method = JavaReflectionUtil.method(ClassLoader.class, Class.class, "defineClass", String.class, byte[].class, int.class, int.class);
    @SuppressWarnings("unchecked")
    Class<T> clazz = method.invoke(type.getClassLoader(), typeName, bytecode, 0, bytecode.length);
    return clazz;
}
 
Example 15
Source File: LowerTest.java    From turbine with Apache License 2.0 4 votes vote down vote up
@Test
public void invalidConstants() throws Exception {
  Path lib = temporaryFolder.newFile("lib.jar").toPath();
  try (OutputStream os = Files.newOutputStream(lib);
      JarOutputStream jos = new JarOutputStream(os)) {
    jos.putNextEntry(new JarEntry("Lib.class"));

    ClassWriter cw = new ClassWriter(0);
    cw.visit(52, Opcodes.ACC_SUPER, "Lib", null, "java/lang/Object", null);
    cw.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, "ZCONST", "Z", null, Integer.MAX_VALUE);
    cw.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, "SCONST", "S", null, Integer.MAX_VALUE);
    jos.write(cw.toByteArray());
  }

  ImmutableMap<String, String> input =
      ImmutableMap.of(
          "Test.java",
          Joiner.on('\n')
              .join(
                  "class Test {",
                  "  static final short SCONST = Lib.SCONST + 0;",
                  "  static final boolean ZCONST = Lib.ZCONST || false;",
                  "}"));

  Map<String, byte[]> actual = IntegrationTestSupport.runTurbine(input, ImmutableList.of(lib));

  Map<String, Object> values = new LinkedHashMap<>();
  new ClassReader(actual.get("Test"))
      .accept(
          new ClassVisitor(Opcodes.ASM7) {
            @Override
            public FieldVisitor visitField(
                int access, String name, String desc, String signature, Object value) {
              values.put(name, value);
              return super.visitField(access, name, desc, signature, value);
            }
          },
          0);

  assertThat(values).containsEntry("SCONST", -1);
  assertThat(values).containsEntry("ZCONST", 1);
}
 
Example 16
Source File: ComplexIndyGenerator.java    From bumblebench with Apache License 2.0 4 votes vote down vote up
public static byte[] makeExample() throws Throwable {
	      ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
	      cw.visit(V1_7, ACC_PUBLIC | ACC_SUPER, "ComplexIndy", null, "java/lang/Object", null);
	      		      
	      MethodVisitor mv;
	      {
	         mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "gwtTest", "(Ljava/lang/Object;)Ljava/lang/String;", null, null);
	         mv.visitCode();
		 mv.visitVarInsn(ALOAD, 0);
	         mv.visitInvokeDynamicInsn("gwtBootstrap", "(Ljava/lang/Object;)Ljava/lang/String;",
	               new Handle(
	            		   H_INVOKESTATIC, 
	            		   "BootstrapMethods", 
	            		   "fibBootstrap",
	            		   Type.getType(
	            				 "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"
	            		   ).getDescriptor())
	         );
	         mv.visitInsn(ARETURN);
	         mv.visitMaxs(0, 0);
	         mv.visitEnd();
	      }

	{
		mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "fibIndy", "(I)I", null, null);
		mv.visitCode();
		mv.visitVarInsn(ILOAD, 0);
		mv.visitInvokeDynamicInsn("fibBootstrap", "(I)I",
			new Handle(
				H_INVOKESTATIC, 
				"BootstrapMethods", 
				"fibBootstrap",
				Type.getType(
					"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"
				).getDescriptor())
			);
		mv.visitInsn(IRETURN);
		mv.visitMaxs(0, 0);
		mv.visitEnd();
	}
cw.visitEnd();
return cw.toByteArray();
}
 
Example 17
Source File: EventSubscriber.java    From Hyperium with GNU Lesser General Public License v3.0 4 votes vote down vote up
private Class<?> createHandler(Method callback) {
    // ClassName$methodName_EventClass_XXX
    String name = objName + "$" + callback.getName() + "_" + callback.getParameters()[0].getType().getSimpleName() + "_" + (ID++);
    String eventType = Type.getInternalName(callback.getParameterTypes()[0]);

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;
    String desc = name.replace(".", "/");
    String instanceClassName = instance.getClass().getName().replace(".", "/");

    cw.visit(V1_6, ACC_PUBLIC | ACC_SUPER, desc, null, "java/lang/Object", new String[]{ "cc/hyperium/event/EventSubscriber$EventHandler" });

    cw.visitSource(".dynamic", null);
    {
        cw.visitField(ACC_PUBLIC, "instance", "Ljava/lang/Object;", null, null).visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(PUTFIELD, desc, "instance", "Ljava/lang/Object;");
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "handle", "(Ljava/lang/Object;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, desc, "instance", "Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, instanceClassName);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitTypeInsn(CHECKCAST, eventType);
        mv.visitMethodInsn(INVOKEVIRTUAL, instanceClassName, callback.getName(), Type.getMethodDescriptor(callback), false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();

    byte[] handlerClassBytes = cw.toByteArray();
    return LOADER.define(name, handlerClassBytes);
}
 
Example 18
Source File: ASMGen.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static byte[] dump() throws Exception {

		ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
		FieldVisitor fv;
		MethodVisitor mv;
		AnnotationVisitor av0;

		cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER,
				"com/beetl/performance/lab/asm/UserAsmAccessor1", null,
				"java/lang/Object",
				new String[] { "com/beetl/performance/lab/asm/Access" });

		{
			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_PUBLIC,
					"get",
					"(Ljava/lang/Object;I)Ljava/lang/Object;",
					null,
					new String[] { "com/beetl/performance/lab/asm/ASMCastException" });
			mv.visitCode();
			Label l0 = new Label();
			Label l1 = new Label();
			Label l2 = new Label();
			mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
			mv.visitInsn(ACONST_NULL);
			mv.visitVarInsn(ASTORE, 3);
			mv.visitLabel(l0);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitTypeInsn(CHECKCAST, "com/beetl/performance/lab/User");
			mv.visitVarInsn(ASTORE, 3);
			mv.visitLabel(l1);
			Label l3 = new Label();
			mv.visitJumpInsn(GOTO, l3);
			mv.visitLabel(l2);
			mv.visitFrame(Opcodes.F_FULL, 4, new Object[] {
					"com/beetl/performance/lab/asm/UserAsmAccessor",
					"java/lang/Object", Opcodes.INTEGER,
					"com/beetl/performance/lab/User" }, 1,
					new Object[] { "java/lang/Exception" });
			mv.visitVarInsn(ASTORE, 4);
			mv.visitTypeInsn(NEW,
					"com/beetl/performance/lab/asm/ASMCastException");
			mv.visitInsn(DUP);
			mv.visitMethodInsn(INVOKESPECIAL,
					"com/beetl/performance/lab/asm/ASMCastException", "<init>",
					"()V");
			mv.visitInsn(ATHROW);
			mv.visitLabel(l3);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ILOAD, 2);
			Label l4 = new Label();
			Label l5 = new Label();
			Label l6 = new Label();
			mv.visitTableSwitchInsn(1, 2, l6, new Label[] { l4, l5 });
			mv.visitLabel(l4);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 3);
			mv.visitMethodInsn(INVOKEVIRTUAL, "com/beetl/performance/lab/User",
					"getName", "()Ljava/lang/String;");
			mv.visitInsn(ARETURN);
			mv.visitLabel(l5);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 3);
			mv.visitMethodInsn(INVOKEVIRTUAL, "com/beetl/performance/lab/User",
					"getId", "()I");
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf",
					"(I)Ljava/lang/Integer;");
			mv.visitInsn(ARETURN);
			mv.visitLabel(l6);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
			mv.visitInsn(DUP);
			mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException",
					"<init>", "()V");
			mv.visitInsn(ATHROW);
			mv.visitMaxs(2, 5);
			mv.visitEnd();
		}
		cw.visitEnd();

		return cw.toByteArray();
	}
 
Example 19
Source File: ProxyChannelFactory.java    From JCTools with Apache License 2.0 4 votes vote down vote up
/**
 * Create a proxy channel using a user supplied back end.
 * 
 * @param capacity
 *            The minimum capacity for unprocessed invocations the channel
 *            should support
 * @param iFace
 *            Interface the proxy must implement
 * @param waitStrategy
 *            A wait strategy to be invoked when the backing data structure
 *            is full
 * @param backendType
 *            The back end type, the proxy will inherit from this channel
 *            type. The back end type must define a constructor with signature:
 *            <code>(int capacity, int primitiveMessageSize, int referenceMessageSize)</code>
 * @return A proxy channel instance
 */
public static <E> ProxyChannel<E> createProxy(int capacity, 
        Class<E> iFace, 
        WaitStrategy waitStrategy,
        Class<? extends ProxyChannelRingBuffer> backendType) {
    if (!iFace.isInterface()) {
        throw new IllegalArgumentException("Not an interface: " + iFace);
    }
    
    

    String generatedName = Type.getInternalName(iFace) + "$JCTools$ProxyChannel$" + backendType.getSimpleName();
    Class<?> preExisting = findExisting(generatedName, iFace);
    if (preExisting != null) {
        return instantiate(preExisting, capacity, waitStrategy);
    }

    List<Method> relevantMethods = findRelevantMethods(iFace);
    if (relevantMethods.isEmpty()) {
        throw new IllegalArgumentException("Does not declare any abstract methods: " + iFace);
    }

    // max number of reference arguments of any method
    int referenceMessageSize = 0;
    // max bytes required to store the primitive args of a call frame of any method
    int primitiveMessageSize = 0;
    
    for (Method method : relevantMethods) {
        int primitiveMethodSize = 0;
        int referenceCount = 0;
        for (Class<?> parameterType : method.getParameterTypes()) {
            if (parameterType.isPrimitive()) {
                primitiveMethodSize += primitiveMemorySize(parameterType);
            } else {
                referenceCount++;
            }
        }
        primitiveMessageSize = Math.max(primitiveMessageSize, primitiveMethodSize);
        referenceMessageSize = Math.max(referenceMessageSize, referenceCount);
    }
    
    // We need to add an int to this for the 'type' value on the message frame
    primitiveMessageSize += primitiveMemorySize(int.class);
    
    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);

    classWriter.visit(Opcodes.V1_4,
            Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL,
            generatedName,
            null,
            Type.getInternalName(backendType),
            new String[]{Type.getInternalName(ProxyChannel.class), Type.getInternalName(iFace)});
    implementInstanceFields(classWriter);
    implementConstructor(classWriter, backendType, generatedName, primitiveMessageSize, referenceMessageSize);
    implementProxyInstance(classWriter, iFace, generatedName);
    implementProxy(classWriter, iFace, generatedName);

    implementUserMethods(classWriter, relevantMethods, generatedName, backendType);
    implementProcess(classWriter, backendType, relevantMethods, iFace, generatedName);

    classWriter.visitEnd();

    synchronized (ProxyChannelFactory.class) {
        preExisting = findExisting(generatedName, iFace);
        if (preExisting != null) {
            return instantiate(preExisting, capacity, waitStrategy);
        }
        byte[] byteCode = classWriter.toByteArray();
        printClassBytes(byteCode);
        // Caveat: The interface and JCTools must be on the same class loader. Maybe class loader should be an argument? Overload?
        Class<?> definedClass = UnsafeAccess.UNSAFE.defineClass(generatedName, byteCode, 0, byteCode.length, iFace.getClassLoader(), null);
        return instantiate(definedClass, capacity, waitStrategy);
    }
}
 
Example 20
Source File: ApplicationFactory.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
public static byte[] create(String name, String context) {

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

        cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER,
                name.replace('.', '/'),
                null,
                "javax/ws/rs/core/Application", null);

        int lastDot = name.lastIndexOf('.');
        String simpleName = name.substring(lastDot + 1);
        cw.visitSource(simpleName + ".java", null);

        {
            av0 = cw.visitAnnotation("Ljavax/ws/rs/ApplicationPath;", true);
            av0.visit("value", "/");
            av0.visitEnd();
        }
        {
            mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitLineNumber(10, l0);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/core/Application", "<init>", "()V", false);
            mv.visitInsn(RETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this",
                    "L" + name.replace('.', '/') + ";",
                    null,
                    l0,
                    l1,
                    0);
            mv.visitMaxs(1, 1);
            mv.visitEnd();
        }
        cw.visitEnd();

        return cw.toByteArray();
    }