public void addInterface(Class<?> iface) { interfaces.add(new ClassType(iface)); }
@Test public void arrayType() { assertEquals(Types.BYTE.asArrayType(), factory.get(Byte[].class)); assertEquals(Types.BYTE_P.asArrayType(), factory.get(byte[].class)); }
@Override public String getGenericName(boolean asArgType, Set<String> packages, Set<String> classes) { if (!asArgType) { if (Types.OBJECT.equals(type)) { return "?"; } else { String genericName = super.getGenericName(true, packages, classes); return Strings.isNullOrEmpty(genericName) ? "?" : "? extends " + genericName; } } else { return super.getGenericName(asArgType, packages, classes); } }
@Override public String getGenericName(boolean asArgType, Set<String> packages, Set<String> classes) { if (parameters.isEmpty()) { return ClassUtils.getName(javaClass, packages, classes); } else { StringBuilder builder = new StringBuilder(); builder.append(ClassUtils.getName(javaClass, packages, classes)); builder.append("<"); boolean first = true; for (Type parameter : parameters) { if (!first) { builder.append(", "); } if (parameter == null || parameter.getFullName().equals(getFullName())) { builder.append("?"); } else { builder.append(parameter.getGenericName(false, packages, classes)); } first = false; } builder.append(">"); return builder.toString(); } }
@SuppressWarnings("rawtypes") public void register(TypeCategory category, @Nullable Class<? extends Expression> expr, @Nullable Class<? extends Path> path, @Nullable Class<? extends TemplateExpression> template) { if (expr != null) { exprTypes.put(category, new ClassType(expr)); } if (path != null) { pathTypes.put(category, new ClassType(path)); } if (template != null) { templateTypes.put(category, new ClassType(template)); } }
@Test public void ArrayType() { assertEquals(Types.BYTE.asArrayType(), factory.get(Byte[].class)); assertEquals(Types.BYTE_P.asArrayType(), factory.get(byte[].class)); }
@Test public void arrayType() { assertEquals(Types.BYTE.asArrayType(), factory.get(Byte[].class)); assertEquals(Types.BYTE_P.asArrayType(), factory.get(byte[].class)); }
private Type createOther(Class<?> cl, boolean entity, AnnotationHelper annotationHelper, Annotation annotation, Type[] parameters) { TypeCategory typeCategory = TypeCategory.get(cl.getName()); if (annotationHelper != null) { typeCategory = annotationHelper.getTypeByAnnotation(cl, annotation); } else if (!typeCategory.isSubCategoryOf(TypeCategory.COMPARABLE) && Comparable.class.isAssignableFrom(cl) && !cl.equals(Comparable.class)) { typeCategory = TypeCategory.COMPARABLE; } else if (embeddableTypes.contains(cl)) { typeCategory = TypeCategory.CUSTOM; } else if (typeCategory == TypeCategory.SIMPLE && entity) { typeCategory = TypeCategory.ENTITY; } else if (unknownAsEntity && typeCategory == TypeCategory.SIMPLE && !cl.getName().startsWith("java")) { typeCategory = TypeCategory.CUSTOM; } return new ClassType(typeCategory, cl, parameters); }
Type[] tempParams = (Type[]) Array.newInstance(Type.class, ReflectionUtils.getTypeParameterCount(genericType)); cache.put(key, new ClassType(cl, tempParams)); Type[] parameters = getParameters(cl, genericType); value = new ClassType(TypeCategory.ENUM, cl); } else if (Number.class.isAssignableFrom(cl) && Comparable.class.isAssignableFrom(cl)) { value = new ClassType(TypeCategory.NUMERIC, cl, parameters); } else if (entity) { value = createOther(cl, entity, annotationHelper, annotation, parameters);
@Test public void properties() 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))); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void properties() 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))); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); CompileUtils.assertCompiles("QEntity", writer.toString()); }
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(); }
@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); } });
Type superType = new ClassType(TypeCategory.SIMPLE, ConstructorExpression.class, model); writer.beginClass(queryType, superType); writer.privateStaticFinal(Types.LONG_P, "serialVersionUID", model.hashCode() + "L");
@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); } });
@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))); }