/** * 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;") ); }
/** * Copy constructor. * * @param prototype the prototype */ protected MethodImpl(final Method prototype) { parent = prototype.getParent().orElse(null); name = requireNonNull(prototype).getName(); type = requireNonNull(prototype.getType()); javadoc = prototype.getJavadoc().map(Copier::copy).orElse(null); annotations = Copier.copy(prototype.getAnnotations()); imports = Copier.copy(prototype.getImports()); generics = Copier.copy(prototype.getGenerics()); params = Copier.copy(prototype.getFields()); code = Copier.copy(prototype.getCode(), s -> s); modifiers = Copier.copy(prototype.getModifiers(), c -> c.copy(), EnumSet.noneOf(Modifier.class)); exceptions = new HashSet<>(prototype.getExceptions()); }
@Override public InterfaceMethodImpl copy() { return new InterfaceMethodImpl(m.copy()); }
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 )); }
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)); }
@Override protected Class makeCodeGenModel(File file) { requireNonNull(file); final Method getMetadata = Method.of("getMetadata", DefaultType.optional(String.class)) .protected_() .add(OVERRIDE); final Method initializer = Method.of("init", String.class).static_().private_(); final ProjectMutator<? extends Project> project = Project.deepCopy(getSupport().projectOrThrow()).mutator(); int lineCnt = 0; for (String line : seg) { subMethod.add( indent("\"" + line.replace("\\", "\\\\") .replace("\"", "\\\"") }).forEach(subMethod -> subMethod.add( ").forEachOrdered(" + STRING_BUILDER_NAME + "::append);" )); initializer.add("final StringBuilder " + STRING_BUILDER_NAME + " = new StringBuilder();"); subInitializers.stream().forEachOrdered(si -> { initializer.add(si.getName() + "(" + STRING_BUILDER_NAME + ");"); }); initializer.add("return " + STRING_BUILDER_NAME + ".toString();"); getMetadata.add("return Optional.of(METADATA);");
final Method fromDatabase = Method.of(FROM_DATABASE_METHOD, enumType) .public_().static_() .add(Field.of(DATABASE_NAME, String.class)) .add("if (" + DATABASE_NAME + " == null) return null;") .add("switch (" + DATABASE_NAME + ") {"); final Method fromDatabaseOrdinal = Method.of(FROM_DATABASE_ORDINAL_METHOD, enumType) .public_().static_() .add(Field.of(DATABASE_ORDINAL, Integer.class)) .add("if (" + DATABASE_ORDINAL + " == null) return null;") .add("switch (" + DATABASE_ORDINAL + ") {"); fromDatabase.add(indent( "case \"" + constant + "\" : return " + namer.javaStaticFieldName(constant) + ";" )); fromDatabaseOrdinal.add(indent( "case " + ordinal + " : return " + namer.javaStaticFieldName(constant) + ";" .add(indent("default : throw new UnsupportedOperationException(")) .add(indent("\"Unknown enum constant '\" + " + DATABASE_NAME + " + \"'.\"", 2)) .add(indent(");")) .add("}"); .add(indent("default : throw new UnsupportedOperationException(")) .add(indent("\"Unknown enum ordinal '\" + " + DATABASE_ORDINAL + " + \"'.\"", 2)) .add(indent(");"))
/** * Returns the a method with the specified signature exists. * * @param model the model * @param method the method name to look for * @param params the number of parameters in the signature * @return <code>true</code> if found, else <code>false</code> */ protected boolean hasMethod(T model, String method, int params) { requireNonNull(model); requireNonNull(method); requireNonNull(params); Method found = null; for (Method m : model.getMethods()) { if (method.equals(m.getName()) && m.getFields().size() == params) { found = m; break; } } return found != null; } }
@Override public InterfaceMethod set(Type type) { m.set(type); return this; }
/** * Adds an {@link Override}-annotation to this model. * * @return this instance */ default Method override() { return add(DefaultAnnotationUsage.OVERRIDE); } }
@Override public String getName() { return m.getName(); }
@Override public List<AnnotationUsage> getAnnotations() { return m.getAnnotations(); }
@Override public Set<Type> getExceptions() { return m.getExceptions(); }
@Override public List<String> getCode() { return m.getCode(); }
@SuppressWarnings("unchecked") final HasMethods<?> casted = (HasMethods<?>) model; casted.getMethods().forEach(c -> c.call(new AlignTabs<>()));
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; }
@Override protected Class makeCodeGenModel(File file) { requireNonNull(file); final Method getMetadata = Method.of("getMetadata", DefaultType.optional(String.class)) .protected_() .add(OVERRIDE); final Method initializer = Method.of("init", String.class).static_().private_(); final ProjectMutator<? extends Project> project = Project.deepCopy(getSupport().projectOrThrow()).mutator(); int lineCnt = 0; for (String line : seg) { subMethod.add( indent("\"" + line.replace("\\", "\\\\") .replace("\"", "\\\"") }).forEach(subMethod -> subMethod.add( ").forEachOrdered(" + STRING_BUILDER_NAME + "::append);" )); initializer.add("final StringBuilder " + STRING_BUILDER_NAME + " = new StringBuilder();"); subInitializers.stream().forEachOrdered(si -> { initializer.add(si.getName() + "(" + STRING_BUILDER_NAME + ");"); }); initializer.add("return " + STRING_BUILDER_NAME + ".toString();"); getMetadata.add("return Optional.of(METADATA);");
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() + "();"); }