use of org.apache.xbean.asm9.ClassWriter in project component-runtime by Talend.
the class ProxyGenerator method generateProxy.
public Class<?> generateProxy(final ClassLoader loader, final Class<?> classToProxy, final String plugin, final String key) {
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
final String proxyClassName = fixPreservedPackages((classToProxy.getSigners() != null ? getSignedClassProxyName(classToProxy) : classToProxy.getName()) + "$$TalendServiceProxy");
final String classFileName = proxyClassName.replace('.', '/');
final String[] interfaceNames = { Type.getInternalName(Serializable.class) };
final String superClassName = Type.getInternalName(classToProxy);
cw.visit(findJavaVersion(classToProxy), ACC_PUBLIC + ACC_SUPER + ACC_SYNTHETIC, classFileName, null, superClassName, interfaceNames);
cw.visitSource(classFileName + ".java", null);
if (!Serializable.class.isAssignableFrom(classToProxy)) {
try {
classToProxy.getMethod("writeReplace");
} catch (final NoSuchMethodException e) {
createSerialisation(cw, plugin, key);
}
}
final boolean hasInterceptors = hasInterceptors(classToProxy);
if (hasInterceptors) {
cw.visitField(ACC_PRIVATE, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class), null, null).visitEnd();
cw.visitField(ACC_PRIVATE | ACC_STATIC, FIELD_INTERCEPTED_METHODS, Type.getDescriptor(Method[].class), null, null).visitEnd();
}
createConstructor(cw, classToProxy, superClassName, classFileName, Stream.of(classToProxy.getDeclaredConstructors()).filter(c -> {
final int modifiers = c.getModifiers();
return Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers);
}).sorted((o1, o2) -> {
// prefer public constructor and then the smallest parameter count
final int mod1 = o1.getModifiers();
final int mod2 = o2.getModifiers();
if (Modifier.isProtected(mod1) && !Modifier.isPublic(mod2)) {
return 1;
}
if (Modifier.isProtected(mod2) && !Modifier.isPublic(mod1)) {
return -1;
}
return o1.getParameterCount() - o2.getParameterCount();
}).findFirst().orElseThrow(() -> new IllegalArgumentException(classToProxy + " has no default constructor, put at least a protected one")), hasInterceptors);
final Method[] interceptedMethods;
if (hasInterceptors) {
final Collection<Annotation> globalInterceptors = Stream.of(classToProxy.getAnnotations()).filter(this::isInterceptor).collect(toList());
final AtomicInteger methodIndex = new AtomicInteger();
interceptedMethods = Stream.of(classToProxy.getMethods()).filter(m -> !"<init>".equals(m.getName()) && (!globalInterceptors.isEmpty() || Stream.of(m.getAnnotations()).anyMatch(this::isInterceptor))).peek(method -> delegateMethod(cw, method, classFileName, methodIndex.getAndIncrement())).toArray(Method[]::new);
} else {
interceptedMethods = null;
}
final Class<Object> objectClass = Unsafes.defineAndLoadClass(loader, proxyClassName, cw.toByteArray());
if (hasInterceptors) {
try {
final Field interceptedMethodsField = objectClass.getDeclaredField(FIELD_INTERCEPTED_METHODS);
interceptedMethodsField.setAccessible(true);
interceptedMethodsField.set(null, interceptedMethods);
} catch (final Exception e) {
throw new IllegalStateException(e);
}
}
return objectClass;
}
use of org.apache.xbean.asm9.ClassWriter 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 in project tomee by apache.
the class ValidationGenerator method generate.
public byte[] generate() throws ProxyGenerationException {
generatedMethods.clear();
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
final String generatedClassName = getName().replace('.', '/');
cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, generatedClassName, null, "java/lang/Object", null);
{
// public constructor
final MethodVisitor 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();
}
generateMethods(cw);
/**
* Read all parent classes and copy the public methods we need
* into our new class.
*/
Class current = clazz;
while (current != null && !current.equals(Object.class)) {
try {
final ClassReader classReader = new ClassReader(DynamicSubclass.readClassFile(current));
classReader.accept(new CopyMethodAnnotations(), ClassReader.SKIP_CODE);
} catch (final IOException e) {
throw new ProxyGenerationException(e);
}
current = current.getSuperclass();
}
return cw.toByteArray();
}
use of org.apache.xbean.asm9.ClassWriter in project tomee by apache.
the class ServiceClasspathTest method subclass.
public static File subclass(final Class<?> parent, final String subclassName) throws Exception {
final String subclassNameInternal = subclassName.replace('.', '/');
final byte[] bytes;
{
final ClassWriter cw = new ClassWriter(0);
final String parentClassNameInternal = parent.getName().replace('.', '/');
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + ACC_SUPER, subclassNameInternal, null, parentClassNameInternal, null);
final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, parentClassNameInternal, "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
cw.visitEnd();
bytes = cw.toByteArray();
}
return Archive.archive().add(subclassNameInternal + ".class", bytes).asJar();
}
use of org.apache.xbean.asm9.ClassWriter in project tomee by apache.
the class JpaTest method addNewField.
public static byte[] addNewField(final byte[] origBytes) {
final ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
final FieldAdderClassVisitor visitor = new FieldAdderClassVisitor(classWriter);
final ClassReader classReader = new ClassReader(origBytes);
classReader.accept(visitor, 0);
return classWriter.toByteArray();
}
Aggregations