List of usage examples for org.objectweb.asm Label Label
public Label()
From source file:com.asakusafw.dag.compiler.builtin.MasterCheckOperatorGenerator.java
License:Apache License
@Override protected void defineProcess(MethodVisitor method, Context context, UserOperator operator, LocalVarRef master, LocalVarRef transaction, FieldRef impl, Map<OperatorProperty, FieldRef> dependencies, ClassDescription target) {/*from ww w . java 2 s .c om*/ OperatorOutput found = operator.getOutputs().get(MasterCheck.ID_OUTPUT_FOUND); OperatorOutput missed = operator.getOutputs().get(MasterCheck.ID_OUTPUT_MISSED); Label onNull = new Label(); Label end = new Label(); master.load(method); getConst(method, null); method.visitJumpInsn(Opcodes.IF_ACMPEQ, onNull); dependencies.get(found).load(method); transaction.load(method); invokeResultAdd(method); method.visitJumpInsn(Opcodes.GOTO, end); method.visitLabel(onNull); dependencies.get(missed).load(method); transaction.load(method); invokeResultAdd(method); method.visitLabel(end); }
From source file:com.asakusafw.dag.compiler.builtin.MasterJoinOperatorGenerator.java
License:Apache License
@Override protected void defineProcess(MethodVisitor method, Context context, UserOperator operator, LocalVarRef master, LocalVarRef transaction, FieldRef impl, Map<OperatorProperty, FieldRef> dependencies, ClassDescription target) {// w ww.j av a 2 s .c om OperatorOutput joined = operator.getOutput(MasterJoin.ID_OUTPUT_JOINED); OperatorOutput missed = operator.getOutput(MasterJoin.ID_OUTPUT_MISSED); Label onNull = new Label(); Label end = new Label(); master.load(method); getConst(method, null); method.visitJumpInsn(Opcodes.IF_ACMPEQ, onNull); dependencies.get(joined).load(method); performJoin(method, context, operator, master, transaction, impl, dependencies, target); invokeResultAdd(method); method.visitJumpInsn(Opcodes.GOTO, end); method.visitLabel(onNull); dependencies.get(missed).load(method); transaction.load(method); invokeResultAdd(method); method.visitLabel(end); }
From source file:com.asakusafw.dag.compiler.builtin.MasterJoinUpdateOperatorGenerator.java
License:Apache License
@Override protected void defineProcess(MethodVisitor method, Context context, UserOperator operator, LocalVarRef master, LocalVarRef transaction, FieldRef impl, Map<OperatorProperty, FieldRef> dependencies, ClassDescription target) {/* www .j a va 2 s. co m*/ OperatorOutput found = operator.getOutputs().get(MasterCheck.ID_OUTPUT_FOUND); OperatorOutput missed = operator.getOutputs().get(MasterCheck.ID_OUTPUT_MISSED); Label onNull = new Label(); Label end = new Label(); master.load(method); getConst(method, null); method.visitJumpInsn(Opcodes.IF_ACMPEQ, onNull); List<ValueRef> arguments = new ArrayList<>(); arguments.add(impl); arguments.add(master); arguments.add(transaction); appendExtraViews(arguments::add, operator, dependencies::get); appendArguments(arguments::add, operator, dependencies::get); invoke(method, context, operator, arguments); dependencies.get(found).load(method); transaction.load(method); invokeResultAdd(method); method.visitJumpInsn(Opcodes.GOTO, end); method.visitLabel(onNull); dependencies.get(missed).load(method); transaction.load(method); invokeResultAdd(method); method.visitLabel(end); }
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 va2 s.c o 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 w w. j ava2 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);/*from w ww . j av a 2 s . co 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 a v a 2 s . c o m*/ } 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 ww w . ja v a 2 s .com 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);/*w ww. j a va 2 s .c o 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. * /* w w w . j a v a 2 s.com*/ * 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(); }