public String generateHashCode(Property property, String result, String thisName, boolean jdk5) { StringBuffer buf = new StringBuffer(); if (c2j.getMetaAsBool(property, "use-in-equals")) { String javaTypeName = c2j.getJavaTypeName(property, jdk5, this); boolean isPrimitive = c2j.isPrimitive(javaTypeName); if (isPrimitive) { buf.append(result).append(" = 37 * ").append(result).append(" + "); String thisValue = thisName + "." + getGetterSignature(property) + "()"; if ("char".equals(javaTypeName) || "int".equals(javaTypeName) || "short".equals(javaTypeName) || "byte".equals(javaTypeName)) { buf.append(thisValue); } else if ("boolean".equals(javaTypeName)) { buf.append("(" + thisValue + "?1:0)"); } else { buf.append("(int) "); buf.append(thisValue); } buf.append(";"); } else { if (javaTypeName.endsWith("[]")) { if (jdk5) { buf.append(result).append(" = 37 * ").append(result).append(" + "); buf.append("( ") .append(getGetterSignature(property)) .append("() == null ? 0 : " + importType("java.util.Arrays") + ".hashCode(") .append(thisName) .append(".") .append(getGetterSignature(property)) .append("())") .append(" )") .append(";"); } else { buf.append(internalGenerateArrayHashcode(property, javaTypeName, result, thisName)); } } else { buf.append(result).append(" = 37 * ").append(result).append(" + "); buf.append("( ") .append(getGetterSignature(property)) .append("() == null ? 0 : ") .append(thisName) .append(".") .append(getGetterSignature(property)) .append("()") .append(".hashCode()") .append(" )") .append(";"); } } } return buf.toString(); }
protected String generateEquals( String thisName, String otherName, Iterator<Property> allPropertiesIterator, boolean useGenerics) { StringBuffer buf = new StringBuffer(); while (allPropertiesIterator.hasNext()) { Property property = (Property) allPropertiesIterator.next(); if (buf.length() > 0) buf.append("\n && "); String javaTypeName = c2j.getJavaTypeName(property, useGenerics, this); buf.append( internalgenerateEquals( javaTypeName, thisName + "." + getGetterSignature(property) + "()", otherName + "." + getGetterSignature(property) + "()")); } if (buf.length() == 0) { return "false"; } else { return buf.toString(); } }
public String getFieldInitialization(Property p, boolean useGenerics) { if (hasMetaAttribute(p, "default-value")) { return MetaAttributeHelper.getMetaAsString(p.getMetaAttribute("default-value")); } if (c2j.getJavaTypeName(p, false) == null) { throw new IllegalArgumentException(); } else if (p.getValue() instanceof Collection) { Collection col = (Collection) p.getValue(); DefaultInitializor initialization = (DefaultInitializor) col.accept( new DefaultValueVisitor(true) { public Object accept(Bag o) { return new DefaultInitializor("java.util.ArrayList", true); } public Object accept(org.hibernate.mapping.List o) { return new DefaultInitializor("java.util.ArrayList", true); } public Object accept(org.hibernate.mapping.Map o) { if (o.isSorted()) { return new DefaultInitializor("java.util.TreeMap", false); } else { return new DefaultInitializor("java.util.HashMap", true); } } public Object accept(IdentifierBag o) { return new DefaultInitializor("java.util.ArrayList", true); } public Object accept(Set o) { if (o.isSorted()) { return new DefaultInitializor("java.util.TreeSet", false); } else { return new DefaultInitializor("java.util.HashSet", true); } } public Object accept(PrimitiveArray o) { return null; // TODO: default init for arrays ? } public Object accept(Array o) { return null; // TODO: default init for arrays ? } }); if (initialization != null) { String comparator = null; String decl = null; if (col.isSorted()) { comparator = col.getComparatorClassName(); } if (useGenerics) { decl = c2j.getGenericCollectionDeclaration((Collection) p.getValue(), true, importContext); } return initialization.getDefaultValue(comparator, decl, this); } else { return null; } } else { return null; } }
public String getJavaTypeName(Property p, boolean useGenerics) { return c2j.getJavaTypeName(p, useGenerics, this); }
/** * Method getGetterSignature. * * @return String */ public String getGetterSignature(Property p) { String prefix = c2j.getJavaTypeName(p, false).equals("boolean") ? "is" : "get"; return prefix + beanCapitalize(p.getName()); }