List of usage examples for org.objectweb.asm MethodVisitor visitMaxs
public void visitMaxs(final int maxStack, final int maxLocals)
From source file:jvstm.atomic.ProcessParNestAnnotations.java
License:Open Source License
private static void generateCallable(File classFile, String className, String callableClass, MethodNode mn, boolean readOnly, boolean unsafe) { Type returnType = Type.getReturnType(mn.desc); List<Type> arguments = new ArrayList<Type>(Arrays.asList(Type.getArgumentTypes(mn.desc))); if (!isStatic(mn)) arguments.add(0, Type.getObjectType(className)); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(V1_6, ACC_FINAL, callableClass, unsafe ? "Ljvstm/UnsafeParallelTask<" : "Ljvstm/ParallelTask<" + (isPrimitive(returnType) ? toObject(returnType) : (returnType.equals(Type.VOID_TYPE) ? Type.getObjectType("java/lang/Void") : returnType)) .getDescriptor() + ">;", unsafe ? "jvstm/UnsafeParallelTask" : "jvstm/ParallelTask", new String[] {}); cw.visitSource("JVSTM Generated Wrapper Class", null); // Create fields to hold arguments {/*from w w w .ja va 2 s .c om*/ int fieldPos = 0; for (Type t : arguments) { cw.visitField(ACC_PRIVATE | ACC_FINAL, "arg" + (fieldPos++), t.getDescriptor(), null, null); } } // Create constructor { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", getCallableCtorDesc(className, mn), null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, unsafe ? "jvstm/UnsafeParallelTask" : "jvstm/ParallelTask", "<init>", "()V"); int localsPos = 0; int fieldPos = 0; for (Type t : arguments) { mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(t.getOpcode(ILOAD), localsPos + 1); mv.visitFieldInsn(PUTFIELD, callableClass, "arg" + fieldPos++, t.getDescriptor()); localsPos += t.getSize(); } mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // Create execute method { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "execute", "()Ljava/lang/Object;", null, null); mv.visitCode(); int fieldPos = 0; for (Type t : arguments) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, callableClass, "arg" + fieldPos++, t.getDescriptor()); } mv.visitMethodInsn(isStatic(mn) ? INVOKESTATIC : INVOKEVIRTUAL, className, mn.name, mn.desc); if (returnType.equals(Type.VOID_TYPE)) mv.visitInsn(ACONST_NULL); else if (isPrimitive(returnType)) boxWrap(returnType, mv); mv.visitInsn(ARETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // Create the readOnly method { if (readOnly) { MethodVisitor mv = cw.visitMethod(ACC_PROTECTED, "isReadOnly", "()Z", null, null); mv.visitCode(); mv.visitInsn(ICONST_0); mv.visitInsn(IRETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } } /* protected boolean isReadOnly() { return false; }*/ // Write the callable class file in the same directory as the original // class file String callableFileName = callableClass.substring(Math.max(callableClass.lastIndexOf('/'), 0)) + ".class"; writeClassFile(new File((classFile.getParent() == null ? "" : classFile.getParent() + File.separatorChar) + callableFileName), cw.toByteArray()); }
From source file:kilim.analysis.ClassWeaver.java
License:Open Source License
/** * Create a custom class (structure) to hold the state. The name of the * state reflects the numbers of the various VMtypes in valInfoList. class * kilim.SO2IJ3 reflects a class that stores two Objects one Integer and 3 * longs.//from w ww . j a v a 2s .com * * <pre> * class kilim.SO2IJ3 extends kilim.State { * public Object f1, f2; * public int f3; * public long f4, f5, f6; * } * </pre> * * If there's no data to store, we use the kilim.State class directly to * store the basic amount of data necessary to restore the stack. */ String createStateClass(ValInfoList valInfoList) { int numByType[] = { 0, 0, 0, 0, 0 }; for (ValInfo vi : valInfoList) { numByType[vi.vmt]++; } String className = makeClassName(numByType); if (stateClasses.get().contains(className)) { return className; } stateClasses.get().add(className); ClassWriter cw = new ClassWriter(0); cw.visit(V1_1, ACC_PUBLIC | ACC_FINAL, className, null, "kilim/State", null); // Create default constructor // <init>() { // super(); // call java/lang/Object.<init>() // } MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mw.visitInsn(ALOAD_0); mw.visitMethodInsn(INVOKESPECIAL, STATE_CLASS, "<init>", "()V"); mw.visitInsn(RETURN); // this code uses a maximum of one stack element and one local variable mw.visitMaxs(1, 1); mw.visitEnd(); // create fields of the appropriate type. for (ValInfo vi : valInfoList) { cw.visitField(ACC_PUBLIC, vi.fieldName, vi.fieldDesc(), null, null); } addClassInfo(new ClassInfo(className, cw.toByteArray())); return className; }
From source file:kilim.analysis.MethodFlow.java
License:Open Source License
@Override /**// w ww . j a v a 2s. co m * Copied verbatim from MethodNode except for the instruction processing. * Unlike MethodNode, we don't keep LabelNodes inline, so we need to * do visitLabel ourselves. * * @param mv a method visitor. */ public void accept(final MethodVisitor mv) { // visits the method attributes int i, j, n; if (annotationDefault != null) { AnnotationVisitor av = mv.visitAnnotationDefault(); acceptAnnotation(av, null, annotationDefault); av.visitEnd(); } n = visibleAnnotations == null ? 0 : visibleAnnotations.size(); for (i = 0; i < n; ++i) { AnnotationNode an = (AnnotationNode) visibleAnnotations.get(i); an.accept(mv.visitAnnotation(an.desc, true)); } n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size(); for (i = 0; i < n; ++i) { AnnotationNode an = (AnnotationNode) invisibleAnnotations.get(i); an.accept(mv.visitAnnotation(an.desc, false)); } n = visibleParameterAnnotations == null ? 0 : visibleParameterAnnotations.length; for (i = 0; i < n; ++i) { List<?> l = visibleParameterAnnotations[i]; if (l == null) { continue; } for (j = 0; j < l.size(); ++j) { AnnotationNode an = (AnnotationNode) l.get(j); an.accept(mv.visitParameterAnnotation(i, an.desc, true)); } } n = invisibleParameterAnnotations == null ? 0 : invisibleParameterAnnotations.length; for (i = 0; i < n; ++i) { List<?> l = invisibleParameterAnnotations[i]; if (l == null) { continue; } for (j = 0; j < l.size(); ++j) { AnnotationNode an = (AnnotationNode) l.get(j); an.accept(mv.visitParameterAnnotation(i, an.desc, false)); } } n = attrs == null ? 0 : attrs.size(); for (i = 0; i < n; ++i) { mv.visitAttribute((Attribute) attrs.get(i)); } // visits the method's code if (instructions.size() > 0) { mv.visitCode(); // visits try catch blocks for (i = 0; i < tryCatchBlocks.size(); ++i) { ((TryCatchBlockNode) tryCatchBlocks.get(i)).accept(mv); } // visits instructions for (i = 0; i < instructions.size(); ++i) { Label l = getLabelAt(i); if (l != null) { mv.visitLabel(l); } ((AbstractInsnNode) instructions.get(i)).accept(mv); } Label l = getLabelAt(instructions.size()); if (l != null) { mv.visitLabel(l); } // visits local variables n = localVariables == null ? 0 : localVariables.size(); for (i = 0; i < n; ++i) { ((LocalVariableNode) localVariables.get(i)).accept(mv); } // visits line numbers /* TODO this was in ASM 2.3.3 but not 3.x or 4.0, find a substitute or remove n = lineNumbers == null ? 0 : lineNumbers.size(); for (i = 0; i < n; ++i) { ((LineNumberNode) lineNumbers.get(i)).accept(mv); } */ // visits maxs mv.visitMaxs(maxStack, maxLocals); } mv.visitEnd(); }
From source file:kilim.analysis.MethodWeaver.java
License:Open Source License
private void visitCode(MethodVisitor mv) { mv.visitCode();//from w w w. j ava 2 s .c o m visitTryCatchBlocks(mv); visitInstructions(mv); // TODO visitLineNumbers(mv); visitLocals(mv); mv.visitMaxs(maxStack, maxVars); }
From source file:kilim.analysis.MethodWeaver.java
License:Open Source License
void makeNotWovenMethod(ClassVisitor cv, MethodFlow mf) { if (classWeaver.isInterface()) return;//from www.j av a2s . c om // Turn of abstract modifier int access = mf.access; access &= ~Constants.ACC_ABSTRACT; MethodVisitor mv = cv.visitMethod(access, mf.name, mf.desc, mf.signature, ClassWeaver.toStringArray(mf.exceptions)); mv.visitCode(); visitAttrs(mv); mv.visitMethodInsn(INVOKESTATIC, TASK_CLASS, "errNotWoven", "()V"); String rdesc = TypeDesc.getReturnTypeDesc(mf.desc); // stack size depends on return type, because we want to load // a constant of the appropriate size on the stack for // the corresponding xreturn instruction. int stacksize = 0; if (rdesc != D_VOID) { // ICONST_0; IRETURN or ACONST_NULL; ARETURN etc. stacksize = TypeDesc.isDoubleWord(rdesc) ? 2 : 1; int vmt = VMType.toVmType(rdesc); mv.visitInsn(VMType.constInsn[vmt]); mv.visitInsn(VMType.retInsn[vmt]); } else { mv.visitInsn(RETURN); } int numlocals; if ((mf.access & Constants.ACC_ABSTRACT) != 0) { // The abstract method doesn't contain the number of locals required to hold the // args, so we need to calculate it. numlocals = getNumWordsInSig() + 1 /* fiber */; if (!mf.isStatic()) numlocals++; } else { numlocals = mf.maxLocals + 1; } mv.visitMaxs(stacksize, numlocals); mv.visitEnd(); }
From source file:kilim.analysis.SAMweaver.java
License:Open Source License
/** * Generate a method like this:/*from w w w . j a v a 2 s .com*/ * * <pre> * private static $shim$1 (I callee, ...args..., f fiber) { * load each arg * call interface.method * f.setCallee(arg0) * xret * } * </pre> * * @param cv */ public void accept(ClassVisitor cv) { String shimDesc = getShimDesc(); MethodVisitor mv = cv.visitMethod(ACC_STATIC | ACC_PRIVATE, getShimMethodName(), shimDesc, null, getExceptions()); // load arguments int ivar = 0; for (String argType : TypeDesc.getArgumentTypes(shimDesc)) { int vmt = VMType.toVmType(argType); mv.visitVarInsn(VMType.loadInsn[vmt], ivar); ivar += VMType.category[vmt]; // register width = 2 if double or // long, 1 otherwise } int fiberVar = ivar - 1; // invoke interface String fiberDesc = desc.replace(")", Constants.D_FIBER + ")"); /* * Fixed java.lang.IncompatibleClassChangeError * Show in testcase kilim.test.TestAbstractExtends */ if (itf) { mv.visitMethodInsn(INVOKEINTERFACE, interfaceName, methodName, fiberDesc, true); } else { mv.visitMethodInsn(INVOKEVIRTUAL, interfaceName, methodName, fiberDesc, false); } // store callee object reference in fiber mv.visitVarInsn(ALOAD, fiberVar); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, FIBER_CLASS, "setCallee", "(" + D_OBJECT + ")V", false); // return .. RETURN (if void) or ARETURN, IRETURN, etc. String retDesc = TypeDesc.getReturnTypeDesc(shimDesc); if (retDesc.charAt(0) == 'V') { mv.visitInsn(RETURN); } else { int vmt = VMType.toVmType(retDesc); mv.visitInsn(VMType.retInsn[vmt]); } mv.visitMaxs(0, 0); // maxLocals and maxStackDepth will be computed by asm's MethodWriter mv.visitEnd(); }
From source file:kr.debop4j.core.reflect.ConstructorAccess.java
License:Apache License
/** ?? . */ static private void insertConstructor(ClassWriter cw) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode();// w w w . ja v a 2 s. com mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, ReflectConsts.CONSTRUCTOR_ACCESS_PATH, "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); }
From source file:kr.debop4j.core.reflect.ConstructorAccess.java
License:Apache License
/** ? . */ static void insertNewInstance(ClassWriter cw, String classNameInternal) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "()Ljava/lang/Object;", null, null); mv.visitCode();/*from w w w. ja va2 s . c o m*/ mv.visitTypeInsn(NEW, classNameInternal); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>", "()V"); mv.visitInsn(ARETURN); mv.visitMaxs(2, 1); mv.visitEnd(); }
From source file:kr.debop4j.core.reflect.ConstructorAccess.java
License:Apache License
/** inner ? */ static void insertNewInstanceInner(ClassWriter cw, String classNameInternal, String enclosingClassNameInternal) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "(Ljava/lang/Object;)Ljava/lang/Object;", null, null);/*from w w w . j a va 2s. c om*/ mv.visitCode(); if (enclosingClassNameInternal != null) { mv.visitTypeInsn(NEW, classNameInternal); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, enclosingClassNameInternal); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;"); mv.visitInsn(POP); mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>", "(L" + enclosingClassNameInternal + ";)V"); mv.visitInsn(ARETURN); mv.visitMaxs(4, 2); } else { mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException"); mv.visitInsn(DUP); mv.visitLdcInsn("Not an inner class."); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(ATHROW); mv.visitMaxs(3, 2); } mv.visitEnd(); }
From source file:kr.debop4j.core.reflect.FieldAccess.java
License:Apache License
static private void insertConstructor(ClassWriter cw) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode();/*from ww w .j a v a2 s .c o m*/ mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, ReflectConsts.FIELD_ACCESS_PATH, "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); }