List of usage examples for org.objectweb.asm MethodVisitor visitLabel
public void visitLabel(final Label label)
From source file:org.codehaus.groovy.classgen.Verifier.java
License:Apache License
protected void addGroovyObjectInterfaceAndMethods(ClassNode node, final String classInternalName) { if (!node.isDerivedFromGroovyObject()) node.addInterface(ClassHelper.make(GroovyObject.class)); FieldNode metaClassField = getMetaClassField(node); boolean shouldAnnotate = classNode.getModule().getContext() != null; AnnotationNode generatedAnnotation = shouldAnnotate ? new AnnotationNode(ClassHelper.make(GENERATED_ANNOTATION)) : null;// ww w .ja v a 2 s . c o m AnnotationNode internalAnnotation = shouldAnnotate ? new AnnotationNode(ClassHelper.make(INTERNAL_ANNOTATION)) : null; if (!node.hasMethod("getMetaClass", Parameter.EMPTY_ARRAY)) { metaClassField = setMetaClassFieldIfNotExists(node, metaClassField); MethodNode methodNode = addMethod(node, !shouldAnnotate, "getMetaClass", ACC_PUBLIC, ClassHelper.METACLASS_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() { @Override public void visit(MethodVisitor mv) { Label nullLabel = new Label(); /* * the code is: * if (this.metaClass==null) { * this.metaClass = this.$getStaticMetaClass() * return this.metaClass * } else { * return this.metaClass * } * with the optimization that the result of the * first this.metaClass is duped on the operand * stack and reused for the return in the else part */ mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitInsn(DUP); mv.visitJumpInsn(IFNULL, nullLabel); mv.visitInsn(ARETURN); mv.visitLabel(nullLabel); mv.visitInsn(POP); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "$getStaticMetaClass", "()Lgroovy/lang/MetaClass;", false); mv.visitFieldInsn(PUTFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitInsn(ARETURN); } })); if (shouldAnnotate) { methodNode.addAnnotation(generatedAnnotation); methodNode.addAnnotation(internalAnnotation); } } Parameter[] parameters = new Parameter[] { new Parameter(ClassHelper.METACLASS_TYPE, "mc") }; if (!node.hasMethod("setMetaClass", parameters)) { metaClassField = setMetaClassFieldIfNotExists(node, metaClassField); Statement setMetaClassCode; if (isFinal(metaClassField.getModifiers())) { ConstantExpression text = new ConstantExpression("cannot set read-only meta class"); ConstructorCallExpression cce = new ConstructorCallExpression( ClassHelper.make(IllegalArgumentException.class), text); setMetaClassCode = new ExpressionStatement(cce); } else { List list = new ArrayList(); list.add(new BytecodeInstruction() { @Override public void visit(MethodVisitor mv) { /* * the code is (meta class is stored in 1): * this.metaClass = <1> */ mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitInsn(RETURN); } }); setMetaClassCode = new BytecodeSequence(list); } MethodNode methodNode = addMethod(node, !shouldAnnotate, "setMetaClass", ACC_PUBLIC, ClassHelper.VOID_TYPE, SET_METACLASS_PARAMS, ClassNode.EMPTY_ARRAY, setMetaClassCode); if (shouldAnnotate) { methodNode.addAnnotation(generatedAnnotation); methodNode.addAnnotation(internalAnnotation); } } }
From source file:org.codehaus.groovy.runtime.callsite.CallSiteGenerator.java
License:Apache License
private static MethodVisitor writeMethod(ClassWriter cw, String name, int argumentCount, final String superClass, CachedMethod cachedMethod, String receiverType, String parameterDescription, boolean useArray) { MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "call" + name, "(L" + receiverType + ";" + parameterDescription + ")Ljava/lang/Object;", null, null); mv.visitCode();//from w ww .j a va 2 s .co m final Label tryStart = new Label(); mv.visitLabel(tryStart); // call for checking if method is still valid for (int i = 0; i < argumentCount; ++i) mv.visitVarInsn(Opcodes.ALOAD, i); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClass, "checkCall", "(Ljava/lang/Object;" + parameterDescription + ")Z", false); Label l0 = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, l0); // valid method branch Class callClass = cachedMethod.getDeclaringClass().getTheClass(); boolean useInterface = callClass.isInterface(); String type = BytecodeHelper.getClassInternalName(callClass.getName()); String descriptor = BytecodeHelper.getMethodDescriptor(cachedMethod.getReturnType(), cachedMethod.getNativeParameterTypes()); // prepare call int invokeMethodCode = Opcodes.INVOKEVIRTUAL; if (cachedMethod.isStatic()) { invokeMethodCode = Opcodes.INVOKESTATIC; } else { mv.visitVarInsn(Opcodes.ALOAD, 1); BytecodeHelper.doCast(mv, callClass); if (useInterface) invokeMethodCode = Opcodes.INVOKEINTERFACE; } Class<?>[] parameters = cachedMethod.getPT(); int size = parameters.length; for (int i = 0; i < size; i++) { if (useArray) { // unpack argument from Object[] mv.visitVarInsn(Opcodes.ALOAD, 2); BytecodeHelper.pushConstant(mv, i); mv.visitInsn(Opcodes.AALOAD); } else { mv.visitVarInsn(Opcodes.ALOAD, i + 2); } // cast argument to parameter class, inclusive unboxing // for methods with primitive types BytecodeHelper.doCast(mv, parameters[i]); } // make call mv.visitMethodInsn(invokeMethodCode, type, cachedMethod.getName(), descriptor, useInterface); // produce result BytecodeHelper.box(mv, cachedMethod.getReturnType()); if (cachedMethod.getReturnType() == void.class) { mv.visitInsn(Opcodes.ACONST_NULL); } // return mv.visitInsn(Opcodes.ARETURN); // fall back after method change mv.visitLabel(l0); for (int i = 0; i < argumentCount; ++i) mv.visitVarInsn(Opcodes.ALOAD, i); if (!useArray) { mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ArrayUtil", "createArray", "(" + parameterDescription + ")[Ljava/lang/Object;", false); } mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/callsite/CallSiteArray", "defaultCall" + name, "(Lorg/codehaus/groovy/runtime/callsite/CallSite;L" + receiverType + ";[Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitInsn(Opcodes.ARETURN); // exception unwrapping for stackless exceptions final Label tryEnd = new Label(); mv.visitLabel(tryEnd); final Label catchStart = new Label(); mv.visitLabel(catchStart); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter", "unwrap", "(Lgroovy/lang/GroovyRuntimeException;)Ljava/lang/Throwable;", false); mv.visitInsn(Opcodes.ATHROW); mv.visitTryCatchBlock(tryStart, tryEnd, catchStart, "groovy/lang/GroovyRuntimeException"); mv.visitMaxs(0, 0); mv.visitEnd(); return mv; }
From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java
License:Apache License
/** * When an object doesn't implement the GroovyObject interface, we generate bytecode for the * {@link GroovyObject} interface methods. Otherwise, the superclass is expected to implement them. */// w ww. j a v a 2s . c o m private void createGroovyObjectSupport() { visitField(ACC_PRIVATE + ACC_TRANSIENT, "metaClass", "Lgroovy/lang/MetaClass;", null, null); // getMetaClass MethodVisitor mv; { mv = super.visitMethod(ACC_PUBLIC, "getMetaClass", "()Lgroovy/lang/MetaClass;", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;"); Label l1 = new Label(); mv.visitJumpInsn(IFNONNULL, l1); Label l2 = new Label(); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "getMetaClass", "(Ljava/lang/Class;)Lgroovy/lang/MetaClass;", false); mv.visitFieldInsn(PUTFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitLabel(l1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;"); mv.visitInsn(ARETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } // setMetaClass { mv = super.visitMethod(ACC_PUBLIC, "setMetaClass", "(Lgroovy/lang/MetaClass;)V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitMaxs(2, 2); mv.visitEnd(); } }
From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java
License:Apache License
protected MethodVisitor makeDelegateToClosureCall(final String name, final String desc, final String signature, final String[] exceptions, final int accessFlags) { MethodVisitor mv = super.visitMethod(accessFlags, name, desc, signature, exceptions); // TraceMethodVisitor tmv = new TraceMethodVisitor(mv); // mv = tmv; mv.visitCode();/* www. ja va 2 s . c o m*/ int stackSize = 0; // method body should be: // this.$delegate$closure$methodName.call(new Object[] { method arguments }) Type[] args = Type.getArgumentTypes(desc); int arrayStore = args.length + 1; BytecodeHelper.pushConstant(mv, args.length); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); // stack size = 1 stackSize = 1; int idx = 1; for (int i = 0; i < args.length; i++) { Type arg = args[i]; mv.visitInsn(DUP); // stack size = 2 BytecodeHelper.pushConstant(mv, i); // array index, stack size = 3 // primitive types must be boxed boxPrimitiveType(mv, idx, arg); idx += registerLen(arg); stackSize = Math.max(4, 3 + registerLen(arg)); mv.visitInsn(AASTORE); // store value into array } mv.visitVarInsn(ASTORE, arrayStore); // store array int arrayIndex = arrayStore; mv.visitVarInsn(ALOAD, 0); // load this mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map mv.visitLdcInsn(name); // load method name mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true); arrayStore++; mv.visitVarInsn(ASTORE, arrayStore); // if null, test if wildcard exists Label notNull = new Label(); mv.visitIntInsn(ALOAD, arrayStore); mv.visitJumpInsn(IFNONNULL, notNull); mv.visitVarInsn(ALOAD, 0); // load this mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map mv.visitLdcInsn("*"); // load wildcard mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true); mv.visitVarInsn(ASTORE, arrayStore); mv.visitLabel(notNull); mv.visitVarInsn(ALOAD, arrayStore); mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(this.getClass()), "ensureClosure", "(Ljava/lang/Object;)Lgroovy/lang/Closure;", false); mv.visitVarInsn(ALOAD, arrayIndex); // load argument array stackSize++; mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Closure", "call", "([Ljava/lang/Object;)Ljava/lang/Object;", false); // call closure unwrapResult(mv, desc); mv.visitMaxs(stackSize, arrayStore + 1); mv.visitEnd(); // System.out.println("tmv.getText() = " + tmv.getText()); return null; }
From source file:org.codehaus.groovy.tools.DgmConverter.java
License:Apache License
private static void createIsValidMethodMethod(CachedMethod method, ClassWriter cw, String className) { MethodVisitor mv; if (method.getParamsCount() == 2 && method.getParameterTypes()[0].isNumber && method.getParameterTypes()[1].isNumber) { // 1 param meta method mv = cw.visitMethod(ACC_PUBLIC, "isValidMethod", "([Ljava/lang/Class;)Z", null, null); mv.visitCode();// w w w . j ava 2s .c om mv.visitVarInsn(ALOAD, 1); Label l0 = new Label(); mv.visitJumpInsn(IFNULL, l0); 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, 1); mv.visitInsn(ICONST_0); mv.visitInsn(AALOAD); mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/CachedClass", "isAssignableFrom", "(Ljava/lang/Class;)Z", false); Label l1 = new Label(); mv.visitJumpInsn(IFEQ, l1); mv.visitLabel(l0); mv.visitInsn(ICONST_1); Label l2 = new Label(); mv.visitJumpInsn(GOTO, l2); mv.visitLabel(l1); mv.visitInsn(ICONST_0); mv.visitLabel(l2); mv.visitInsn(IRETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } }
From source file:org.codehaus.groovy.transform.sc.transformers.CompareIdentityExpression.java
License:Apache License
@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);/*from w w w .j a va2 s . c om*/ 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); } }
From source file:org.codehaus.groovy.transform.sc.transformers.CompareToNullExpression.java
License:Apache License
@Override public void visit(final GroovyCodeVisitor visitor) { if (visitor instanceof AsmClassGenerator) { AsmClassGenerator acg = (AsmClassGenerator) visitor; WriterController controller = acg.getController(); MethodVisitor mv = controller.getMethodVisitor(); objectExpression.visit(acg);/*from www. j a v a2s .c o m*/ ClassNode top = controller.getOperandStack().getTopOperand(); if (ClassHelper.isPrimitiveType(top)) { controller.getOperandStack().pop(); mv.visitInsn(equalsNull ? ICONST_0 : ICONST_1); controller.getOperandStack().push(ClassHelper.boolean_TYPE); return; } Label zero = new Label(); mv.visitJumpInsn(equalsNull ? IFNONNULL : IFNULL, zero); mv.visitInsn(ICONST_1); Label end = new Label(); mv.visitJumpInsn(GOTO, end); mv.visitLabel(zero); mv.visitInsn(ICONST_0); mv.visitLabel(end); controller.getOperandStack().replace(ClassHelper.boolean_TYPE); } else { super.visit(visitor); } }
From source file:org.diorite.inject.impl.utils.AsmUtils.java
License:Open Source License
public static int printLineNumber(MethodVisitor mv, int lineNumber) { if (lineNumber == -1) { return -1; }//from ww w . j a v a 2 s . c o m Label label = new Label(); mv.visitLabel(label); mv.visitLineNumber(lineNumber, label); return lineNumber + 1; }
From source file:org.eclipse.core.databinding.pojo.bindable.internal.asm.ClassBindable.java
License:Open Source License
/** * Add the implementation of the _bindable_getPropertyChangeSupport method * to the class. The result is a method that looks as follows: * // ww w . j a v a 2 s. c o m * private PropertyChangeSupport _bindable_getPropertyChangeSupport() { if * (_bindable_propertyChangeSupport == null) { * this._bindable_propertyChangeSupport = new PropertyChangeSupport( this); * } return _bindable_propertyChangeSupport; } */ public void addGetPropertyChangeSupport() { // private PropertyChangeSupport _bindable_getPropertyChangeSupport() MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, PCS_GETTER, "()" + PCS_SIGNATURE, null, null); mv.visitCode(); // if (_bindable_propertyChangeSupport == null) mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, PCS_FIELD, PCS_SIGNATURE); Label l0 = new Label(); mv.visitJumpInsn(IFNONNULL, l0); // this._bindable_propertyChangeSupport = new PropertyChangeSupport( // this); mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, PCS_SHORT_SIGNATURE); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, PCS_SHORT_SIGNATURE, "<init>", "(Ljava/lang/Object;)V"); mv.visitFieldInsn(PUTFIELD, className, PCS_FIELD, PCS_SIGNATURE); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); // return _bindable_propertyChangeSupport; mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, PCS_FIELD, PCS_SIGNATURE); mv.visitInsn(ARETURN); mv.visitMaxs(4, 1); }
From source file:org.eclipse.golo.compiler.JavaBytecodeGenerationGoloIrVisitor.java
License:Open Source License
private void writeAugmentationApplicationsMetaData() { /* create a metadata method that given a target class name hashcode * returns a String array containing the names of applied * augmentations//from w ww . j a va2 s . com */ List<Augmentation> applications = new ArrayList<>(this.currentModule.getAugmentations()); int applicationsSize = applications.size(); writeMetaData("augmentationApplications", applications.stream().map(Augmentation::getTarget) .map(PackageAndClass::toString).toArray(String[]::new)); Label defaultLabel = new Label(); Label[] labels = new Label[applicationsSize]; int[] keys = new int[applicationsSize]; String[][] namesArrays = new String[applicationsSize][]; // cases of the switch statement MUST be sorted applications.sort(Comparator.comparingInt(o -> o.getTarget().toString().hashCode())); int i = 0; for (Augmentation application : applications) { labels[i] = new Label(); keys[i] = application.getTarget().toString().hashCode(); namesArrays[i] = application.getNames().toArray(new String[application.getNames().size()]); i++; } MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, "$augmentationApplications", "(I)[Ljava/lang/String;", null, null); mv.visitCode(); mv.visitVarInsn(ILOAD, 0); mv.visitLookupSwitchInsn(defaultLabel, keys, labels); for (i = 0; i < applicationsSize; i++) { mv.visitLabel(labels[i]); loadInteger(mv, namesArrays[i].length); mv.visitTypeInsn(ANEWARRAY, "java/lang/String"); for (int j = 0; j < namesArrays[i].length; j++) { mv.visitInsn(DUP); loadInteger(mv, j); mv.visitLdcInsn(namesArrays[i][j]); mv.visitInsn(AASTORE); } mv.visitInsn(ARETURN); } mv.visitLabel(defaultLabel); loadInteger(mv, 0); mv.visitTypeInsn(ANEWARRAY, "java/lang/String"); mv.visitInsn(ARETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }