use of org.apache.xbean.asm5.ClassWriter in project jodd by oblac.
the class ProxettaBuilder method process.
/**
* Reads the target and creates destination class.
*/
protected void process() {
if (targetInputStream == null) {
throw new ProxettaException("Target missing");
}
// create class reader
ClassReader classReader;
try {
classReader = new ClassReader(targetInputStream);
} catch (IOException ioex) {
throw new ProxettaException("Error reading class input stream", ioex);
}
// reads information
TargetClassInfoReader targetClassInfoReader = new TargetClassInfoReader(proxetta.getClassLoader());
classReader.accept(targetClassInfoReader, 0);
this.destClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
// create proxy
if (log.isDebugEnabled()) {
log.debug("processing: " + classReader.getClassName());
}
WorkData wd = process(classReader, targetClassInfoReader);
// store important data
proxyApplied = wd.proxyApplied;
proxyClassName = wd.thisReference.replace('/', '.');
}
use of org.apache.xbean.asm5.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<String, List<Method>>();
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();
}
use of org.apache.xbean.asm5.ClassWriter in project tomee by apache.
the class DynamicSubclass method generateBytes.
private static byte[] generateBytes(final Class<?> classToProxy) 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())) {
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();
}
use of org.apache.xbean.asm5.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;
}
use of org.apache.xbean.asm5.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();
}
Aggregations