use of com.querydsl.codegen.Property in project querydsl by querydsl.
the class HibernateDomainExporter method collectTypes.
@Override
protected void collectTypes() throws IOException, XMLStreamException, ClassNotFoundException, NoSuchMethodException {
// super classes
Iterator<?> superClassMappings = configuration.getMappedSuperclassMappings();
while (superClassMappings.hasNext()) {
MappedSuperclass msc = (MappedSuperclass) superClassMappings.next();
EntityType entityType = createSuperType(msc.getMappedClass());
if (msc.getDeclaredIdentifierProperty() != null) {
handleProperty(entityType, msc.getMappedClass(), msc.getDeclaredIdentifierProperty());
}
Iterator<?> properties = msc.getDeclaredPropertyIterator();
while (properties.hasNext()) {
handleProperty(entityType, msc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
}
}
// entity classes
Iterator<?> classMappings = configuration.getClassMappings();
while (classMappings.hasNext()) {
PersistentClass pc = (PersistentClass) classMappings.next();
EntityType entityType = createEntityType(pc.getMappedClass());
if (pc.getDeclaredIdentifierProperty() != null) {
handleProperty(entityType, pc.getMappedClass(), pc.getDeclaredIdentifierProperty());
} else if (!pc.isInherited() && pc.hasIdentifierProperty()) {
logger.info(entityType.toString() + pc.getIdentifierProperty());
handleProperty(entityType, pc.getMappedClass(), pc.getIdentifierProperty());
} else if (pc.getIdentifier() != null) {
KeyValue identifier = pc.getIdentifier();
if (identifier instanceof Component) {
Component component = (Component) identifier;
Iterator<?> properties = component.getPropertyIterator();
if (component.isEmbedded()) {
while (properties.hasNext()) {
handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
}
} else {
String name = component.getNodeName();
Class<?> clazz = component.getType().getReturnedClass();
Type propertyType = getType(pc.getMappedClass(), clazz, name);
AnnotatedElement annotated = getAnnotatedElement(pc.getMappedClass(), name);
Property property = createProperty(entityType, name, propertyType, annotated);
entityType.addProperty(property);
// handle component properties
EntityType embeddedType = createEmbeddableType(propertyType);
while (properties.hasNext()) {
handleProperty(embeddedType, clazz, (org.hibernate.mapping.Property) properties.next());
}
}
}
// TODO handle other KeyValue subclasses such as Any, DependentValue and ToOne
}
Iterator<?> properties = pc.getDeclaredPropertyIterator();
while (properties.hasNext()) {
handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next());
}
}
}
use of com.querydsl.codegen.Property 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.codegen.Property in project querydsl by querydsl.
the class ExtendedBeanSerializerTest method equals_hashcode_tostring.
@Test
public void equals_hashcode_tostring() throws Exception {
Property idCol = new Property(type, "id", new ClassType(Integer.class));
idCol.addAnnotation(new ColumnImpl("ID"));
Property subIdCol = new Property(type, "sub_id", new ClassType(String.class));
subIdCol.addAnnotation(new ColumnImpl("SUB_ID"));
Property nameCol = new Property(type, "name", new ClassType(String.class));
nameCol.addAnnotation(new ColumnImpl("NAME"));
type.addProperty(idCol);
type.addProperty(subIdCol);
type.addProperty(nameCol);
type.getData().put(PrimaryKeyData.class, Arrays.asList(new PrimaryKeyData("PK", new String[] { "ID", "SUB_ID" })));
ExtendedBeanSerializer extendedBeanSerializer = new ExtendedBeanSerializer();
extendedBeanSerializer.setAddToString(true);
FileWriter fw = new FileWriter(srcFile);
extendedBeanSerializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(fw));
fw.close();
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { compileFolder.getRoot().toURI().toURL() });
int retCode = new SimpleCompiler().run(null, System.out, System.err, srcFile.getAbsolutePath());
assertEquals("The generated source should compile", 0, retCode);
Class<?> cls = Class.forName(FULL_NAME, true, classLoader);
ReflectionHelper reflection = new ReflectionHelper(cls);
Object obj1 = cls.newInstance();
Object obj1a = cls.newInstance();
Object obj2 = cls.newInstance();
reflection.setValues(obj1, 1, "##", "X");
reflection.setValues(obj1a, 1, "##", null);
reflection.setValues(obj2, 2, "--", "Y");
assertEquals(obj1, obj1a);
assertEquals(obj1.hashCode(), obj1a.hashCode());
assertNotEquals(obj1, obj2);
assertEquals(obj1.toString(), "DomainClass#1;##");
}
use of com.querydsl.codegen.Property in project querydsl by querydsl.
the class JPADomainExporter method handleProperty.
private void handleProperty(EntityType entityType, Class<?> cl, Attribute<?, ?> p) throws NoSuchMethodException, ClassNotFoundException {
Class<?> clazz = Object.class;
try {
clazz = p.getJavaType();
} catch (MappingException e) {
// ignore
}
Type propertyType = getType(cl, clazz, p.getName());
AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
propertyType = getTypeOverride(propertyType, annotated);
if (propertyType == null) {
return;
}
if (p.isCollection()) {
if (p instanceof MapAttribute) {
MapAttribute<?, ?, ?> map = (MapAttribute<?, ?, ?>) p;
Type keyType = typeFactory.get(map.getKeyJavaType());
Type valueType = typeFactory.get(map.getElementType().getJavaType());
valueType = getPropertyType(p, valueType);
propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
} else {
Type valueType = typeFactory.get(((PluralAttribute<?, ?, ?>) p).getElementType().getJavaType());
valueType = getPropertyType(p, valueType);
propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), valueType));
}
} else {
propertyType = getPropertyType(p, propertyType);
}
Property property = createProperty(entityType, p.getName(), propertyType, annotated);
entityType.addProperty(property);
}
use of com.querydsl.codegen.Property in project querydsl by querydsl.
the class MetaDataExporter method handleColumn.
private void handleColumn(EntityType classModel, String tableName, ResultSet columns) throws SQLException {
String columnName = normalize(columns.getString("COLUMN_NAME"));
String normalizedColumnName = namingStrategy.normalizeColumnName(columnName);
int columnType = columns.getInt("DATA_TYPE");
String typeName = columns.getString("TYPE_NAME");
Number columnSize = (Number) columns.getObject("COLUMN_SIZE");
Number columnDigits = (Number) columns.getObject("DECIMAL_DIGITS");
int columnIndex = columns.getInt("ORDINAL_POSITION");
int nullable = columns.getInt("NULLABLE");
String columnDefaultValue = columns.getString("COLUMN_DEF");
String propertyName = namingStrategy.getPropertyName(normalizedColumnName, classModel);
Class<?> clazz = configuration.getJavaType(columnType, typeName, columnSize != null ? columnSize.intValue() : 0, columnDigits != null ? columnDigits.intValue() : 0, tableName, columnName);
if (clazz == null) {
clazz = Object.class;
}
TypeCategory fieldType = TypeCategory.get(clazz.getName());
if (Number.class.isAssignableFrom(clazz)) {
fieldType = TypeCategory.NUMERIC;
} else if (Enum.class.isAssignableFrom(clazz)) {
fieldType = TypeCategory.ENUM;
}
Type typeModel = new ClassType(fieldType, clazz);
Property property = createProperty(classModel, normalizedColumnName, propertyName, typeModel);
ColumnMetadata column = ColumnMetadata.named(normalizedColumnName).ofType(columnType).withIndex(columnIndex);
if (nullable == DatabaseMetaData.columnNoNulls) {
column = column.notNull();
}
if (columnSize != null) {
column = column.withSize(columnSize.intValue());
}
if (columnDigits != null) {
column = column.withDigits(columnDigits.intValue());
}
property.getData().put("COLUMN", column);
if (columnAnnotations) {
property.addAnnotation(new ColumnImpl(normalizedColumnName));
}
if (validationAnnotations) {
if (nullable == DatabaseMetaData.columnNoNulls && columnDefaultValue == null) {
property.addAnnotation(new NotNullImpl());
}
int size = columns.getInt("COLUMN_SIZE");
if (size > 0 && clazz.equals(String.class)) {
property.addAnnotation(new SizeImpl(0, size));
}
}
classModel.addProperty(property);
}
Aggregations