Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Add schema id to class definition and get package name from generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Oct 24, 2017
1 parent 8ada974 commit 49eb7bc
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public Class<?> compile(
final File classesOutputDirectory) {

final JavaCompilerUtil compiler = new JavaCompilerUtil(generationContext.getOutputDirectoryPath().toFile(), classesOutputDirectory);
return compiler.compiledClassOf(generationContext.getPackageName(), classGenerator.getSimpleClassName());
return compiler.compiledClassOf(classGenerator.getPackageName(), classGenerator.getSimpleClassName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,47 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* Defines a class that will be generated as a java POJO
*/
public class ClassDefinition extends FieldDefinition {

private final List<Definition> fieldDefinitions = new ArrayList<>();
private final Optional<String> id;

private boolean allowAdditionalProperties = false;
private boolean root = false;

/**
* Creates a ClassDefinition that is not the root class.
*
* @param type The {@link DefinitionType} of the class. Can be a
* {@link DefinitionType#CLASS}
* {@link DefinitionType#ENUM} or
* {@link DefinitionType#COMBINED}
* @param type The {@link DefinitionType} of the class. Can be a
* {@link DefinitionType#CLASS}
* {@link DefinitionType#ENUM} or
* {@link DefinitionType#COMBINED}
* @param fieldName The name of the field that will be used in the generated POJO
* @param id The id of the field used to create package and className
*/
public ClassDefinition(final DefinitionType type, final String fieldName, final String id) {
super(type, fieldName);
this.id = Optional.ofNullable(id);
}

/**
* Creates a ClassDefinition that is not the root class and has not id.
*
* @param type The {@link DefinitionType} of the class. Can be a
* {@link DefinitionType#CLASS}
* {@link DefinitionType#ENUM} or
* {@link DefinitionType#COMBINED}
* DefinitionType#COMBINED}
* @param fieldName The name of the field that will be used in the generated POJO
*/
public ClassDefinition(final DefinitionType type, final String fieldName) {
super(type, fieldName);
this.id = Optional.empty();
}

/**
Expand Down Expand Up @@ -96,10 +115,20 @@ public void setRoot(final boolean root) {
this.root = root;
}

/**
* Gets the id of the definition. If present id is used to create the class name and package
*
* @return Optional id
*/
public Optional<String> getId() {
return id;
}

@Override
public String toString() {
return "ClassDefinition{" +
"fieldName=" + getFieldName() +
", id=" + getId().toString() +
", type=" + type() +
", required=" + isRequired() +
", allowAdditionalProperties=" + allowAdditionalProperties +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
public class CombinedDefinition extends ClassDefinition {

public CombinedDefinition(final String fieldName) {
super(COMBINED, fieldName);
public CombinedDefinition(final String fieldName, final String id) {
super(COMBINED, fieldName, id);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ public interface ClassGeneratable {

/**
* Gets the simple name of the POJO that is to be generated
*
* @return The simple name of the POJO
*/
String getSimpleClassName();

/**
* Gets the package name of the POJO that is to be generated
*
* @return The package name of the POJO
*/
String getPackageName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public String getSimpleClassName() {
return getClassName().simpleName();
}

@Override
public String getPackageName() {
return getClassName().packageName();
}

private ClassName getClassName() {
return classNameFactory.createClassNameFrom(classDefinition);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.justice.generation.pojo.generators;

import uk.gov.justice.generation.pojo.dom.ClassDefinition;
import uk.gov.justice.generation.pojo.dom.Definition;
import uk.gov.justice.generation.pojo.plugin.PluginContext;
import uk.gov.justice.generation.pojo.plugin.TypeNamePluginProcessor;
Expand All @@ -9,9 +8,8 @@
import com.squareup.javapoet.TypeName;

/**
* Generates the correct {@link TypeName} for the specifed {@link Definition}.
* Used for generating the correct return types and parameters. Can handle
* generic types.
* Generates the correct {@link TypeName} for the specifed {@link Definition}. Used for generating
* the correct return types and parameters. Can handle generic types.
*
* The behaviour can be modified using {@link uk.gov.justice.generation.pojo.plugin.typemodifying.TypeModifyingPlugin}s
*/
Expand All @@ -30,9 +28,8 @@ public ClassNameFactory(
/**
* Generate to correct return type/parameter type for the specified {@link Definition}
*
* @param definition The definition for which to generate the correct return type
* @param definition The definition for which to generate the correct return type
* @param pluginContext The {@link PluginContext}
*
* @return The correct type for returns and parameters
*/
public TypeName createTypeNameFrom(final Definition definition, final PluginContext pluginContext) {
Expand Down Expand Up @@ -63,8 +60,8 @@ public TypeName createTypeNameFrom(final Definition definition, final PluginCont
typeName = typeNameProvider.typeNameForString();
break;

case CLASS:
case ENUM:
case CLASS:
case COMBINED:
default:
typeName = typeNameProvider.typeNameForClass(definition);
Expand All @@ -73,7 +70,7 @@ public TypeName createTypeNameFrom(final Definition definition, final PluginCont
return typeNamePluginProcessor.processTypeNamePlugins(typeName, definition, pluginContext);
}

public ClassName createClassNameFrom(final ClassDefinition classDefinition) {
return typeNameProvider.typeNameForClass(classDefinition);
public ClassName createClassNameFrom(final Definition definition) {
return typeNameProvider.typeNameForClass(definition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STATIC;
import static org.apache.commons.lang3.StringUtils.capitalize;

import uk.gov.justice.generation.pojo.dom.EnumDefinition;
import uk.gov.justice.generation.pojo.plugin.PluginContext;
Expand All @@ -35,21 +34,19 @@ public class EnumGenerator implements ClassGeneratable {
private final EnumDefinition enumDefinition;
private final ClassNameFactory classNameFactory;
private final PluginContext pluginContext;
private final String className;

public EnumGenerator(final EnumDefinition definition,
final ClassNameFactory classNameFactory,
final PluginContext pluginContext) {
this.enumDefinition = definition;
this.classNameFactory = classNameFactory;
this.pluginContext = pluginContext;
this.className = capitalize(enumDefinition.getFieldName());
}

@Override
public TypeSpec generate() {

final Builder enumBuilder = enumBuilder(className).addModifiers(PUBLIC);
final Builder enumBuilder = enumBuilder(getClassName()).addModifiers(PUBLIC);

enumDefinition.getEnumValues().forEach(enumValue -> {
final String enumName = constructEnumNameFrom(enumValue);
Expand Down Expand Up @@ -104,10 +101,19 @@ private MethodSpec buildValueForMethod() {

@Override
public String getSimpleClassName() {
return className;
return getClassName().simpleName();
}

@Override
public String getPackageName() {
return getClassName().packageName();
}

private String constructEnumNameFrom(final String enumValue) {
return enumValue.isEmpty() ? BLANK_ENUM_NAME : enumValue.toUpperCase().replace(SPACE, UNDERSCORE);
}

private ClassName getClassName() {
return classNameFactory.createClassNameFrom(enumDefinition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.squareup.javapoet.TypeName;

/**
* Gets the correct class name for a java type. Used for return types, fields and parameters.
* Can handle generics and lists
* Gets the correct class name for a java type. Used for return types, fields and parameters. Can
* handle generics and lists
*/
public class TypeNameProvider {

Expand All @@ -32,13 +32,13 @@ public TypeNameProvider(final GenerationContext generationContext) {
}

/**
* Generates the correct class name for an array. As an array is always implemented as
* a java {@link List} the class name will always be a list of the type specified by
* the {@link Definition}
* Generates the correct class name for an array. As an array is always implemented as a java
* {@link List} the class name will always be a list of the type specified by the {@link
* Definition}
*
* @param definition The {@link Definition} from which to create the class name
* @param classNameFactory The {@link ClassNameFactory} to create the underlying class name
* (not including any generics)
* @param definition The {@link Definition} from which to create the class name
* @param classNameFactory The {@link ClassNameFactory} to create the underlying class name (not
* including any generics)
* @return The correct type name
*/
public TypeName typeNameForArray(
Expand All @@ -62,9 +62,9 @@ public TypeName typeNameForArray(
* Creates the correct java type name for a {@link ReferenceDefinition} by using the child
* reference as the java type. Can handle arrays and generics
*
* @param definition The {@link ReferenceDefinition}
* @param classNameFactory The {@link ClassNameFactory} to create the underlying class name
* (not including any generics)
* @param definition The {@link ReferenceDefinition}
* @param classNameFactory The {@link ClassNameFactory} to create the underlying class name (not
* including any generics)
* @return The correct type name
*/
public TypeName typeNameForReference(
Expand All @@ -83,6 +83,7 @@ public TypeName typeNameForReference(

/**
* Generates the correct type name for a {@link String}
*
* @return The class name for {@link String}
*/
public ClassName typeNameForString() {
Expand All @@ -91,8 +92,8 @@ public ClassName typeNameForString() {

/**
* Generates the correct type name for a java class
* @param definition The definition from which to generated the type name
*
* @param definition The definition from which to generated the type name
* @return The class name for a java class
*/
public ClassName typeNameForClass(final Definition definition) {
Expand All @@ -101,6 +102,7 @@ public ClassName typeNameForClass(final Definition definition) {

/**
* Generates the correct type name for a {@link BigDecimal}
*
* @return The class name for {@link BigDecimal}
*/
public ClassName typeNameForNumber() {
Expand All @@ -109,6 +111,7 @@ public ClassName typeNameForNumber() {

/**
* Generates the correct type name for a {@link Integer}
*
* @return The class name for {@link Integer}
*/
public ClassName typeNameForInteger() {
Expand All @@ -117,6 +120,7 @@ public ClassName typeNameForInteger() {

/**
* Generates the correct type name for a {@link Boolean}
*
* @return The class name for {@link Boolean}
*/
public ClassName typeNameForBoolean() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public String getSimpleClassName() {
return BUILDER_SIMPLE_NAME;
}

@Override
public String getPackageName() {
throw new UnsupportedOperationException();
}

public MethodSpec generateStaticGetBuilderMethod() {
return methodBuilder(classDefinition.getFieldName())
.addModifiers(PUBLIC, STATIC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public DefaultDefinitionFactory(final ReferenceValueParser referenceValueParser,
@Override
public Definition constructRootDefinitionFor(final String fieldName, final Schema schema) {

final String id = schema.getId();

final ClassDefinition definition;
if (schema instanceof CombinedSchema) {
definition = new CombinedDefinition(fieldName);
definition = new CombinedDefinition(fieldName, id);
} else {
definition = new ClassDefinition(CLASS, fieldName);
definition = new ClassDefinition(CLASS, fieldName, id);
}

definition.setRoot(true);
Expand All @@ -74,15 +76,15 @@ public Definition constructDefinitionFor(final String fieldName, final Schema sc
}

if (schema instanceof CombinedSchema) {
return new CombinedDefinition(fieldName);
return new CombinedDefinition(fieldName, schema.getId());
}

if (schema instanceof ObjectSchema) {
return new ClassDefinition(CLASS, fieldName);
return new ClassDefinition(CLASS, fieldName, schema.getId());
}

if (schema instanceof ArraySchema) {
return new ClassDefinition(ARRAY, fieldName);
return new ClassDefinition(ARRAY, fieldName, schema.getId());
}

if (schema instanceof EnumSchema) {
Expand Down Expand Up @@ -111,7 +113,7 @@ public Definition constructDefinitionFor(final String fieldName, final Schema sc
}

if (schema instanceof EmptySchema) {
return new ClassDefinition(CLASS, fieldName);
return new ClassDefinition(CLASS, fieldName, schema.getId());
}

throw new UnsupportedSchemaException(format(EXCEPTION_FORMAT_MESSAGE, schema.getClass().getSimpleName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SourceWriter {
*/
public void write(final ClassGeneratable classGenerator, final GenerationContext generationContext) {
final TypeSpec typeSpec = classGenerator.generate();
writeClass(generationContext.getOutputDirectoryPath(), generationContext.getPackageName(), typeSpec);
writeClass(generationContext.getOutputDirectoryPath(), classGenerator.getPackageName(), typeSpec);
}

private void writeClass(final Path outputDirectory, final String packageName, final TypeSpec typeSpec) {
Expand Down
Loading

0 comments on commit 49eb7bc

Please sign in to comment.