/** * Adds explicit imports for all {@link Type} references mentioned in the * specified {@link File}. * * @param file the file to add imports in */ @Override public void accept(File file) { findTypesIn(requireNonNull(file)).forEach( (s, t) -> file.add(Import.of(t)) ); }
/** * Copy constructor. * * @param prototype the prototype */ protected FileImpl(File prototype) { this.name = requireNonNull(prototype).getName(); this.doc = prototype.getJavadoc().map(Copier::copy).orElse(null); this.imports = Copier.copy(prototype.getImports()); this.classes = Copier.copy(prototype.getClasses(), c -> c.copy()); }
@Override public File get() { final T generated = creator.generate(document); final File file = File.of(getSupport().baseDirectoryName() + "/" + (isInGeneratedPackage() ? "generated/" : "") + generated.getName() + ".java" ); file.add(generated); file.call(new AutoImports(getCodeGenerator().getDependencyMgr())); file.call(new AlignTabs<>()); return file; }
/** * Creates a full name for the specified class in the specified file, * suitable to be used as a TypeName. * * @param file the file to reference * @param clazz the class to reference * @return the full name of the class */ public static String nameOf(File file, ClassOrInterface<?> clazz) { String name = Formatting.fileToClassName(file.getName()) .flatMap(Formatting::packageName) .orElseThrow( () -> new RuntimeException( "File did not have appropriate name." ) ) + "." + pathTo(file, clazz.getName()); return name; }
builder.forEveryTable(Translator.Phase.POST_MAKE, (intrf, table) -> { file.getImports().removeIf(i -> i.getType().equals(StringToEnumTypeMapper.class)); file.getImports().removeIf(i -> i.getType().equals(IntegerToEnumTypeMapper.class)); file.add(Import.of(OptionalUtil.class));
/** * Generates getters, setters and adders for the fields in the specified * model and sets the fields to private. Method signatures that has been * excluded in the construction of this control will be excluded from the * generation. * <p> * For fields of a subtype to {@link Collection}, an 'adder' will be * generated instead of a 'setter'. * <p> * For fields of type {@link Optional}, the inner type will be used in the * setter parameter but the wrapped type will be used as output for the * getter. * * @param file the model to generate for */ @Override public void accept(File file) { file.getClasses().stream() .filter(Class.class::isInstance) .map(Class.class::cast) .forEach(c -> accept(file, c)); }
/** * Renders the 'package'-part of the file. In java, the package should only * be present if the file is located in a directory in the sources folder. * If the file is in the global package, an empty string will be returned. * <p> * The package part will be suffixed by two new line characters if a package * was outputted. * <p> * Example: <pre>"package com.speedment.example;\n\n"</pre> * * @param file the file * @return the package part or an empty string */ private String renderPackage(File file) { final Optional<String> name = fileToClassName(file.getName()); if (name.isPresent()) { final Optional<String> pack = packageName(name.get()); if (pack.isPresent()) { return "package " + pack.get() + ";" + dnl(); } } return ""; } }
/** * Generates getters, setters and adders for the fields in the specified * model and sets the fields to private. Method signatures that has been * excluded in the construction of this control will be excluded from the * generation. * <p> * For fields of a subtype to {@link Collection}, an 'adder' will be * generated instead of a 'setter'. * <p> * For fields of type {@link Optional}, the inner type will be used in the * setter parameter but the wrapped type will be used as output for the * getter. * * @param file the model to generate for */ @Override public void accept(File file) { file.getClasses().stream() .filter(Class.class::isInstance) .map(Class.class::cast) .forEach(c -> accept(file, c)); }
public static String[] generateFieldsBody( TranslatorSupport<Table> support, File file, Supplier<Stream<? extends Column>> columnsSupplier) { file.add(Import.of(Stream.class)); // If there are no matching columns: if (columnsSupplier.get().noneMatch($ -> true)) { return new String[]{"return Stream.empty();"}; } else { final List<String> rows = new LinkedList<>(); rows.add("return Stream.of("); rows.add(indent(columnsSupplier.get() .filter(HasEnabled::isEnabled) .map(Column::getJavaName) .map(support.namer()::javaStaticFieldName) .map(field -> support.typeName() + "." + field) .collect(joining("," + nl())) )); rows.add(");"); return rows.toArray(new String[rows.size()]); } }
/** * Copy constructor. * * @param prototype the prototype */ protected FileImpl(File prototype) { this.name = requireNonNull(prototype).getName(); this.doc = prototype.getJavadoc().map(Copier::copy).orElse(null); this.imports = Copier.copy(prototype.getImports()); this.classes = Copier.copy(prototype.getClasses(), c -> c.copy()); }
public void writeToFile(TranslatorManager delegator, Project project, Meta<File, String> meta, boolean overwriteExisting) { events.notify(new FileGenerated(project, meta)); delegator.writeToFile(project, meta.getModel().getName(), meta.getResult(), overwriteExisting); }
@Override public File get() { final File file = File.of(getSupport().baseDirectoryName() + "/" + (isInGeneratedPackage() ? "generated/" : "") + getClassOrInterfaceName() + ".java" ); final T item = makeCodeGenModel(file); if (!item.getJavadoc().isPresent()) { item.set(getJavaDoc()); } file.add(item); finializeFile(file); file.call(new AutoImports(getCodeGenerator().getDependencyMgr())); file.call(new AlignTabs<>()); return file; }
/** * Generates getters, setters and adders for the fields in the specified * model and sets the fields to private. Method signatures that has been * excluded in the construction of this control will be excluded from the * generation. * <p> * For fields of a subtype to {@link Collection}, an 'adder' will be * generated instead of a 'setter'. * <p> * For fields of type {@link Optional}, the inner type will be used in the * setter parameter but the wrapped type will be used as output for the * getter. * * @param file the model to generate for */ @Override public void accept(File file) { file.getClasses().stream() .filter(Class.class::isInstance) .map(Class.class::cast) .forEach(c -> accept(file, c)); }
public static String[] generateApplyResultSetBody( ReadFromResultSet readFromResultSet, TranslatorSupport<Table> support, File file, Supplier<Stream<? extends Column>> columnsSupplier) { file.add(Import.of(SQLException.class)); final List<String> rows = new LinkedList<>(); rows.add("return createEntity()"); final Stream.Builder<String> streamBuilder = Stream.builder(); final AtomicInteger position = new AtomicInteger(1); columnsSupplier.get() .filter(HasEnabled::isEnabled) .forEachOrdered(c -> streamBuilder.add(indent(".set" + support.namer().javaTypeName(c.getJavaName()) + "(\t " + readFromResultSet.readFromResultSet(file, c, position) + ")")) ); streamBuilder.add(indent(";")); streamBuilder.build().forEachOrdered(rows::add); return rows.toArray(new String[rows.size()]); }
/** * Copy constructor. * * @param prototype the prototype */ protected FileImpl(File prototype) { this.name = requireNonNull(prototype).getName(); this.doc = prototype.getJavadoc().map(Copier::copy).orElse(null); this.imports = Copier.copy(prototype.getImports()); this.classes = Copier.copy(prototype.getClasses(), c -> c.copy()); }
@Override public Optional<String> transform(Generator gen, File model) { requireNonNulls(gen, model); final DependencyManager mgr = gen.getDependencyMgr(); mgr.clearDependencies(); final String pack = fileToClassName(model.getName()) .flatMap(Formatting::packageName) .orElse(""); mgr.setCurrentPackage(pack); final Optional<String> view = Optional.of( renderJavadoc(gen, model) + renderPackage(model) + renderImports(gen, model) + renderClasses(gen, model) ); mgr.unsetCurrentPackage(pack); return view; }
file.add(Import.of(ResultSetUtil.class).static_().setStaticMember("*")); file.add(Import.of(SimpleType.create(c.getDatabaseType()))); sb.append("(").append(shortName(c.getDatabaseType())).append(") "); } else { if (isCastingRequired(c, getterName)) { file.add(Import.of(SimpleType.create(c.getDatabaseType()))); sb.append("(").append(shortName(c.getDatabaseType())).append(") ");
@Override public Label createNode() { final Label label = new Label(""); events.on(BeforeGenerate.class, bg -> runLater(() -> label.setText("Generating..."))); events.on(FileGenerated.class, fg -> runLater(() -> label.setText(fg.meta().getModel().getName()))); events.on(AfterGenerate.class, ag -> runLater(() -> label.setText(""))); return label; }
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; }
/** * Creates a full name for the specified class in the specified file, * suitable to be used as a TypeName. * * @param file the file to reference * @param clazz the class to reference * @return the full name of the class */ public static String nameOf(File file, ClassOrInterface<?> clazz) { String name = Formatting.fileToClassName(file.getName()) .flatMap(Formatting::packageName) .orElseThrow( () -> new RuntimeException( "File did not have appropriate name." ) ) + "." + pathTo(file, clazz.getName()); return name; }