public List<Parameter> transformParams(List<? extends VariableElement> params) { List<Parameter> parameters = new ArrayList<Parameter>(params.size()); for (VariableElement param : params) { Type paramType = getType(param); parameters.add(new Parameter(param.getSimpleName().toString(), paramType)); } return parameters; }
@Override public Parameter apply(Property input) { return new Parameter(input.getName(), input.getType()); } };
private void addConstructors(Class<?> cl, EntityType type) { for (Constructor<?> constructor : cl.getConstructors()) { if (constructor.isAnnotationPresent(QueryProjection.class)) { List<Parameter> parameters = Lists.newArrayList(); for (int i = 0; i < constructor.getParameterTypes().length; i++) { Type parameterType = typeFactory.get( constructor.getParameterTypes()[i], constructor.getGenericParameterTypes()[i]); for (Annotation annotation : constructor.getParameterAnnotations()[i]) { if (annotation.annotationType().equals(QueryType.class)) { QueryType queryType = (QueryType) annotation; parameterType = parameterType.as(TypeCategory.valueOf(queryType.value().name())); } } parameters.add(new Parameter("param" + i, parameterType)); } type.addConstructor(new com.mysema.codegen.model.Constructor(parameters)); } } }
protected void listAccessor(EntityType model, Property field, CodeWriter writer) throws IOException { String escapedName = field.getEscapedName(); Type queryType = typeMappings.getPathType(field.getParameter(0), model, false); writer.beginPublicMethod(queryType, escapedName, new Parameter("index", Types.INT)); writer.line(RETURN + escapedName + ".get(index);").end(); writer.beginPublicMethod(queryType, escapedName, new Parameter("index", new ClassType(Expression.class, Types.INTEGER))); writer.line(RETURN + escapedName + ".get(index);").end(); }
protected void mapAccessor(EntityType model, Property field, CodeWriter writer) throws IOException { String escapedName = field.getEscapedName(); Type queryType = typeMappings.getPathType(field.getParameter(1), model, false); writer.beginPublicMethod(queryType, escapedName, new Parameter("key", field.getParameter(0))); writer.line(RETURN + escapedName + ".get(key);").end(); writer.beginPublicMethod(queryType, escapedName, new Parameter("key", new ClassType(Expression.class, field.getParameter(0)))); writer.line(RETURN + escapedName + ".get(key);").end(); }
@Test public void constructors() throws IOException { Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false,false); EntityType type = new EntityType(typeModel); // constructor Parameter firstName = new Parameter("firstName", Types.STRING); Parameter lastName = new Parameter("lastName", Types.STRING); Parameter age = new Parameter("age", Types.INTEGER); type.addConstructor(new Constructor(Arrays.asList(firstName, lastName, age))); Writer writer = new StringWriter(); ProjectionSerializer serializer = new ProjectionSerializer(new JavaTypeMappings()); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("Expression<String> firstName")); assertTrue(writer.toString().contains("Expression<String> lastName")); assertTrue(writer.toString().contains("Expression<Integer> age")); }
List<Parameter> params = new ArrayList<Parameter>(); for (Class<?> paramType : m.getParameterTypes()) { params.add(new Parameter("arg" + params.size(), new ClassType(paramType)));
writer.end(); Parameter parameter = new Parameter(propertyName, property.getType()); writer.beginPublicMethod(Types.VOID, "set" + BeanUtils.capitalize(propertyName), parameter); writer.line("this.", propertyName, " = ", propertyName, ";");
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(); }
@Override public Parameter apply(Parameter p) { Type type; if (!asExpr) { type = typeMappings.getExprType( p.getType(), model, false, false, true); } else if (p.getType().isFinal()) { type = new ClassType(Expression.class, p.getType()); } else { type = new ClassType(Expression.class, new TypeExtends(p.getType())); } return new Parameter(p.getName(), type); } });
@Override public Parameter apply(Parameter p) { Type type; if (!asExpr) { type = typeMappings.getExprType(p.getType(), model, false, false, true); } else if (p.getType().isFinal()) { type = new ClassType(Expression.class, p.getType()); } else { type = new ClassType(Expression.class, new TypeExtends(p.getType())); } return new Parameter(p.getName(), type); } });
if (model.isFinal()) { Type type = new ClassType(Path.class, simpleModel); writer.beginConstructor(new Parameter("path", type)); } else { Type type = new ClassType(Path.class, new TypeExtends(simpleModel)); writer.beginConstructor(new Parameter("path", type)); writer.beginConstructor(new Parameter("type", type), PATH_METADATA, PATH_INITS); writer.line("super(type, metadata, inits" + additionalParams + ");"); initEntityFields(writer, config, model);
@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))); }
@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(); }
@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(); }
@Test public void Constructors() throws IOException{ Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.mysema.query.DomainClass", "com.mysema.query", "DomainClass", false,false); EntityType type = new EntityType(typeModel); // constructor Parameter firstName = new Parameter("firstName", Types.STRING); Parameter lastName = new Parameter("lastName", Types.STRING); Parameter age = new Parameter("age", Types.INTEGER); type.addConstructor(new Constructor(Arrays.asList(firstName, lastName, age))); Writer writer = new StringWriter(); ProjectionSerializer serializer = new ProjectionSerializer(new JavaTypeMappings()); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("Expression<String> firstName")); assertTrue(writer.toString().contains("Expression<String> lastName")); assertTrue(writer.toString().contains("Expression<Integer> age")); }
@Test public void constructors() throws IOException { Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false,false); EntityType type = new EntityType(typeModel); // constructor Parameter firstName = new Parameter("firstName", Types.STRING); Parameter lastName = new Parameter("lastName", Types.STRING); Parameter age = new Parameter("age", Types.INTEGER); type.addConstructor(new Constructor(Arrays.asList(firstName, lastName, age))); Writer writer = new StringWriter(); ProjectionSerializer serializer = new ProjectionSerializer(new JavaTypeMappings()); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("Expression<String> firstName")); assertTrue(writer.toString().contains("Expression<String> lastName")); assertTrue(writer.toString().contains("Expression<Integer> age")); }
@SuppressWarnings("unchecked") @Before public void setUp() { // type Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.mysema.query.DomainClass", "com.mysema.query", "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))); }
@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))); }