public EntityType handleProjectionType(TypeElement e) { Type c = typeFactory.getType(e.asType(), true); EntityType entityType = new EntityType(c.as(TypeCategory.ENTITY), configuration.getVariableNameFunction()); typeMappings.register(entityType, queryTypeFactory.create(entityType)); List<? extends Element> elements = e.getEnclosedElements(); handleConstructors(entityType, elements); return entityType; }
public void include(Supertype supertype) { EntityType entityType = supertype.getEntityType(); for (Delegate delegate : entityType.getDelegates()) { addDelegate(delegate); } for (Property property : entityType.getProperties()) { addProperty(property.createCopy(this)); } }
@Override public void serialize(EntityType model, SerializerConfig serializerConfig, CodeWriter writer) throws IOException { String simpleName = model.getSimpleName(); if (!model.getPackageName().isEmpty()) { writer.packageDecl(model.getPackageName()); if (model.hasLists()) { importedClasses.add(List.class.getName()); if (model.hasCollections()) { importedClasses.add(Collection.class.getName()); if (model.hasSets()) { importedClasses.add(Set.class.getName()); if (model.hasMaps()) { importedClasses.add(Map.class.getName()); for (Annotation annotation : model.getAnnotations()) { writer.annotation(annotation); if (printSupertype && model.getSuperType() != null) { writer.beginClass(model, model.getSuperType().getType()); } else { writer.beginClass(model); for (Property property : model.getProperties()) { if (propertyAnnotations) {
@Override public SerializerConfig getSerializerConfig(EntityType entityType) { if (typeToConfig.containsKey(entityType.getFullName())) { return typeToConfig.get(entityType.getFullName()); } else if (packageToConfig.containsKey(entityType.getPackageName())) { return packageToConfig.get(entityType.getPackageName()); } else { return defaultSerializerConfig; } }
public void extendTypes() { for (EntityType entityType : entityTypeCache.values()) { if (entityType.getProperties().isEmpty()) { for (Map.Entry<List<String>, EntityType> entry : entityTypeCache.entrySet()) { if (entry.getKey().get(0).equals(entityType.getFullName()) && !entry.getValue().getProperties().isEmpty()) { for (Property property : entry.getValue().getProperties()) { entityType.addProperty(property); } break; } } } } } }
@Test public void primitive_array() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); entityType.addProperty(new Property(entityType, "bytes", new ClassType(byte[].class))); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public final SimplePath<byte[]> bytes")); CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void include() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); entityType.addProperty(new Property(entityType, "b", new ClassType(TypeCategory.BOOLEAN, Boolean.class))); entityType.addProperty(new Property(entityType, "c", new ClassType(TypeCategory.COMPARABLE, String.class))); //entityType.addProperty(new Property(entityType, "cu", new ClassType(TypeCategory.CUSTOM, PropertyType.class))); entityType.addProperty(new Property(entityType, "d", new ClassType(TypeCategory.DATE, Date.class))); entityType.addProperty(new Property(entityType, "e", new ClassType(TypeCategory.ENUM, PropertyType.class))); entityType.addProperty(new Property(entityType, "dt", new ClassType(TypeCategory.DATETIME, Date.class))); entityType.addProperty(new Property(entityType, "i", new ClassType(TypeCategory.NUMERIC, Integer.class))); entityType.addProperty(new Property(entityType, "s", new ClassType(TypeCategory.STRING, String.class))); entityType.addProperty(new Property(entityType, "t", new ClassType(TypeCategory.TIME, Time.class))); EntityType subType = new EntityType(new SimpleType(TypeCategory.ENTITY, "Entity2", "", "Entity2",false,false)); subType.include(new Supertype(type, entityType)); typeMappings.register(subType, queryTypeFactory.create(subType)); serializer.serialize(subType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); CompileUtils.assertCompiles("QEntity2", writer.toString()); }
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); 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)); classModel.getData().put("schema", schemaAndTable.getSchema()); classModel.getData().put("table", schemaAndTable.getTable());
if (!model.getPackageName().isEmpty() && !queryType.getPackageName().equals(model.getPackageName()) && !queryType.getSimpleName().equals(model.getSimpleName())) { String fullName = model.getFullName(); String packageName = model.getPackageName(); if (fullName.substring(packageName.length() + 1).contains(".")) { fullName = fullName.substring(0, fullName.lastIndexOf('.')); classes.add(Path.class); if (!model.getConstructors().isEmpty()) { classes.add(ConstructorExpression.class); classes.add(Projections.class); if (model.hasEntityFields() || model.hasInits()) { inits = true; } else { Set<TypeCategory> collections = Sets.newHashSet(TypeCategory.COLLECTION, TypeCategory.LIST, TypeCategory.SET); for (Property property : model.getProperties()) { if (!property.isInherited() && collections.contains(property.getType().getCategory())) { inits = true;
@Test public void uncapSimpleName_escaped2() { ClassType typeModel = new ClassType(TypeCategory.ENTITY, Object.class); EntityType entityModel = new EntityType(typeModel); assertEquals("object", entityModel.getModifiedSimpleName()); entityModel.addProperty(new Property(entityModel, "OBJECT", "object", typeModel, Collections.<String> emptyList(), false)); assertEquals("object1", entityModel.getModifiedSimpleName()); }
private Set<String> getAnnotationTypes(EntityType model) { Set<String> imports = new HashSet<String>(); for (Annotation annotation : model.getAnnotations()) { imports.add(annotation.annotationType().getName()); } if (propertyAnnotations) { for (Property property : model.getProperties()) { for (Annotation annotation : property.getAnnotations()) { imports.add(annotation.annotationType().getName()); } } } return imports; }
@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.querydsl.codegen.Point property = " + "new com.querydsl.codegen.Point(forProperty(\"property\"));")); }
Property property = createProperty(type, entry.getKey(), entry.getValue(), ann); if (property != null) { type.addProperty(property);
@SuppressWarnings("unchecked") @Before public void setUp() { // type Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false, false); type = new EntityType(typeModel); // property type.addProperty(new Property(type, "entityField", type)); type.addProperty(new Property(type, "collection", new ClassType(TypeCategory.COLLECTION, Collection.class, typeModel))); type.addProperty(new Property(type, "listField", new ClassType(TypeCategory.LIST, List.class, typeModel))); type.addProperty(new Property(type, "setField", new ClassType(TypeCategory.SET, Set.class, typeModel))); type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class, typeModel))); type.addProperty(new Property(type, "mapField", new ClassType(TypeCategory.MAP, List.class, typeModel, typeModel))); type.addProperty(new Property(type, "superTypeField", new TypeExtends(new ClassType(TypeCategory.MAP, List.class, typeModel, typeModel)))); type.addProperty(new Property(type, "extendsTypeField", new TypeSuper(new ClassType(TypeCategory.MAP, List.class, typeModel, typeModel)))); for (Class<?> cl : Arrays.asList(Boolean.class, Comparable.class, Integer.class, Date.class, java.sql.Date.class, java.sql.Time.class)) { Type classType = new ClassType(TypeCategory.get(cl.getName()), cl); type.addProperty(new Property(type, StringUtils.uncapitalize(cl.getSimpleName()), classType)); } // constructor Parameter firstName = new Parameter("firstName", new ClassType(TypeCategory.STRING, String.class)); Parameter lastName = new Parameter("lastName", new ClassType(TypeCategory.STRING, String.class)); type.addConstructor(new Constructor(Arrays.asList(firstName, lastName))); }
Type queryType = typeMappings.getPathType(model, model, true); TypeCategory category = model.getOriginalCategory(); Class<? extends Path> pathType; if (model.getProperties().isEmpty()) { switch (category) { case COMPARABLE : pathType = ComparablePath.class; break; for (Annotation annotation : model.getAnnotations()) { writer.annotation(annotation); long serialVersionUID = model.getFullName().hashCode(); writer.privateStaticFinal(Types.LONG_P, "serialVersionUID", serialVersionUID + "L");
public void writeRepository(EntityType model, EntityType repositoryModel, CodeWriter writer) throws IOException { String simpleName = model.getSimpleName(); // package if (!model.getPackageName().isEmpty()) { writer.packageDecl(repositoryModel.getPackageName()); } Set<String> importedClasses = new HashSet<String>(); importedClasses.add(JpaRepository.class.getName()); importedClasses.add(QueryDslPredicateExecutor.class.getName()); importedClasses.add(model.getFullName()); writer.importClasses(importedClasses.toArray(new String[importedClasses.size()])); // javadoc writer.javadoc("This is a @Generated Repository(", " " + repositoryModel.getFullName() + ")"); writer.line( " interface " + repositoryModel.getSimpleName() + " : JpaRepository<" + simpleName + ", Long>, QueryDslPredicateExecutor<" + simpleName + "> {}"); }
Type queryType = typeMappings.getPathType(model, model, true); TypeCategory category = model.getOriginalCategory(); Class<? extends Path> pathType; if (model.getProperties().isEmpty()) { switch (category) { case COMPARABLE : pathType = ComparablePath.class; break; for (Annotation annotation : model.getAnnotations()) { writer.annotation(annotation); writer.privateStaticFinal(Types.LONG_P, "serialVersionUID", model.hashCode() + "L");
@Override public String getDefaultAlias(EntityType entityType) { return entityType.getData().get("table").toString(); }
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); registerConfig(entityType); 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; } }
@Test public void delegates() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); Delegate delegate = new Delegate(type, type, "test", Collections.<Parameter>emptyList(), Types.STRING); entityType.addDelegate(delegate); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("return Entity.test(this);")); CompileUtils.assertCompiles("QEntity", writer.toString()); }