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:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a set JSON object property method. * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method.//from w ww .j a va 2s . c o m * @param propertyName The name of the property. * @param jsonObjectType The type of the JSON object. */ private void generateSetJsonObjectPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, String propertyName, Type jsonObjectType) { MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(propertyName); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getSetJsonObjectPropertyMethod()); methodVisitor.visitInsn(RETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitLocalVariable(propertyName, jsonObjectType, null, start, end, 1); methodVisitor.visitMaxs(3, 2); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a get composite JSON object property method. * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method.//ww w. j a v a 2 s. com * @param propertyName The name of the property. * @param jsonObjectType The type of the JSON object. * @param elementType The type of the element. */ private void generateGetCompositeJsonObjectPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, String propertyName, Type jsonObjectType, Type elementType) { MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label l0 = new Label(); Label l1 = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(propertyName); methodVisitor.visitLdcInsn(jsonObjectType); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getGetJsonObjectPropertyMethod()); methodVisitor.visitTypeInsn(CHECKCAST, jsonObjectType); methodVisitor.visitVarInsn(ASTORE, 1); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitJumpInsn(IFNULL, l0); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitLdcInsn(elementType); methodVisitor.visitMethodInsn(INVOKEINTERFACE, jsonObjectType, this.getCastCompositeJsonObjectElementMethod(jsonObjectType)); methodVisitor.visitJumpInsn(GOTO, l1); methodVisitor.visitLabel(l0); methodVisitor.visitFrame(Opcodes.F_APPEND, 1, new Object[] { jsonObjectType }, 0, null); methodVisitor.visitInsn(ACONST_NULL); methodVisitor.visitLabel(l1); methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { jsonObjectType }); methodVisitor.visitInsn(ARETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitLocalVariable("_" + propertyName, jsonObjectType, null, start, end, 1); methodVisitor.visitMaxs(3, 2); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a get adapted primitive property method. * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method./*from w ww.j a v a 2 s. c o m*/ * @param propertyName The name of the property. * @param jsonPrimitiveType The type of the JSON primitive. * @param adapterType The type of the adapter. * @param propertyType The type of the property. */ private void generateGetAdaptedPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, String propertyName, Type jsonPrimitiveType, Type adapterType, Type propertyType) { MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(propertyName); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getGetJsonPrimitivePropertyMethod(jsonPrimitiveType)); methodVisitor.visitVarInsn(ASTORE, 1); methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getType(JsonObjectFactory.class), new Method("get", Type.getType(JsonObjectFactory.class), new Type[] {})); methodVisitor.visitLdcInsn(adapterType); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, Type.getType(JsonObjectFactory.class), new Method("getJsonPropertyAdapter", Type.getType(JsonPropertyAdapter.class), new Type[] { Type.getType(Class.class) })); methodVisitor.visitTypeInsn(CHECKCAST, adapterType); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, adapterType, this.getFromJsonPropertyMethod(jsonPrimitiveType, propertyType)); methodVisitor.visitInsn(ARETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitLocalVariable("_" + propertyName, jsonPrimitiveType, null, start, end, 1); methodVisitor.visitMaxs(2, 2); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a set adapted primitive property method. * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method.//from w w w.j a va 2 s .co m * @param propertyName The name of the property. * @param jsonPrimitiveType The type of the JSON primitive. * @param adapterType The type of the adapter. * @param propertyType The type of the property. */ private void generateSetAdaptedPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, String propertyName, Type jsonPrimitiveType, Type adapterType, Type propertyType) { MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getType(JsonObjectFactory.class), new Method("get", Type.getType(JsonObjectFactory.class), new Type[] {})); methodVisitor.visitLdcInsn(adapterType); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, Type.getType(JsonObjectFactory.class), new Method("getJsonPropertyAdapter", Type.getType(JsonPropertyAdapter.class), new Type[] { Type.getType(Class.class) })); methodVisitor.visitTypeInsn(CHECKCAST, adapterType); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, adapterType, this.getToJsonPropertyMethod(jsonPrimitiveType, propertyType)); methodVisitor.visitVarInsn(ASTORE, 2); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(propertyName); methodVisitor.visitVarInsn(ALOAD, 2); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getSetJsonPrimitivePropertyMethod(jsonPrimitiveType)); methodVisitor.visitInsn(RETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitLocalVariable(propertyName, propertyType, null, start, end, 1); methodVisitor.visitLocalVariable("_" + propertyName, jsonPrimitiveType, null, start, end, 2); methodVisitor.visitMaxs(3, 3); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a get adapted object property method. * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method./*from www.ja v a 2s .c o m*/ * @param propertyName The name of the property. * @param jsonObjectType The type of the JSON object. * @param adapterType The type of the adapter. * @param propertyType The type of the property. */ private void generateGetAdaptedObjectPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, String propertyName, Type jsonObjectType, Type adapterType, Type propertyType) { MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(propertyName); methodVisitor.visitLdcInsn(jsonObjectType); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getGetJsonObjectPropertyMethod()); methodVisitor.visitTypeInsn(CHECKCAST, jsonObjectType); methodVisitor.visitVarInsn(ASTORE, 1); methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getType(JsonObjectFactory.class), new Method("get", Type.getType(JsonObjectFactory.class), new Type[] {})); methodVisitor.visitLdcInsn(adapterType); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, Type.getType(JsonObjectFactory.class), new Method("getJsonPropertyAdapter", Type.getType(JsonPropertyAdapter.class), new Type[] { Type.getType(Class.class) })); methodVisitor.visitTypeInsn(CHECKCAST, adapterType); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, adapterType, this.getFromJsonPropertyMethod(jsonObjectType, propertyType)); methodVisitor.visitInsn(ARETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitLocalVariable("_" + propertyName, jsonObjectType, null, start, end, 1); methodVisitor.visitMaxs(3, 2); methodVisitor.visitEnd(); }
From source file:org.kjots.json.object.JsonObjectGeneratorBase.java
License:Apache License
/** * Generate a set adapted object property method. * * @param classVisitor The class visitor. * @param jsonObjectImplType The type of the JSON object implementation. * @param method The method./*from w w w .ja va 2 s . c o m*/ * @param propertyName The name of the property. * @param jsonObjectType The type of the JSON object. * @param adapterType The type of the adapter. * @param propertyType The type of the property. */ private void generateSetAdaptedObjectPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method, String propertyName, Type jsonObjectType, Type adapterType, Type propertyType) { MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null); Label start = new Label(); Label end = new Label(); methodVisitor.visitCode(); methodVisitor.visitLabel(start); methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getType(JsonObjectFactory.class), new Method("get", Type.getType(JsonObjectFactory.class), new Type[] {})); methodVisitor.visitLdcInsn(adapterType); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, Type.getType(JsonObjectFactory.class), new Method("getJsonPropertyAdapter", Type.getType(JsonPropertyAdapter.class), new Type[] { Type.getType(Class.class) })); methodVisitor.visitTypeInsn(CHECKCAST, adapterType); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, adapterType, this.getToJsonPropertyMethod(jsonObjectType, propertyType)); methodVisitor.visitVarInsn(ASTORE, 2); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitLdcInsn(propertyName); methodVisitor.visitVarInsn(ALOAD, 2); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getSetJsonObjectPropertyMethod()); methodVisitor.visitInsn(RETURN); methodVisitor.visitLabel(end); methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0); methodVisitor.visitLocalVariable(propertyName, propertyType, null, start, end, 1); methodVisitor.visitLocalVariable("_" + propertyName, jsonObjectType, null, start, end, 2); methodVisitor.visitMaxs(3, 3); methodVisitor.visitEnd(); }
From source file:org.lwjglx.autostack.Transformer.java
License:Open Source License
public byte[] transform(ClassLoader loader, final String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { try {/*from www. j a v a 2 s . co m*/ if (className == null || className.startsWith("java/") || className.startsWith("sun/") || className.startsWith("jdk/internal/") || className.startsWith("org/lwjgl/")) return null; for (String pack : packages) if (!className.startsWith(pack)) return null; ClassReader cr = new ClassReader(classfileBuffer); final Map<String, Integer> stackMethods = new HashMap<String, Integer>(); // Scan all methods that need auto-stack if (debugTransform) System.out.println("[autostack] scanning methods in class: " + className.replace('/', '.')); cr.accept(new ClassVisitor(ASM7) { public MethodVisitor visitMethod(final int access, final String methodName, final String methodDesc, String signature, String[] exceptions) { if ((access & (ACC_NATIVE | ACC_ABSTRACT)) != 0) { // Don't try to analyze native or abstract methods. return null; } MethodVisitor mv = new MethodVisitor(ASM7) { boolean mark, catches, notransform, nostackparam, forcestack; public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if ("Lorg/lwjglx/autostack/NoTransform;".equals(desc)) notransform = true; else if ("Lorg/lwjglx/autostack/NoStackParam;".equals(desc)) nostackparam = true; else if ("Lorg/lwjglx/autostack/UseNewStack;".equals(desc)) forcestack = true; return null; } public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { if (opcode == INVOKESTATIC && !itf && (owner.startsWith("org/lwjgl/") && (name.equals("mallocStack") || name.equals("callocStack")) || owner.equals(MEMORYSTACK) && (name.equals("stackGet") || name.equals("stackPop") || name.equals("stackPush") || name.startsWith("stackMalloc") || name.startsWith("stackCalloc") || name.startsWith("nstackMalloc") || name.startsWith("nstackCalloc") || name.equals("stackUTF8") || name.equals("stackASCII") || name.equals("stackUTF16") || name.equals("stackFloats") || name.equals("stackInts") || name.equals("stackBytes") || name.equals("stackShorts") || name.equals("stackPointers") || name.equals("stackLongs")))) { mark = true; } } public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { catches = true; } public void visitEnd() { int flag = (access & ACC_PRIVATE) != 0 ? 8 : 0; flag |= nostackparam ? 16 : 0; if (mark || notransform || forcestack || nostackparam) { if (notransform) { flag |= 2; if (debugTransform) System.out.println("[autostack] will not transform method: " + className.replace('/', '.') + "." + methodName); } else { if (checkStack) flag |= 4; flag |= catches ? 1 : 0; if (debugTransform) System.out.println("[autostack] will transform method: " + className.replace('/', '.') + "." + methodName); } stackMethods.put(methodName + methodDesc, Integer.valueOf(flag)); } } }; return mv; } }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); if (stackMethods.isEmpty()) return null; // Now, transform all such methods if (debugTransform) System.out.println("[autostack] transforming methods in class: " + className.replace('/', '.')); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS); cr.accept(new ClassVisitor(ASM7, cw) { boolean classDefaultNewStack = defaultNewStack; boolean classNoTransform; @Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { cv.visit(version, access, name, signature, superName, interfaces); if (!checkStack) { return; } /* Generate simple synthetic "compare stack pointers and throw if not equal" method */ MethodVisitor mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, "$checkStack$", "(II)V", null, new String[] { "java/lang/AssertionError" }); { mv.visitCode(); mv.visitVarInsn(ILOAD, 0); mv.visitVarInsn(ILOAD, 1); Label l0 = new Label(); mv.visitJumpInsn(IF_ICMPEQ, l0); mv.visitTypeInsn(NEW, "java/lang/IllegalStateException"); mv.visitInsn(DUP); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitLdcInsn("Stack pointers differ: "); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ILOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false); mv.visitLdcInsn(" != "); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitVarInsn(ILOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitInsn(RETURN); mv.visitMaxs(5, 2); mv.visitEnd(); } mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, "$checkStackWithThrowable$", "(Ljava/lang/Throwable;II)Ljava/lang/Throwable;", null, null); { mv.visitCode(); mv.visitVarInsn(ILOAD, 1); mv.visitVarInsn(ILOAD, 2); Label l0 = new Label(); mv.visitJumpInsn(IF_ICMPEQ, l0); mv.visitTypeInsn(NEW, "java/lang/IllegalStateException"); mv.visitInsn(DUP); mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); mv.visitInsn(DUP); mv.visitLdcInsn("Stack pointers differ: "); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(ILOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false); mv.visitLdcInsn(" != "); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); mv.visitVarInsn(ILOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false); mv.visitInsn(ARETURN); mv.visitLabel(l0); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(ARETURN); mv.visitMaxs(5, 3); mv.visitEnd(); } } public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if ("Lorg/lwjglx/autostack/UseCallerStack;".equals(desc)) { if (debugTransform) System.out.println( "[autostack] class declares to use caller stack for all methods, unless overridden by method"); classDefaultNewStack = false; return null; } else if ("Lorg/lwjglx/autostack/UseNewStack;".equals(desc)) { if (debugTransform) System.out.println( "[autostack] class declares to use new stack for all methods, unless overridden by method"); classDefaultNewStack = true; return null; } else if ("Lorg/lwjglx/autostack/NoTransform;".equals(desc)) { if (debugTransform) System.out.println("[autostack] class declares to not transform any methods"); classNoTransform = true; return null; } return cv.visitAnnotation(desc, visible); } public MethodVisitor visitMethod(final int access, final String name, final String desc, String signature, String[] exceptions) { Integer info = stackMethods.get(name + desc); if (info == null) return super.visitMethod(access, name, desc, signature, exceptions); boolean catches = (info.intValue() & 1) == 1; final boolean notransform = classNoTransform || (info.intValue() & 2) == 2; if (debugTransform && !notransform) System.out.println( "[autostack] transform method: " + className.replace('/', '.') + "." + name); final boolean memoryStackParam = stackAsParameter && (access & ACC_PRIVATE) != 0 && (info.intValue() & 16) == 0; MethodVisitor mv; final Type[] paramTypes = Type.getArgumentTypes(desc); final boolean isStatic = (access & ACC_STATIC) != 0; final boolean isConstructor = "<init>".equals(name); if (memoryStackParam) { if (debugTransform) System.out.println( "[autostack] changing signature of method to add additional MemoryStack parameter"); // Add additional MemoryStack parameter to the method signature index of the local stays the same int paramEndIndex = desc.indexOf(')'); String beforeDesc = desc.substring(0, paramEndIndex); String afterDesc = desc.substring(paramEndIndex); mv = super.visitMethod(access | ACC_SYNTHETIC, name, beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, signature, exceptions); // Re-introduce the original method which just delegates if (debugTransform) System.out.println("[autostack] adding delegate method with original signature"); MethodVisitor omv = super.visitMethod(access, name, desc, signature, exceptions); omv.visitCode(); int param = 0; if (!isStatic) { omv.visitVarInsn(ALOAD, 0); param++; } for (int i = 0; i < paramTypes.length; i++) { omv.visitVarInsn(paramTypes[i].getOpcode(ILOAD), param); param += paramTypes[i].getSize(); } omv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, "stackGet", "()L" + MEMORYSTACK + ";", false); boolean isPrivate = (access & ACC_PRIVATE) != 0; int opcode = isStatic ? INVOKESTATIC : isPrivate ? INVOKESPECIAL : INVOKEVIRTUAL; omv.visitMethodInsn(opcode, className, name, beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, false); Type retType = Type.getReturnType(desc); omv.visitInsn(retType.getOpcode(IRETURN)); omv.visitMaxs(-1, -1); omv.visitEnd(); } else { mv = super.visitMethod(access, name, desc, signature, exceptions); } if (catches) mv = new TryCatchBlockSorter(mv, access, name, desc, signature, exceptions); mv = new MethodVisitor(ASM7, mv) { Label tryLabel = new Label(); Label finallyLabel = new Label(); int lastLine = 0; boolean newStack = classDefaultNewStack; int stackVarIndex; int stackPointerVarIndex; int firstAdditionalLocal; int additionalLocals; Object[] replacedLocals; public void visitInsn(int opcode) { if (notransform) { mv.visitInsn(opcode); return; } if (opcode >= IRETURN && opcode <= RETURN && (newStack || checkStack)) { if (debugRuntime && newStack && !checkStack) { mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("[autostack] restore stack pointer because of return at " + className.replace('/', '.') + "." + name + ":" + lastLine); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); } if (newStack && !checkStack) { mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "setPointer", "(I)V", false); } else if (checkStack) { mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false); mv.visitMethodInsn(INVOKESTATIC, className, "$checkStack$", "(II)V", false); } } mv.visitInsn(opcode); } public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if ("Lorg/lwjglx/autostack/UseCallerStack;".equals(desc)) { if (!notransform) { if (debugTransform) System.out.println("[autostack] method declares to use caller stack"); newStack = false; } return null; } else if ("Lorg/lwjglx/autostack/UseNewStack;".equals(desc)) { if (!notransform) { if (debugTransform) System.out.println("[autostack] method declares to use new stack"); newStack = true; } return null; } else if ("Lorg/lwjglx/autostack/NoTransform;".equals(desc)) { return null; } else if ("Lorg/lwjglx/autostack/NoStackParam;".equals(desc)) { return null; } return mv.visitAnnotation(desc, visible); } public void visitVarInsn(int opcode, int var) { if (notransform) { mv.visitVarInsn(opcode, var); return; } if (var >= firstAdditionalLocal) var += additionalLocals; mv.visitVarInsn(opcode, var); } public void visitIincInsn(int var, int increment) { if (notransform) { mv.visitIincInsn(var, increment); return; } if (var >= firstAdditionalLocal) var += additionalLocals; mv.visitIincInsn(var, increment); } public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) { if (notransform) { mv.visitFrame(type, nLocal, local, nStack, stack); return; } if (type == F_FULL) { int noThis = isStatic ? 0 : 1; Object[] locals = new Object[local.length + additionalLocals]; if (!isStatic) locals[0] = local[0]; int replacementLength = replacedLocals.length; System.arraycopy(replacedLocals, noThis, locals, noThis, replacementLength - noThis); int len = locals.length - replacementLength; System.arraycopy(local, replacementLength - additionalLocals, locals, replacementLength, len); mv.visitFrame(type, nLocal + additionalLocals, locals, nStack, stack); } else mv.visitFrame(type, nLocal, local, nStack, stack); } public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { if (notransform) { mv.visitLocalVariable(name, desc, signature, start, end, index); return; } if (index >= firstAdditionalLocal) index += additionalLocals; mv.visitLocalVariable(name, desc, signature, start, end, index); } private boolean doesNotTakeStackItself(String desc) { return desc.lastIndexOf("L" + MEMORYSTACK + ";)") == -1; } public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { String completeName = name + desc; Integer info = stackMethods.get(completeName); if (opcode != INVOKESTATIC || notransform) { mv.visitMethodInsn(opcode, owner, name, desc, itf); return; } if (stackAsParameter && info != null && (info.intValue() & 8) != 0 && (info.intValue() & 16) == 0) { /* Rewrite invocation to have additional MemoryStack parameter */ if (debugTransform) System.out.println( "[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> " + owner.replace('/', '.') + "." + name + "(..., MemoryStack)"); int paramEndIndex = desc.indexOf(')'); String beforeDesc = desc.substring(0, paramEndIndex); String afterDesc = desc.substring(paramEndIndex); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(opcode, owner, name, beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, itf); return; } if (owner.startsWith("org/lwjgl/") && (name.equals("mallocStack") || name.equals("callocStack")) && doesNotTakeStackItself(desc)) { if (debugTransform) System.out.println( "[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokestatic " + owner.replace('/', '.') + "." + name); mv.visitVarInsn(ALOAD, stackVarIndex); int paramEndIndex = desc.indexOf(')'); String beforeDesc = desc.substring(0, paramEndIndex); String afterDesc = desc.substring(paramEndIndex); mv.visitMethodInsn(opcode, owner, name, beforeDesc + "L" + MEMORYSTACK + ";" + afterDesc, false); } else if (owner.equals(MEMORYSTACK) && name.equals("stackGet")) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex); mv.visitVarInsn(ALOAD, stackVarIndex); } else if (owner.equals(MEMORYSTACK) && (name.equals("stackPush") || name.equals("stackPop"))) { String newName = "p" + name.substring(6); if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (owner.equals(MEMORYSTACK) && (name.startsWith("stackMalloc") || name.startsWith("stackCalloc"))) { String newName = name.substring(5, 6).toLowerCase() + name.substring(6); if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (owner.equals(MEMORYSTACK) && (name.startsWith("nstackMalloc") || name.startsWith("nstackCalloc"))) { String newName = "n" + name.substring(6, 7).toLowerCase() + name.substring(7); if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); if ("(I)J".equals(desc)) { mv.visitInsn(SWAP); } else { // (II)J mv.visitInsn(DUP_X2); mv.visitInsn(POP); } mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (owner.equals(MEMORYSTACK) && (name.equals("stackASCII") || name.equals("stackUTF8") || name.equals("stackUTF16"))) { String newName = name.substring(5); boolean withBoolean = desc.startsWith("(Ljava/lang/CharSequence;Z"); if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); if (withBoolean) { mv.visitInsn(DUP_X2); mv.visitInsn(POP); } else { mv.visitInsn(SWAP); } mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (owner.equals(MEMORYSTACK) && (name.equals("stackFloats") || name.equals("stackInts") || name.equals("stackBytes") || name.equals("stackShorts") || name.equals("stackPointers") && (desc.startsWith("([Lorg/lwjgl/system/Pointer;") || desc.startsWith("(Lorg/lwjgl/system/Pointer;")))) { String newName = name.substring(5, 6).toLowerCase() + name.substring(6); Type[] argTypes = Type.getArgumentTypes(desc); if (argTypes.length == 1 && argTypes[0].getSort() == Type.ARRAY) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (argTypes.length == 1) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (argTypes.length == 2) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(DUP_X2); mv.visitInsn(POP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (argTypes.length == 3) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(DUP2_X2); mv.visitInsn(POP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); mv.visitInsn(SWAP); mv.visitInsn(POP); } else { if (debugTransform) System.out.println("[autostack] failed to rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + ". Not yet implemented."); /* Give up. Not possible without an additional local */ mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, name, desc, itf); } } else if (owner.equals(MEMORYSTACK) && name.equals("stackLongs")) { String newName = name.substring(5, 6).toLowerCase() + name.substring(6); Type[] argTypes = Type.getArgumentTypes(desc); if (argTypes.length == 1 && argTypes[0].getSort() == Type.ARRAY) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else if (argTypes.length == 1) { if (debugTransform) System.out.println("[autostack] rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + " --> aload " + stackVarIndex + "; invokevirtual " + MEMORYSTACK.replace('/', '.') + "." + newName); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitInsn(DUP_X2); mv.visitInsn(POP); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, newName, desc, itf); } else { if (debugTransform) System.out.println("[autostack] failed to rewrite invocation of " + owner.replace('/', '.') + "." + name + " at line " + lastLine + ". Not yet implemented."); /* Give up. Not possible without an additional local */ mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, name, desc, itf); } } else { mv.visitMethodInsn(opcode, owner, name, desc, itf); } } public void visitLineNumber(int line, Label start) { mv.visitLineNumber(line, start); lastLine = line; } public void visitCode() { if (notransform) { mv.visitCode(); return; } additionalLocals = newStack || checkStack ? 2 : 1; replacedLocals = new Object[paramTypes.length + additionalLocals + (isStatic ? 0 : 1)]; if (!newStack && !checkStack) { replacedLocals[replacedLocals.length - 1] = MEMORYSTACK; } else { replacedLocals[replacedLocals.length - 2] = MEMORYSTACK; replacedLocals[replacedLocals.length - 1] = INTEGER; } if (!isStatic) replacedLocals[0] = isConstructor ? TOP : className; int var = isStatic ? 0 : 1; for (int t = 0, i = var; t < paramTypes.length; t++, i++) { Type type = paramTypes[t]; var += type.getSize(); switch (type.getSort()) { case Type.INT: case Type.BYTE: case Type.BOOLEAN: case Type.SHORT: case Type.CHAR: replacedLocals[i] = INTEGER; break; case Type.LONG: replacedLocals[i] = LONG; break; case Type.FLOAT: replacedLocals[i] = FLOAT; break; case Type.DOUBLE: replacedLocals[i] = DOUBLE; break; case Type.OBJECT: case Type.ARRAY: replacedLocals[i] = type.getInternalName(); break; default: throw new AssertionError("Unhandled parameter type: " + type); } } firstAdditionalLocal = var; stackVarIndex = var; stackPointerVarIndex = var + 1; mv.visitCode(); if (newStack && !checkStack || checkStack) { if (!memoryStackParam) { mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, "stackGet", "()L" + MEMORYSTACK + ";", false); mv.visitInsn(DUP); mv.visitVarInsn(ASTORE, stackVarIndex); } else { mv.visitVarInsn(ALOAD, stackVarIndex); } mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false); mv.visitVarInsn(ISTORE, stackPointerVarIndex); if (debugRuntime && newStack && !checkStack) { mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("[autostack] save stack pointer ["); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("] at begin of " + className.replace('/', '.') + "." + name); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); } mv.visitLabel(tryLabel); if (!memoryStackParam) mv.visitFrame(F_APPEND, 2, new Object[] { MEMORYSTACK, INTEGER }, 0, null); else mv.visitFrame(F_APPEND, 1, new Object[] { INTEGER }, 0, null); } else if (!newStack && !checkStack) { if (!memoryStackParam) { mv.visitMethodInsn(INVOKESTATIC, MEMORYSTACK, "stackGet", "()L" + MEMORYSTACK + ";", false); mv.visitVarInsn(ASTORE, stackVarIndex); } if (debugRuntime) { mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("[autostack] current stack pointer is ["); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("] at begin of " + className.replace('/', '.') + "." + name); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); } mv.visitLabel(tryLabel); if (!memoryStackParam) mv.visitFrame(F_APPEND, 1, new Object[] { MEMORYSTACK }, 0, null); } } public void visitMaxs(int maxStack, int maxLocals) { if (notransform) { mv.visitMaxs(maxStack, maxLocals); return; } if (newStack && !checkStack || checkStack) { mv.visitLabel(finallyLabel); mv.visitFrame(F_FULL, replacedLocals.length, replacedLocals, 1, new Object[] { "java/lang/Throwable" }); mv.visitTryCatchBlock(tryLabel, finallyLabel, finallyLabel, null); if (debugRuntime && newStack && !checkStack) { mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("[autostack] restore stack pointer because of throw ["); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitInsn(DUP); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false); mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn( "] at " + className.replace('/', '.') + "." + name + ":" + lastLine); mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); } if (newStack && !checkStack) { mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "setPointer", "(I)V", false); } if (checkStack) { mv.visitVarInsn(ILOAD, stackPointerVarIndex); mv.visitVarInsn(ALOAD, stackVarIndex); mv.visitMethodInsn(INVOKEVIRTUAL, MEMORYSTACK, "getPointer", "()I", false); mv.visitMethodInsn(INVOKESTATIC, className, "$checkStackWithThrowable$", "(Ljava/lang/Throwable;II)Ljava/lang/Throwable;", false); } mv.visitInsn(ATHROW); } mv.visitMaxs(-1, maxLocals + additionalLocals); } }; return mv; } }, 0); byte[] arr = cw.toByteArray(); if (trace) { cr = new ClassReader(arr); cr.accept(new TraceClassVisitor(new PrintWriter(System.out)), 0); } return arr; } catch (Throwable t) { t.printStackTrace(); throw new RuntimeException(t); } }
From source file:org.mbte.groovypp.compiler.asm.VisitLocalVariable.java
License:Apache License
public void visit(MethodVisitor mv) { mv.visitLocalVariable(name, desc, null, start, end, index); }
From source file:org.simantics.databoard.binding.classfactory.AsmTypeClassFactory.java
License:Open Source License
public Class<?> createRecordClass(RecordType type) throws BindingConstructionException { ClassWriter cw = new ClassWriter(0); AnnotationVisitor av0;/*www. j av a 2s. c om*/ // Create class name SignatureVisitor sv = new SignatureVisitor(); type.accept(sv, null); String sig = sv.sb.toString() + "_" + Integer.toHexString(sv.hashcode); String hash = Integer.toHexString(type.hashCode()); String className = "org.simantics.databoard.RecordType_" + sig; String classSig = "org/simantics/databoard/RecordType_" + sig; Class<?> superClass = makeBean ? Bean.class : Object.class; String superType = AsmBindingClassLoader.toClassCanonicalName(superClass); cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, classSig, null, superType, null); if (type.isReferable()) { av0 = cw.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(Referable.class), true); av0.visitEnd(); } for (int i = 0; i < type.getComponentCount(); i++) { Component c = type.getComponent(i); Datatype fieldType = c.type; boolean isOptional = fieldType instanceof OptionalType; boolean isIdentifier = type.isIdentifier(i); if (isOptional) { fieldType = ((OptionalType) fieldType).componentType; } // boolean isArray = fieldType instanceof ArrayType; // if ( isArray ) { // fieldType = ((ArrayType) fieldType).componentType; // } String fieldName = toJavaFieldName(c.name); BindingRequest br = classFactory.getClass(fieldType); FieldVisitor fv = cw.visitField(ACC_PUBLIC, fieldName, br.signature, br.descriptor.equals(br.signature) ? null : br.descriptor, null); // Visit annotations if (br.annotations != null && br.annotations.length > 0) { for (Annotation a : br.annotations) { if (a instanceof Arguments) { Arguments arg = (Arguments) a; av0 = fv.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(Arguments.class), true); AnnotationVisitor av1 = av0.visitArray("value"); for (Class<?> clzz : arg.value()) { av1.visit(null, Type.getType(AsmBindingClassLoader.toTypeDescriptor(clzz))); } av1.visitEnd(); av0.visitEnd(); } isIdentifier |= a instanceof Identifier; if (a instanceof Length) { Length arg = (Length) a; av0 = fv.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(Length.class), true); AnnotationVisitor av1 = av0.visitArray("value"); for (String s : arg.value()) av1.visit(null, s); av1.visitEnd(); av0.visitEnd(); } if (a instanceof MIMEType) { MIMEType arg = (MIMEType) a; av0 = fv.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(MIMEType.class), true); av0.visit("value", arg.value()); av0.visitEnd(); } if (a instanceof Name) { Name arg = (Name) a; av0 = fv.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(Name.class), true); av0.visit("value", arg.value()); av0.visitEnd(); } isOptional |= a instanceof Optional; if (a instanceof org.simantics.databoard.annotations.Pattern) { org.simantics.databoard.annotations.Pattern arg = (org.simantics.databoard.annotations.Pattern) a; av0 = fv.visitAnnotation(AsmBindingClassLoader .toTypeDescriptor(org.simantics.databoard.annotations.Pattern.class), true); av0.visit("value", arg.value()); av0.visitEnd(); } if (a instanceof Range) { Range arg = (Range) a; av0 = fv.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(Range.class), true); av0.visit("value", arg.value()); av0.visitEnd(); } if (a instanceof Unit) { Unit arg = (Unit) a; av0 = fv.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(Unit.class), true); av0.visit("value", arg.value()); av0.visitEnd(); } } } if (isIdentifier) { av0 = fv.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(Identifier.class), true); av0.visitEnd(); } if (isOptional) { av0 = fv.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(Optional.class), true); av0.visitEnd(); } fv.visitEnd(); } // Constructor { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); // super() if (!makeBean) { mv.visitLabel(l0); mv.visitLineNumber(20, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, superType, "<init>", "()V"); } else { // super( getStaticBinding() ) mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, classSig, "getStaticBinding", "()Lorg/simantics/databoard/binding/Binding;"); mv.visitMethodInsn(INVOKESPECIAL, "org/simantics/databoard/util/Bean", "<init>", "(Lorg/simantics/databoard/binding/Binding;)V"); } mv.visitInsn(RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + classSig + ";", null, l0, l1, 0); if (!makeBean) { mv.visitMaxs(1, 1); } else { mv.visitMaxs(2, 1); } mv.visitEnd(); } // BINDING { FieldVisitor fv = cw.visitField(ACC_STATIC, "BINDING", "Lorg/simantics/databoard/binding/Binding;", null, null); fv.visitEnd(); MethodVisitor mv = cw.visitMethod(ACC_STATIC, "getStaticBinding", "()Lorg/simantics/databoard/binding/Binding;", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitFieldInsn(GETSTATIC, classSig, "BINDING", "Lorg/simantics/databoard/binding/Binding;"); Label l1 = new Label(); mv.visitJumpInsn(IFNONNULL, l1); mv.visitLdcInsn(Type.getType("L" + classSig + ";")); mv.visitMethodInsn(INVOKESTATIC, "org/simantics/databoard/Bindings", "getBindingUnchecked", "(Ljava/lang/Class;)Lorg/simantics/databoard/binding/Binding;"); mv.visitFieldInsn(PUTSTATIC, classSig, "BINDING", "Lorg/simantics/databoard/binding/Binding;"); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitFieldInsn(GETSTATIC, classSig, "BINDING", "Lorg/simantics/databoard/binding/Binding;"); mv.visitInsn(ARETURN); mv.visitMaxs(1, 0); mv.visitEnd(); } cw.visitEnd(); byte[] data = cw.toByteArray(); Class<?> clazz = defineClass(className, data, 0, data.length); BindingRequest br = new BindingRequest(clazz); return clazz; }
From source file:org.simantics.databoard.binding.classfactory.AsmTypeClassFactory.java
License:Open Source License
public Class<?> createUnionClass(UnionType ut) throws BindingConstructionException { ClassWriter cw = new ClassWriter(0); // Create class name SignatureVisitor sv = new SignatureVisitor(); ut.accept(sv, null);/*from w w w. j a v a 2s. c o m*/ String sig = sv.sb.toString() + "_" + Integer.toHexString(sv.hashcode); String hash = Integer.toHexString(ut.hashCode()); String className = "org.simantics.databoard.UnionType_" + sig; String classSig = "org/simantics/databoard/UnionType_" + sig; Class<?> superClass = Object.class; String superType = AsmBindingClassLoader.toClassCanonicalName(superClass); cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, classSig, null, superType, null); AnnotationVisitor av0 = cw.visitAnnotation(AsmBindingClassLoader.toTypeDescriptor(Union.class), true); AnnotationVisitor av1 = av0.visitArray("value"); for (Component c : ut.components) { BindingRequest br = classFactory.getClass(c.type); av1.visit(null, Type.getType(br.descriptor)); } av1.visitEnd(); av0.visitEnd(); // Constructor { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(20, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + classSig + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } cw.visitEnd(); byte[] data = cw.toByteArray(); return defineClass(className, data, 0, data.length); }