From 6e7146ea9571b9900c87f6d5a7145bb501e47435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Tue, 15 Nov 2011 20:33:53 +0200 Subject: [PATCH] #43 : added flags for skipping fields and getters --- .../mysema/query/codegen/GenericExporter.java | 83 +++++++++++-------- .../query/codegen/GenericExporterTest.java | 48 ++++++----- 2 files changed, 79 insertions(+), 52 deletions(-) diff --git a/querydsl-core/src/main/java/com/mysema/query/codegen/GenericExporter.java b/querydsl-core/src/main/java/com/mysema/query/codegen/GenericExporter.java index 2f748b88f..dccf8ec8d 100644 --- a/querydsl-core/src/main/java/com/mysema/query/codegen/GenericExporter.java +++ b/querydsl-core/src/main/java/com/mysema/query/codegen/GenericExporter.java @@ -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 handled = new HashSet(); + // 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; + } + } diff --git a/querydsl-core/src/test/java/com/mysema/query/codegen/GenericExporterTest.java b/querydsl-core/src/test/java/com/mysema/query/codegen/GenericExporterTest.java index 687510cbb..5070c8606 100644 --- a/querydsl-core/src/test/java/com/mysema/query/codegen/GenericExporterTest.java +++ b/querydsl-core/src/test/java/com/mysema/query/codegen/GenericExporterTest.java @@ -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()); } }