Search in sources :

Example 11 with ClassWriter

use of org.apache.xbean.asm9.ClassWriter in project tomee by apache.

the class LocalBeanProxyFactory method generateProxy.

public static byte[] generateProxy(final Class<?> classToProxy, final String proxyName, final Class<?>... interfaces) throws ProxyGenerationException {
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    final String proxyClassFileName = proxyName.replace('.', '/');
    final String classFileName = classToProxy.getName().replace('.', '/');
    // push class signature
    final String[] interfaceNames = new String[interfaces.length];
    for (int i = 0; i < interfaces.length; i++) {
        final Class<?> anInterface = interfaces[i];
        interfaceNames[i] = anInterface.getName().replace('.', '/');
    }
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, proxyClassFileName, null, classFileName, interfaceNames);
    cw.visitSource(classFileName + ".java", null);
    cw.visitAnnotation("L" + Proxy.class.getName().replace('.', '/') + ";", true).visitEnd();
    // push InvocationHandler fields
    cw.visitField(ACC_FINAL + ACC_PRIVATE, BUSSINESS_HANDLER_NAME, "Ljava/lang/reflect/InvocationHandler;", null, null).visitEnd();
    cw.visitField(ACC_FINAL + ACC_PRIVATE, NON_BUSINESS_HANDLER_NAME, "Ljava/lang/reflect/InvocationHandler;", null, null).visitEnd();
    final Map<String, List<Method>> methodMap = new HashMap<>();
    getNonPrivateMethods(classToProxy, methodMap);
    for (final Class<?> anInterface : interfaces) {
        getNonPrivateMethods(anInterface, methodMap);
    }
    // Iterate over the public methods
    for (final Map.Entry<String, List<Method>> entry : methodMap.entrySet()) {
        for (final Method method : entry.getValue()) {
            final String name = method.getName();
            if (Modifier.isPublic(method.getModifiers()) || method.getParameterTypes().length == 0 && ("finalize".equals(name) || "clone".equals(name))) {
                // forward invocations of any public methods or
                // finalize/clone methods to businessHandler
                processMethod(cw, method, proxyClassFileName, BUSSINESS_HANDLER_NAME);
            } else {
                // forward invocations of any other methods to nonBusinessHandler
                processMethod(cw, method, proxyClassFileName, NON_BUSINESS_HANDLER_NAME);
            }
        }
    }
    return cw.toByteArray();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map) ClassWriter(org.apache.xbean.asm9.ClassWriter)

Example 12 with ClassWriter

use of org.apache.xbean.asm9.ClassWriter in project tomee by apache.

the class DynamicSubclass method visitConstructor.

private static MethodVisitor visitConstructor(final ClassWriter cw, final String proxyClassFileName, final String classFileName, final Constructor<?> constructor) {
    final String descriptor = Type.getConstructorDescriptor(constructor);
    final String[] exceptions = new String[constructor.getExceptionTypes().length];
    for (int i = 0; i < exceptions.length; i++) {
        exceptions[i] = Type.getInternalName(constructor.getExceptionTypes()[i]);
    }
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    int index = 1;
    for (final Type type : Type.getArgumentTypes(descriptor)) {
        mv.visitVarInsn(type.getOpcode(ILOAD), index);
        index += size(type);
    }
    mv.visitMethodInsn(INVOKESPECIAL, classFileName, "<init>", descriptor, false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(PUTFIELD, proxyClassFileName, "this$handler", "Ljava/lang/reflect/InvocationHandler;");
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 1);
    return mv;
}
Also used : Type(org.apache.xbean.asm9.Type) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 13 with ClassWriter

use of org.apache.xbean.asm9.ClassWriter in project tomee by apache.

the class MakeTxLookup method createNewHibernateStrategy.

private static void createNewHibernateStrategy(final File basedir, final String target, final String abstractJtaPlatformPackage) throws Exception {
    final ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, target.replace('.', '/'), null, abstractJtaPlatformPackage + "/AbstractJtaPlatform", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, abstractJtaPlatformPackage + "/AbstractJtaPlatform", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "locateTransactionManager", "()Ljavax/transaction/TransactionManager;", null, null);
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/OpenEJB", "getTransactionManager", "()Ljavax/transaction/TransactionManager;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "locateUserTransaction", "()Ljavax/transaction/UserTransaction;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        final Label l1 = new Label();
        final Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "javax/naming/NamingException");
        mv.visitLabel(l0);
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/loader/SystemInstance", "get", "()Lorg/apache/openejb/loader/SystemInstance;", false);
        mv.visitLdcInsn(Type.getType("Lorg/apache/openejb/spi/ContainerSystem;"));
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/openejb/loader/SystemInstance", "getComponent", "(Ljava/lang/Class;)Ljava/lang/Object;", false);
        mv.visitTypeInsn(CHECKCAST, "org/apache/openejb/spi/ContainerSystem");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/openejb/spi/ContainerSystem", "getJNDIContext", "()Ljavax/naming/Context;", true);
        mv.visitLdcInsn("comp/UserTransaction");
        mv.visitMethodInsn(INVOKEINTERFACE, "javax/naming/Context", "lookup", "(Ljava/lang/String;)Ljava/lang/Object;", true);
        mv.visitTypeInsn(CHECKCAST, "javax/transaction/UserTransaction");
        mv.visitLabel(l1);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "javax/naming/NamingException" });
        mv.visitVarInsn(ASTORE, 1);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    write(basedir, cw, target.replace('.', '/'));
}
Also used : Label(org.apache.xbean.asm9.Label) ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 14 with ClassWriter

use of org.apache.xbean.asm9.ClassWriter in project apex-malhar by apache.

the class BeanClassGenerator method createAndWriteBeanClass.

/**
 * Creates a class from given field information and writes it to the output stream. Also returns byte[] of compiled
 * class
 *
 * @param fqcn         fully qualified class name
 * @param fieldList    field list describing the class
 * @param outputStream stream to which the class is persisted
 * @throws JSONException
 * @throws IOException
 */
public static byte[] createAndWriteBeanClass(String fqcn, List<TupleSchemaRegistry.SQLFieldInfo> fieldList, FSDataOutputStream outputStream) throws JSONException, IOException {
    ClassNode classNode = new ClassNode();
    // generated class will only run on JRE 1.7 or above
    classNode.version = Opcodes.V1_7;
    classNode.access = Opcodes.ACC_PUBLIC;
    classNode.name = fqcn.replace('.', '/');
    classNode.superName = "java/lang/Object";
    // add default constructor
    addDefaultConstructor(classNode);
    for (TupleSchemaRegistry.SQLFieldInfo fieldInfo : fieldList) {
        String fieldName = fieldInfo.getColumnName();
        String fieldType = fieldInfo.getType().getJavaType().getName();
        String fieldJavaType = getJavaType(fieldType);
        addPrivateField(classNode, fieldName, fieldJavaType);
        String fieldNameForMethods = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
        if (fieldJavaType.equals(getJavaType("java.util.Date"))) {
            addDateFields(classNode, fieldName, fieldNameForMethods, "java/util/Date");
        } else {
            addGetter(classNode, fieldName, fieldNameForMethods, fieldJavaType);
            addSetter(classNode, fieldName, fieldNameForMethods, fieldJavaType);
        }
    }
    addToStringMethod(classNode, fieldList);
    addHashCodeMethod(classNode, fieldList);
    addEqualsMethod(classNode, fieldList);
    // Write the class
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(cw);
    cw.visitEnd();
    byte[] classBytes = cw.toByteArray();
    if (outputStream != null) {
        outputStream.write(classBytes);
        outputStream.close();
    }
    return classBytes;
}
Also used : ClassNode(org.apache.xbean.asm5.tree.ClassNode) TupleSchemaRegistry(org.apache.apex.malhar.sql.schema.TupleSchemaRegistry) ClassWriter(org.apache.xbean.asm5.ClassWriter)

Example 15 with ClassWriter

use of org.apache.xbean.asm9.ClassWriter in project tomee by apache.

the class LocalBeanProxyFactory method visit.

public static MethodVisitor visit(final ClassWriter cw, final Method method, final String proxyName, final String handlerName) throws ProxyGenerationException {
    final Class<?> returnType = method.getReturnType();
    final Class<?>[] parameterTypes = method.getParameterTypes();
    final Class<?>[] exceptionTypes = method.getExceptionTypes();
    final int modifiers = method.getModifiers();
    // push the method definition
    int modifier = 0;
    if (Modifier.isPublic(modifiers)) {
        modifier = ACC_PUBLIC;
    } else if (Modifier.isProtected(modifiers)) {
        modifier = ACC_PROTECTED;
    }
    final MethodVisitor mv = cw.visitMethod(modifier, method.getName(), getMethodSignatureAsString(returnType, parameterTypes), null, null);
    mv.visitCode();
    // push try/catch block, to catch declared exceptions, and to catch java.lang.Throwable
    final Label l0 = new Label();
    final Label l1 = new Label();
    final Label l2 = new Label();
    if (exceptionTypes.length > 0) {
        mv.visitTryCatchBlock(l0, l1, l2, "java/lang/reflect/InvocationTargetException");
    }
    // push try code
    mv.visitLabel(l0);
    final String classNameToOverride = method.getDeclaringClass().getName().replace('.', '/');
    mv.visitLdcInsn(Type.getType("L" + classNameToOverride + ";"));
    // the following code generates the bytecode for this line of Java:
    // Method method = <proxy>.class.getMethod("add", new Class[] { <array of function argument classes> });
    // get the method name to invoke, and push to stack
    mv.visitLdcInsn(method.getName());
    // create the Class[]
    createArrayDefinition(mv, parameterTypes.length, Class.class);
    int length = 1;
    // push parameters into array
    for (int i = 0; i < parameterTypes.length; i++) {
        // keep copy of array on stack
        mv.visitInsn(DUP);
        final Class<?> parameterType = parameterTypes[i];
        // push number onto stack
        pushIntOntoStack(mv, i);
        if (parameterType.isPrimitive()) {
            final String wrapperType = getWrapperType(parameterType);
            mv.visitFieldInsn(GETSTATIC, wrapperType, "TYPE", "Ljava/lang/Class;");
        } else {
            mv.visitLdcInsn(Type.getType(getAsmTypeAsString(parameterType, true)));
        }
        mv.visitInsn(AASTORE);
        if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType)) {
            length += 2;
        } else {
            length++;
        }
    }
    // invoke getMethod() with the method name and the array of types
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
    // store the returned method for later
    mv.visitVarInsn(ASTORE, length);
    // the following code generates bytecode equivalent to:
    // return ((<returntype>) invocationHandler.invoke(this, method, new Object[] { <function arguments }))[.<primitive>Value()];
    final Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(ALOAD, 0);
    // get the invocationHandler field from this class
    mv.visitFieldInsn(GETFIELD, proxyName, handlerName, "Ljava/lang/reflect/InvocationHandler;");
    // we want to pass "this" in as the first parameter
    mv.visitVarInsn(ALOAD, 0);
    // and the method we fetched earlier
    mv.visitVarInsn(ALOAD, length);
    // need to construct the array of objects passed in
    // create the Object[]
    createArrayDefinition(mv, parameterTypes.length, Object.class);
    int index = 1;
    // push parameters into array
    for (int i = 0; i < parameterTypes.length; i++) {
        // keep copy of array on stack
        mv.visitInsn(DUP);
        final Class<?> parameterType = parameterTypes[i];
        // push number onto stack
        pushIntOntoStack(mv, i);
        if (parameterType.isPrimitive()) {
            final String wrapperType = getWrapperType(parameterType);
            mv.visitVarInsn(getVarInsn(parameterType), index);
            mv.visitMethodInsn(INVOKESTATIC, wrapperType, "valueOf", "(" + getPrimitiveLetter(parameterType) + ")L" + wrapperType + ";", false);
            mv.visitInsn(AASTORE);
            if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType)) {
                index += 2;
            } else {
                index++;
            }
        } else {
            mv.visitVarInsn(ALOAD, index);
            mv.visitInsn(AASTORE);
            index++;
        }
    }
    // invoke the invocationHandler
    mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/reflect/InvocationHandler", "invoke", "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;", true);
    // cast the result
    mv.visitTypeInsn(CHECKCAST, getCastType(returnType));
    if (returnType.isPrimitive() && !Void.TYPE.equals(returnType)) {
        // get the primitive value
        mv.visitMethodInsn(INVOKEVIRTUAL, getWrapperType(returnType), getPrimitiveMethod(returnType), "()" + getPrimitiveLetter(returnType), false);
    }
    // push return
    mv.visitLabel(l1);
    if (!Void.TYPE.equals(returnType)) {
        mv.visitInsn(getReturnInsn(returnType));
    } else {
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
    }
    // catch InvocationTargetException
    if (exceptionTypes.length > 0) {
        mv.visitLabel(l2);
        mv.visitVarInsn(ASTORE, length);
        final Label l5 = new Label();
        mv.visitLabel(l5);
        for (int i = 0; i < exceptionTypes.length; i++) {
            final Class<?> exceptionType = exceptionTypes[i];
            mv.visitLdcInsn(Type.getType("L" + exceptionType.getName().replace('.', '/') + ";"));
            mv.visitVarInsn(ALOAD, length);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false);
            final Label l6 = new Label();
            mv.visitJumpInsn(IFEQ, l6);
            final Label l7 = new Label();
            mv.visitLabel(l7);
            mv.visitVarInsn(ALOAD, length);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
            mv.visitTypeInsn(CHECKCAST, exceptionType.getName().replace('.', '/'));
            mv.visitInsn(ATHROW);
            mv.visitLabel(l6);
            if (i == exceptionTypes.length - 1) {
                mv.visitTypeInsn(NEW, "java/lang/reflect/UndeclaredThrowableException");
                mv.visitInsn(DUP);
                mv.visitVarInsn(ALOAD, length);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/reflect/UndeclaredThrowableException", "<init>", "(Ljava/lang/Throwable;)V", false);
                mv.visitInsn(ATHROW);
            }
        }
    }
    // finish this method
    mv.visitMaxs(0, 0);
    return mv;
}
Also used : Label(org.apache.xbean.asm9.Label) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Aggregations

MethodVisitor (org.apache.xbean.asm9.MethodVisitor)10 ClassWriter (org.apache.xbean.asm9.ClassWriter)8 ClassWriter (org.apache.xbean.asm6.ClassWriter)7 Method (java.lang.reflect.Method)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Stream (java.util.stream.Stream)4 ZipEntry (java.util.zip.ZipEntry)4 ClassReader (org.apache.xbean.asm6.ClassReader)4 MethodVisitor (org.apache.xbean.asm6.MethodVisitor)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Optional.ofNullable (java.util.Optional.ofNullable)3 JarEntry (java.util.jar.JarEntry)3 JarOutputStream (java.util.jar.JarOutputStream)3