use of org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES in project component-runtime by Talend.
the class PluginGenerator method createChainPlugin.
public File createChainPlugin(final File dir, final String plugin) {
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());
} catch (final IOException e) {
fail(e.getMessage());
}
});
} catch (final IOException e) {
throw new IllegalStateException(e);
}
return target;
}
use of org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES in project openwebbeans by apache.
the class AbstractMetaDataDiscoveryTest method createBean.
private void createBean(final JarOutputStream outputStream, final String resource, final Class<?> itf) throws IOException {
outputStream.putNextEntry(new JarEntry(resource));
final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
// make it count for annotated mode
writer.visitAnnotation(Type.getDescriptor(ApplicationScoped.class), true).visitEnd();
writer.visit(V1_8, ACC_PUBLIC + ACC_SUPER, resource.substring(0, resource.length() - ".class".length()), null, Type.getInternalName(Object.class), itf == null ? null : new String[] { Type.getInternalName(itf) });
writer.visitSource(resource.replace(".class", ".java"), null);
final MethodVisitor constructor = writer.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
constructor.visitCode();
constructor.visitVarInsn(ALOAD, 0);
constructor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
constructor.visitInsn(RETURN);
constructor.visitMaxs(1, 1);
constructor.visitEnd();
writer.visitEnd();
outputStream.write(writer.toByteArray());
outputStream.closeEntry();
}
use of org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES in project component-runtime by Talend.
the class PluginGenerator method createChainPlugin.
public File createChainPlugin(final File dir, final String plugin) {
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());
} catch (final IOException e) {
fail(e.getMessage());
}
});
} catch (final IOException e) {
throw new IllegalStateException(e);
}
return target;
}
use of org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES in project component-runtime by Talend.
the class RepositoryModelBuilderTest method createChainPlugin.
private File createChainPlugin(final File dir, final String plugin) {
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());
} catch (final IOException e) {
fail(e.getMessage());
}
});
} catch (final IOException e) {
throw new IllegalStateException(e);
}
return target;
}
use of org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES in project component-runtime by Talend.
the class GenericComponentExtensionSupportTest method run.
@Test
void run(@TempDir final Path path) throws IOException {
final Path pluginFolder = path.resolve("test-plugins_" + UUID.randomUUID().toString());
Files.createDirectories(pluginFolder);
final Path target = pluginFolder.resolve("test-compo.jar");
try (final JarOutputStream outputStream = new JarOutputStream(Files.newOutputStream(target))) {
final String packageName = "org.talend.generated.test." + getClass().getSimpleName();
final String sourcePackage = "org/talend/test/generic";
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("META-INF/services/" + GenericComponentExtension.class.getName()));
outputStream.write((packageName + '.' + MyGenericImpl.class.getSimpleName()).getBytes(StandardCharsets.UTF_8));
outputStream.closeEntry();
} catch (final IOException e) {
throw new IllegalStateException(e);
}
try (final ComponentManager manager = new ComponentManager(new File("target/test-dependencies"), "META-INF/test/dependencies", null)) {
manager.addPlugin(target.toAbsolutePath().toString());
final Mapper mapper = manager.findMapper("my-generic", "the-first", 1, singletonMap("a", "a->1")).orElseThrow(IllegalStateException::new);
// we bypass a bit the lifecycle cause we know the test component we
final Input input = mapper.create();
// use
final Record next = Record.class.cast(input.next());
assertEquals("my-generic", next.getString("plugin"));
assertEquals("the-first", next.getString("name"));
assertEquals("a", next.getString("key"));
assertEquals("a->1", next.getString("value"));
assertNull(input.next());
}
}
Aggregations