List of usage examples for org.objectweb.asm MethodVisitor visitLabel
public void visitLabel(final Label label)
From source file:com.asakusafw.dag.compiler.builtin.SummarizeOperatorGenerator.java
License:Apache License
private static void defineCheckNull(ClassWriter writer, UserOperator operator, DataModelReference inputType) { MethodVisitor method = writer.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, METHOD_CHECK_NON_NULL, Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(ValueOption.class), typeOf(Object.class), typeOf(String.class)), null, null);/*from w w w .j a v a2 s . co m*/ LocalVarRef optionVar = new LocalVarRef(Opcodes.ALOAD, 0); LocalVarRef objectVar = new LocalVarRef(Opcodes.ALOAD, 1); LocalVarRef nameVar = new LocalVarRef(Opcodes.ALOAD, 2); // if (option.isNull()) { Label ifEnd = new Label(); optionVar.load(method); getNullity(method, VALUE_DESC); method.visitJumpInsn(Opcodes.IFEQ, ifEnd); // new NullPointerException ... method.visitTypeInsn(Opcodes.NEW, typeOf(NullPointerException.class).getInternalName()); method.visitInsn(Opcodes.DUP); // str = String.format("<type>.%s must not be null (in <operator>): %s", name, object) getConst(method, String.format("%s.%%s must not be null (in %s.%s): %%s", inputType.getDeclaration().getSimpleName(), operator.getMethod().getDeclaringClass().getSimpleName(), operator.getMethod().getName())); getArray(method, typeOf(Object.class), new LocalVarRef[] { nameVar, objectVar }); method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(String.class).getInternalName(), "format", Type.getMethodDescriptor(typeOf(String.class), typeOf(String.class), typeOf(Object[].class)), false); // throw new NullPointerException(str) method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(NullPointerException.class).getInternalName(), CONSTRUCTOR_NAME, Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(String.class)), false); method.visitInsn(Opcodes.ATHROW); method.visitLabel(ifEnd); // } method.visitInsn(Opcodes.RETURN); method.visitMaxs(0, 0); method.visitEnd(); }
From source file:com.asakusafw.dag.compiler.codegen.DataComparatorGenerator.java
License:Apache License
private static void defineCompare(ClassWriter writer, DataModelReference reference, List<Group.Ordering> orderings) { MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "compare", DESC_COMPARE, null, new String[] { typeOf(IOException.class).getInternalName(), }); LocalVarRef a = new LocalVarRef(Opcodes.ALOAD, 1); LocalVarRef b = new LocalVarRef(Opcodes.ALOAD, 2); for (Group.Ordering ordering : orderings) { PropertyReference property = Invariants .requireNonNull(reference.findProperty(ordering.getPropertyName())); // int diff = ValueOptionSerDe.compareT({a, b}, {b, a}); switch (ordering.getDirection()) { case ASCENDANT: a.load(v);// w ww. j a va 2 s. c o m b.load(v); break; case DESCENDANT: b.load(v); a.load(v); break; default: throw new AssertionError(ordering); } v.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(ValueOptionSerDe.class).getInternalName(), Invariants.requireNonNull(METHOD_NAMES.get(property.getType())), DESC_COMPARE, false); LocalVarRef cmp = putLocalVar(v, Type.INT, 3); Label eq = new Label(); // if (diff != 0) { cmp.load(v); v.visitJumpInsn(Opcodes.IFEQ, eq); // return diff; cmp.load(v); v.visitInsn(Opcodes.IRETURN); // } @ eq v.visitLabel(eq); } getConst(v, 0); v.visitInsn(Opcodes.IRETURN); v.visitMaxs(0, 0); v.visitEnd(); }
From source file:com.asakusafw.dag.compiler.codegen.ObjectComparatorGenerator.java
License:Apache License
private static void defineCompare(ClassWriter writer, DataModelReference reference, List<Group.Ordering> orderings) { MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "compare", Type.getMethodDescriptor(typeOf(int.class), typeOf(Object.class), typeOf(Object.class)), null, null);// ww w . java 2s . c o m LocalVarRef a = cast(v, 1, reference.getDeclaration()); LocalVarRef b = cast(v, 2, reference.getDeclaration()); for (Group.Ordering ordering : orderings) { LocalVarRef left; LocalVarRef right; switch (ordering.getDirection()) { case ASCENDANT: left = a; right = b; break; case DESCENDANT: left = b; right = a; break; default: throw new AssertionError(ordering); } // int diff = left.getXOption().compareTo(right.getXOption()); PropertyReference property = Invariants .requireNonNull(reference.findProperty(ordering.getPropertyName())); left.load(v); getOption(v, property); right.load(v); getOption(v, property); v.visitMethodInsn(Opcodes.INVOKEINTERFACE, TYPE_COMPARABLE.getInternalName(), "compareTo", Type.getMethodDescriptor(typeOf(int.class), typeOf(Object.class)), true); LocalVarRef cmp = putLocalVar(v, Type.INT, 3); Label eq = new Label(); // if (diff != 0) { cmp.load(v); v.visitJumpInsn(Opcodes.IFEQ, eq); // return diff; cmp.load(v); v.visitInsn(Opcodes.IRETURN); // } @ eq v.visitLabel(eq); } getConst(v, 0); v.visitInsn(Opcodes.IRETURN); v.visitMaxs(0, 0); v.visitEnd(); }
From source file:com.asakusafw.dag.compiler.directio.OutputPatternSerDeGenerator.java
License:Apache License
private static void putGetProperty(ClassWriter writer, DataModelReference reference, OutputPattern pattern) { List<PropertyReference> properties = pattern.getResourcePattern().stream() .filter(s -> s.getKind() == OutputPattern.SourceKind.PROPERTY).map(CompiledSegment::getTarget) .collect(Collectors.toList()); if (properties.isEmpty()) { return;//from w w w. j av a 2 s . com } MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "getProperty", Type.getMethodDescriptor(typeOf(Object.class), typeOf(Object.class), typeOf(int.class)), null, null); LocalVarRef object = cast(v, 1, reference.getDeclaration()); LocalVarRef index = new LocalVarRef(Opcodes.ILOAD, 2); Label[] caseLabels = properties.stream().map(o -> new Label()).toArray(Label[]::new); Label defaultLabel = new Label(); index.load(v); v.visitTableSwitchInsn(0, caseLabels.length - 1, defaultLabel, caseLabels); for (int i = 0, n = properties.size(); i < n; i++) { v.visitLabel(caseLabels[i]); PropertyReference property = properties.get(i); object.load(v); getOption(v, property); v.visitInsn(Opcodes.ARETURN); } v.visitLabel(defaultLabel); getNew(v, Descriptions.typeOf(AssertionError.class)); v.visitInsn(Opcodes.ATHROW); v.visitMaxs(0, 0); v.visitEnd(); }
From source file:com.asakusafw.dag.compiler.jdbc.PreparedStatementAdapterGenerator.java
License:Apache License
private static void defineBody(ClassWriter writer, DataModelReference dataType, List<PropertyReference> properties, Optional<FieldRef> dateBuf) { MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "drive", //$NON-NLS-1$ Type.getMethodDescriptor(typeOf(void.class), typeOf(PreparedStatement.class), typeOf(Object.class)), null, new String[] { typeOf(SQLException.class).getInternalName(), }); LocalVarRef row = new LocalVarRef(Opcodes.ALOAD, 1); LocalVarRef object = cast(v, 2, dataType.getDeclaration()); int columnIndex = 0; for (PropertyReference property : properties) { columnIndex++;/*from w w w . j ava 2s . c o m*/ object.load(v); getOption(v, property); LocalVarRef option = putLocalVar(v, Type.OBJECT, 3); Label elseIf = new Label(); Label endIf = new Label(); // if (option.isNull()) { option.load(v); v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(ValueOption.class).getInternalName(), "isNull", //$NON-NLS-1$ Type.getMethodDescriptor(typeOf(boolean.class)), false); v.visitJumpInsn(Opcodes.IFEQ, elseIf); row.load(v); getConst(v, columnIndex); doSetNull(v, property); v.visitJumpInsn(Opcodes.GOTO, endIf); // } else { @elseIf v.visitLabel(elseIf); row.load(v); getConst(v, columnIndex); option.load(v); doSetValue(v, property, dateBuf); // } @endIf v.visitLabel(endIf); } v.visitInsn(Opcodes.RETURN); v.visitMaxs(0, 0); v.visitEnd(); }
From source file:com.asakusafw.dag.compiler.jdbc.ResultSetAdapterGenerator.java
License:Apache License
private static void defineBody(ClassWriter writer, DataModelReference dataType, List<PropertyReference> properties, FieldRef valueBuffer, Optional<FieldRef> calendarBuf) { MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "extract", Type.getMethodDescriptor(typeOf(Object.class), typeOf(ResultSet.class)), null, new String[] { typeOf(SQLException.class).getInternalName(), }); LocalVarRef rs = new LocalVarRef(Opcodes.ALOAD, 1); valueBuffer.load(v);/* www . j a v a 2 s . co m*/ LocalVarRef buf = putLocalVar(v, Type.OBJECT, 2); int columnIndex = 0; Set<PropertyName> sawSet = new HashSet<>(); for (PropertyReference property : properties) { columnIndex++; Label elseIf = new Label(); Label endIf = new Label(); buf.load(v); getOption(v, property); rs.load(v); getConst(v, columnIndex); doGetValue(v, property, calendarBuf); // if (rs.wasNull()) { rs.load(v); v.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(ResultSet.class).getInternalName(), "wasNull", Type.getMethodDescriptor(typeOf(boolean.class)), true); v.visitJumpInsn(Opcodes.IFEQ, elseIf); if (isWideType(property)) { v.visitInsn(Opcodes.POP2); } else { v.visitInsn(Opcodes.POP); } doSetNull(v); v.visitJumpInsn(Opcodes.GOTO, endIf); // } else { @elseIf v.visitLabel(elseIf); doSetValue(v, property); // } @endIf v.visitLabel(endIf); sawSet.add(property.getName()); } // resets other properties for (PropertyReference property : dataType.getProperties()) { if (sawSet.contains(property.getName())) { continue; } buf.load(v); getOption(v, property); doSetNull(v); } buf.load(v); v.visitInsn(Opcodes.ARETURN); v.visitMaxs(0, 0); v.visitEnd(); }
From source file:com.centimia.orm.jaqu.ext.asm.JaquClassAdapter.java
License:Open Source License
/** * Generates the new method body, copy the old to a new method and connect them. * /*from w w w . j av a2s .c om*/ * the structure of the new method is:<br> * <br><b><div style="background:lightgray"> * <pre> * public [CollectionType] [getterName]() { * if ([fieldName] == null){ * try { * if (null == db || db.isClosed()) * throw new RuntimeException("Cannot initialize a 'Relation' outside an open session!!!. Try initializing the field directly within the class."); * Method method = db.getClass().getDeclaredMethod("getRelationFromDb", String.class, Object.class, Class.class); * method.setAccessible(true); * children = (Collection<TestTable>)method.invoke(db, [fieldName], this, TestTable.class); * method.setAccessible(false); * } * catch (Exception e) { * if (e instanceof RuntimeException) * throw (RuntimeException)e; * throw new RuntimeException(e.getMessage(), e); * } * } * return $orig_[getterName](); * } * </pre> * </div> * * @param access * @param desc * @param signature * @param exceptions * @param name * @param newName * @param fieldName */ private void generateNewMethodBody(int access, String desc, String signature, String[] exceptions, String name, String newName, String fieldName) { MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions); String fieldSignature = signature.substring(signature.indexOf(')') + 1, signature.lastIndexOf('<')) + ";"; String type = signature.substring(signature.indexOf('<') + 1, signature.indexOf('>')); String cast = desc.substring(desc.indexOf("java/"), desc.indexOf(';')); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature); Label l3 = new Label(); mv.visitJumpInsn(IFNONNULL, l3); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); Label l4 = new Label(); mv.visitJumpInsn(IFNULL, l4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "isClosed", "()Z", false); Label l5 = new Label(); mv.visitJumpInsn(IFEQ, l5); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitLdcInsn( "Cannot initialize a 'Relation' outside an open session!!!. Try initializing the field directly within the class."); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); mv.visitLdcInsn("getRelationFromDb"); mv.visitInsn(ICONST_3); mv.visitTypeInsn(ANEWARRAY, "java/lang/Class"); mv.visitInsn(DUP); mv.visitInsn(ICONST_0); mv.visitLdcInsn(Type.getType("Ljava/lang/String;")); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_1); mv.visitLdcInsn(Type.getType("Ljava/lang/Object;")); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_2); mv.visitLdcInsn(Type.getType("Ljava/lang/Class;")); mv.visitInsn(AASTORE); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ICONST_1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "setAccessible", "(Z)V", false); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitInsn(ICONST_3); mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); mv.visitInsn(DUP); mv.visitInsn(ICONST_0); mv.visitLdcInsn(fieldName); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_1); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(AASTORE); mv.visitInsn(DUP); mv.visitInsn(ICONST_2); mv.visitLdcInsn(Type.getType(type)); mv.visitInsn(AASTORE); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, cast); mv.visitFieldInsn(PUTFIELD, className, fieldName, fieldSignature); mv.visitVarInsn(ALOAD, 1); mv.visitInsn(ICONST_0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "setAccessible", "(Z)V", false); mv.visitLabel(l1); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" }); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(INSTANCEOF, "java/lang/RuntimeException"); Label l6 = new Label(); mv.visitJumpInsn(IFEQ, l6); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, "java/lang/RuntimeException"); mv.visitInsn(ATHROW); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Exception" }, 0, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "getMessage", "()Ljava/lang/String;", false); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, className, newName, desc, false); mv.visitInsn(ARETURN); mv.visitMaxs(7, 2); mv.visitEnd(); }
From source file:com.centimia.orm.jaqu.ext.asm.JaquClassAdapter.java
License:Open Source License
/** * Generates the new method body, copy the old to a new method and connect them. * the structure of the new method is:<br> * <br><b><div style="background:lightgray"> * <pre>/*from w w w . j a v a2s .com*/ * public [entityType] [getterName]() { * if ([field] == null) { * try { * if (null == db) * throw new RuntimeException("Cannot initialize 'Relation' outside an open session!!!. Try initializing field directly within the class."); * * [parentType] parent = this.getClass().newInstance(); * [entityType] desc = TestB.class.newInstance(); * * // get the primary key * Object pk = db.getPrimaryKey(this); * * // get the object * [entityValue] = db.from(desc).innerJoin(parent).on(parent.[entityValue]).is(desc).where(db.getPrimaryKey(parent)).is(pk).selectFirst(); * } * catch (Exception e) { * if (e instanceof RuntimeException) * throw (RuntimeException)e; * throw new RuntimeException(e.getMessage(), e); * } * } * return $orig_[getterName](); * } * </pre> * </div> * * @param access * @param desc * @param exceptions * @param name - current method name * @param newName - new method name (the orig...) * @param fieldName */ public void generateLazyRelation(int access, String desc, String[] exceptions, String name, String newName, String fieldName) { MethodVisitor mv = cv.visitMethod(access, name, desc, null, exceptions); String fieldSignature = desc.substring(desc.indexOf(')') + 1); String fieldClassName = desc.substring(desc.indexOf(')') + 2, desc.length() - 1); mv.visitCode(); Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature); Label l3 = new Label(); mv.visitJumpInsn(IFNULL, l3); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature); mv.visitFieldInsn(GETFIELD, fieldClassName, "isLazy", "Z"); mv.visitJumpInsn(IFEQ, l3); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); Label l4 = new Label(); mv.visitJumpInsn(IFNULL, l4); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "isClosed", "()Z", false); Label l5 = new Label(); mv.visitJumpInsn(IFEQ, l5); mv.visitLabel(l4); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitLdcInsn( "Cannot initialize 'Relation' outside an open session!!!. Try initializing field directly within the class."); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "newInstance", "()Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, className); mv.visitVarInsn(ASTORE, 1); mv.visitLdcInsn(Type.getType(fieldSignature)); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "newInstance", "()Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, fieldClassName); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "getPrimaryKey", "(Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitVarInsn(ASTORE, 3); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "from", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryInterface;", false); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEINTERFACE, "com/centimia/orm/jaqu/QueryInterface", "innerJoin", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoin;", true); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(GETFIELD, className, fieldName, fieldSignature); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoin", "on", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoinCondition;", false); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoinCondition", "is", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryJoinWhere;", false); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "db", "Lcom/centimia/orm/jaqu/Db;"); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/Db", "getPrimaryKey", "(Ljava/lang/Object;)Ljava/lang/Object;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryJoinWhere", "where", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryCondition;", false); mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryCondition", "is", "(Ljava/lang/Object;)Lcom/centimia/orm/jaqu/QueryWhere;", false); mv.visitMethodInsn(INVOKEVIRTUAL, "com/centimia/orm/jaqu/QueryWhere", "selectFirst", "()Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, fieldClassName); mv.visitFieldInsn(PUTFIELD, className, fieldName, fieldSignature); mv.visitLabel(l1); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" }); mv.visitVarInsn(ASTORE, 1); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(INSTANCEOF, "java/lang/RuntimeException"); Label l6 = new Label(); mv.visitJumpInsn(IFEQ, l6); mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, "java/lang/RuntimeException"); mv.visitInsn(ATHROW); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/Exception" }, 0, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "getMessage", "()Ljava/lang/String;", false); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V", false); mv.visitInsn(ATHROW); mv.visitLabel(l3); mv.visitFrame(Opcodes.F_CHOP, 1, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, className, newName, desc, false); mv.visitInsn(ARETURN); mv.visitMaxs(4, 4); mv.visitEnd(); }
From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java
License:Open Source License
protected void generateWriteStaticGroup(Schema schema, ClassVisitor cv, String genClassInternalName, boolean javaClassCodec) { // method writeStaticGroupWithId - switch MethodVisitor mv = cv.visitMethod(ACC_PROTECTED, "writeStaticGroupWithId", "(Lcom/cinnober/msgcodec/io/ByteSink;Ljava/lang/Object;)V", null, new String[] { "java/io/IOException" }); int nextVar = 3; mv.visitCode();/*from ww w . j a v a2 s . c om*/ mv.visitVarInsn(ALOAD, 2); if (javaClassCodec) { mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); } else { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, genClassInternalName, "groupTypeAccessor", "Lcom/cinnober/msgcodec/GroupTypeAccessor;"); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEINTERFACE, "com/cinnober/msgcodec/GroupTypeAccessor", "getGroupType", "(Ljava/lang/Object;)Ljava/lang/Object;", true); } int groupTypeVar = nextVar++; mv.visitInsn(DUP); mv.visitVarInsn(ASTORE, groupTypeVar); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "hashCode", "()I", false); // switch on class.hashCode() Map<Integer, ObjectHashCodeSwitchCase<Object>> casesByHashCode = new TreeMap<>(); Map<Integer, Label> labelsByGroupId = new TreeMap<>(); for (GroupDef group : schema.getGroups()) { Object groupType = group.getGroupType(); int groupHash = groupType.hashCode(); ObjectHashCodeSwitchCase<Object> hashCase = casesByHashCode.get(groupHash); if (hashCase == null) { hashCase = new ObjectHashCodeSwitchCase<>(groupHash); casesByHashCode.put(hashCase.hashCode, hashCase); } hashCase.add(groupType); } Label unknownHashLabel = new Label(); { int[] caseValues = new int[casesByHashCode.size()]; int i = 0; for (int hashCode : casesByHashCode.keySet()) { caseValues[i++] = hashCode; } Label[] caseLabels = new Label[casesByHashCode.size()]; i = 0; for (ObjectHashCodeSwitchCase<Object> hashCase : casesByHashCode.values()) { caseLabels[i++] = hashCase.label; } mv.visitLookupSwitchInsn(unknownHashLabel, caseValues, caseLabels); } for (ObjectHashCodeSwitchCase<Object> hashCase : casesByHashCode.values()) { mv.visitLabel(hashCase.label); mv.visitFrame(F_SAME, 0, null, 0, null); for (ObjectSwitchCase<Object> classCase : hashCase.cases) { GroupDef group = schema.getGroup(classCase.object); int groupId = schema.getGroup(classCase.object).getId(); if (groupId != -1) { labelsByGroupId.put(groupId, classCase.label); } mv.visitVarInsn(ALOAD, groupTypeVar); if (javaClassCodec) { mv.visitLdcInsn(getJavaType(classCase.object)); mv.visitJumpInsn(IF_ACMPEQ, classCase.label); } else { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, genClassInternalName, "groupType_" + group.getName(), "Ljava/lang/Object;"); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false); mv.visitJumpInsn(IFNE, classCase.label); // IFNE = if not false } } } // Default case for class hashcode switch, do lookup using schema.getGroup(Object) { Label unknownGroupIdLabel = new Label(); mv.visitLabel(unknownHashLabel); mv.visitFrame(F_SAME, 0, null, 0, null); Label[] groupIdLabels = new Label[labelsByGroupId.size()]; int[] groupIds = new int[groupIdLabels.length]; { int i = 0; for (Entry<Integer, Label> entry : labelsByGroupId.entrySet()) { groupIds[i] = entry.getKey(); groupIdLabels[i] = new Label(); i++; } } mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, genClassInternalName, "schema", "Lcom/cinnober/msgcodec/Schema;"); mv.visitVarInsn(ALOAD, groupTypeVar); mv.visitMethodInsn(INVOKEVIRTUAL, SCHEMA_INAME, "getGroup", "(Ljava/lang/Object;)Lcom/cinnober/msgcodec/GroupDef;", false); // check for null result from getGroup Label noGroupDefLabel = new Label(); mv.visitInsn(DUP); mv.visitJumpInsn(IFNULL, noGroupDefLabel); mv.visitMethodInsn(INVOKEVIRTUAL, GROUPDEF_INAME, "getId", "()I", false); // Switch on the group id mv.visitLookupSwitchInsn(unknownHashLabel, groupIds, groupIdLabels); // Cases for the group ids for (int i = 0; i < groupIds.length; i++) { mv.visitLabel(groupIdLabels[i]); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitJumpInsn(GOTO, labelsByGroupId.get(groupIds[i])); } mv.visitLabel(noGroupDefLabel); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitInsn(POP); // Throw exception if there is no match on class or group id mv.visitLabel(unknownGroupIdLabel); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, groupTypeVar); mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "unknownGroupType", "(Ljava/lang/Object;)Ljava/lang/IllegalArgumentException;", false); mv.visitInsn(ATHROW); } // Generate the labeled calls to group writer methods for (ObjectHashCodeSwitchCase<Object> hashCase : casesByHashCode.values()) { for (ObjectSwitchCase<Object> classCase : hashCase.cases) { Object groupType = classCase.object; GroupDef group = schema.getGroup(groupType); String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec); mv.visitLabel(classCase.label); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); // this mv.visitVarInsn(ALOAD, 1); // out mv.visitVarInsn(ALOAD, 2); // obj if (javaClassCodec) { mv.visitTypeInsn(CHECKCAST, getTypeInternalName(groupType, javaClassCodec)); } mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "writeStaticGroupWithId_" + group.getName(), "(Lcom/cinnober/msgcodec/io/ByteSink;" + groupDescriptor + ")V", false); mv.visitInsn(RETURN); } } mv.visitMaxs(4, nextVar); mv.visitEnd(); }
From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java
License:Open Source License
protected void generateReadStaticGroup(Schema schema, ClassVisitor cv, String genClassInternalName, boolean javaClassCodec) { MethodVisitor mv = cv.visitMethod(ACC_PROTECTED, "readStaticGroup", "(ILcom/cinnober/msgcodec/io/ByteSource;)Ljava/lang/Object;", null, new String[] { "java/io/IOException" }); int nextVar = 3; mv.visitCode();/* w w w.j a v a 2s .com*/ Map<Integer, Label> labelsByGroupId = new TreeMap<>(); for (GroupDef group : schema.getGroups()) { if (group.getId() != -1) { labelsByGroupId.put(group.getId(), new Label()); } } mv.visitVarInsn(ILOAD, 1); // group id Label unknownGroupIdLabel = new Label(); { int[] caseValues = new int[labelsByGroupId.size()]; int i = 0; for (int groupId : labelsByGroupId.keySet()) { caseValues[i++] = groupId; } Label[] caseLabels = labelsByGroupId.values().toArray(new Label[labelsByGroupId.size()]); mv.visitLookupSwitchInsn(unknownGroupIdLabel, caseValues, caseLabels); } for (Map.Entry<Integer, Label> caseEntry : labelsByGroupId.entrySet()) { GroupDef group = schema.getGroup(caseEntry.getKey().intValue()); Object groupType = group.getGroupType(); String groupDescriptor = getTypeDescriptor(groupType, javaClassCodec); mv.visitLabel(caseEntry.getValue()); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 2); mv.visitMethodInsn(INVOKEVIRTUAL, genClassInternalName, "readStaticGroup_" + group.getName(), "(Lcom/cinnober/msgcodec/io/ByteSource;)" + groupDescriptor, false); mv.visitInsn(ARETURN); } // default case mv.visitLabel(unknownGroupIdLabel); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ILOAD, 1); mv.visitMethodInsn(INVOKESTATIC, baseclassIName, "unknownGroupId", "(I)Lcom/cinnober/msgcodec/DecodeException;", false); mv.visitInsn(ATHROW); mv.visitMaxs(2, nextVar); mv.visitEnd(); }