private Method addNewSubMethod(List<Method> methods) { final Method m = Method.of(INIT_PART_METHOD_NAME + methods.size(), void.class).private_().static_() .add(Field.of(STRING_BUILDER_NAME, StringBuilder.class)); methods.add(m); m.add("Stream.of("); return m; }
private Method hashCodeMethod(File file) { final Method method = Method.of("hashCode", int.class) .public_() .add(OVERRIDE) .add("int hash = 7;"); columns().forEachOrdered(c -> { final StringBuilder str = new StringBuilder(); str.append("hash = 31 * hash + "); final Type type = typeMappers.get(c).getJavaType(c); if (!usesOptional(c) && DefaultType.isPrimitive(type)) { str.append(DefaultType.wrapperFor(type).getSimpleName()); } else { str.append("Objects"); } str.append(".hashCode("); if (usesOptional(c)) { file.add(Import.of(OptionalUtil.class)); str.append("OptionalUtil.unwrap("); } str.append("get").append(getSupport().typeName(c)).append("()"); if (usesOptional(c)) str.append(')'); str.append(");"); method.add(str.toString()); }); method.add("return hash;"); return method; }
/** * The <code>hashCode()</code>-part of the <code>accept</code> method. * * @param model the model */ protected void acceptHashcode(T model) { requireNonNull(model); model.add(Method.of(HASHCODE, int.class) .set( Javadoc.of( "Generates a hashCode for this object. If any field is " + "changed to another value, the hashCode may be different. " + "Two objects with the same values are guaranteed to have " + "the same hashCode. Two objects with the same hashCode are " + "not guaranteed to have the same hashCode." ) .add(RETURN.setText("The hash code.")) ).public_() .add(OVERRIDE) .add("int hash = 7;") .add(model.getFields().stream() .map(this::hash) .collect(Collectors.joining(nl())) ) .add("return hash;") ); }
file.add(Import.of(applicationImplType())); final Method build = Method.of("build", applicationType()) .public_().add(OVERRIDE) .add(Field.of("injector", Injector.class))
@Override protected Class makeCodeGenModel(File file) { requireNonNull(file); final Method getMetadata = Method.of("getMetadata", DefaultType.optional(String.class)) .protected_() .add(OVERRIDE); .private_().final_().static_(); final Method initializer = Method.of("init", String.class).static_().private_(); final ProjectMutator<? extends Project> project = Project.deepCopy(getSupport().projectOrThrow()).mutator();
private Method generateCreateEntity(File file) { final Type entityImplType = getSupport().entityImplType(); file.add(Import.of(entityImplType)); return Method.of("createEntity", entityImplType).protected_() .add("return new " + getSupport().entityImplName() + "();"); }
public static Method generateFields( TranslatorSupport<Table> support, File file, String methodName, Supplier<Stream<? extends Column>> columnsSupplier) { return Method.of(methodName, DefaultType.stream( SimpleParameterizedType.create( com.speedment.runtime.field.Field.class, support.entityType() ) ) ) .public_().add(OVERRIDE) .add(generateFieldsBody(support, file, columnsSupplier)); }
private Method generateApplyResultSet( TranslatorSupport<Table> support, File file, Supplier<Stream<? extends Column>> columnsSupplier) { return Method.of("apply", support.entityType()) .protected_() .add(SQLException.class) .add(Field.of("resultSet", ResultSet.class)) .add(Field.of(OFFSET_PARAMETER_NAME, int.class)) .add(generateApplyResultSetBody( this::readFromResultSet, support, file, columnsSupplier )); }
model.add(Method.of(EQUALS, boolean.class) .set( Javadoc.of(
final Method createHelpers = Method.of(CREATE_HELPERS_METHOD_NAME, void.class) .add(withExecuteBefore(file)) .add(Field.of("projectComponent", ProjectComponent.class)) .add(generateCreateEntity(file)) .add( Method.of("identifier", tableIdentifierType) .public_() .add(OVERRIDE) Method.of("entityMapper", SimpleParameterizedType.create(SqlFunction.class, ResultSet.class, getSupport().entityType())) .public_() .add(OVERRIDE) Method.of("entityMapper", SimpleParameterizedType.create(SqlFunction.class, ResultSet.class, getSupport().entityType())) .public_() .add(OVERRIDE)
final Method fromDatabase = Method.of(FROM_DATABASE_METHOD, enumType) .public_().static_() .add(Field.of(DATABASE_NAME, String.class)) final Method fromDatabaseOrdinal = Method.of(FROM_DATABASE_ORDINAL_METHOD, enumType) .public_().static_() .add(Field.of(DATABASE_ORDINAL, Integer.class)) colEnum.add(Method.of(TO_DATABASE_METHOD, String.class) .public_().add("return " + DATABASE_NAME + ";") ); colEnum.add(Method.of(TO_DATABASE_ORDINAL_METHOD, int.class) .public_().add("return " + DATABASE_ORDINAL + ";") ); .add(wrapIfPrimitive(dbType)) .add(enumType) .add(Method.of("getLabel", String.class) .public_().add(OVERRIDE) .add("return \"" + shortName(dbType.getTypeName()) + " to " + enumShortName + " Mapper\";") .add(Method.of("getOrdering", TypeMapper.Ordering.class) .public_().add(OVERRIDE) .add("return " + TypeMapper.Ordering.class.getSimpleName() + "." + TypeMapper.Ordering.RETAIN.name() + ";") .add(Method.of("getJavaTypeCategory", TypeMapper.Category.class)
.collect(joining("," + nl())) ) + nl() + "))"))) .add(Method.of("getEntityClass", classOf(getSupport().entityType())) .default_().add(OVERRIDE) .add("return " + getSupport().entityName() + ".class;")
final Method add = Method.of("add", self) .set(Javadoc.of() .setText("Adds the specified " + lcfirst(shortName(param.getType().getTypeName())) + " to this " + shortName(model.getName()) + ".") final Method set = Method.of("set" + ucfirst(f.getName()), self) .set(Javadoc.of() .setText("Sets the " + f.getName() + " of this " + shortName(model.getName()) + ".") final Method get = Method.of("get" + ucfirst(f.getName()), f.getType()) .set(Javadoc.of() .setText("Gets the " + f.getName() + " of this " + shortName(model.getName()) + ".")
protected Method toStringMethod(File file) { file.add(Import.of(StringJoiner.class)); file.add(Import.of(Objects.class)); final Method m = Method.of("toString", String.class) .public_() .add(OVERRIDE) .add("final StringJoiner sj = new StringJoiner(\", \", \"{ \", \" }\");"); columns().forEachOrdered(col -> { final String getter; if (usesOptional(col)) { file.add(Import.of(OptionalUtil.class)); getter = "OptionalUtil.unwrap(get" + getSupport().typeName(col) + "())"; } else { getter = "get" + getSupport().typeName(col) + "()"; } m.add("sj.add(\"" + getSupport().variableName(col) + " = \" \t+ Objects.toString(" + getter + "));"); }); m.add("return \"" + getSupport().entityImplName() + " \" + sj.toString();"); return m; }
private Method equalsMethod() { final String thatName = "that"; final String thatCastedName = thatName + getSupport().entityName(); final Method method = Method.of("equals", boolean.class) .public_() .add(OVERRIDE) .add(Field.of(thatName, Object.class)) .add("if (this == that) { return true; }") .add("if (!(" + thatName + " instanceof " + getSupport().entityName() + ")) { return false; }") .add("final " + getSupport().entityName() + " " + thatCastedName + " = (" + getSupport().entityName() + ")" + thatName + ";"); columns().forEachOrdered(c -> { final String getter = "get" + getSupport().typeName(c); final Type type = typeMappers.get(c).getJavaType(c); if (usesOptional(c) || !DefaultType.isPrimitive(type)) { method.add("if (!Objects.equals(this." + getter + "(), " + thatCastedName + "." + getter + "())) { return false; }"); } else { method.add("if (this." + getter + "() != " + thatCastedName + "." + getter + "()) { return false; }"); } }); method.add("return true;"); return method; }
.add(Method.of("getDbmsId", String.class).public_() .add(OVERRIDE) .add(returnString(getSupport().dbmsOrThrow().getId())) .add(Method.of("getSchemaId", String.class).public_() .add(OVERRIDE) .add(returnString(getSupport().schemaOrThrow().getId())) .add(Method.of("getTableId", String.class).public_() .add(OVERRIDE) .add(returnString(getSupport().tableOrThrow().getId())) .add(Method.of("getColumnId", String.class).public_() .add(OVERRIDE) .add("return this.columnId;") .add(Method.of("asTableIdentifier", tableIdentifierType).public_() .add(OVERRIDE) .add("return this.tableIdentifier;") intrf.add(Method.of(GETTER_METHOD_PREFIX + getSupport().typeName(col), retType) .set(Javadoc.of( "Returns the " + getSupport().variableName(col) intrf.add(Method.of(SETTER_METHOD_PREFIX + getSupport().typeName(col), getSupport().entityType()) .add(Field.of(getSupport().variableName(col), typeMappers.get(col).getJavaType(col))) .set(Javadoc.of( intrf.add(Method.of(FINDER_METHOD_PREFIX + getSupport().typeName(col),
.add(Method.of(GETTER_METHOD_PREFIX + getSupport().typeName(col), retType) .public_().add(OVERRIDE) .add("return " + getter + ";")); .add(Method.of(SETTER_METHOD_PREFIX + getSupport().typeName(col), getSupport().entityType()) .public_() .add(OVERRIDE) final String getterName = optionalGetterName(typeMappers, col).orElse(""); clazz.add(Method.of(FINDER_METHOD_PREFIX + getSupport().typeName(col), col.isNullable() ? DefaultType.optional(fuSupport.entityType())
private Method addNewSubMethod(List<Method> methods) { final Method m = Method.of(INIT_PART_METHOD_NAME + methods.size(), void.class).private_().static_() .add(Field.of(STRING_BUILDER_NAME, StringBuilder.class)); methods.add(m); m.add("Stream.of("); return m; }
private Method generateCreateEntity(File file) { final Type entityImplType = getSupport().entityImplType(); file.add(Import.of(entityImplType)); return Method.of("createEntity", entityImplType).protected_() .add("return new " + getSupport().entityImplName() + "();"); }