private static String toEquals(JavaType type, Collection<JavaProperty> properties) { String simpleName = type.getClassName(); JavaType superClass = type.getSuperClass(); StringBuilder sb = new StringBuilder(); sb.append("\n"); sb.append("if (this == o) return true;").append("\n"); sb.append("if (o == null || getClass() != o.getClass()) return false;").append("\n"); //If base fluent is the superclass just skip. if (!Constants.BASE_FLUENT.getClassName().equals(superClass.getClassName())) { sb.append("if (!super.equals(o)) return false;").append("\n"); } sb.append(simpleName).append(" that = (").append(simpleName).append(") o;").append("\n"); for (JavaProperty property : properties) { String name = property.getName(); if (BuilderUtils.isPrimitive(property.getType())) { sb.append("if (").append(name).append(" != ").append("that.").append(name).append(") return false;").append("\n"); } else if (BuilderUtils.isDescendant(type, property.getType())) { sb.append("if (").append(name).append(" != null &&").append(name).append(" != this ? !").append(name).append(".equals(that.").append(name).append(") :") .append("that.").append(name).append(" != null &&").append(name).append(" != this ) return false;").append("\n"); } else { sb.append("if (").append(name).append(" != null ? !").append(name).append(".equals(that.").append(name).append(") :") .append("that.").append(name).append(" != null) return false;").append("\n"); } } sb.append("return true;").append("\n"); return sb.toString(); }
/** * Checks if a type is an descendant of an other type * @param item The base type. * @param candidate The candidate type. * @return true if candidate is a descendant of base type. */ public static boolean isDescendant(JavaType item, JavaType candidate) { if (item == null || candidate == null) { return false; } else if (item.equals(candidate)) { return true; } else if (isDescendant(item.getSuperClass(), candidate)) { return true; } else { for (JavaType interfaceType : item.getInterfaces()) { if (isDescendant(interfaceType, candidate)) { return true; } } return false; } }
@Override public JavaType apply(JavaType item) { BuilderContext ctx = BuilderContextManager.getContext(); JavaType fluent = SHALLOW_FLUENT.apply(item); List<JavaType> generics = new ArrayList<JavaType>(); for (JavaType generic : item.getGenericTypes()) { generics.add(generic); } JavaType generic = typeExtends(Constants.T, fluent); generics.add(generic); JavaType superClass = isBuildable(item.getSuperClass()) ? SHALLOW_FLUENT.apply(item.getSuperClass()) : ctx.getBaseFluentClass().getType(); return new JavaTypeBuilder(item) .withClassName(item.getClassName() + "Fluent") .withPackageName(item.getPackageName()) .withGenericTypes(generics.toArray(new JavaType[generics.size()])) .withSuperClass(superClass) .withInterfaces(new HashSet(Arrays.asList(ctx.getFluentInterface().getType()))) .build(); }