use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class AsmClassGenerator method storeThisInstanceField.
private void storeThisInstanceField(FieldExpression expression) {
MethodVisitor mv = controller.getMethodVisitor();
FieldNode field = expression.getField();
boolean setReferenceFromReference = field.isHolder() && expression.isUseReferenceDirectly();
String ownerName = (field.getOwner().equals(controller.getClassNode())) ? controller.getInternalClassName() : BytecodeHelper.getClassInternalName(field.getOwner());
OperandStack operandStack = controller.getOperandStack();
if (setReferenceFromReference) {
// rhs is ready to use reference, just put it in the field
mv.visitVarInsn(ALOAD, 0);
operandStack.push(controller.getClassNode());
operandStack.swap();
mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
} else if (field.isHolder()) {
// rhs is normal value, set the value in the Reference
operandStack.doGroovyCast(field.getOriginType());
operandStack.box();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, ownerName, expression.getFieldName(), BytecodeHelper.getTypeDescription(field.getType()));
mv.visitInsn(SWAP);
mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
} else {
// rhs is normal value, set normal value
operandStack.doGroovyCast(field.getOriginType());
mv.visitVarInsn(ALOAD, 0);
operandStack.push(controller.getClassNode());
operandStack.swap();
mv.visitFieldInsn(PUTFIELD, ownerName, field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
}
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class AsmClassGenerator method onLineNumber.
public void onLineNumber(ASTNode statement, String message) {
MethodVisitor mv = controller.getMethodVisitor();
if (statement == null)
return;
int line = statement.getLineNumber();
this.currentASTNode = statement;
if (line < 0)
return;
if (!ASM_DEBUG && line == controller.getLineNumber())
return;
controller.setLineNumber(line);
if (mv != null) {
Label l = new Label();
mv.visitLabel(l);
mv.visitLineNumber(line, l);
}
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class AsmClassGenerator method visitClassExpression.
/**
* load class object on stack
*/
public void visitClassExpression(ClassExpression expression) {
ClassNode type = expression.getType();
MethodVisitor mv = controller.getMethodVisitor();
if (BytecodeHelper.isClassLiteralPossible(type) || BytecodeHelper.isSameCompilationUnit(controller.getClassNode(), type)) {
if (controller.getClassNode().isInterface()) {
InterfaceHelperClassNode interfaceClassLoadingClass = controller.getInterfaceClassLoadingClass();
if (BytecodeHelper.isClassLiteralPossible(interfaceClassLoadingClass)) {
BytecodeHelper.visitClassLiteral(mv, interfaceClassLoadingClass);
controller.getOperandStack().push(ClassHelper.CLASS_Type);
return;
}
} else {
BytecodeHelper.visitClassLiteral(mv, type);
controller.getOperandStack().push(ClassHelper.CLASS_Type);
return;
}
}
String staticFieldName = getStaticFieldName(type);
referencedClasses.put(staticFieldName, type);
String internalClassName = controller.getInternalClassName();
if (controller.getClassNode().isInterface()) {
internalClassName = BytecodeHelper.getClassInternalName(controller.getInterfaceClassLoadingClass());
mv.visitFieldInsn(GETSTATIC, internalClassName, staticFieldName, "Ljava/lang/Class;");
} else {
mv.visitMethodInsn(INVOKESTATIC, internalClassName, "$get$" + staticFieldName, "()Ljava/lang/Class;", false);
}
controller.getOperandStack().push(ClassHelper.CLASS_Type);
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class AsmClassGenerator method visitConstructorOrMethod.
protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
controller.resetLineNumber();
Parameter[] parameters = node.getParameters();
String methodType = BytecodeHelper.getMethodDescriptor(node.getReturnType(), parameters);
String signature = BytecodeHelper.getGenericsMethodSignature(node);
int modifiers = node.getModifiers();
if (isVargs(node.getParameters()))
modifiers |= Opcodes.ACC_VARARGS;
MethodVisitor mv = cv.visitMethod(modifiers, node.getName(), methodType, signature, buildExceptions(node.getExceptions()));
controller.setMethodVisitor(mv);
visitAnnotations(node, mv);
for (int i = 0; i < parameters.length; i++) {
visitParameterAnnotations(parameters[i], i, mv);
}
// Add parameter names to the MethodVisitor (jdk8+ only)
if (getCompileUnit().getConfig().getParameters()) {
for (int i = 0; i < parameters.length; i++) {
// TODO handle ACC_SYNTHETIC for enum method parameters?
mv.visitParameter(parameters[i].getName(), 0);
}
}
if (controller.getClassNode().isAnnotationDefinition() && !node.isStaticConstructor()) {
visitAnnotationDefault(node, mv);
} else if (!node.isAbstract()) {
Statement code = node.getCode();
mv.visitCode();
// fast path for getter/setters etc.
if (code instanceof BytecodeSequence && ((BytecodeSequence) code).getInstructions().size() == 1 && ((BytecodeSequence) code).getInstructions().get(0) instanceof BytecodeInstruction) {
((BytecodeInstruction) ((BytecodeSequence) code).getInstructions().get(0)).visit(mv);
} else {
visitStdMethod(node, isConstructor, parameters, code);
}
//mv.visitInsn(NOP);
try {
mv.visitMaxs(0, 0);
} catch (Exception e) {
throw new GroovyRuntimeException("ASM reporting processing error for " + controller.getClassNode() + "#" + node.getName() + " with signature " + node.getTypeDescriptor() + " in " + sourceFile + ":" + node.getLineNumber(), e);
}
}
mv.visitEnd();
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class AsmClassGenerator method storeStaticField.
private void storeStaticField(FieldExpression expression) {
MethodVisitor mv = controller.getMethodVisitor();
FieldNode field = expression.getField();
boolean holder = field.isHolder() && !controller.isInClosureConstructor();
controller.getOperandStack().doGroovyCast(field);
String ownerName = (field.getOwner().equals(controller.getClassNode())) ? controller.getInternalClassName() : BytecodeHelper.getClassInternalName(field.getOwner());
if (holder) {
controller.getOperandStack().box();
mv.visitFieldInsn(GETSTATIC, ownerName, expression.getFieldName(), BytecodeHelper.getTypeDescription(field.getType()));
mv.visitInsn(SWAP);
mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
} else {
mv.visitFieldInsn(PUTSTATIC, ownerName, expression.getFieldName(), BytecodeHelper.getTypeDescription(field.getType()));
}
controller.getOperandStack().remove(1);
}
Aggregations