Search in sources :

Example 6 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project SpongeCommon by SpongePowered.

the class ClassEventListenerFactory method generateClass.

private static byte[] generateClass(String name, Class<?> handle, Method method, Class<?> eventClass, Class<? extends EventFilter> filter) {
    name = name.replace('.', '/');
    final String handleName = Type.getInternalName(handle);
    final String handleDescriptor = Type.getDescriptor(handle);
    final String filterName = Type.getInternalName(filter);
    String eventDescriptor = "(";
    for (int i = 0; i < method.getParameterCount(); i++) {
        eventDescriptor += Type.getDescriptor(method.getParameterTypes()[i]);
    }
    eventDescriptor += ")V";
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    FieldVisitor fv;
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, BASE_HANDLER, null);
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "FILTER", "L" + filterName + ";", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, filterName);
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, filterName, "<init>", "()V", false);
        mv.visitFieldInsn(PUTSTATIC, name, "FILTER", "L" + filterName + ";");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", '(' + handleDescriptor + ")V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, BASE_HANDLER, "<init>", "(Ljava/lang/Object;)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "handle", HANDLE_METHOD_DESCRIPTOR, null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, name, "FILTER", "L" + filterName + ";");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(EventFilter.class), "filter", FILTER_DESCRIPTOR, true);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 2);
        Label l2 = new Label();
        mv.visitJumpInsn(IFNULL, l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "handle", "Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, handleName);
        for (int i = 0; i < method.getParameterCount(); i++) {
            mv.visitVarInsn(ALOAD, 2);
            mv.visitIntInsn(BIPUSH, i);
            mv.visitInsn(AALOAD);
            Type paramType = Type.getType(method.getParameterTypes()[i]);
            GeneratorUtils.visitUnboxingMethod(mv, paramType);
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, handleName, method.getName(), eventDescriptor, false);
        mv.visitLabel(l2);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Type(org.objectweb.asm.Type) Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 7 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project teiid by teiid.

the class RestASMBasedWebArchiveBuilder method getApplicationClass.

public byte[] getApplicationClass(ArrayList<String> models) {
    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;
    MethodVisitor mv;
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "org/teiid/jboss/rest/TeiidRestApplication", null, "javax/ws/rs/core/Application", null);
    {
        fv = cw.visitField(ACC_PRIVATE, "singletons", "Ljava/util/Set;", "Ljava/util/Set<Ljava/lang/Object;>;", null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE, "empty", "Ljava/util/Set;", "Ljava/util/Set<Ljava/lang/Class<*>;>;", null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/core/Application", "<init>", "()V");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/HashSet");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, "java/util/HashSet");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "empty", "Ljava/util/Set;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
        mv.visitTypeInsn(NEW, "org/teiid/jboss/rest/CustomApiListingResource");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "org/teiid/jboss/rest/CustomApiListingResource", "<init>", "()V");
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "add", "(Ljava/lang/Object;)Z");
        mv.visitInsn(POP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
        mv.visitTypeInsn(NEW, "io/swagger/jaxrs/listing/SwaggerSerializers");
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, "io/swagger/jaxrs/listing/SwaggerSerializers", "<init>", "()V");
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "add", "(Ljava/lang/Object;)Z");
        mv.visitInsn(POP);
        mv.visitVarInsn(ALOAD, 0);
        for (int i = 0; i < models.size(); i++) {
            mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
            mv.visitTypeInsn(NEW, "org/teiid/jboss/rest/" + models.get(i));
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, "org/teiid/jboss/rest/" + models.get(i), "<init>", "()V");
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(POP);
            if (i < models.size() - 1) {
                mv.visitVarInsn(ALOAD, 0);
            }
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(3, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getClasses", "()Ljava/util/Set;", "()Ljava/util/Set<Ljava/lang/Class<*>;>;", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "empty", "Ljava/util/Set;");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getSingletons", "()Ljava/util/Set;", "()Ljava/util/Set<Ljava/lang/Object;>;", null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 8 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project ecf by eclipse.

the class ProxyGenerator method visit.

/**
 * @param version
 *            version
 * @param access
 *            access
 * @param name
 *            name
 * @param signature
 *            signature
 * @param superName
 *            superName
 * @param interfaces
 *            interfaces
 * @see org.objectweb.asm.ClassVisitor#visit(int, int, java.lang.String,
 *      java.lang.String, java.lang.String, java.lang.String[])
 */
public void visit(final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces) {
    MethodVisitor method;
    final FieldVisitor field;
    // initial class / interface ?
    if (name.equals(smartProxyClassNameDashed) || (smartProxyClassName == null && name.equals(serviceInterfaceNames[0].replace('.', '/')))) {
        if (RemoteOSGiServiceImpl.PROXY_DEBUG) {
            RemoteOSGiServiceImpl.log.log(LogService.LOG_DEBUG, // $NON-NLS-1$
            "creating proxy class " + implName);
        }
        final String[] serviceInterfaces = new String[serviceInterfaceNames.length + 1];
        for (int i = 0; i < serviceInterfaceNames.length; i++) {
            serviceInterfaces[i] = serviceInterfaceNames[i].replace('.', '/');
        }
        // $NON-NLS-1$
        serviceInterfaces[serviceInterfaceNames.length] = "org/osgi/framework/BundleActivator";
        if ((access & ACC_INTERFACE) == 0) {
            // we have a smart proxy
            final Set ifaces = new HashSet();
            ifaces.addAll(Arrays.asList(interfaces));
            // $NON-NLS-1$
            ifaces.add("org/osgi/framework/BundleActivator");
            ifaces.addAll(Arrays.asList(serviceInterfaces));
            // V1_1
            writer.visit((version >= V1_5 && RemoteOSGiServiceImpl.IS_JAVA5) ? V1_5 : V1_2, ACC_PUBLIC + ACC_SUPER, implName, null, superName, (String[]) ifaces.toArray(new String[ifaces.size()]));
            if (java.util.Arrays.asList(interfaces).contains("ch/ethz/iks/r_osgi/SmartProxy")) {
                // $NON-NLS-1$
                addLifecycleSupport = true;
            }
        } else {
            // we have an interface
            writer.visit((version >= V1_5 && RemoteOSGiServiceImpl.IS_JAVA5) ? V1_5 : V1_2, ACC_PUBLIC + ACC_SUPER, implName, null, "java/lang/Object", // $NON-NLS-1$
            serviceInterfaces);
            if (RemoteOSGiServiceImpl.PROXY_DEBUG) {
                RemoteOSGiServiceImpl.log.log(LogService.LOG_DEBUG, // $NON-NLS-1$
                "Creating Proxy Bundle from Interfaces " + Arrays.asList(serviceInterfaceNames));
            }
            // creates a MethodWriter for the (implicit) constructor
            method = // $NON-NLS-1$ //$NON-NLS-2$
            writer.visitMethod(// $NON-NLS-1$ //$NON-NLS-2$
            ACC_PUBLIC, // $NON-NLS-1$ //$NON-NLS-2$
            "<init>", // $NON-NLS-1$ //$NON-NLS-2$
            "()V", // $NON-NLS-1$ //$NON-NLS-2$
            null, null);
            method.visitVarInsn(ALOAD, 0);
            // $NON-NLS-1$
            method.visitMethodInsn(// $NON-NLS-1$
            INVOKESPECIAL, // $NON-NLS-1$
            "java/lang/Object", "<init>", // $NON-NLS-1$ //$NON-NLS-2$
            "()V");
            method.visitInsn(RETURN);
            method.visitMaxs(2, 1);
            method.visitEnd();
            // add the remaining interfaces to the list to be visited later
            for (int i = 1; i < serviceInterfaceNames.length; i++) {
                if (!visitedInterfaces.contains(serviceInterfaceNames[i])) {
                    superInterfaces.add(serviceInterfaceNames[i].replace('.', '/'));
                }
            }
        }
        field = writer.visitField(ACC_PRIVATE, "endpoint", // $NON-NLS-1$ //$NON-NLS-2$
        "L" + ENDPOINT_I + ";", null, // $NON-NLS-1$
        null);
        field.visitEnd();
        {
            method = // $NON-NLS-1$
            writer.visitMethod(// $NON-NLS-1$
            ACC_PUBLIC, // $NON-NLS-1$
            "start", // $NON-NLS-1$
            "(Lorg/osgi/framework/BundleContext;)V", // $NON-NLS-1$
            null, // $NON-NLS-1$
            new String[] { "java/lang/Exception" });
            method.visitCode();
            method.visitVarInsn(ALOAD, 1);
            method.visitVarInsn(ALOAD, 1);
            method.visitLdcInsn(Remoting.class.getName());
            method.visitMethodInsn(INVOKEINTERFACE, // $NON-NLS-1$
            "org/osgi/framework/BundleContext", // $NON-NLS-1$
            "getServiceReference", // $NON-NLS-1$
            "(Ljava/lang/String;)Lorg/osgi/framework/ServiceReference;");
            method.visitMethodInsn(INVOKEINTERFACE, // $NON-NLS-1$
            "org/osgi/framework/BundleContext", // $NON-NLS-1$
            "getService", // $NON-NLS-1$
            "(Lorg/osgi/framework/ServiceReference;)Ljava/lang/Object;");
            method.visitTypeInsn(CHECKCAST, REMOTING_I);
            method.visitVarInsn(ASTORE, 2);
            method.visitVarInsn(ALOAD, 0);
            method.visitVarInsn(ALOAD, 2);
            method.visitLdcInsn(uri);
            method.visitMethodInsn(INVOKEINTERFACE, REMOTING_I, "getEndpoint", // $NON-NLS-1$ //$NON-NLS-2$
            "(Ljava/lang/String;)L" + ENDPOINT_I + // $NON-NLS-1$
            ";");
            method.visitFieldInsn(PUTFIELD, implName, "endpoint", // $NON-NLS-1$ //$NON-NLS-2$
            "L" + ENDPOINT_I + // $NON-NLS-1$
            ";");
            method.visitVarInsn(ALOAD, 0);
            method.visitFieldInsn(GETFIELD, implName, "endpoint", // $NON-NLS-1$ //$NON-NLS-2$
            "L" + ENDPOINT_I + // $NON-NLS-1$
            ";");
            method.visitLdcInsn(uri);
            method.visitVarInsn(ALOAD, 1);
            final int len = serviceInterfaceNames.length;
            if (len < 6) {
                method.visitInsn(ICONST[serviceInterfaceNames.length]);
            } else {
                method.visitIntInsn(BIPUSH, len);
            }
            // $NON-NLS-1$
            method.visitTypeInsn(ANEWARRAY, "java/lang/String");
            for (int i = 0; i < len && i < 6; i++) {
                method.visitInsn(DUP);
                method.visitInsn(ICONST[i]);
                method.visitLdcInsn(serviceInterfaceNames[i]);
                method.visitInsn(AASTORE);
            }
            for (int i = 6; i < len; i++) {
                method.visitInsn(DUP);
                method.visitIntInsn(BIPUSH, i);
                method.visitLdcInsn(serviceInterfaceNames[i]);
                method.visitInsn(AASTORE);
            }
            method.visitVarInsn(ALOAD, 0);
            method.visitVarInsn(ALOAD, 0);
            method.visitFieldInsn(GETFIELD, implName, "endpoint", // $NON-NLS-1$ //$NON-NLS-2$
            "L" + ENDPOINT_I + // $NON-NLS-1$
            ";");
            method.visitLdcInsn(uri);
            method.visitMethodInsn(INVOKEINTERFACE, ENDPOINT_I, // $NON-NLS-1$
            "getProperties", // $NON-NLS-1$
            "(Ljava/lang/String;)Ljava/util/Dictionary;");
            method.visitMethodInsn(INVOKEINTERFACE, // $NON-NLS-1$
            "org/osgi/framework/BundleContext", // $NON-NLS-1$
            "registerService", // $NON-NLS-1$
            "([Ljava/lang/String;Ljava/lang/Object;Ljava/util/Dictionary;)Lorg/osgi/framework/ServiceRegistration;");
            method.visitMethodInsn(INVOKEINTERFACE, ENDPOINT_I, // $NON-NLS-1$
            "trackRegistration", // $NON-NLS-1$
            "(Ljava/lang/String;Lorg/osgi/framework/ServiceRegistration;)V");
            method.visitVarInsn(ALOAD, 0);
            method.visitFieldInsn(GETFIELD, implName, "endpoint", // $NON-NLS-1$ //$NON-NLS-2$
            "L" + ENDPOINT_I + // $NON-NLS-1$
            ";");
            method.visitLdcInsn(uri);
            method.visitMethodInsn(INVOKEINTERFACE, ENDPOINT_I, // $NON-NLS-1$
            "getProperties", // $NON-NLS-1$
            "(Ljava/lang/String;)Ljava/util/Dictionary;");
            method.visitLdcInsn(RemoteOSGiService.PRESENTATION);
            // $NON-NLS-1$
            method.visitMethodInsn(// $NON-NLS-1$
            INVOKEVIRTUAL, // $NON-NLS-1$
            "java/util/Dictionary", "get", // $NON-NLS-1$ //$NON-NLS-2$
            "(Ljava/lang/Object;)Ljava/lang/Object;");
            // $NON-NLS-1$
            method.visitTypeInsn(CHECKCAST, "java/lang/String");
            method.visitVarInsn(ASTORE, 3);
            method.visitVarInsn(ALOAD, 3);
            final Label l0 = new Label();
            method.visitJumpInsn(IFNULL, l0);
            method.visitVarInsn(ALOAD, 3);
            // $NON-NLS-1$
            method.visitMethodInsn(// $NON-NLS-1$
            INVOKESTATIC, // $NON-NLS-1$
            "java/lang/Class", "forName", // $NON-NLS-1$ //$NON-NLS-2$
            "(Ljava/lang/String;)Ljava/lang/Class;");
            // $NON-NLS-1$
            method.visitMethodInsn(// $NON-NLS-1$
            INVOKEVIRTUAL, // $NON-NLS-1$
            "java/lang/Class", "newInstance", // $NON-NLS-1$ //$NON-NLS-2$
            "()Ljava/lang/Object;");
            method.visitTypeInsn(CHECKCAST, UICOMP_I);
            method.visitVarInsn(ASTORE, 4);
            method.visitVarInsn(ALOAD, 4);
            method.visitVarInsn(ALOAD, 0);
            method.visitVarInsn(ALOAD, 1);
            method.visitMethodInsn(INVOKEINTERFACE, UICOMP_I, // $NON-NLS-1$
            "initComponent", // $NON-NLS-1$
            "(Ljava/lang/Object;Lorg/osgi/framework/BundleContext;)V");
            method.visitVarInsn(ALOAD, 1);
            method.visitLdcInsn(ServiceUIComponent.class.getName());
            method.visitVarInsn(ALOAD, 4);
            method.visitVarInsn(ALOAD, 0);
            method.visitFieldInsn(GETFIELD, implName, "endpoint", // $NON-NLS-1$ //$NON-NLS-2$
            "L" + ENDPOINT_I + // $NON-NLS-1$
            ";");
            method.visitLdcInsn(uri);
            method.visitMethodInsn(INVOKEINTERFACE, ENDPOINT_I, // $NON-NLS-1$
            "getPresentationProperties", // $NON-NLS-1$
            "(Ljava/lang/String;)Ljava/util/Dictionary;");
            method.visitMethodInsn(INVOKEINTERFACE, // $NON-NLS-1$
            "org/osgi/framework/BundleContext", // $NON-NLS-1$
            "registerService", // $NON-NLS-1$
            "(Ljava/lang/String;Ljava/lang/Object;Ljava/util/Dictionary;)Lorg/osgi/framework/ServiceRegistration;");
            method.visitInsn(POP);
            method.visitLabel(l0);
            if (addLifecycleSupport) {
                method.visitVarInsn(ALOAD, 0);
                method.visitVarInsn(ALOAD, 1);
                // $NON-NLS-1$
                method.visitMethodInsn(// $NON-NLS-1$
                INVOKEVIRTUAL, // $NON-NLS-1$
                implName, // $NON-NLS-1$
                "started", // $NON-NLS-1$
                "(Lorg/osgi/framework/BundleContext;)V");
            }
            method.visitInsn(RETURN);
            method.visitMaxs(7, 5);
            method.visitEnd();
        }
        {
            method = // $NON-NLS-1$
            writer.visitMethod(// $NON-NLS-1$
            ACC_PUBLIC, // $NON-NLS-1$
            "stop", // $NON-NLS-1$
            "(Lorg/osgi/framework/BundleContext;)V", // $NON-NLS-1$
            null, // $NON-NLS-1$
            new String[] { "java/lang/Exception" });
            method.visitCode();
            method.visitVarInsn(ALOAD, 0);
            method.visitFieldInsn(GETFIELD, implName, "endpoint", // $NON-NLS-1$//$NON-NLS-2$
            "L" + ENDPOINT_I + // $NON-NLS-1$
            ";");
            method.visitLdcInsn(uri);
            method.visitMethodInsn(INVOKEINTERFACE, ENDPOINT_I, "untrackRegistration", // $NON-NLS-1$ //$NON-NLS-2$
            "(Ljava/lang/String;)V");
            method.visitVarInsn(ALOAD, 0);
            method.visitInsn(ACONST_NULL);
            method.visitFieldInsn(PUTFIELD, implName, "endpoint", // $NON-NLS-1$ //$NON-NLS-2$
            "L" + ENDPOINT_I + // $NON-NLS-1$
            ";");
            if (addLifecycleSupport) {
                method.visitVarInsn(ALOAD, 0);
                method.visitVarInsn(ALOAD, 1);
                // $NON-NLS-1$
                method.visitMethodInsn(// $NON-NLS-1$
                INVOKEVIRTUAL, // $NON-NLS-1$
                implName, // $NON-NLS-1$
                "stopped", // $NON-NLS-1$
                "(Lorg/osgi/framework/BundleContext;)V");
            }
            method.visitInsn(RETURN);
            method.visitMaxs(2, 2);
            method.visitEnd();
        }
    }
    // add the interfaces to the list to be visited later
    for (int i = 0; i < interfaces.length; i++) {
        if (!visitedInterfaces.contains(interfaces[i])) {
            superInterfaces.add(interfaces[i]);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint) MethodVisitor(org.objectweb.asm.MethodVisitor) HashSet(java.util.HashSet)

Example 9 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project LanternServer by LanternPowered.

the class ClassEventListenerFactory method generateClass.

private static byte[] generateClass(String name, Class<?> handle, Method method, Class<?> eventClass, Class<? extends EventFilter> filter) {
    name = name.replace('.', '/');
    final String handleName = Type.getInternalName(handle);
    final String handleDescriptor = Type.getDescriptor(handle);
    final String filterName = Type.getInternalName(filter);
    String eventDescriptor = "(";
    for (int i = 0; i < method.getParameterCount(); i++) {
        eventDescriptor += Type.getDescriptor(method.getParameterTypes()[i]);
    }
    eventDescriptor += ")V";
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    FieldVisitor fv;
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, BASE_HANDLER, null);
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "FILTER", "L" + filterName + ";", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, filterName);
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, filterName, "<init>", "()V", false);
        mv.visitFieldInsn(PUTSTATIC, name, "FILTER", "L" + filterName + ";");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", '(' + handleDescriptor + ")V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, BASE_HANDLER, "<init>", "(Ljava/lang/Object;)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "handle", HANDLE_METHOD_DESCRIPTOR, null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, name, "FILTER", "L" + filterName + ";");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(EventFilter.class), "filter", FILTER_DESCRIPTOR, true);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 2);
        Label l2 = new Label();
        mv.visitJumpInsn(IFNULL, l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "handle", "Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, handleName);
        for (int i = 0; i < method.getParameterCount(); i++) {
            mv.visitVarInsn(ALOAD, 2);
            mv.visitIntInsn(BIPUSH, i);
            mv.visitInsn(AALOAD);
            Type paramType = Type.getType(method.getParameterTypes()[i]);
            GeneratorUtils.visitUnboxingMethod(mv, paramType);
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, handleName, method.getName(), eventDescriptor, false);
        mv.visitLabel(l2);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Type(org.objectweb.asm.Type) Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 10 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project LanternServer by LanternPowered.

the class InventoryConstructorFactory method generateCarriedClass.

/**
 * Gets the generated class for the target class that
 * also implements {@link CarriedInventory}.
 *
 * @param inventoryType The inventory type
 * @return The carried inventory type
 */
private Class<? extends AbstractInventory> generateCarriedClass(Class<? extends AbstractInventory> inventoryType) {
    final String name = inventoryType.getName() + "$$Carried";
    final String className = name.replace('.', '/');
    // Generate the class bytecode and load it
    final ClassWriter cw = new ClassWriter(0);
    final String superName = Type.getInternalName(inventoryType);
    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, className, String.format("L%s;L%s<L%s;>;", superName, CARRIED_INVENTORY_NAME, CARRIER_NAME), superName, new String[] { CARRIED_INVENTORY_NAME });
    final FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, CARRIER_REF_FIELD, CARRIER_REFERENCE_DESC, String.format("L%s<L%s;>;", CARRIER_REFERENCE_NAME, CARRIER_NAME), null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, superName, "<init>", "()V", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(Type.getType(CARRIER_DESC));
    mv.visitMethodInsn(INVOKESTATIC, CARRIER_REFERENCE_NAME, "of", String.format("(Ljava/lang/Class;)L%s;", CARRIER_REFERENCE_NAME), true);
    mv.visitFieldInsn(PUTFIELD, className, CARRIER_REF_FIELD, CARRIER_REFERENCE_DESC);
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PROTECTED, "setCarrier", String.format("(L%s;)V", CARRIER_NAME), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKESPECIAL, superName, "setCarrier", String.format("(L%s;)V", CARRIER_NAME), false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, CARRIER_REF_FIELD, CARRIER_REFERENCE_DESC);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, CARRIER_REFERENCE_NAME, "set", String.format("(L%s;)V", CARRIER_NAME), true);
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getCarrier", "()Ljava/util/Optional;", String.format("()Ljava/util/Optional<L%s;>;", CARRIER_NAME), null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, CARRIER_REF_FIELD, CARRIER_REFERENCE_DESC);
    mv.visitMethodInsn(INVOKEINTERFACE, CARRIER_REFERENCE_NAME, "get", "()Ljava/util/Optional;", true);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    cw.visitEnd();
    return this.classLoader.defineClass(name, cw.toByteArray());
}
Also used : FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

FieldVisitor (org.objectweb.asm.FieldVisitor)90 MethodVisitor (org.objectweb.asm.MethodVisitor)59 Label (org.objectweb.asm.Label)33 ClassWriter (org.objectweb.asm.ClassWriter)28 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)9 ClassReader (org.objectweb.asm.ClassReader)8 Type (org.objectweb.asm.Type)7 Method (java.lang.reflect.Method)6 ClassVisitor (org.objectweb.asm.ClassVisitor)6 IOException (java.io.IOException)4 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 LitString (lucee.transformer.expression.literal.LitString)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 PrintWriter (java.io.PrintWriter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2