mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-24 21:07:26 +08:00
#43 : added flags for skipping fields and getters
This commit is contained in:
parent
3b0d93c4a6
commit
6e7146ea95
@ -74,6 +74,8 @@ public class GenericExporter {
|
||||
private final CodegenModule codegenModule = new CodegenModule();
|
||||
|
||||
private final SerializerConfig serializerConfig = SimpleSerializerConfig.DEFAULT;
|
||||
|
||||
private boolean handleFields = true, handleMethods = true;
|
||||
|
||||
@Nullable
|
||||
private File targetFolder;
|
||||
@ -222,45 +224,51 @@ public class GenericExporter {
|
||||
|
||||
private void addProperties(Class<?> cl, EntityType type) {
|
||||
Set<String> handled = new HashSet<String>();
|
||||
|
||||
// fields
|
||||
for (Field field : cl.getDeclaredFields()) {
|
||||
if (!Modifier.isStatic(field.getModifiers())) {
|
||||
AnnotatedElement annotated = ReflectionUtils.getAnnotatedElement(cl, field.getName(), field.getType());
|
||||
Method method = ReflectionUtils.getGetterOrNull(cl, field.getName(), field.getType());
|
||||
Type propertyType = null;
|
||||
if (method != null) {
|
||||
propertyType = getPropertyType(cl, annotated, method.getReturnType(), method.getGenericReturnType());
|
||||
} else {
|
||||
propertyType = getPropertyType(cl, annotated, field.getType(), field.getGenericType());
|
||||
if (handleFields) {
|
||||
for (Field field : cl.getDeclaredFields()) {
|
||||
if (!Modifier.isStatic(field.getModifiers())) {
|
||||
AnnotatedElement annotated = ReflectionUtils.getAnnotatedElement(cl, field.getName(), field.getType());
|
||||
Method method = ReflectionUtils.getGetterOrNull(cl, field.getName(), field.getType());
|
||||
Type propertyType = null;
|
||||
if (method != null) {
|
||||
propertyType = getPropertyType(cl, annotated, method.getReturnType(), method.getGenericReturnType());
|
||||
} else {
|
||||
propertyType = getPropertyType(cl, annotated, field.getType(), field.getGenericType());
|
||||
}
|
||||
Property property = createProperty(type, field.getName(), propertyType, field);
|
||||
if (property != null) {
|
||||
type.addProperty(property);
|
||||
}
|
||||
handled.add(field.getName());
|
||||
}
|
||||
Property property = createProperty(type, field.getName(), propertyType, field);
|
||||
if (property != null) {
|
||||
type.addProperty(property);
|
||||
}
|
||||
handled.add(field.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// getters
|
||||
for (Method method : cl.getDeclaredMethods()) {
|
||||
if (method.getParameterTypes().length == 0
|
||||
&& (method.getName().startsWith("get") || method.getName().startsWith("is"))) {
|
||||
String propertyName;
|
||||
if (method.getName().startsWith("get")) {
|
||||
propertyName = BeanUtils.uncapitalize(method.getName().substring(3));
|
||||
} else {
|
||||
propertyName = BeanUtils.uncapitalize(method.getName().substring(2));
|
||||
if (handleMethods) {
|
||||
for (Method method : cl.getDeclaredMethods()) {
|
||||
if (method.getParameterTypes().length == 0
|
||||
&& (method.getName().startsWith("get") || method.getName().startsWith("is"))) {
|
||||
String propertyName;
|
||||
if (method.getName().startsWith("get")) {
|
||||
propertyName = BeanUtils.uncapitalize(method.getName().substring(3));
|
||||
} else {
|
||||
propertyName = BeanUtils.uncapitalize(method.getName().substring(2));
|
||||
}
|
||||
if (handled.contains(propertyName)) {
|
||||
continue;
|
||||
}
|
||||
Type propertyType = getPropertyType(cl, method, method.getReturnType(), method.getGenericReturnType());
|
||||
Property property = createProperty(type, propertyName, propertyType, method);
|
||||
if (property != null) {
|
||||
type.addProperty(property);
|
||||
}
|
||||
}
|
||||
if (handled.contains(propertyName)) {
|
||||
continue;
|
||||
}
|
||||
Type propertyType = getPropertyType(cl, method, method.getReturnType(), method.getGenericReturnType());
|
||||
Property property = createProperty(type, propertyName, propertyType, method);
|
||||
if (property != null) {
|
||||
type.addProperty(property);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Type getPropertyType(Class<?> cl, AnnotatedElement annotated, Class<?> type, java.lang.reflect.Type genericType) {
|
||||
@ -408,4 +416,13 @@ public class GenericExporter {
|
||||
public void setPackageSuffix(String suffix) {
|
||||
codegenModule.bind(CodegenModule.PACKAGE_SUFFIX, suffix);
|
||||
}
|
||||
|
||||
public void setHandleFields(boolean b) {
|
||||
handleFields = b;
|
||||
}
|
||||
|
||||
public void setHandleMethods(boolean b) {
|
||||
handleMethods = b;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
@ -29,16 +30,16 @@ public class GenericExporterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Export_With_Package_Suffix() {
|
||||
exporter.setTargetFolder(new File("target/gen4"));
|
||||
exporter.setPackageSuffix("types");
|
||||
public void OverrideSerializer() {
|
||||
exporter.setTargetFolder(new File("target/gen2"));
|
||||
exporter.setSerializerClass(EntitySerializer.class);
|
||||
exporter.export(getClass().getPackage());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleEmbeddable.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleEmbedded.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleEntity.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleEntityInterface.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleSupertype.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegen/subtypes/QExampleEntity2.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleEmbeddable.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleEmbedded.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleEntity.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleEntityInterface.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleSupertype.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/sub/QExampleEntity2.java").exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -52,18 +53,27 @@ public class GenericExporterTest {
|
||||
assertTrue(new File("target/gen3/com/mysema/query/codegen/QExampleSupertype.java").exists());
|
||||
assertTrue(new File("target/gen3/com/mysema/query/codegen/sub/QExampleEntity2.java").exists());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void OverrideSerializer() {
|
||||
exporter.setTargetFolder(new File("target/gen2"));
|
||||
exporter.setSerializerClass(EntitySerializer.class);
|
||||
public void Export_With_Package_Suffix() {
|
||||
exporter.setTargetFolder(new File("target/gen4"));
|
||||
exporter.setPackageSuffix("types");
|
||||
exporter.export(getClass().getPackage());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleEmbeddable.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleEmbedded.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleEntity.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleEntityInterface.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/QExampleSupertype.java").exists());
|
||||
assertTrue(new File("target/gen2/com/mysema/query/codegen/sub/QExampleEntity2.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleEmbeddable.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleEmbedded.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleEntity.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleEntityInterface.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegentypes/QExampleSupertype.java").exists());
|
||||
assertTrue(new File("target/gen4/com/mysema/query/codegen/subtypes/QExampleEntity2.java").exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Export_Handle_No_Methods_Nor_Fields() {
|
||||
exporter.setTargetFolder(new File("target/gen5"));
|
||||
exporter.setHandleFields(false);
|
||||
exporter.setHandleMethods(false);
|
||||
exporter.export(getClass().getPackage());
|
||||
assertTrue(new File("target/gen5/com/mysema/query/codegen/QExampleEmbeddable.java").exists());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user