private void serialize(Map<Class<?>, EntityType> types, Serializer serializer) throws IOException { for (EntityType entityType : types.values()) { if (serialized.add(entityType)) { Type type = typeMappings.getPathType(entityType, entityType, true); String packageName = type.getPackageName(); String className = packageName.length() > 0 ? (packageName + "." + type.getSimpleName()) : type.getSimpleName(); write(serializer, className.replace('.', '/') + ".java", entityType); } } }
private static void registerTypes(TypeMappings typeMappings) { Map<Class<?>, Class<?>> mappings = Maps.newHashMap(); mappings.put(GeometryCollection.class, GeometryCollectionPath.class); mappings.put(Geometry.class, GeometryPath.class); mappings.put(LinearRing.class, LinearRingPath.class); mappings.put(LineString.class, LineStringPath.class); mappings.put(MultiLineString.class, MultiLineStringPath.class); mappings.put(MultiPoint.class, MultiPointPath.class); mappings.put(MultiPolygon.class, MultiPolygonPath.class); mappings.put(Point.class, PointPath.class); mappings.put(Polygon.class, PolygonPath.class); mappings.put(PolyHedralSurface.class, PolyhedralSurfacePath.class); for (Map.Entry<Class<?>, Class<?>> entry : mappings.entrySet()) { Type type = new ClassType(entry.getKey()); typeMappings.register(type, new ClassType(entry.getValue(), type)); } }
@Test public void GetPathType_Of_InnerClass() { TypeMappings typeMappings = new JavaTypeMappings(); EntityType model = new EntityType(new ClassType(TypeMappingsTest.class)); EntityType type = new EntityType(new ClassType(Entity.class)); typeMappings.register(type, new QueryTypeFactoryImpl("Q","","").create(type)); Type pathType = typeMappings.getPathType(type, model, false); assertEquals("QTypeMappingsTest_Entity", pathType.getSimpleName()); }
@Test public void IsRegistered() { TypeMappings typeMappings = new JavaTypeMappings(); typeMappings.register(new ClassType(Double[].class), new ClassType(Point.class)); assertTrue(typeMappings.isRegistered(new ClassType(Double[].class))); }
@Test public void CustomType() throws IOException{ SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); entityType.addProperty(new Property(entityType, "property", new ClassType(Double[].class))); typeMappings.register(new ClassType(Double[].class), new ClassType(Point.class)); typeMappings.register(entityType, queryTypeFactory.create(entityType)); assertTrue(typeMappings.isRegistered(entityType.getProperties().iterator().next().getType())); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains( "public final com.mysema.query.codegen.Point property = " + "new com.mysema.query.codegen.Point(forProperty(\"property\"));")); }
private EntityType createEntityType(Class<?> cl, Map<Class<?>, EntityType> types) { if (allTypes.containsKey(cl)) { return allTypes.get(cl); } else { EntityType type = typeFactory.getEntityType(cl); typeMappings.register(type, queryTypeFactory.create(type)); if (!cl.getSuperclass().equals(Object.class)) { type.addSupertype(new Supertype(typeFactory.get(cl.getSuperclass(), cl.getGenericSuperclass()))); } types.put(cl, type); allTypes.put(cl, type); return type; } }
private void serialize(Map<String, EntityType> types, Serializer serializer) throws IOException { for (EntityType entityType : types.values()) { if (serialized.add(entityType)) { Type type = typeMappings.getPathType(entityType, entityType, true); String packageName = entityType.getPackageName(); String className = packageName.length() > 0 ? (packageName + "." + type.getSimpleName()) : type.getSimpleName(); write(serializer, className.replace('.', '/') + sourceSuffix, entityType); } } }
private EntityType createEntityType(Class<?> cl, Map<String, EntityType> types) { if (types.containsKey(cl.getName())) { return types.get(cl.getName()); } else { EntityType type = new EntityType(new ClassType(TypeCategory.ENTITY, cl)); typeMappings.register(type, queryTypeFactory.create(type)); if (cl.getSuperclass() != null && !cl.getSuperclass().equals(Object.class)) { type.addSupertype(new Supertype(new ClassType(cl.getSuperclass()))); } types.put(cl.getName(), type); allTypes.put(cl.getName(), type); return type; } }
@Override protected void introDefaultInstance(CodeWriter writer, EntityType entityType, String defaultName) throws IOException { String variableName = !defaultName.isEmpty() ? defaultName : namingStrategy.getDefaultVariableName(entityType); String alias = namingStrategy.getDefaultAlias(entityType); Type queryType = typeMappings.getPathType(entityType, entityType, true); writer.publicStaticFinal(queryType, variableName, NEW + queryType.getSimpleName() + "(\"" + alias + "\")"); }
protected EntityType createEntityType(Type type, Map<Class<?>, EntityType> types) { Class<?> key = type.getJavaClass(); if (allTypes.containsKey(key)) { return allTypes.get(key); } else { EntityType entityType = new EntityType(type, variableNameFunction); typeMappings.register(entityType, queryTypeFactory.create(entityType)); Class<?> superClass = key.getSuperclass(); if (entityType.getSuperType() == null && superClass != null && !superClass.equals(Object.class)) { entityType.addSupertype(new Supertype(typeFactory.get(superClass, key.getGenericSuperclass()))); } types.put(key, entityType); allTypes.put(key, entityType); return entityType; } }
@Override protected void introClassHeader(CodeWriter writer, EntityType model) throws IOException { Type queryType = typeMappings.getPathType(model, model, true); writer.line("@Generated(\"", getClass().getName(), "\")"); TypeCategory category = model.getOriginalCategory(); // serialize annotations only, if no bean types are used if (model.equals(queryType)) { for (Annotation annotation : model.getAnnotations()) { writer.annotation(annotation); } } writer.beginClass(queryType, new ClassType(category, entityPathType, model)); writer.privateStaticFinal(Types.LONG_P, "serialVersionUID", String.valueOf(model.hashCode())); }
@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()) { 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()+" {")); } }
@Override protected void customField(EntityType model, Property field, SerializerConfig config, CodeWriter writer) throws IOException { Type queryType = typeMappings.getPathType(field.getType(), model, false); String localRawName = writer.getRawName(field.getType()); serialize(model, field, queryType, writer, "create" + field.getType().getSimpleName(), writer.getClassConstant(localRawName)); }
@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()) { 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 EntityType createEntityType(@Nullable String schemaName, String tableName, final String className) { EntityType classModel; if (beanSerializer == null) { String packageName = normalizePackage(module.getPackageName(), schemaName); String simpleName = module.getPrefix() + className + module.getSuffix(); Type classTypeModel = new SimpleType(TypeCategory.ENTITY, packageName + "." + simpleName, packageName, simpleName, false, false); classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS)); typeMappings.register(classModel, classModel); } else { String beanPackage = normalizePackage(beanPackageName, schemaName); String simpleName = module.getBeanPrefix() + className + module.getBeanSuffix(); Type classTypeModel = new SimpleType(TypeCategory.ENTITY, beanPackage + "." + simpleName, beanPackage, simpleName, false, false); classModel = new EntityType(classTypeModel, module.get(Function.class, CodegenModule.VARIABLE_NAME_FUNCTION_CLASS)); Type mappedType = queryTypeFactory.create(classModel); entityToWrapped.put(classModel, mappedType); typeMappings.register(classModel, mappedType); } classModel.getData().put("schema", schemaName); classModel.getData().put("table", tableName); return classModel; }
@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 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> {")); }
@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> {")); }
@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)); // System.out.println(writer); assertTrue(writer.toString().contains("public class QLocale extends EntityPathBase<Locale> {")); }
@Test public void SuperType() throws IOException{ EntityType superType = new EntityType(new SimpleType(TypeCategory.ENTITY, "Entity2", "", "Entity2",false,false)); SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type, Collections.singleton(new Supertype(superType, superType))); typeMappings.register(superType, queryTypeFactory.create(superType)); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public final QEntity2 _super = new QEntity2(this);")); }