Search in sources :

Example 16 with ClassWriter

use of jodd.asm5.ClassWriter in project tomee by apache.

the class DynamicSubclass method generateBytes.

private static byte[] generateBytes(final Class<?> classToProxy, final boolean proxyNonAbstractMethods) throws ProxyGenerationException {
    final Map<String, MethodVisitor> visitors = new HashMap<>();
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    final String proxyClassFileName = getSubclassName(classToProxy).replace('.', '/');
    final String classFileName = classToProxy.getName().replace('.', '/');
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, proxyClassFileName, null, classFileName, null);
    cw.visitSource(classFileName + ".java", null);
    // push InvocationHandler field
    cw.visitField(ACC_FINAL + ACC_PRIVATE, "this$handler", "Ljava/lang/reflect/InvocationHandler;", null, null).visitEnd();
    for (final Constructor<?> constructor : classToProxy.getConstructors()) {
        if (!Modifier.isPublic(constructor.getModifiers())) {
            continue;
        }
        final MethodVisitor mv = visitConstructor(cw, proxyClassFileName, classFileName, constructor);
        visitors.put("<init>" + Type.getConstructorDescriptor(constructor), mv);
    }
    final Map<String, List<Method>> methodMap = new HashMap<>();
    getNonPrivateMethods(classToProxy, methodMap);
    // Iterate over the public methods
    for (final Map.Entry<String, List<Method>> entry : methodMap.entrySet()) {
        for (final Method method : entry.getValue()) {
            if (Modifier.isAbstract(method.getModifiers()) || (proxyNonAbstractMethods && Modifier.isPublic(method.getModifiers()))) {
                final MethodVisitor visitor = LocalBeanProxyFactory.visit(cw, method, proxyClassFileName, "this$handler");
                visitors.put(method.getName() + Type.getMethodDescriptor(method), visitor);
            }
        }
    }
    copyClassAnnotations(classToProxy, cw);
    copyMethodAnnotations(classToProxy, visitors);
    // This should never be reached, but just in case
    for (final MethodVisitor visitor : visitors.values()) {
        visitor.visitEnd();
    }
    return cw.toByteArray();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map) ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 17 with ClassWriter

use of jodd.asm5.ClassWriter in project tomee by apache.

the class MakeTxLookup method createHibernteStrategy.

private static void createHibernteStrategy(final File baseDir) throws Exception {
    final String factory = HIBERNATE_FACTORY;
    final String classFilePath = factory.replace('.', '/');
    final String sourceFileName = factory.substring(factory.lastIndexOf('.') + 1, factory.length()) + ".java";
    final ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, classFilePath, null, "java/lang/Object", new String[] { "org/hibernate/transaction/TransactionManagerLookup" });
    cw.visitSource(sourceFileName, 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, "getTransactionManager", "(Ljava/util/Properties;)Ljavax/transaction/TransactionManager;", null, new String[] { "org/hibernate/HibernateException" });
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/OpenEJB", "getTransactionManager", "()Ljavax/transaction/TransactionManager;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getUserTransactionName", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        mv.visitLdcInsn("java:comp/UserTransaction");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getTransactionIdentifier", "(Ljavax/transaction/Transaction;)Ljava/lang/Object;", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    write(baseDir, cw, classFilePath);
}
Also used : ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 18 with ClassWriter

use of jodd.asm5.ClassWriter in project tomee by apache.

the class MakeTxLookup method createTopLinkStrategy.

private static void createTopLinkStrategy(final File baseDir) throws Exception {
    final String factory = TOPLINK_FACTORY;
    final String classFilePath = factory.replace('.', '/');
    final String sourceFileName = factory.substring(factory.lastIndexOf('.') + 1, factory.length()) + ".java";
    final ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, classFilePath, null, "oracle/toplink/essentials/transaction/JTATransactionController", null);
    cw.visitSource(sourceFileName, null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "oracle/toplink/essentials/transaction/JTATransactionController", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "acquireTransactionManager", "()Ljavax/transaction/TransactionManager;", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/OpenEJB", "getTransactionManager", "()Ljavax/transaction/TransactionManager;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    write(baseDir, cw, classFilePath);
}
Also used : ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 19 with ClassWriter

use of jodd.asm5.ClassWriter in project component-runtime by Talend.

the class PluginGenerator method createProcessor.

public byte[] createProcessor(final JarOutputStream outputStream, final String packageName, final String name) throws IOException {
    final String className = packageName + "/AProcessor.class";
    outputStream.putNextEntry(new ZipEntry(className));
    final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
    final AnnotationVisitor processorAnnotation = writer.visitAnnotation(Type.getDescriptor(Processor.class), true);
    processorAnnotation.visit("family", "comp");
    processorAnnotation.visit("name", name);
    processorAnnotation.visitEnd();
    writer.visit(V1_8, ACC_PUBLIC + ACC_SUPER, className.substring(0, className.length() - ".class".length()), null, Type.getInternalName(Object.class), new String[] { Serializable.class.getName().replace(".", "/") });
    writer.visitSource(className.replace(".class", ".java"), null);
    addConstructor(writer);
    // generate a processor
    final MethodVisitor emitMethod = writer.visitMethod(ACC_PUBLIC, "emit", "(L" + packageName + "/AModel;)L" + packageName + "/AModel;", null, new String[0]);
    emitMethod.visitAnnotation(Type.getDescriptor(ElementListener.class), true).visitEnd();
    emitMethod.visitCode();
    emitMethod.visitTypeInsn(NEW, packageName + "/AModel");
    emitMethod.visitInsn(DUP);
    emitMethod.visitMethodInsn(INVOKESPECIAL, packageName + "/AModel", "<init>", "()V", false);
    emitMethod.visitInsn(ARETURN);
    emitMethod.visitInsn(ARETURN);
    emitMethod.visitMaxs(1, 1);
    emitMethod.visitEnd();
    writer.visitEnd();
    return writer.toByteArray();
}
Also used : Processor(org.talend.sdk.component.api.processor.Processor) ZipEntry(java.util.zip.ZipEntry) AnnotationVisitor(org.apache.xbean.asm9.AnnotationVisitor) ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 20 with ClassWriter

use of jodd.asm5.ClassWriter in project component-runtime by Talend.

the class PluginGenerator method createChainPlugin.

public File createChainPlugin(final File dir, final String plugin, final int maxBatchSize) {
    final File target = new File(dir, plugin);
    try (final JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(target))) {
        final String packageName = toPackage(target.getParentFile().getParentFile().getName()).replace(".", "/");
        final String sourcePackage = "org/talend/test";
        final String fromPack = sourcePackage.replace('/', '.');
        final String toPack = packageName.replace('.', '/');
        final File root = new File(jarLocation(getClass()), sourcePackage);
        ofNullable(root.listFiles()).map(Stream::of).orElseGet(Stream::empty).filter(c -> c.getName().endsWith(".class")).forEach(clazz -> {
            try (final InputStream is = new FileInputStream(clazz)) {
                final ClassReader reader = new ClassReader(is);
                final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
                reader.accept(new ClassRemapper(writer, new Remapper() {

                    @Override
                    public String map(final String key) {
                        return key.replace(sourcePackage, toPack).replace(fromPack, packageName);
                    }
                }), EXPAND_FRAMES);
                outputStream.putNextEntry(new JarEntry(toPack + '/' + clazz.getName()));
                outputStream.write(writer.toByteArray());
                outputStream.closeEntry();
            } catch (final IOException e) {
                fail(e.getMessage());
            }
        });
        outputStream.putNextEntry(new JarEntry("TALEND-INF/local-configuration.properties"));
        outputStream.write(("_maxBatchSize.value=" + maxBatchSize).getBytes(StandardCharsets.UTF_8));
        outputStream.closeEntry();
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    return target;
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Action(org.talend.sdk.component.api.service.Action) V1_8(org.apache.xbean.asm9.Opcodes.V1_8) COMPUTE_FRAMES(org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES) EXPAND_FRAMES(org.apache.xbean.asm9.ClassReader.EXPAND_FRAMES) JarEntry(java.util.jar.JarEntry) Processor(org.talend.sdk.component.api.processor.Processor) DUP(org.apache.xbean.asm9.Opcodes.DUP) Type(org.apache.xbean.asm9.Type) ClassReader(org.apache.xbean.asm9.ClassReader) MethodVisitor(org.apache.xbean.asm9.MethodVisitor) ElementListener(org.talend.sdk.component.api.processor.ElementListener) ACC_PUBLIC(org.apache.xbean.asm9.Opcodes.ACC_PUBLIC) JarOutputStream(java.util.jar.JarOutputStream) ZipEntry(java.util.zip.ZipEntry) ACC_SUPER(org.apache.xbean.asm9.Opcodes.ACC_SUPER) RETURN(org.apache.xbean.asm9.Opcodes.RETURN) ClassWriter(org.apache.xbean.asm9.ClassWriter) Optional.ofNullable(java.util.Optional.ofNullable) AnnotationVisitor(org.apache.xbean.asm9.AnnotationVisitor) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Remapper(org.apache.xbean.asm9.commons.Remapper) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) Consumer(java.util.function.Consumer) NEW(org.apache.xbean.asm9.Opcodes.NEW) Stream(java.util.stream.Stream) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) Service(org.talend.sdk.component.api.service.Service) ALOAD(org.apache.xbean.asm9.Opcodes.ALOAD) INVOKESPECIAL(org.apache.xbean.asm9.Opcodes.INVOKESPECIAL) ClassRemapper(org.apache.xbean.asm9.commons.ClassRemapper) ARETURN(org.apache.xbean.asm9.Opcodes.ARETURN) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) ClassRemapper(org.apache.xbean.asm9.commons.ClassRemapper) FileInputStream(java.io.FileInputStream) ClassWriter(org.apache.xbean.asm9.ClassWriter) Remapper(org.apache.xbean.asm9.commons.Remapper) ClassRemapper(org.apache.xbean.asm9.commons.ClassRemapper) FileOutputStream(java.io.FileOutputStream) ClassReader(org.apache.xbean.asm9.ClassReader) JarOutputStream(java.util.jar.JarOutputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) File(java.io.File)

Aggregations

ClassWriter (org.apache.xbean.asm9.ClassWriter)19 MethodVisitor (org.apache.xbean.asm9.MethodVisitor)12 IOException (java.io.IOException)7 ClassReader (org.apache.xbean.asm9.ClassReader)7 File (java.io.File)6 InputStream (java.io.InputStream)6 JarEntry (java.util.jar.JarEntry)6 JarOutputStream (java.util.jar.JarOutputStream)6 Stream (java.util.stream.Stream)6 ZipEntry (java.util.zip.ZipEntry)6 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 Optional.ofNullable (java.util.Optional.ofNullable)5 JarLocation.jarLocation (org.apache.ziplock.JarLocation.jarLocation)5 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)4 List (java.util.List)4 AnnotationVisitor (org.apache.xbean.asm9.AnnotationVisitor)4 EXPAND_FRAMES (org.apache.xbean.asm9.ClassReader.EXPAND_FRAMES)4 COMPUTE_FRAMES (org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES)4