List of usage examples for org.objectweb.asm MethodVisitor visitLocalVariable
public void visitLocalVariable(final String name, final String descriptor, final String signature, final Label start, final Label end, final int index)
From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java
License:Open Source License
protected void generateReadOnlyPropertyMethod(PropertyInfo property) { MethodVisitor mv = cv.visitMethod(property.readAccessLevel + ACC_FINAL, property.readOnlyPropertyMethodName, "()" + property.readOnlyPropertyInterfaceSignature, property.readOnlyPropertyInterfaceSignatureGeneric != null ? "()" + property.readOnlyPropertyInterfaceSignatureGeneric : null,//from w w w . j a v a 2 s . c om null); { AnnotationVisitor av0 = mv.visitAnnotation("L" + Property.class.getName().replace('.', '/') + ";", true); av0.visit("writeable", Boolean.FALSE); av0.visit("name", property.propertyName); av0.visit("dataSignature", property.dataSignature); av0.visit("dataSignatureGeneric", property.dataSignatureGeneric != null ? property.dataSignatureGeneric : ""); av0.visit("humanReadableName", property.humanReadablePropertyName != null ? property.humanReadablePropertyName : ""); av0.visitEnd(); } mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature); Label l1 = new Label(); mv.visitJumpInsn(IFNONNULL, l1); Label l2 = new Label(); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, property.propertyDataType); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn(property.propertyName); mv.visitMethodInsn(INVOKESPECIAL, property.propertyDataType, "<init>", "(Ljava/lang/Object;Ljava/lang/String;)V"); mv.visitFieldInsn(PUTFIELD, className, property.propertyFieldName, property.propertyDataSignature); mv.visitLabel(l1); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature); mv.visitMethodInsn(INVOKEVIRTUAL, property.propertyDataType, "getReadOnlyProperty", "()" + property.readOnlyPropertyInterfaceSignature); mv.visitTypeInsn(CHECKCAST, property.readOnlyPropertyInterfaceType); mv.visitInsn(ARETURN); Label l3 = new Label(); mv.visitLabel(l3); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l3, 0); mv.visitMaxs(5, 1); mv.visitEnd(); }
From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java
License:Open Source License
protected void generateInternalGetterMethod(PropertyInfo property) { MethodVisitor mv = cv.visitMethod(ACC_PRIVATE + ACC_FINAL, property.internalGetterMethodName, "()" + property.dataSignature, property.dataSignatureGeneric != null ? "()" + property.dataSignatureGeneric : null, null); mv.visitCode();/*w w w.ja v a 2 s .c o m*/ Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, className, property.propertyMethodName, "()" + property.propertyInterfaceSignature); mv.visitMethodInsn(INVOKEVIRTUAL, property.propertyInterfaceType, "getValue", "()Ljava/lang/Object;"); mv.visitTypeInsn(CHECKCAST, property.dataType); mv.visitInsn(ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); }
From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java
License:Open Source License
protected void generateInternalSetterMethod(PropertyInfo property) { MethodVisitor mv = cv.visitMethod(ACC_PRIVATE + ACC_FINAL, property.internalSetterMethodName, "(" + property.dataSignature + ")V", property.dataSignatureGeneric != null ? "(" + property.dataSignatureGeneric + ")V" : null, null); mv.visitCode();//from w w w . j a v a2s . co m Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, className, property.propertyMethodName, "()" + property.propertyInterfaceSignature); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, property.propertyInterfaceType, "setValue", "(Ljava/lang/Object;)V"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l2, 0); mv.visitLocalVariable("value", property.dataSignature, null, l0, l2, 1); mv.visitMaxs(2, 2); mv.visitEnd(); }
From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java
License:Open Source License
protected void generateGetterMethod(PropertyInfo property) { MethodVisitor mv = cv.visitMethod(property.readAccessLevel, property.getterMethodName, "()" + property.dataSignature, property.dataSignatureGeneric != null ? "()" + property.dataSignatureGeneric : null, null); if (property.enableSerialization == true && config.serialization == Config.Serialization.JAXB) { AnnotationVisitor av0 = mv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true); av0.visitEnd();/*ww w . j ava 2 s . co m*/ } mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, className, property.internalGetterMethodName, "()" + property.dataSignature); mv.visitInsn(ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); }
From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java
License:Open Source License
protected void generateSetterMethod(PropertyInfo property) { MethodVisitor mv = cv.visitMethod(property.writeAccessLevel, property.setterMethodName, "(" + property.dataSignature + ")V", property.dataSignatureGeneric != null ? "(" + property.dataSignatureGeneric + ")V" : null, null); mv.visitCode();//from w w w. j av a2s. co m Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, className, property.internalSetterMethodName, "(" + property.dataSignature + ")V"); Label l1 = new Label(); mv.visitLabel(l1); mv.visitInsn(RETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l2, 0); mv.visitLocalVariable("value", property.dataSignature, null, l0, l2, 1); mv.visitMaxs(2, 2); mv.visitEnd(); }
From source file:de.javanarior.vo.generator.ByteCodeGenerator.java
License:Apache License
/** * Generate a implementation of a value object. * * @param valueType//w w w .jav a 2s .c om * - value object type * @param technicalType * - to which the value object is mapped * @param wrapperClass * - abstract wrapper class, correspond to the technical type * @return class name and byte code in a container */ /* CHECKSTYLE:OFF */ public static ByteCodeContainer generate(Class<?> valueType, Class<? extends Comparable<?>> technicalType, @SuppressWarnings("rawtypes") Class<? extends AbstractValue> wrapperClass) { /* CHECKSTYLE:ON */ if (!isInterface(valueType)) { throw new IllegalArgumentException("Could not generate implementation for class " + valueType.getName() + ". Please provide interface"); } ClassWriter classWriter = new ClassWriter(0); MethodVisitor methodVisitor; classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, implementationClassName(valueType), "L" + parentClassName(wrapperClass) + "<" + addTypeDiscriptor(valueType) + ">;" + addTypeDiscriptor(valueType), parentClassName(wrapperClass), implementedInterfaces(valueType)); methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR, methodDescriptor(technicalType), null, null); methodVisitor.visitCode(); Label label0 = new Label(); methodVisitor.visitLabel(label0); /* CHECKSTYLE:OFF */ methodVisitor.visitLineNumber(8, label0); /* CHECKSTYLE:ON */ methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitVarInsn(getILOADOpCode(technicalType), 1); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, parentClassName(wrapperClass), CONSTRUCTOR, methodDescriptor(technicalType), false); Label label1 = new Label(); methodVisitor.visitLabel(label1); /* CHECKSTYLE:OFF */ methodVisitor.visitLineNumber(9, label1); /* CHECKSTYLE:ON */ methodVisitor.visitInsn(Opcodes.RETURN); Label label2 = new Label(); methodVisitor.visitLabel(label2); methodVisitor.visitLocalVariable("this", addTypeSignature(implementationClassName(valueType)), null, label0, label2, 0); methodVisitor.visitLocalVariable("value", getType(technicalType), null, label0, label2, 1); int stackSize = getStackSize(Type.getDescriptor(technicalType)); methodVisitor.visitMaxs(stackSize, stackSize); methodVisitor.visitEnd(); classWriter.visitEnd(); return new ByteCodeContainer(binaryClassName(valueType), classWriter.toByteArray()); }
From source file:de.kandid.model.Emitter.java
License:Apache License
/** * Assembles a class that implements the given interface by generating the byte code. * @param interfaze the interface to implement * @return the class/*from w w w . j av a2s . co m*/ */ private static Class<? extends Emitter<?>> makeClass(Class<?> interfaze) { String nameClass = _nameEmitter + '$' + interfaze.getName().replace('.', '$'); String nameInterface = Type.getInternalName(interfaze); // ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); ClassWriter cw = new ClassWriter(0); cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, nameClass, null, _nameEmitter, new String[] { name(interfaze) }); // Generate default construcotor MethodVisitor cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); cv.visitVarInsn(ALOAD, 0); cv.visitMethodInsn(INVOKESPECIAL, _nameEmitter, "<init>", "()V"); cv.visitInsn(RETURN); cv.visitMaxs(1, 1); // Generate methods Method[] methods = interfaze.getMethods(); for (int i = 0; i < methods.length; ++i) { final Method m = methods[i]; if (m.getReturnType() != void.class) throw new IllegalArgumentException("Method " + m.toGenericString() + " must not return a value"); final String descMethod = Type.getMethodDescriptor(m); final MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED, m.getName(), descMethod, null, null); final Type[] argTypes = Type.getArgumentTypes(m); // for (int i = 0; i < _end; i += 2) // ((Listener) _listeners[i]).send(...); int localStart = 1; // give one for "this" for (Type at : argTypes) localStart += at.getSize(); Label entry = new Label(); Label exit = new Label(); mw.visitLabel(entry); // _isFiring = true; mw.visitVarInsn(ALOAD, 0); mw.visitInsn(Opcodes.ICONST_1); mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor()); // setup local variables: i, _listeners, _end mw.visitLocalVariable("i", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart); mw.visitInsn(Opcodes.ICONST_0); mw.visitIntInsn(Opcodes.ISTORE, localStart); mw.visitLocalVariable("listeners", _descObjectArray, null, entry, exit, localStart + 1); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, nameClass, "_listeners", _descObjectArray); mw.visitIntInsn(Opcodes.ASTORE, localStart + 1); mw.visitLocalVariable("end", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart + 2); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, nameClass, "_end", Type.INT_TYPE.getDescriptor()); mw.visitIntInsn(Opcodes.ISTORE, localStart + 2); final Label condition = new Label(); mw.visitJumpInsn(GOTO, condition); final Label loop = new Label(); mw.visitLabel(loop); //((Listener) _listeners[i]).doSomething() mw.visitIntInsn(Opcodes.ALOAD, localStart + 1); mw.visitIntInsn(Opcodes.ILOAD, localStart); mw.visitInsn(Opcodes.AALOAD); mw.visitTypeInsn(CHECKCAST, nameInterface); int offs = 1; // give one for "this" for (Type at : argTypes) { mw.visitVarInsn(at.getOpcode(ILOAD), offs); offs += at.getSize(); } mw.visitMethodInsn(INVOKEINTERFACE, nameInterface, m.getName(), descMethod); // i += 2 mw.visitIincInsn(localStart, 2); // if (i < end) goto loop mw.visitLabel(condition); mw.visitIntInsn(Opcodes.ILOAD, localStart); mw.visitIntInsn(Opcodes.ILOAD, localStart + 2); mw.visitJumpInsn(Opcodes.IF_ICMPLT, loop); // _isFiring = false; mw.visitVarInsn(ALOAD, 0); mw.visitInsn(Opcodes.ICONST_0); mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor()); mw.visitLabel(exit); mw.visitInsn(RETURN); mw.visitMaxs(localStart + 2, localStart + 3); mw.visitEnd(); } cw.visitEnd(); return _loader.loadClass(cw, nameClass.replace('/', '.')); }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
public byte[] visit(ClassDeclarationNode classDecl) { // prolog/*ww w .j ava 2s. c o m*/ cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); programName = classDecl.getID().getName(); fullPath = classPackage + "/" + classDecl.getID().getName(); cw.visit(V1_8, ACC_PUBLIC + ACC_STATIC, fullPath, null, "java/lang/Object", null); cw.visitSource(classDecl.getID().getName() + ".java", null); FieldVisitor fv; List<Symbol> classSymbols = new ArrayList<Symbol>(); classSymbols.addAll(SymbolTableManager.getInstance().getClassSymbolManager(programName) .getCurrentSymbolTable().getSymbols()); for (ASTNode property : classDecl.getProperties()) { classSymbols.add(((PropertyNode) property).getID().symbol); } for (Symbol symbol : classSymbols) { String type; if (symbol.getType() == DijkstraType.ARRAY) { ArraySymbol as = (ArraySymbol) symbol; if (as.getArrayType() == DijkstraType.INT) { type = "[J"; } else if (as.getArrayType() == DijkstraType.FLOAT) { type = "[D"; } else { type = "[Z"; } } else { if (symbol.getType() == DijkstraType.INT) { type = "J"; } else if (symbol.getType() == DijkstraType.FLOAT) { type = "D"; } else { type = "Z"; } } fv = cw.visitField(ACC_PUBLIC, symbol.getId(), type, null, null); fv.visitEnd(); } final StringBuilder sig = new StringBuilder(); sig.append('('); for (Symbol property : ((ClassSymbol) classDecl.getID().symbol).getPropertySymbols()) { if (property.getType() == DijkstraType.INT) { sig.append('J'); } else if (property.getType() == DijkstraType.FLOAT) { sig.append('D'); } else { sig.append('Z'); } } sig.append(")V"); mvStack.push(cw.visitMethod(ACC_PUBLIC, "<init>", sig.toString(), null, null)); MethodVisitor mv = mvStack.peek(); mv.visitCode(); final Label startLabel = new Label(); mv.visitLabel(startLabel); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); int localAddr = 1; for (Symbol param : ((ClassSymbol) classDecl.getID().symbol).getPropertySymbols()) { mv.visitVarInsn(ALOAD, 0); if (param.getType() == DijkstraType.INT) { mv.visitVarInsn(LLOAD, localAddr); mv.visitFieldInsn(PUTFIELD, fullPath, param.getId(), "J"); localAddr += 2; } else if (param.getType() == DijkstraType.FLOAT) { mv.visitVarInsn(DLOAD, localAddr); mv.visitFieldInsn(PUTFIELD, fullPath, param.getId(), "D"); localAddr += 2; } else { mv.visitVarInsn(ILOAD, localAddr); mv.visitFieldInsn(PUTFIELD, fullPath, param.getId(), "Z"); localAddr += 1; } } processingClassFields = true; inClass = true; for (ASTNode body : classDecl.getDeclarations()) { body.accept(this); } inClass = false; processingClassFields = false; mv.visitInsn(RETURN); final Label endLabel = new Label(); mv.visitLabel(endLabel); mv.visitLocalVariable("this", "L" + fullPath + ";", null, startLabel, endLabel, 0); int paramLoc = 1; for (Symbol param : ((ClassSymbol) classDecl.getID().symbol).getPropertySymbols()) { if (param.getType() == DijkstraType.INT) { mv.visitLocalVariable(param.getId(), "J", null, startLabel, endLabel, paramLoc); paramLoc += 2; } else if (param.getType() == DijkstraType.FLOAT) { mv.visitLocalVariable(param.getId(), "D", null, startLabel, endLabel, paramLoc); paramLoc += 2; } else { mv.visitLocalVariable(param.getId(), "Z", null, startLabel, endLabel, paramLoc); paramLoc += 1; } } mv.visitMaxs(0, 0); mv.visitEnd(); mvStack.pop(); inClass = true; for (ASTNode body : classDecl.getDeclarations()) { body.accept(this); } inClass = false; // Actual end of generation cw.visitEnd(); return cw.toByteArray(); }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
public byte[] visit(ProcedureDeclarationNode procDeclNode) { if (processingClassFields) { return null; }//from w ww. ja v a 2 s . com final String methodName = procDeclNode.getIDNode().getName(); final StringBuilder sig = new StringBuilder(); sig.append('('); for (IDNode param : procDeclNode.getParamList()) { if (param.getType() == DijkstraType.INT) { sig.append('J'); } else if (param.getType() == DijkstraType.FLOAT) { sig.append('D'); } else { sig.append('Z'); } } sig.append(")V"); if (inClass) { mvStack.push(cw.visitMethod(ACC_PUBLIC, methodName, sig.toString(), null, null)); } else { mvStack.push(cw.visitMethod(ACC_PUBLIC + ACC_STATIC, methodName, sig.toString(), null, null)); } final MethodVisitor mv = mvStack.peek(); JVMInfo.enterScope(inClass); // Load the parameters into the JVMInfo so it knows the proper addresses for (IDNode param : procDeclNode.getParamList()) { param.getAddress(); } mv.visitCode(); final Label startLabel = new Label(); mv.visitLabel(startLabel); procDeclNode.getCompoundNode().accept(this); mv.visitInsn(RETURN); final Label endLabel = new Label(); mv.visitLabel(endLabel); int paramLoc = 0; for (IDNode param : procDeclNode.getParamList()) { if (param.getType() == DijkstraType.INT) { mv.visitLocalVariable(param.getName(), "J", null, startLabel, endLabel, paramLoc); paramLoc += 2; } else if (param.getType() == DijkstraType.FLOAT) { mv.visitLocalVariable(param.getName(), "D", null, startLabel, endLabel, paramLoc); paramLoc += 2; } else { mv.visitLocalVariable(param.getName(), "Z", null, startLabel, endLabel, paramLoc); paramLoc += 1; } } mv.visitMaxs(0, 0); mv.visitEnd(); JVMInfo.exitScope(); mvStack.pop(); return null; }
From source file:dijkstra.gen.DijkstraCodeGenerator.java
License:Open Source License
public byte[] visit(FunctionDeclarationNode funDeclNode) { if (processingClassFields) { return null; }/*w ww .ja v a 2s .co m*/ final String methodName = funDeclNode.getIDNode().getName(); final StringBuilder sig = new StringBuilder(); sig.append('('); for (IDNode param : funDeclNode.getParamList()) { if (param.getType() == DijkstraType.INT) { sig.append('J'); } else if (param.getType() == DijkstraType.FLOAT) { sig.append('D'); } else { sig.append('Z'); } } sig.append(')'); sig.append("[Ljava/lang/Object;"); if (inClass) { mvStack.push(cw.visitMethod(ACC_PUBLIC, methodName, sig.toString(), null, null)); } else { mvStack.push(cw.visitMethod(ACC_PUBLIC + ACC_STATIC, methodName, sig.toString(), null, null)); } final MethodVisitor mv = mvStack.peek(); JVMInfo.enterScope(inClass); // Load the parameters into the JVMInfo so it knows the proper addresses for (IDNode param : funDeclNode.getParamList()) { param.getAddress(); } mv.visitIntInsn(SIPUSH, funDeclNode.getReturnTypes().size()); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); returnArrayStack.push(new ArraySymbol(null, UNDEFINED)); mv.visitVarInsn(ASTORE, JVMInfo.getAddressForSymbol(returnArrayStack.peek())); mv.visitInsn(ICONST_0); returnIndexStack.push(new Symbol(null, UNDEFINED)); mv.visitVarInsn(ISTORE, JVMInfo.getAddressForSymbol(returnIndexStack.peek())); mv.visitCode(); final Label startLabel = new Label(); mv.visitLabel(startLabel); funDeclNode.getCompoundNode().accept(this); returnIndexStack.pop(); mv.visitVarInsn(ALOAD, JVMInfo.getAddressForSymbol(returnArrayStack.pop())); mv.visitInsn(ARETURN); final Label endLabel = new Label(); mv.visitLabel(endLabel); int paramLoc = 0; for (IDNode param : funDeclNode.getParamList()) { if (param.getType() == DijkstraType.INT) { mv.visitLocalVariable(param.getName(), "J", null, startLabel, endLabel, paramLoc); paramLoc += 2; } else if (param.getType() == DijkstraType.FLOAT) { mv.visitLocalVariable(param.getName(), "D", null, startLabel, endLabel, paramLoc); paramLoc += 2; } else { mv.visitLocalVariable(param.getName(), "Z", null, startLabel, endLabel, paramLoc); paramLoc += 1; } } mv.visitMaxs(0, 0); mv.visitEnd(); JVMInfo.exitScope(); mvStack.pop(); return null; }