f.private_(); if (isCollection(f.getType())) { f.final_(); final ParameterizedType paramType = (ParameterizedType) f.getType(); final Field param = Field.of(singular(f.getName()), paramType.getActualTypeArguments()[0]); final Method add = Method.of("add", self) .set(Javadoc.of() .setText("Adds the specified " + lcfirst(shortName(param.getType().getTypeName())) + " to this " + shortName(model.getName()) + ".") .add(JavadocTag.of("param", param.getName(), "the new value")) .add(JavadocTag.of("return", "a reference to this object")) ).public_() .add(param) .add("this." + f.getName() + ".add(" + param.getName() + ");") .add("return this;"); final Method set = Method.of("set" + ucfirst(f.getName()), self) .set(Javadoc.of() .setText("Sets the " + f.getName() + " of this " + shortName(model.getName()) + ".") .add(JavadocTag.of("param", f.getName(), "the new value")) .add(JavadocTag.of("return", "a reference to this object")) ).public_(); if (isOptional(f.getType())) { final ParameterizedType paramType = (ParameterizedType) f.getType();
/** * Creates a public final static {@link Field} with the specified value and * adds it to this model. * * @param name the name of the field * @param type the type of the field * @param value the value of the field * @return a reference to this * * @since 2.5 */ default T constant(final String name, Type type, Value<?> value) { return add(Field.of(name, type) .public_().final_().static_() .set(value) ); }
/** * Copy constructor. * * @param prototype the prototype */ protected FieldImpl(Field prototype) { name = requireNonNull(prototype).getName(); type = prototype.getType(); imports = Copier.copy(prototype.getImports()); value = prototype.getValue().map(Copier::copy).orElse(null); javadoc = prototype.getJavadoc().map(Copier::copy).orElse(null); annotations = Copier.copy(prototype.getAnnotations()); modifiers = Copier.copy(prototype.getModifiers(), c -> c.copy(), EnumSet.noneOf(Modifier.class)); }
/** * Creates a {@link Field} and adds it to this model. The field will be * given the default modifiers for this type, for an example, a * {@link Class} will be {@code public final}. * * @param name the name of the field * @param type the type of the field * @return a reference to this * * @since 2.5 */ default T field(final String name, Type type) { return add(Field.of(name, type).public_().final_()); }
.add(Field.of(DATABASE_NAME, String.class) .private_().final_() ).add(Field.of(DATABASE_ORDINAL, int.class) .private_().final_() ); .add(Field.of(DATABASE_NAME, String.class)) .add("if (" + DATABASE_NAME + " == null) return null;") .add("switch (" + DATABASE_NAME + ") {"); .add(Field.of(DATABASE_ORDINAL, Integer.class)) .add("if (" + DATABASE_ORDINAL + " == null) return null;") .add("switch (" + DATABASE_ORDINAL + ") {"); .add(Field.of(DATABASE_NAME, String.class)) .add(Field.of(DATABASE_ORDINAL, int.class)) .add("this." + DATABASE_NAME + " = " + DATABASE_NAME + ";") .add("this." + DATABASE_ORDINAL + " = " + DATABASE_ORDINAL + ";") .filter(f -> f.getName().equals(fieldName)) .findFirst().orElseThrow(() -> new SpeedmentGeneratorException( "Expected a static field with name '" + .add(Field.of("column", Column.class)) .add("return " + TypeMapper.Category.class.getSimpleName() + "." + TypeMapper.Category.ENUM.name() + ";") .add(Field.of("column", Column.class)) .add("return " + enumShortName + ".class;")
final Method createHelpers = Method.of(CREATE_HELPERS_METHOD_NAME, void.class) .add(withExecuteBefore(file)) .add(Field.of("projectComponent", ProjectComponent.class)) .add("final Project project = projectComponent.getProject();"); .add(Field.of("tableIdentifier", tableIdentifierType).private_().final_()) .public_() .add(OVERRIDE) .add(Field.of(OFFSET_PARAMETER_NAME, int.class)) .add("return rs -> apply(rs, offset);") ); clazz.add(Field.of(tmsName, tmsType).private_());
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; }
@Override public Optional<String> transform(Generator gen, Annotation model) { requireNonNulls(gen, model); return Optional.of( renderAnnotations(gen, model) + renderAnnotations(gen, model) + "@interface " + renderName(gen, model) + block( model.getFields().stream().map(f -> // Field javadoc (optional) renderJavadoc(gen, model) + // Field declaration gen.on(f.getType()) + " " + f.getName() + "()" + // Default value (optional) ifelse(gen.on(f.getValue()), v -> " default " + v, "") + ";" ).collect(joining(dnl())) ) ); } }
.add(SimpleParameterizedType.create(Manager.class, getSupport().entityType())) .add( Field.of( "IDENTIFIER", SimpleParameterizedType.create(TableIdentifier.class, getSupport().entityType()) ).set(Value.ofInvocation(TableIdentifier.class, "of", Stream.<HasAlias>of( table.getParentOrThrow().getParentOrThrow(), )) .add(Field.of("FIELDS", list(SimpleParameterizedType.create( com.speedment.runtime.field.Field.class, getSupport().entityType()) )).set(Value.ofReference("unmodifiableList(asList(" + nl() + indent( table.columns() .sorted(comparing(Column::getOrdinalPosition))
@Override public InterfaceField set(Type type) { f.set(type); return this; }
@Override public void accept(Class aClass) { aClass.add(Constructor.newPublic() .call(constr -> aClass.getFields().stream() .filter(f -> f.getModifiers().contains(FINAL)) .map(Field::copy) .peek(f -> f.getModifiers().clear()) .forEachOrdered(f -> { constr.add(f).imports(Objects.class, "requireNonNull"); if (isPrimitive(f.getType())) { constr.add(format("this.%1$s = %1$s;", f.getName())); } else { constr.add(format("this.%1$s = requireNonNull(%1$s);", f.getName())); } }) ) ); } }
@Override public Optional<Value<?>> getValue() { return f.getValue(); }
@Override public String getName() { return f.getName(); }
/** * Sets all method parameters in the specified model to <code>final</code>. * * @param model the model to operate on */ @Override public void accept(T model) { requireNonNull(model).getMethods() .forEach(m -> m.getFields() .forEach(f -> f.final_()) ); } }
@Override public List<AnnotationUsage> getAnnotations() { return f.getAnnotations(); }
@Override public InterfaceFieldImpl copy() { return new InterfaceFieldImpl(f.copy()); }
@Override public Set<Modifier> getModifiers() { return f.getModifiers(); }