Skip to content

Commit

Permalink
New files now generate
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Feb 18, 2016
1 parent d7d8aa9 commit 0bca692
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 62 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/speedment/Manager.java
Expand Up @@ -119,6 +119,13 @@ public interface Manager<ENTITY> extends Lifecyclable<Manager<ENTITY>> {
* @return the entity class for this Manager
*/
Class<ENTITY> getEntityClass();

/**
* Returns the entity class for this Manager.
*
* @return the entity class for this Manager
*/
Class<? extends Manager<ENTITY>> getManagerClass();

// Json
/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/speedment/code/StandardTranslatorKey.java
Expand Up @@ -40,14 +40,14 @@ public final class StandardTranslatorKey {
public final static TranslatorKey<Table, Interface>
ENTITY = new TranslatorKeyImpl<>("Entity", Interface.class),
MANAGER = new TranslatorKeyImpl<>("Manager", Interface.class),
GENERATED_ENTITY = new TranslatorKeyImpl<>("Entity", Interface.class),
GENERATED_MANAGER = new TranslatorKeyImpl<>("Manager", Interface.class);
GENERATED_ENTITY = new TranslatorKeyImpl<>("GeneratedEntity", Interface.class),
GENERATED_MANAGER = new TranslatorKeyImpl<>("GeneratedManager", Interface.class);

public final static TranslatorKey<Table, Class>
ENTITY_IMPL = new TranslatorKeyImpl<>("EntityImpl", Class.class),
MANAGER_IMPL = new TranslatorKeyImpl<>("ManagerImpl", Class.class),
GENERATED_ENTITY_IMPL = new TranslatorKeyImpl<>("EntityImpl", Class.class),
GENERATED_MANAGER_IMPL = new TranslatorKeyImpl<>("ManagerImpl", Class.class);
GENERATED_ENTITY_IMPL = new TranslatorKeyImpl<>("GeneratedEntityImpl", Class.class),
GENERATED_MANAGER_IMPL = new TranslatorKeyImpl<>("GeneratedManagerImpl", Class.class);

public static Stream<TranslatorKey<Project, Class>> projectTranslatorKeys() {
return Stream.of(SPEEDMENT_APPLICATION, SPEEDMENT_APPLICATION_METADATA);
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/speedment/internal/codegen/util/Formatting.java
Expand Up @@ -79,8 +79,8 @@ public static String withFirst(String input, Function<Character, String> callbac
return EMPTY;
} else {
return String.join(EMPTY,
callback.apply(input.charAt(0)),
input.subSequence(1, input.length())
callback.apply(input.charAt(0)),
input.subSequence(1, input.length())
);
}
}
Expand Down Expand Up @@ -150,6 +150,18 @@ public static String indent(String text) {
return tab + text.replaceAll("\\r?\\n", nltab);
}

/**
* Indents one level after each new-line-character as defined by
* <code>nl()</code>. f multiple strings are specified, new-line characters
* will be added between them.
*
* @param text The text to indent.
* @return The indented text.
*/
public static String indent(String... text) {
return tab + String.join(nl(), text).replaceAll("\\r?\\n", nltab);
}

/**
* Indents a variable number of levels level after each new-line-character
* as defined by <code>nl()</code>.
Expand Down
Expand Up @@ -32,19 +32,18 @@
*/
public abstract class AbstractBaseEntity<ENTITY> implements Entity<ENTITY> {

private final transient Speedment speedment;

public AbstractBaseEntity(Speedment speedment) {
this.speedment = requireNonNull(speedment);
}

protected Speedment speedment() {
return speedment;
}
/**
* Returns the Speedment instance. This method will be implemented by the
* instantiating class using an anonymous class and should therefore not be
* implemented in named child classes.
*
* @return the {@link Speedment} instance
*/
protected abstract Speedment speedment();

@Override
public String toJson(JsonEncoder<ENTITY> jsonFormatter) {
return requireNonNull(jsonFormatter).apply((selfAsEntity()));
public String toJson(JsonEncoder<ENTITY> encoder) {
return requireNonNull(encoder).apply((selfAsEntity()));
}

@Override
Expand Down
Expand Up @@ -76,7 +76,6 @@ public abstract class DefaultJavaClassTranslator<DOC extends Document & HasName

public static final String GETTER_METHOD_PREFIX = "get",
SETTER_METHOD_PREFIX = "set",
BUILDER_METHOD_PREFIX = SETTER_METHOD_PREFIX,
JAVADOC_MESSAGE =
"\n<p>\nThis file is safe to edit. It will not be overwritten by the " +
"code generator.",
Expand Down Expand Up @@ -329,44 +328,41 @@ public Constructor emptyConstructor() {
}

public enum CopyConstructorMode {
SETTER, FIELD, BUILDER;
SETTER, FIELD;
}

public Constructor copyConstructor(Type type, CopyConstructorMode mode) {
final Constructor constructor = Constructor.of().public_()
.add(Field.of("speedment", Type.of(Speedment.class)))
.add("super(speedment);")
.add(Field.of(variableName(), type).final_());
final Constructor constructor = Constructor.of().protected_()
.add(Field.of(variableName(), type));

columns().forEachOrdered(c -> {
switch (mode) {
case FIELD: {
constructor.add("this." + variableName(c) + " = " + variableName() + "." + GETTER_METHOD_PREFIX + typeName(c) + "();");
break;
}
case SETTER:
case BUILDER: {
final String setterPrefix = (mode == SETTER)
? SETTER_METHOD_PREFIX : BUILDER_METHOD_PREFIX;

case SETTER: {
if (c.isNullable()) {
constructor.add(
variableName() + "."
+ GETTER_METHOD_PREFIX + typeName(c)
+ "().ifPresent(this::"
+ setterPrefix + typeName(c)
+ SETTER_METHOD_PREFIX + typeName(c)
+ ");"
);
} else {
constructor.add(
setterPrefix + typeName(c)
SETTER_METHOD_PREFIX + typeName(c)
+ "(" + variableName()
+ ".get" + typeName(c)
+ "());"
);
}
break;
}
default: throw new UnsupportedOperationException(
"Unknown mode '" + mode + "'."
);
}
});

Expand Down
Expand Up @@ -53,8 +53,8 @@ private ClassType(String typeSuffix, String implSuffix) {

this.type = Type.of(fullyQualifiedTypeName() + typeSuffix);
this.implType = Type.of(fullyQualifiedTypeName() + typeSuffix + implSuffix);
this.generatedType = Type.of(fullyQualifiedTypeName(GENERATED_PACKAGE) + GENERATED_PREFIX + typeSuffix);
this.generatedImplType = Type.of(fullyQualifiedTypeName(GENERATED_PACKAGE) + GENERATED_PREFIX + typeSuffix + implSuffix);
this.generatedType = Type.of(fullyQualifiedTypeName(GENERATED_PACKAGE, GENERATED_PREFIX) + typeSuffix);
this.generatedImplType = Type.of(fullyQualifiedTypeName(GENERATED_PACKAGE, GENERATED_PREFIX) + typeSuffix + implSuffix);
this.optionalType = Type.of(Optional.class).add(Generic.of().add(type));
}

Expand Down Expand Up @@ -97,7 +97,7 @@ public Type getOptionalType() {

protected final ClassType
entity = new ClassType("", "Impl"),
builder = new ClassType("Builder", "Impl"),
// builder = new ClassType("Builder", "Impl"),
manager = new ClassType("Manager", "Impl");

protected final Generic
Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.speedment.config.db.trait.HasMainInterface;
import com.speedment.config.db.trait.HasName;
import com.speedment.internal.codegen.lang.models.ClassOrInterface;
import static com.speedment.internal.codegen.util.Formatting.ucfirst;
import com.speedment.internal.util.JavaLanguageNamer;
import static com.speedment.internal.util.document.DocumentUtil.relativeName;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -211,11 +212,45 @@ default String fullyQualifiedTypeName() {
* fully qualified type names</a>
*/
default String fullyQualifiedTypeName(String subPath) {
return fullyQualifiedTypeName(subPath, "");
}

/**
* Returns the fully qualified type name of the current document. The specified
* sub-path will be added after the base package name and before the type
* name of the node. The sub-path should not contain either leading nor
* trailing dots.
* <p>
* Example:
* <ul>
* <li><code>com.speedment.example.employeesschema.EmployeesSchema</code>
* <li><code>com.speedment.example.usertable.UserTable</code>
* <li><code>com.speedment.example.usertable.firstname.Firstname</code>
* </ul>
* <p>
* Note that this method is only meant to work with nodes at
* {@code Table} or higher level in the hierarchy. It will return a
* result for all documents located in a valid hierarchy, but the result might
* not be as intended.
*
* @param subPath A prefix that will be added to the "class"-part of the
* type name.
* @param subPath A sub-path to be added at the end of the 'package'-part of
* the qualified type name. This value can be {@code null} and in that
* case an ordinary fullyQualifiedTypeName will be returned.
* @return the fully qualified type name of the current document
* @see
* <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.5.5.2">Concerning
* fully qualified type names</a>
*/
default String fullyQualifiedTypeName(String subPath, String filePrefix) {
requireNonNull(filePrefix);

// subPath is nullable
if (subPath == null || subPath.isEmpty()) {
return basePackageName() + "." + typeName(getAliasDocument());
return basePackageName() + "." + ucfirst(filePrefix) + typeName(getAliasDocument());
} else {
return basePackageName() + "." + subPath + "." + typeName(getAliasDocument());
return basePackageName() + "." + subPath + "." + ucfirst(filePrefix) + typeName(getAliasDocument());
}
}

Expand Down
Expand Up @@ -37,7 +37,8 @@ public EntityImplTranslator(Speedment speedment, Generator cg, Table configEntit
@Override
protected Class make(File file) {
return newBuilder(file, entity.getImplName()).build()
.public_().add(entity.getType()).setSupertype(entity.getGeneratedImplType());
.public_().abstract_()
.add(entity.getType()).setSupertype(entity.getGeneratedImplType());
}

@Override
Expand Down
Expand Up @@ -34,7 +34,7 @@
import static com.speedment.internal.codegen.lang.models.constants.DefaultType.OPTIONAL;
import static com.speedment.internal.codegen.lang.models.constants.DefaultType.STRING;
import com.speedment.internal.core.code.AbstractBaseEntity;
import static com.speedment.internal.core.code.DefaultJavaClassTranslator.BUILDER_METHOD_PREFIX;
import static com.speedment.internal.core.code.DefaultJavaClassTranslator.SETTER_METHOD_PREFIX;
import static com.speedment.internal.core.code.DefaultJavaClassTranslator.GETTER_METHOD_PREFIX;
import com.speedment.internal.core.code.EntityAndManagerTranslator;
import java.util.ArrayList;
Expand All @@ -57,7 +57,6 @@
public final class GeneratedEntityImplTranslator extends EntityAndManagerTranslator<Class> {

private static final String
SPEEDMENT_NAME = "speedment",
MANAGER_OF_METHOD = "managerOf_";

public GeneratedEntityImplTranslator(Speedment speedment, Generator gen, Table table) {
Expand Down Expand Up @@ -92,7 +91,7 @@ protected Class make(File file) {
/*** Setters ***/
.forEveryColumn((clazz, col) -> {
clazz
.add(Method.of(BUILDER_METHOD_PREFIX + typeName(col), entity.getImplType())
.add(Method.of(SETTER_METHOD_PREFIX + typeName(col), entity.getType())
.public_().final_()
.add(OVERRIDE)
.add(fieldFor(col))
Expand Down Expand Up @@ -157,13 +156,11 @@ protected Class make(File file) {
/*** Class details **/
.build()
.public_()
.final_()
.abstract_()
.setSupertype(Type.of(AbstractBaseEntity.class).add(Generic.of().add(entity.getType())))
.add(entity.getType())
.add(Constructor.of().add(Field.of(SPEEDMENT_NAME, Type.of(Speedment.class)))
.add("super(" + SPEEDMENT_NAME + ");")
)
.add(copyConstructor(entity.getType(), CopyConstructorMode.BUILDER));
.add(Constructor.of().protected_())
.add(copyConstructor(entity.getType(), CopyConstructorMode.SETTER));

/*** Create aggregate streaming functions, if any ***/
fkStreamers.keySet().stream().forEach((referencingTable) -> {
Expand All @@ -185,6 +182,8 @@ protected Class make(File file) {
newClass.add(method);
}
});

file.add(Import.of(Type.of(Speedment.class)));

newClass
.add(copy())
Expand All @@ -201,7 +200,15 @@ protected Class make(File file) {

private Method copy() {
return Method.of("copy", entity.getType()).public_().add(OVERRIDE)
.add("return new " + entity.getImplName() + "(speedment(), this);");
.add(
"final " + Speedment.class.getSimpleName() + " speedment = speedment();",
"return new " + entity.getGeneratedImplName() + "(this) {", indent(
"@Override",
"protected final " + Speedment.class.getSimpleName() + " speedment() {", indent(
"return speedment;"
), "}"
), "};"
);

}

Expand Down
Expand Up @@ -22,6 +22,8 @@
import com.speedment.internal.codegen.lang.models.File;
import com.speedment.internal.codegen.lang.models.Class;
import com.speedment.config.db.Table;
import com.speedment.internal.codegen.lang.models.Constructor;
import com.speedment.internal.codegen.lang.models.Field;
import com.speedment.internal.codegen.lang.models.Type;

/**
Expand All @@ -38,7 +40,12 @@ public EntityManagerImplTranslator(Speedment speedment, Generator gen, Table tab
@Override
protected Class make(File file) {
return newBuilder(file, manager.getImplName()).build()
.public_().add(manager.getType()).setSupertype(manager.getGeneratedImplType());
.public_().final_()
.add(manager.getType()).setSupertype(manager.getGeneratedImplType())
.add(Constructor.of().public_()
.add(Field.of("speedment", Type.of(Speedment.class)))
.add("super(speedment);")
);
}

@Override
Expand Down
Expand Up @@ -37,6 +37,7 @@
import com.speedment.internal.codegen.lang.models.values.ReferenceValue;
import com.speedment.internal.codegen.util.Formatting;
import static com.speedment.internal.codegen.util.Formatting.block;
import static com.speedment.internal.codegen.util.Formatting.indent;
import static com.speedment.internal.codegen.util.Formatting.nl;
import static com.speedment.internal.core.code.DefaultJavaClassTranslator.GETTER_METHOD_PREFIX;
import static com.speedment.internal.core.code.DefaultJavaClassTranslator.SETTER_METHOD_PREFIX;
Expand Down Expand Up @@ -90,13 +91,13 @@ protected Class make(File file) {
);
})
.build()
.public_()
.public_().abstract_()
.setSupertype(Type.of(AbstractSqlManager.class)
.add(Generic.of().add(entity.getType()))
)
.call(i -> file.add(Import.of(entity.getImplType())))
.add(Constructor.of()
.public_()
.protected_()
.add(Field.of(SPEEDMENT_VARIABLE_NAME, Type.of(Speedment.class)))
.add("super(" + SPEEDMENT_VARIABLE_NAME + ");")
.add("setSqlEntityMapper(this::defaultReadEntity);"))
Expand All @@ -112,8 +113,15 @@ protected Class make(File file) {
add(defaultReadEntity(file))
.add(Method.of("newInstance", entity.getType())
.public_().add(OVERRIDE)
.add("return new " + Formatting.shortName(entity.getImplType().getName()) + "(" + SPEEDMENT_VARIABLE_NAME + ");")
.add("return new " + entity.getImplName() + "() {", indent(
"@Override",
"protected " + Speedment.class.getSimpleName() + " speedment() {", indent(
"return " + SPEEDMENT_VARIABLE_NAME + ";"
), "}"
), "};"
)
.call($ -> file.add(Import.of(entity.getImplType())))
.call($ -> file.add(Import.of(Type.of(Speedment.class))))
)
.add(generatePrimaryKeyFor(file));
}
Expand Down

0 comments on commit 0bca692

Please sign in to comment.