Search in sources :

Example 1 with JumpInsnNode

use of org.apache.xbean.asm5.tree.JumpInsnNode in project apex-malhar by apache.

the class BeanClassGenerator method addHashCodeMethod.

/**
 * This will add a hashCode method for class being generated. <br>
 * Algorithm is as follows: <br>
 * <i><p>
 * int hashCode = 7;
 * for (field: all fields) {
 * hashCode = 23 * hashCode + field.hashCode()
 * }
 * </p></i>
 * <br>
 * <b> For primitive field, hashcode implemenented is similar to the one present in its wrapper class. </b>
 *
 * @param classNode
 * @param fieldList
 * @throws JSONException
 */
@SuppressWarnings("unchecked")
private static void addHashCodeMethod(ClassNode classNode, List<TupleSchemaRegistry.SQLFieldInfo> fieldList) throws JSONException {
    MethodNode hashCodeNode = new MethodNode(Opcodes.ACC_PUBLIC, "hashCode", "()I", null, null);
    hashCodeNode.visitAnnotation("Ljava/lang/Override;", true);
    hashCodeNode.instructions.add(new IntInsnNode(Opcodes.BIPUSH, 7));
    hashCodeNode.instructions.add(new VarInsnNode(Opcodes.ISTORE, 1));
    for (TupleSchemaRegistry.SQLFieldInfo fieldInfo : fieldList) {
        String fieldName = fieldInfo.getColumnName();
        String fieldType = fieldInfo.getType().getJavaType().getName();
        String fieldJavaType = getJavaType(fieldType);
        hashCodeNode.instructions.add(new IntInsnNode(Opcodes.BIPUSH, 23));
        hashCodeNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, 1));
        hashCodeNode.instructions.add(new InsnNode(Opcodes.IMUL));
        hashCodeNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        hashCodeNode.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldName, fieldJavaType));
        switch(fieldType) {
            case "boolean":
                LabelNode falseNode = new LabelNode();
                LabelNode trueNode = new LabelNode();
                hashCodeNode.instructions.add(new JumpInsnNode(Opcodes.IFEQ, falseNode));
                hashCodeNode.instructions.add(new IntInsnNode(Opcodes.SIPUSH, 1231));
                hashCodeNode.instructions.add(new JumpInsnNode(Opcodes.GOTO, trueNode));
                hashCodeNode.instructions.add(falseNode);
                hashCodeNode.instructions.add(new IntInsnNode(Opcodes.SIPUSH, 1237));
                hashCodeNode.instructions.add(trueNode);
                break;
            case "byte":
            case "char":
            case "short":
            case "int":
                break;
            case "float":
                hashCodeNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Float", "floatToIntBits", "(F)I", false));
                break;
            case "long":
                hashCodeNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
                hashCodeNode.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldName, fieldJavaType));
                hashCodeNode.instructions.add(new IntInsnNode(Opcodes.BIPUSH, 32));
                hashCodeNode.instructions.add(new InsnNode(Opcodes.LUSHR));
                hashCodeNode.instructions.add(new InsnNode(Opcodes.LXOR));
                hashCodeNode.instructions.add(new InsnNode(Opcodes.L2I));
                break;
            case "double":
                hashCodeNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Double", "doubleToLongBits", "(D)J", false));
                hashCodeNode.instructions.add(new InsnNode(Opcodes.DUP2));
                hashCodeNode.instructions.add(new VarInsnNode(Opcodes.LSTORE, 2));
                hashCodeNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, 2));
                hashCodeNode.instructions.add(new IntInsnNode(Opcodes.BIPUSH, 32));
                hashCodeNode.instructions.add(new InsnNode(Opcodes.LUSHR));
                hashCodeNode.instructions.add(new InsnNode(Opcodes.LXOR));
                hashCodeNode.instructions.add(new InsnNode(Opcodes.L2I));
                break;
            default:
                String objectOwnerType = fieldType.replace('.', '/');
                LabelNode nullNode = new LabelNode();
                LabelNode continueNode = new LabelNode();
                hashCodeNode.instructions.add(new JumpInsnNode(Opcodes.IFNULL, nullNode));
                hashCodeNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
                hashCodeNode.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldName, fieldJavaType));
                hashCodeNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, objectOwnerType, "hashCode", "()I", false));
                hashCodeNode.instructions.add(new JumpInsnNode(Opcodes.GOTO, continueNode));
                hashCodeNode.instructions.add(nullNode);
                hashCodeNode.instructions.add(new InsnNode(Opcodes.ICONST_0));
                hashCodeNode.instructions.add(continueNode);
                break;
        }
        hashCodeNode.instructions.add(new InsnNode(Opcodes.IADD));
        hashCodeNode.instructions.add(new VarInsnNode(Opcodes.ISTORE, 1));
    }
    hashCodeNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, 1));
    hashCodeNode.instructions.add(new InsnNode(Opcodes.IRETURN));
    classNode.methods.add(hashCodeNode);
}
Also used : LabelNode(org.apache.xbean.asm5.tree.LabelNode) FieldInsnNode(org.apache.xbean.asm5.tree.FieldInsnNode) JumpInsnNode(org.apache.xbean.asm5.tree.JumpInsnNode) TypeInsnNode(org.apache.xbean.asm5.tree.TypeInsnNode) InsnNode(org.apache.xbean.asm5.tree.InsnNode) VarInsnNode(org.apache.xbean.asm5.tree.VarInsnNode) MethodInsnNode(org.apache.xbean.asm5.tree.MethodInsnNode) IntInsnNode(org.apache.xbean.asm5.tree.IntInsnNode) LdcInsnNode(org.apache.xbean.asm5.tree.LdcInsnNode) MethodNode(org.apache.xbean.asm5.tree.MethodNode) TupleSchemaRegistry(org.apache.apex.malhar.sql.schema.TupleSchemaRegistry) MethodInsnNode(org.apache.xbean.asm5.tree.MethodInsnNode) JumpInsnNode(org.apache.xbean.asm5.tree.JumpInsnNode) IntInsnNode(org.apache.xbean.asm5.tree.IntInsnNode) FieldInsnNode(org.apache.xbean.asm5.tree.FieldInsnNode) VarInsnNode(org.apache.xbean.asm5.tree.VarInsnNode)

Example 2 with JumpInsnNode

use of org.apache.xbean.asm5.tree.JumpInsnNode in project apex-malhar by apache.

the class BeanClassGenerator method addEqualsMethod.

/**
 * Adds a equals method to underlying class. <br>
 * Algorithm is as follows: <br>
 * <i><p>
 * if (this == other) return true;
 * if (other == null) return false;
 * if (other is not instanceof <this class>) return false;
 * for (field: all fields) {
 * if (other.getField() != this.field) return false;
 * }
 * return true;
 * </p></i>
 * <br>
 *
 * @param classNode
 * @param fieldList
 * @throws JSONException
 */
@SuppressWarnings("unchecked")
private static void addEqualsMethod(ClassNode classNode, List<TupleSchemaRegistry.SQLFieldInfo> fieldList) throws JSONException {
    MethodNode equalsNode = new MethodNode(Opcodes.ACC_PUBLIC, "equals", "(Ljava/lang/Object;)Z", null, null);
    equalsNode.visitAnnotation("Ljava/lang/Override;", true);
    LabelNode l0 = new LabelNode();
    LabelNode l1 = new LabelNode();
    LabelNode l2 = new LabelNode();
    LabelNode l3 = new LabelNode();
    LabelNode l4 = new LabelNode();
    equalsNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    // if (this == other) return true;
    equalsNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
    equalsNode.instructions.add(new JumpInsnNode(Opcodes.IF_ACMPNE, l0));
    equalsNode.instructions.add(new InsnNode(Opcodes.ICONST_1));
    equalsNode.instructions.add(new InsnNode(Opcodes.IRETURN));
    equalsNode.instructions.add(l0);
    // if (other == null) return false;
    equalsNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
    equalsNode.instructions.add(new JumpInsnNode(Opcodes.IFNULL, l1));
    // if (!(other instanceof <this class>)) return false;
    equalsNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
    equalsNode.instructions.add(new TypeInsnNode(Opcodes.INSTANCEOF, classNode.name));
    equalsNode.instructions.add(new JumpInsnNode(Opcodes.IFNE, l2));
    equalsNode.instructions.add(l1);
    equalsNode.instructions.add(new InsnNode(Opcodes.ICONST_0));
    equalsNode.instructions.add(new InsnNode(Opcodes.IRETURN));
    equalsNode.instructions.add(l2);
    // Check if it other object can cast to <this class>
    equalsNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
    equalsNode.instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, classNode.name));
    equalsNode.instructions.add(new VarInsnNode(Opcodes.ASTORE, 2));
    for (int i = 0; i < fieldList.size(); i++) {
        boolean isLast = ((i + 1) == fieldList.size());
        TupleSchemaRegistry.SQLFieldInfo info = fieldList.get(i);
        String fieldName = info.getColumnName();
        String fieldType = info.getType().getJavaType().getName();
        String fieldJavaType = getJavaType(fieldType);
        String getterMethodName = (fieldType.equals("boolean") ? "is" : "get") + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
        equalsNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 2));
        equalsNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, getterMethodName, "()" + fieldJavaType, false));
        equalsNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        equalsNode.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldName, fieldJavaType));
        switch(fieldType) {
            case "boolean":
            case "byte":
            case "char":
            case "short":
            case "int":
                equalsNode.instructions.add(new JumpInsnNode(isLast ? Opcodes.IF_ICMPEQ : Opcodes.IF_ICMPNE, isLast ? l4 : l3));
                break;
            case "long":
                equalsNode.instructions.add(new InsnNode(Opcodes.LCMP));
                equalsNode.instructions.add(new JumpInsnNode(isLast ? Opcodes.IFEQ : Opcodes.IFNE, isLast ? l4 : l3));
                break;
            case "float":
                equalsNode.instructions.add(new InsnNode(Opcodes.FCMPL));
                equalsNode.instructions.add(new JumpInsnNode(isLast ? Opcodes.IFEQ : Opcodes.IFNE, isLast ? l4 : l3));
                break;
            case "double":
                equalsNode.instructions.add(new InsnNode(Opcodes.DCMPL));
                equalsNode.instructions.add(new JumpInsnNode(isLast ? Opcodes.IFEQ : Opcodes.IFNE, isLast ? l4 : l3));
                break;
            default:
                String objectOwnerType = fieldType.replace('.', '/');
                LabelNode nonNullNode = new LabelNode();
                LabelNode continueNode = new LabelNode();
                equalsNode.instructions.add(new JumpInsnNode(Opcodes.IFNONNULL, nonNullNode));
                equalsNode.instructions.add(new JumpInsnNode(isLast ? Opcodes.IFNULL : Opcodes.IFNONNULL, isLast ? l4 : l3));
                equalsNode.instructions.add(new JumpInsnNode(Opcodes.GOTO, continueNode));
                equalsNode.instructions.add(nonNullNode);
                equalsNode.instructions.add(new InsnNode(Opcodes.POP));
                equalsNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
                equalsNode.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldName, fieldJavaType));
                equalsNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 2));
                equalsNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, getterMethodName, "()" + fieldJavaType, false));
                equalsNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, objectOwnerType, "equals", "(Ljava/lang/Object;)Z", false));
                equalsNode.instructions.add(new JumpInsnNode(isLast ? Opcodes.IFNE : Opcodes.IFEQ, isLast ? l4 : l3));
                equalsNode.instructions.add(continueNode);
                break;
        }
    }
    equalsNode.instructions.add(l3);
    equalsNode.instructions.add(new InsnNode(Opcodes.ICONST_0));
    equalsNode.instructions.add(new InsnNode(Opcodes.IRETURN));
    equalsNode.instructions.add(l4);
    equalsNode.instructions.add(new InsnNode(Opcodes.ICONST_1));
    equalsNode.instructions.add(new InsnNode(Opcodes.IRETURN));
    classNode.methods.add(equalsNode);
}
Also used : LabelNode(org.apache.xbean.asm5.tree.LabelNode) TupleSchemaRegistry(org.apache.apex.malhar.sql.schema.TupleSchemaRegistry) FieldInsnNode(org.apache.xbean.asm5.tree.FieldInsnNode) TypeInsnNode(org.apache.xbean.asm5.tree.TypeInsnNode) FieldInsnNode(org.apache.xbean.asm5.tree.FieldInsnNode) JumpInsnNode(org.apache.xbean.asm5.tree.JumpInsnNode) TypeInsnNode(org.apache.xbean.asm5.tree.TypeInsnNode) InsnNode(org.apache.xbean.asm5.tree.InsnNode) VarInsnNode(org.apache.xbean.asm5.tree.VarInsnNode) MethodInsnNode(org.apache.xbean.asm5.tree.MethodInsnNode) IntInsnNode(org.apache.xbean.asm5.tree.IntInsnNode) LdcInsnNode(org.apache.xbean.asm5.tree.LdcInsnNode) MethodNode(org.apache.xbean.asm5.tree.MethodNode) MethodInsnNode(org.apache.xbean.asm5.tree.MethodInsnNode) JumpInsnNode(org.apache.xbean.asm5.tree.JumpInsnNode) VarInsnNode(org.apache.xbean.asm5.tree.VarInsnNode)

Aggregations

TupleSchemaRegistry (org.apache.apex.malhar.sql.schema.TupleSchemaRegistry)2 FieldInsnNode (org.apache.xbean.asm5.tree.FieldInsnNode)2 InsnNode (org.apache.xbean.asm5.tree.InsnNode)2 IntInsnNode (org.apache.xbean.asm5.tree.IntInsnNode)2 JumpInsnNode (org.apache.xbean.asm5.tree.JumpInsnNode)2 LabelNode (org.apache.xbean.asm5.tree.LabelNode)2 LdcInsnNode (org.apache.xbean.asm5.tree.LdcInsnNode)2 MethodInsnNode (org.apache.xbean.asm5.tree.MethodInsnNode)2 MethodNode (org.apache.xbean.asm5.tree.MethodNode)2 TypeInsnNode (org.apache.xbean.asm5.tree.TypeInsnNode)2 VarInsnNode (org.apache.xbean.asm5.tree.VarInsnNode)2