use of org.apache.xbean.asm5.tree.MethodNode in project apex-malhar by apache.
the class BeanClassGenerator method addGetter.
/**
* Add public getter method for given field
* @param classNode ClassNode which needs to be populated with public getter.
* @param fieldName Name of the field for which public getter needs to be added.
* @param fieldNameForMethods Suffix of the getter method, Prefix "is" or "get" is added by this method.
* @param fieldJavaType Java ASM type of the field
*/
@SuppressWarnings("unchecked")
private static void addGetter(ClassNode classNode, String fieldName, String fieldNameForMethods, String fieldJavaType) {
String getterSignature = "()" + fieldJavaType;
MethodNode getterNode = new MethodNode(Opcodes.ACC_PUBLIC, (fieldJavaType.equals(typeIdentifierBoolean) ? "is" : "get") + fieldNameForMethods, getterSignature, null, null);
getterNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
getterNode.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldName, fieldJavaType));
int returnOpCode;
if (fieldJavaType.equals(Character.toString(typeIdentifierBoolean)) || fieldJavaType.equals(Character.toString(typeIdentifierByte)) || fieldJavaType.equals(Character.toString(typeIdentifierChar)) || fieldJavaType.equals(Character.toString(typeIdentifierShort)) || fieldJavaType.equals(Character.toString(typeIdentifierInt))) {
returnOpCode = Opcodes.IRETURN;
} else if (fieldJavaType.equals(Character.toString(typeIdentifierLong))) {
returnOpCode = Opcodes.LRETURN;
} else if (fieldJavaType.equals(Character.toString(typeIdentifierFloat))) {
returnOpCode = Opcodes.FRETURN;
} else if (fieldJavaType.equals(Character.toString(typeIdentifierDouble))) {
returnOpCode = Opcodes.DRETURN;
} else {
returnOpCode = Opcodes.ARETURN;
}
getterNode.instructions.add(new InsnNode(returnOpCode));
classNode.methods.add(getterNode);
}
use of org.apache.xbean.asm5.tree.MethodNode in project apex-malhar by apache.
the class BeanClassGenerator method addDefaultConstructor.
/**
* Add Default constructor for POJO
* @param classNode ClassNode which needs to be populated with constructor
*/
@SuppressWarnings("unchecked")
private static void addDefaultConstructor(ClassNode classNode) {
MethodNode constructorNode = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
constructorNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
constructorNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false));
constructorNode.instructions.add(new InsnNode(Opcodes.RETURN));
classNode.methods.add(constructorNode);
}
use of org.apache.xbean.asm5.tree.MethodNode 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);
}
use of org.apache.xbean.asm5.tree.MethodNode in project apex-core by apache.
the class ASMUtil method getPublicSetter.
/**
* Get a list of setter methods from classnode asmNode
* @param asmNode
* @return empty list if there is no setter method
*/
public static List<MethodNode> getPublicSetter(ClassNode asmNode) {
List<MethodNode> result = new LinkedList<>();
@SuppressWarnings("unchecked") List<MethodNode> mList = asmNode.methods;
for (MethodNode methodNode : mList) {
if (methodNode.name.startsWith("set") && isPublic(methodNode.access) && Type.getArgumentTypes(methodNode.desc).length == 1 && Type.getReturnType(methodNode.desc) == Type.VOID_TYPE) {
result.add(methodNode);
}
}
return result;
}
use of org.apache.xbean.asm5.tree.MethodNode in project apex-core by apache.
the class CompactUtil method compactClassNode.
public static CompactClassNode compactClassNode(ClassNode cn) {
if (cn == null) {
return null;
}
CompactClassNode ccn = new CompactClassNode();
ccn.setAccess(cn.access);
ccn.setDefaultConstructor(compactMethodNode(ASMUtil.getPublicDefaultConstructor(cn)));
List<CompactMethodNode> cmns = new LinkedList<>();
for (MethodNode mn : ASMUtil.getPublicGetter(cn)) {
cmns.add(compactMethodNode(mn));
}
ccn.setGetterMethods(cmns);
cmns = new LinkedList<>();
for (MethodNode mn : ASMUtil.getPublicSetter(cn)) {
cmns.add(compactMethodNode(mn));
}
ccn.setSetterMethods(cmns);
ccn.setPorts(new LinkedList<CompactFieldNode>());
ccn.setName(cn.name);
List<CompactClassNode> ccns = new LinkedList<>();
for (Object icn : cn.innerClasses) {
CompactClassNode inner = new CompactClassNode();
inner.setName(((InnerClassNode) icn).name);
inner.setAccess(((InnerClassNode) icn).access);
}
ccn.setInnerClasses(ccns);
if (ASMUtil.isEnum(cn)) {
ccn.setEnumValues(ASMUtil.getEnumValues(cn));
}
if (cn instanceof ClassNodeType) {
ccn.setCsv(((ClassNodeType) cn).csv);
}
// }
return ccn;
}
Aggregations