use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class DgmConverter method createInvokeMethod.
private static void createInvokeMethod(CachedMethod method, ClassWriter cw, Class returnType, String methodDescriptor) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 1);
BytecodeHelper.doCast(mv, method.getParameterTypes()[0].getTheClass());
loadParameters(method, 2, mv);
mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(method.getDeclaringClass().getTheClass()), method.getName(), methodDescriptor, false);
BytecodeHelper.box(mv, returnType);
if (method.getReturnType() == void.class) {
mv.visitInsn(ACONST_NULL);
}
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class DgmConverter method createConstructor.
private static void createConstructor(ClassWriter cw) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/String;Lorg/codehaus/groovy/reflection/CachedClass;Ljava/lang/Class;[Ljava/lang/Class;)V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 2);
mv.visitVarInsn(ALOAD, 3);
mv.visitVarInsn(ALOAD, 4);
mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/reflection/GeneratedMetaMethod", "<init>", "(Ljava/lang/String;Lorg/codehaus/groovy/reflection/CachedClass;Ljava/lang/Class;[Ljava/lang/Class;)V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class DgmConverter method createDoMethodInvokeMethod.
private static void createDoMethodInvokeMethod(CachedMethod method, ClassWriter cw, String className, Class returnType, String methodDescriptor) {
MethodVisitor mv;
mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "doMethodInvoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", null, null);
mv.visitCode();
if (method.getParamsCount() == 2 && method.getParameterTypes()[0].isNumber && method.getParameterTypes()[1].isNumber) {
mv.visitVarInsn(ALOAD, 1);
BytecodeHelper.doCast(mv, method.getParameterTypes()[0].getTheClass());
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, className, "getParameterTypes", "()[Lorg/codehaus/groovy/reflection/CachedClass;", false);
mv.visitInsn(ICONST_0);
mv.visitInsn(AALOAD);
mv.visitVarInsn(ALOAD, 2);
mv.visitInsn(ICONST_0);
mv.visitInsn(AALOAD);
mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/CachedClass", "coerceArgument", "(Ljava/lang/Object;)Ljava/lang/Object;", false);
// cast argument to parameter class, inclusive unboxing
// for methods with primitive types
Class type = method.getParameterTypes()[1].getTheClass();
BytecodeHelper.doCast(mv, type);
} else {
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, className, "coerceArgumentsToClasses", "([Ljava/lang/Object;)[Ljava/lang/Object;", false);
mv.visitVarInsn(ASTORE, 2);
mv.visitVarInsn(ALOAD, 1);
BytecodeHelper.doCast(mv, method.getParameterTypes()[0].getTheClass());
loadParameters(method, 2, mv);
}
mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(method.getDeclaringClass().getTheClass()), method.getName(), methodDescriptor, false);
BytecodeHelper.box(mv, returnType);
if (method.getReturnType() == void.class) {
mv.visitInsn(ACONST_NULL);
}
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class AsmClassGenerator method visitGStringExpression.
public void visitGStringExpression(GStringExpression expression) {
MethodVisitor mv = controller.getMethodVisitor();
mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/GStringImpl");
mv.visitInsn(DUP);
int size = expression.getValues().size();
BytecodeHelper.pushConstant(mv, size);
mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
for (int i = 0; i < size; i++) {
mv.visitInsn(DUP);
BytecodeHelper.pushConstant(mv, i);
expression.getValue(i).visit(this);
controller.getOperandStack().box();
mv.visitInsn(AASTORE);
}
controller.getOperandStack().remove(size);
List strings = expression.getStrings();
size = strings.size();
BytecodeHelper.pushConstant(mv, size);
mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
for (int i = 0; i < size; i++) {
mv.visitInsn(DUP);
BytecodeHelper.pushConstant(mv, i);
controller.getOperandStack().pushConstant((ConstantExpression) strings.get(i));
controller.getOperandStack().box();
mv.visitInsn(AASTORE);
}
controller.getOperandStack().remove(size);
mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/GStringImpl", "<init>", "([Ljava/lang/Object;[Ljava/lang/String;)V", false);
controller.getOperandStack().push(ClassHelper.GSTRING_TYPE);
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class CompareIdentityExpression method visit.
@Override
public void visit(final GroovyCodeVisitor visitor) {
if (visitor instanceof AsmClassGenerator) {
AsmClassGenerator acg = (AsmClassGenerator) visitor;
WriterController controller = acg.getController();
controller.getTypeChooser().resolveType(leftExpression, controller.getClassNode());
controller.getTypeChooser().resolveType(rightExpression, controller.getClassNode());
MethodVisitor mv = controller.getMethodVisitor();
leftExpression.visit(acg);
controller.getOperandStack().box();
rightExpression.visit(acg);
controller.getOperandStack().box();
Label l1 = new Label();
mv.visitJumpInsn(IF_ACMPNE, l1);
mv.visitInsn(ICONST_1);
Label l2 = new Label();
mv.visitJumpInsn(GOTO, l2);
mv.visitLabel(l1);
mv.visitInsn(ICONST_0);
mv.visitLabel(l2);
controller.getOperandStack().replace(ClassHelper.boolean_TYPE, 2);
} else {
super.visit(visitor);
}
}
Aggregations