use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class GenericExporter method serialize.
private void serialize(Serializer serializer, Map<Class<?>, EntityType> types) throws IOException {
for (Map.Entry<Class<?>, EntityType> entityType : types.entrySet()) {
Type type = typeMappings.getPathType(entityType.getValue(), entityType.getValue(), true);
String packageName = type.getPackageName();
String className = packageName.length() > 0 ? (packageName + "." + type.getSimpleName()) : type.getSimpleName();
SerializerConfig config = serializerConfig;
if (entityType.getKey().isAnnotationPresent(Config.class)) {
config = SimpleSerializerConfig.getConfig(entityType.getKey().getAnnotation(Config.class));
}
String fileSuffix = createScalaSources ? ".scala" : ".java";
write(serializer, className.replace('.', '/') + fileSuffix, config, entityType.getValue());
}
}
use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class BeanSerializerTest method properties.
@Test
public void properties() throws IOException {
// property
type.addProperty(new Property(type, "entityField", type));
type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel)));
type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel)));
type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel)));
type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class)));
type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel)));
for (Class<?> cl : Arrays.<Class<?>>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));
}
BeanSerializer serializer = new BeanSerializer();
serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
String str = writer.toString();
// System.err.println(str);
for (String prop : Arrays.asList("String[] arrayField;", "Boolean boolean$;", "Collection<DomainClass> collection;", "Comparable comparable;", "java.util.Date date;", "DomainClass entityField;", "Integer integer;", "List<DomainClass> listField;", "Map<DomainClass, DomainClass> mapField;", "Set<DomainClass> setField;", "java.sql.Time time;")) {
assertTrue(prop + " was not contained", str.contains(prop));
}
}
use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class TypeFactory method get.
public Type get(boolean entity, Class<?> cl, AnnotatedElement annotated, java.lang.reflect.Type genericType) {
List<Object> key = new ArrayList<>();
key.add(genericType);
AnnotationHelper annotationHelper = null;
Annotation selectedAnnotation = null;
if (annotated != null) {
for (Annotation annotation : annotated.getDeclaredAnnotations()) {
for (AnnotationHelper helper : annotationHelpers) {
if (helper.isSupported(annotation.annotationType())) {
key.add(annotation.annotationType());
selectedAnnotation = annotated.getAnnotation(annotation.annotationType());
annotationHelper = helper;
key.add(helper.getCustomKey(selectedAnnotation));
break;
}
}
}
}
key = Collections.unmodifiableList(key);
if (cache.containsKey(key)) {
Type value = cache.get(key);
if (entity && !(value instanceof EntityType)) {
value = new EntityType(value, variableNameFunction);
cache.put(key, value);
}
return value;
} else {
Type value = create(entity, cl, annotationHelper, selectedAnnotation, genericType, key);
cache.put(key, value);
return value;
}
}
use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class TypeFactory method create.
private Type create(boolean entity, Class<?> cl, AnnotationHelper annotationHelper, Annotation annotation, java.lang.reflect.Type genericType, List<?> key) {
if (cl.isPrimitive()) {
cl = PrimitiveUtils.wrap(cl);
}
Type value;
Type[] tempParams = (Type[]) Array.newInstance(Type.class, ReflectionUtils.getTypeParameterCount(genericType));
cache.put(key, new ClassType(cl, tempParams));
Type[] parameters = getParameters(cl, genericType);
if (cl.isArray()) {
Type componentType = get(cl.getComponentType());
if (cl.getComponentType().isPrimitive()) {
componentType = Types.PRIMITIVES.get(componentType);
}
value = componentType.asArrayType();
} else if (cl.isEnum()) {
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);
} else if (Map.class.isAssignableFrom(cl)) {
value = new SimpleType(Types.MAP, parameters[0], asGeneric(parameters[1]));
} else if (List.class.isAssignableFrom(cl)) {
value = new SimpleType(Types.LIST, asGeneric(parameters[0]));
} else if (Set.class.isAssignableFrom(cl)) {
value = new SimpleType(Types.SET, asGeneric(parameters[0]));
} else if (Collection.class.isAssignableFrom(cl)) {
value = new SimpleType(Types.COLLECTION, asGeneric(parameters[0]));
} else {
value = createOther(cl, entity, annotationHelper, annotation, parameters);
}
if (genericType instanceof TypeVariable) {
TypeVariable tv = (TypeVariable) genericType;
if (tv.getBounds().length == 1 && tv.getBounds()[0].equals(Object.class)) {
value = new TypeSuper(tv.getName(), value);
} else {
value = new TypeExtends(tv.getName(), value);
}
}
if (entity && !(value instanceof EntityType)) {
value = new EntityType(value, variableNameFunction);
}
return value;
}
use of com.querydsl.codegen.utils.model.Type in project querydsl by querydsl.
the class TypeResolver method resolve.
/**
* Resolve type declared in declaringType for context
*
* @param type type to be resolved
* @param declaringType declaration context of type
* @param context target context of type
* @return resolved type
*/
public static Type resolve(Type type, Type declaringType, EntityType context) {
Type resolved = unwrap(type);
String varName = getVarName(resolved);
if (varName != null) {
resolved = resolveVar(resolved, varName, declaringType, context);
} else if (!resolved.getParameters().isEmpty()) {
resolved = resolveWithParameters(resolved, declaringType, context);
}
// rewrap entity type
if (type instanceof EntityType) {
if (!unwrap(type).equals(resolved)) {
resolved = new EntityType(resolved, ((EntityType) type).getSuperTypes());
} else {
// reset to original type
resolved = type;
}
}
return resolved;
}
Aggregations