boolean hasEntityFields = model.hasEntityFields() || superTypeHasEntityFields(model); boolean stringOrBoolean = model.getOriginalCategory() == TypeCategory.STRING || model.getOriginalCategory() == TypeCategory.BOOLEAN; String thisOrSuper = hasEntityFields ? THIS : SUPER; String additionalParams = getAdditionalConstructorParameter(model); String classCast = localName.equals(genericName) ? EMPTY : "(Class) "; constructorsForVariables(writer, model); suppressAllWarnings(writer); constructorContent(writer, model); } else { writer.line("this(", classCast, "path.getType(), path.getMetadata(), PathInits.getFor(path.getMetadata(), INITS));"); } else { if (!localName.equals(genericName)) { suppressAllWarnings(writer); constructorContent(writer, model); writer.end(); suppressAllWarnings(writer); constructorContent(writer, model); writer.beginConstructor(new Parameter("type", type), PATH_METADATA, PATH_INITS); writer.line("super(type, metadata, inits" + additionalParams + ");"); initEntityFields(writer, config, model);
&& property.getType().getCategory() != TypeCategory.CUSTOM && property.getType().getCategory() != TypeCategory.ENTITY) { customField(model, property, config, writer); continue; Type genericQueryType = null; String localRawName = writer.getRawName(property.getType()); String inits = getInits(property); serialize(model, property, queryType, writer, "createString"); break; serialize(model, property, queryType, writer, "createBoolean"); break; serialize(model, property, queryType, writer, "createSimple", writer.getClassConstant(localRawName)); break; serialize(model, property, queryType, writer, "createComparable", writer.getClassConstant(localRawName)); break; serialize(model, property, queryType, writer, "createEnum", writer.getClassConstant(localRawName)); break; serialize(model, property, queryType, writer, "createDate", writer.getClassConstant(localRawName)); break; serialize(model, property, queryType, writer, "createDateTime", writer.getClassConstant(localRawName)); break;
protected void intro(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException { introPackage(writer, model); introImports(writer, config, model); writer.nl(); introJavadoc(writer, model); introClassHeader(writer, model); introFactoryMethods(writer, model); introInits(writer, model); if (config.createDefaultVariable()) { introDefaultInstance(writer, model, config.defaultVariableName()); } if (model.getSuperType() != null && model.getSuperType().getEntityType() != null) { introSuper(writer, model); } }
@Override public void serialize(EntityType model, SerializerConfig config, CodeWriter writer) throws IOException { intro(model, config, writer); // properties serializeProperties(model, config, writer); // constructors constructors(model, config, writer); // delegates for (Delegate delegate : model.getDelegates()) { delegate(model, delegate, config, writer); } // property accessors for (Property property : model.getProperties()) { TypeCategory category = property.getType().getCategory(); if (category == TypeCategory.MAP && config.useMapAccessors()) { mapAccessor(model, property, writer); } else if (category == TypeCategory.LIST && config.useListAccessors()) { listAccessor(model, property, writer); } else if (category == TypeCategory.ENTITY && config.useEntityAccessors()) { entityAccessor(model, property, writer); } } outro(model, writer); }
@Test public void entitySerializer() throws Exception { new EntitySerializer(typeMappings, Collections.<String>emptyList()) .serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); }
@Test public void original_category() throws IOException { Map<TypeCategory, String> categoryToSuperClass = new EnumMap<TypeCategory, String>(TypeCategory.class); categoryToSuperClass.put(TypeCategory.COMPARABLE, "ComparablePath<Entity>"); categoryToSuperClass.put(TypeCategory.ENUM, "EnumPath<Entity>"); categoryToSuperClass.put(TypeCategory.DATE, "DatePath<Entity>"); categoryToSuperClass.put(TypeCategory.DATETIME, "DateTimePath<Entity>"); categoryToSuperClass.put(TypeCategory.TIME, "TimePath<Entity>"); categoryToSuperClass.put(TypeCategory.NUMERIC, "NumberPath<Entity>"); categoryToSuperClass.put(TypeCategory.STRING, "StringPath"); categoryToSuperClass.put(TypeCategory.BOOLEAN, "BooleanPath"); for (Map.Entry<TypeCategory, String> entry : categoryToSuperClass.entrySet()) { SimpleType type = new SimpleType(entry.getKey(), "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(entry.toString(), writer.toString().contains("public class QEntity extends " + entry.getValue() + " {")); } }
protected void constructorsForVariables(CodeWriter writer, EntityType model) throws IOException { String localName = writer.getRawName(model); String genericName = writer.getGenericName(true, model); boolean stringOrBoolean = model.getOriginalCategory() == TypeCategory.STRING || model.getOriginalCategory() == TypeCategory.BOOLEAN; boolean hasEntityFields = model.hasEntityFields() || superTypeHasEntityFields(model); String thisOrSuper = hasEntityFields ? THIS : SUPER; String additionalParams = hasEntityFields ? "" : getAdditionalConstructorParameter(model); if (!localName.equals(genericName)) { suppressAllWarnings(writer); } writer.beginConstructor(new Parameter("variable", Types.STRING)); if (stringOrBoolean) { writer.line(thisOrSuper,"(forVariable(variable)",additionalParams,");"); } else { writer.line(thisOrSuper,"(", localName.equals(genericName) ? EMPTY : "(Class) ", writer.getClassConstant(localName) + COMMA + "forVariable(variable)", hasEntityFields ? ", INITS" : EMPTY, additionalParams,");"); } if (!hasEntityFields) { constructorContent(writer, model); } writer.end(); }
@SuppressWarnings("unchecked") @Override protected void introImports(CodeWriter writer, SerializerConfig config, EntityType model) throws IOException { super.introImports(writer, config, model);
super.outro(model, writer);
@Override protected void customField(EntityType model, Property field, SerializerConfig config, CodeWriter writer) throws IOException { Type queryType = typeMappings.getPathType(field.getType(), model, false); if (queryType.getPackageName().startsWith("com.querydsl")) { String localRawName = writer.getRawName(field.getType()); serialize(model, field, queryType, writer, "create" + field.getType().getSimpleName(), writer.getClassConstant(localRawName)); } else { super.customField(model, field, config, writer); } }
@Override protected void constructorsForVariables(CodeWriter writer, EntityType model) throws IOException { super.constructorsForVariables(writer, model); String localName = writer.getRawName(model); String genericName = writer.getGenericName(true, model); if (!localName.equals(genericName)) { writer.suppressWarnings("all"); } writer.beginConstructor(new Parameter("variable", Types.STRING), new Parameter("schema", Types.STRING), new Parameter("table", Types.STRING)); writer.line(SUPER,"(", writer.getClassConstant(localName) + COMMA + "forVariable(variable), schema, table);"); constructorContent(writer, model); writer.end(); writer.beginConstructor(new Parameter("variable", Types.STRING), new Parameter("schema", Types.STRING)); writer.line(SUPER, "(", writer.getClassConstant(localName), COMMA, "forVariable(variable), schema, \"", model.getData().get("table").toString(), "\");"); constructorContent(writer, model); writer.end(); }
super.serializeProperties(model, config, writer); super.serializeProperties(model, config, writer);
@Test public void originalCategory() throws IOException { Map<TypeCategory, String> categoryToSuperClass = new EnumMap<TypeCategory, String>(TypeCategory.class); categoryToSuperClass.put(TypeCategory.COMPARABLE, "ComparablePath<Entity>"); categoryToSuperClass.put(TypeCategory.ENUM, "EnumPath<Entity>"); categoryToSuperClass.put(TypeCategory.DATE, "DatePath<Entity>"); categoryToSuperClass.put(TypeCategory.DATETIME, "DateTimePath<Entity>"); categoryToSuperClass.put(TypeCategory.TIME, "TimePath<Entity>"); categoryToSuperClass.put(TypeCategory.NUMERIC, "NumberPath<Entity>"); categoryToSuperClass.put(TypeCategory.STRING, "StringPath"); categoryToSuperClass.put(TypeCategory.BOOLEAN, "BooleanPath"); for (Map.Entry<TypeCategory, String> entry : categoryToSuperClass.entrySet()) { StringWriter w = new StringWriter(); SimpleType type = new SimpleType(entry.getKey(), "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(w)); assertTrue(entry.getValue() + " is missing from " + w, w.toString().contains("public class QEntity extends " + entry.getValue() + " {")); } }
@Test public void entitySerializer2() throws Exception { new EntitySerializer(typeMappings,Collections.<String>emptyList()) .serialize(type, new SimpleSerializerConfig(true,true,true,true,""), new JavaWriter(writer)); }
@Test public void correct_imports() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "test.Entity", "test", "Entity",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("import test.Entity;")); assertTrue(writer.toString().contains("public class QEntity extends EntityPathBase<Entity> {")); }
@Test public void entitySerializer() throws Exception { new EntitySerializer(typeMappings, Collections.<String>emptyList()) .serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); }
@Test public void correct_superclass() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "java.util.Locale", "java.util", "Locale",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class QLocale extends EntityPathBase<Locale> {")); CompileUtils.assertCompiles("QLocale", writer.toString()); }
@Test public void entitySerializer2() throws Exception { new EntitySerializer(typeMappings,Collections.<String>emptyList()) .serialize(type, new SimpleSerializerConfig(true,true,true,true,""), new JavaWriter(writer)); }
@Test public void no_package() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class QEntity extends EntityPathBase<Entity> {")); CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void no_package() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class QEntity extends BeanPath<Entity> {")); CompileUtils.assertCompiles("QEntity", writer.toString()); }