use of org.apache.xbean.asm6.MethodVisitor 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.asm6.MethodVisitor 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.asm6.MethodVisitor 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.asm6.MethodVisitor 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('.', '/'));
}
use of org.apache.xbean.asm6.MethodVisitor in project jodd by oblac.
the class ProxettaClassBuilder method makeStaticInitBlock.
/**
* Creates static initialization block that simply calls all
* advice static init methods in correct order.
*/
protected void makeStaticInitBlock() {
if (wd.adviceClinits != null) {
MethodVisitor mv = wd.dest.visitMethod(AsmUtil.ACC_STATIC, CLINIT, DESC_VOID, null, null);
mv.visitCode();
for (String name : wd.adviceClinits) {
mv.visitMethodInsn(INVOKESTATIC, wd.thisReference, name, DESC_VOID, false);
}
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
}
Aggregations