use of com.mysema.codegen.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.mysema.codegen.model.Type in project querydsl by querydsl.
the class GenericExporter method addProperties.
private void addProperties(Class<?> cl, EntityType type) {
Map<String, Type> types = Maps.newHashMap();
Map<String, Annotations> annotations = Maps.newHashMap();
PropertyHandling.Config config = propertyHandling.getConfig(cl);
// fields
if (config.isFields()) {
for (Field field : cl.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
if (Modifier.isTransient(field.getModifiers()) && !field.isAnnotationPresent(QueryType.class)) {
continue;
}
AnnotatedElement annotated = ReflectionUtils.getAnnotatedElement(cl, field.getName(), field.getType());
Type propertyType = getPropertyType(cl, annotated, field.getType(), field.getGenericType());
Annotations ann = new Annotations(field);
types.put(field.getName(), propertyType);
annotations.put(field.getName(), ann);
}
}
}
// getters
if (config.isMethods()) {
for (Method method : cl.getDeclaredMethods()) {
String name = method.getName();
if (method.getParameterTypes().length == 0 && !Modifier.isStatic(method.getModifiers()) && !method.isBridge() && ((name.startsWith("get") && name.length() > 3) || (name.startsWith("is") && name.length() > 2))) {
String propertyName;
if (name.startsWith("get")) {
propertyName = BeanUtils.uncapitalize(name.substring(3));
} else {
propertyName = BeanUtils.uncapitalize(name.substring(2));
}
Type propertyType = getPropertyType(cl, method, method.getReturnType(), method.getGenericReturnType());
if (!types.containsKey(propertyName) || !useFieldTypes) {
types.put(propertyName, propertyType);
}
Annotations ann = annotations.get(propertyName);
if (ann == null) {
ann = new Annotations();
annotations.put(propertyName, ann);
}
ann.addAnnotations(method);
}
}
}
for (Map.Entry<String, Type> entry : types.entrySet()) {
Annotations ann = annotations.get(entry.getKey());
Property property = createProperty(type, entry.getKey(), entry.getValue(), ann);
if (property != null) {
type.addProperty(property);
}
}
}
use of com.mysema.codegen.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;
}
use of com.mysema.codegen.model.Type in project querydsl by querydsl.
the class TypeResolver method resolveVar.
private static Type resolveVar(Type resolved, String varName, Type declaringType, EntityType context) {
// get parameter index of var in declaring type
int index = -1;
for (int i = 0; i < declaringType.getParameters().size(); i++) {
Type param = unwrap(declaringType.getParameters().get(i));
if (Objects.equal(getVarName(param), varName)) {
index = i;
}
}
if (index == -1) {
throw new IllegalStateException("Did not find type " + varName + " in " + declaringType + " for " + context);
}
Supertype type = context.getSuperType();
while (!type.getEntityType().equals(declaringType)) {
type = type.getEntityType().getSuperType();
}
if (!type.getType().getParameters().isEmpty()) {
return type.getType().getParameters().get(index);
} else {
// raw type
return resolved;
}
}
use of com.mysema.codegen.model.Type in project querydsl by querydsl.
the class PropertyTest method equals_and_hashCode.
@Test
public void equals_and_hashCode() {
Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false, false);
EntityType type = new EntityType(typeModel);
Property p1 = new Property(type, "property", type, Collections.<String>emptyList());
Property p2 = new Property(type, "property", type, Collections.<String>emptyList());
assertEquals(p1, p1);
assertEquals(p1, p2);
assertEquals(p1.hashCode(), p2.hashCode());
}
Aggregations