use of com.querydsl.core.util.Annotations in project querydsl by querydsl.
the class TypeElementHandler method handleEntityType.
public EntityType handleEntityType(TypeElement element) {
EntityType entityType = typeFactory.getEntityType(element.asType(), true);
List<? extends Element> elements = element.getEnclosedElements();
VisitorConfig config = configuration.getConfig(element, elements);
Set<String> blockedProperties = new HashSet<String>();
Map<String, TypeMirror> propertyTypes = new HashMap<String, TypeMirror>();
Map<String, TypeMirror> fixedTypes = new HashMap<String, TypeMirror>();
Map<String, Annotations> propertyAnnotations = new HashMap<String, Annotations>();
// constructors
if (config.visitConstructors()) {
handleConstructors(entityType, elements);
}
// fields
if (config.visitFieldProperties()) {
for (VariableElement field : ElementFilter.fieldsIn(elements)) {
String name = field.getSimpleName().toString();
if (configuration.isBlockedField(field)) {
blockedProperties.add(name);
} else if (configuration.isValidField(field)) {
Annotations annotations = new Annotations();
configuration.inspect(field, annotations);
annotations.addAnnotation(field.getAnnotation(QueryType.class));
annotations.addAnnotation(field.getAnnotation(QueryInit.class));
propertyAnnotations.put(name, annotations);
propertyTypes.put(name, field.asType());
TypeMirror fixedType = configuration.getRealType(field);
if (fixedType != null) {
fixedTypes.put(name, fixedType);
}
}
}
}
// methods
if (config.visitMethodProperties()) {
for (ExecutableElement method : ElementFilter.methodsIn(elements)) {
String name = method.getSimpleName().toString();
if (name.startsWith("get") && name.length() > 3 && method.getParameters().isEmpty()) {
name = BeanUtils.uncapitalize(name.substring(3));
} else if (name.startsWith("is") && name.length() > 2 && method.getParameters().isEmpty()) {
name = BeanUtils.uncapitalize(name.substring(2));
} else {
continue;
}
if (configuration.isBlockedGetter(method)) {
blockedProperties.add(name);
} else if (configuration.isValidGetter(method) && !blockedProperties.contains(name)) {
Annotations annotations = propertyAnnotations.get(name);
if (annotations == null) {
annotations = new Annotations();
propertyAnnotations.put(name, annotations);
}
configuration.inspect(method, annotations);
annotations.addAnnotation(method.getAnnotation(QueryType.class));
annotations.addAnnotation(method.getAnnotation(QueryInit.class));
propertyTypes.put(name, method.getReturnType());
TypeMirror fixedType = configuration.getRealType(method);
if (fixedType != null) {
fixedTypes.put(name, fixedType);
}
}
}
}
// fixed types override property types
propertyTypes.putAll(fixedTypes);
for (Map.Entry<String, Annotations> entry : propertyAnnotations.entrySet()) {
Property property = toProperty(entityType, entry.getKey(), propertyTypes.get(entry.getKey()), entry.getValue());
if (property != null) {
entityType.addProperty(property);
}
}
return entityType;
}
use of com.querydsl.core.util.Annotations in project querydsl by querydsl.
the class GenericExporter method addProperties.
private void addProperties(Class<?> cl, EntityType type) {
Map<String, Type> types = new HashMap<>();
Map<String, Annotations> annotations = new HashMap<>();
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.querydsl.core.util.Annotations in project querydsl by querydsl.
the class AbstractDomainExporter method getAnnotatedElement.
protected AnnotatedElement getAnnotatedElement(Class<?> cl, String propertyName) throws NoSuchMethodException {
Field field = ReflectionUtils.getFieldOrNull(cl, propertyName);
Method method = ReflectionUtils.getGetterOrNull(cl, propertyName);
if (field != null) {
if (method != null) {
return new Annotations(field, method);
} else {
return field;
}
} else if (method != null) {
return method;
} else {
throw new IllegalArgumentException("No property found for " + cl.getName() + "." + propertyName);
}
}
Aggregations