mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-27 21:01:15 +08:00
This commit is contained in:
parent
0a4ea969ac
commit
bc8f6941aa
@ -6,6 +6,7 @@
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@ -34,6 +35,8 @@ public final class EntityModel extends TypeModelAdapter implements Comparable<En
|
||||
|
||||
// mutable
|
||||
private boolean hasLists, hasMaps, hasEntityFields;
|
||||
|
||||
private final Set<Annotation> annotations = new HashSet<Annotation>();
|
||||
|
||||
private final Set<MethodModel> methods = new HashSet<MethodModel>();
|
||||
|
||||
@ -65,6 +68,10 @@ public final class EntityModel extends TypeModelAdapter implements Comparable<En
|
||||
constructors.add(co);
|
||||
}
|
||||
|
||||
public void addAnnotation(Annotation annotation){
|
||||
annotations.add(annotation);
|
||||
}
|
||||
|
||||
public void addMethod(MethodModel method){
|
||||
methods.add(method);
|
||||
}
|
||||
@ -153,6 +160,10 @@ public final class EntityModel extends TypeModelAdapter implements Comparable<En
|
||||
return hasMaps;
|
||||
}
|
||||
|
||||
public Set<Annotation> getAnnotations() {
|
||||
return annotations;
|
||||
}
|
||||
|
||||
public void include(EntityModel clazz) {
|
||||
for (MethodModel method : clazz.methods){
|
||||
addMethod(method);
|
||||
|
||||
@ -7,6 +7,7 @@ package com.mysema.query.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -208,6 +209,9 @@ public class EntitySerializer implements Serializer{
|
||||
default : pathType = PEntity.class;
|
||||
}
|
||||
writer.suppressWarnings("serial");
|
||||
for (Annotation annotation : model.getAnnotations()){
|
||||
writer.annotation(annotation);
|
||||
}
|
||||
writer.beginClass(queryType, pathType.getSimpleName() + "<" + localName + ">");
|
||||
}
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
package com.mysema.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.collections15.Transformer;
|
||||
@ -16,22 +17,24 @@ import org.apache.commons.collections15.Transformer;
|
||||
*/
|
||||
public interface CodeWriter extends Appendable{
|
||||
|
||||
CodeWriter beginClass(String simpleName, String superClass, String... interfaces) throws IOException;
|
||||
CodeWriter annotation(Annotation annotation) throws IOException;
|
||||
|
||||
CodeWriter beginInterface(String simpleName, String... interfaces) throws IOException;
|
||||
CodeWriter beginClass(String simpleName, String superClass, String... interfaces) throws IOException;
|
||||
|
||||
<T> CodeWriter beginConstructor(Collection<T> params, Transformer<T, String> transformer) throws IOException;
|
||||
|
||||
CodeWriter beginConstructor(String... params) throws IOException;
|
||||
|
||||
<T> CodeWriter beginMethod(String returnType, String methodName, Collection<T> parameters, Transformer<T, String> transformer) throws IOException;
|
||||
CodeWriter beginInterface(String simpleName, String... interfaces) throws IOException;
|
||||
|
||||
CodeWriter beginMethod(String returnType, String methodName, String... args) throws IOException;
|
||||
|
||||
<T> CodeWriter beginStaticMethod(String type, String name, Collection<T> params, Transformer<T, String> transformer) throws IOException;
|
||||
|
||||
CodeWriter beginLine(String... segments) throws IOException;
|
||||
|
||||
<T> CodeWriter beginMethod(String returnType, String methodName, Collection<T> parameters, Transformer<T, String> transformer) throws IOException;
|
||||
|
||||
CodeWriter beginMethod(String returnType, String methodName, String... args) throws IOException;
|
||||
|
||||
<T> CodeWriter beginStaticMethod(String type, String name, Collection<T> params, Transformer<T, String> transformer) throws IOException;
|
||||
|
||||
CodeWriter end() throws IOException;
|
||||
|
||||
CodeWriter imports(Class<?>... imports) throws IOException;
|
||||
|
||||
@ -6,10 +6,14 @@
|
||||
package com.mysema.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.collections15.Transformer;
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
@ -19,14 +23,57 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
|
||||
private final Appendable appendable;
|
||||
|
||||
private String type;
|
||||
|
||||
private String indent = "";
|
||||
|
||||
private String type;
|
||||
|
||||
public JavaWriter(Appendable appendable){
|
||||
this.appendable = appendable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodeWriter annotation(Annotation annotation) throws IOException {
|
||||
append(indent).append("@" + annotation.annotationType().getName()).append("(");
|
||||
boolean first = true;
|
||||
for (Method method : annotation.annotationType().getDeclaredMethods()){
|
||||
if (!first) append(",");
|
||||
append(method.getName()+"=");
|
||||
try {
|
||||
Object value = method.invoke(annotation);
|
||||
annotationConstant(value);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
return append(")").nl();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void annotationConstant(Object value) throws IOException{
|
||||
if (value instanceof Class){
|
||||
Class<?> clazz = (Class)value;
|
||||
if (!clazz.getPackage().getName().equals("java.lang")){
|
||||
append(clazz.getName()+".class");
|
||||
}else{
|
||||
append(clazz.getSimpleName()+".class");
|
||||
}
|
||||
}else if (value instanceof Number){
|
||||
append(value.toString());
|
||||
}else if (value instanceof Enum){
|
||||
Enum enumValue = (Enum)value;
|
||||
append(enumValue.getDeclaringClass().getName()+"."+enumValue.name());
|
||||
}else if (value instanceof String){
|
||||
append("\"" + StringEscapeUtils.escapeJava(value.toString())+"\"");
|
||||
}else{
|
||||
throw new IllegalArgumentException("Unsupported annotation value : " + value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaWriter append(char c) throws IOException {
|
||||
appendable.append(c);
|
||||
@ -38,7 +85,7 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
appendable.append(csq);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JavaWriter append(CharSequence csq, int start, int end) throws IOException {
|
||||
appendable.append(csq, start, end);
|
||||
@ -60,17 +107,17 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
if (type.contains("<")) type = type.substring(0, type.indexOf('<'));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public <T> CodeWriter beginConstructor(Collection<T> parameters, Transformer<T,String> transformer) throws IOException {
|
||||
append(indent + "public " + type + "(").params(parameters, transformer).append(") {\n");
|
||||
return goIn();
|
||||
}
|
||||
|
||||
|
||||
public CodeWriter beginConstructor(String... parameters) throws IOException{
|
||||
append(indent + "public " + type + "(").params(parameters).append(") {\n");
|
||||
return goIn();
|
||||
}
|
||||
|
||||
|
||||
public CodeWriter beginInterface(String simpleName, String... interfaces) throws IOException {
|
||||
append(indent + "public interface " + simpleName);
|
||||
if (interfaces.length > 0){
|
||||
@ -84,7 +131,7 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CodeWriter beginLine(String... segments) throws IOException {
|
||||
append(indent);
|
||||
@ -98,22 +145,22 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
append(indent + "public " + returnType + " " + methodName + "(").params(parameters, transformer).append(") {\n");
|
||||
return goIn();
|
||||
}
|
||||
|
||||
|
||||
public CodeWriter beginMethod(String returnType, String methodName, String... args) throws IOException{
|
||||
append(indent + "public " + returnType + " " + methodName + "(").params(args).append(") {\n");
|
||||
return goIn();
|
||||
}
|
||||
|
||||
|
||||
public <T> CodeWriter beginStaticMethod(String returnType, String methodName, Collection<T> parameters, Transformer<T, String> transformer) throws IOException {
|
||||
append(indent + "public static " + returnType + " " + methodName + "(").params(parameters, transformer).append(") {\n");
|
||||
return goIn();
|
||||
}
|
||||
|
||||
|
||||
public CodeWriter end() throws IOException{
|
||||
goOut();
|
||||
return line("}\n");
|
||||
}
|
||||
|
||||
|
||||
private CodeWriter goIn(){
|
||||
indent += " ";
|
||||
return this;
|
||||
@ -125,7 +172,8 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public CodeWriter imports(Class<?>... imports) throws IOException{
|
||||
for (Class<?> cl : imports) line("import " + cl.getName() + ";");
|
||||
return this;
|
||||
@ -136,7 +184,6 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public CodeWriter javadoc(String... lines) throws IOException {
|
||||
line("/**");
|
||||
for (String line : lines){
|
||||
@ -155,11 +202,11 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
public String join(String prefix, String suffix, String... args) {
|
||||
return join(prefix, suffix, Arrays.asList(args));
|
||||
}
|
||||
|
||||
|
||||
public CodeWriter line(String... segments) throws IOException{
|
||||
append(indent);
|
||||
for (String segment : segments){
|
||||
@ -174,7 +221,7 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public CodeWriter nl() throws IOException {
|
||||
return append("\n");
|
||||
}
|
||||
@ -230,10 +277,9 @@ public class JavaWriter implements Appendable, CodeWriter{
|
||||
for (Class<?> cl : imports) line("import static " + cl.getName() + ".*;");
|
||||
return this;
|
||||
}
|
||||
|
||||
public CodeWriter suppressWarnings(String type) throws IOException{
|
||||
|
||||
public CodeWriter suppressWarnings(String type) throws IOException{
|
||||
return line("@SuppressWarnings(\"" + type +"\")");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,44 @@
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.annotations.QueryExtensions;
|
||||
|
||||
public class EntityModelTest {
|
||||
|
||||
public static class QueryExt implements QueryExtensions{
|
||||
|
||||
private final Class<?> value;
|
||||
|
||||
public QueryExt(Class<?> value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> value() {
|
||||
return value;
|
||||
}
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return QueryExtensions.class;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void annotation() throws IOException{
|
||||
Annotation annotation = new QueryExt(Object.class);
|
||||
ClassTypeModel typeModel = new ClassTypeModel(TypeCategory.ENTITY, Object.class);
|
||||
EntityModel entityModel = new EntityModel("Q", typeModel);
|
||||
entityModel.addAnnotation(annotation);
|
||||
|
||||
TypeMappings typeMappings = new TypeMappings();
|
||||
EntitySerializer serializer = new EntitySerializer(typeMappings);
|
||||
StringWriter writer = new StringWriter();
|
||||
serializer.serialize(entityModel, SimpleSerializerConfig.DEFAULT, writer);
|
||||
System.out.println(writer);
|
||||
}
|
||||
}
|
||||
@ -16,7 +16,7 @@
|
||||
|
||||
<programlisting language="java"><![CDATA[
|
||||
java.sql.Connection conn; // connection of database containing the schema to use
|
||||
MetaDataExporter exporter = new MetaDataExporter("Q", "com.myproject.domain", "", "", "src/main/java");
|
||||
MetaDataExporter exporter = new MetaDataExporter("Q", "com.myproject.domain", null, null, "src/main/java");
|
||||
exporter.export(conn.getMetaData());
|
||||
]]></programlisting>
|
||||
|
||||
|
||||
@ -97,9 +97,9 @@ public class MetaDataExporter {
|
||||
throw new IllegalArgumentException("packageName needs to be set");
|
||||
|
||||
ResultSet tables = md.getTables(null, schemaPattern, tableNamePattern, null);
|
||||
// ClassModelFactory factory = new ClassModelFactory(new TypeModelFactory());
|
||||
while (tables.next()) {
|
||||
String tableName = tables.getString(3);
|
||||
System.err.println(tableName);
|
||||
TypeModel classTypeModel = new SimpleTypeModel(
|
||||
TypeCategory.ENTITY,
|
||||
"java.lang.Object",
|
||||
@ -107,6 +107,7 @@ public class MetaDataExporter {
|
||||
tableName,
|
||||
false);
|
||||
EntityModel classModel = new EntityModel(namePrefix, classTypeModel);
|
||||
classModel.addAnnotation(new TableImpl(tableName));
|
||||
ResultSet columns = md.getColumns(null, schemaPattern, tables.getString(3), null);
|
||||
while (columns.next()) {
|
||||
String name = columns.getString(4);
|
||||
|
||||
@ -1,3 +1,8 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.sql;
|
||||
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
@ -16,6 +21,9 @@ import java.lang.annotation.Target;
|
||||
@Retention(RUNTIME)
|
||||
public @interface Table {
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
String value();
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2009 Mysema Ltd.
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.sql;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class TableImpl implements Table{
|
||||
|
||||
private final String table;
|
||||
|
||||
public TableImpl(String table){
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String value() {
|
||||
return table;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return Table.class;
|
||||
}
|
||||
|
||||
}
|
||||
@ -24,10 +24,10 @@ public class MetaDataExporterTest {
|
||||
public void testGeneration() throws Exception {
|
||||
Connection conn = getHSQLConnection();
|
||||
Statement st = conn.createStatement();
|
||||
st.execute("drop table survey if exists");
|
||||
st.execute("create table survey (id int,name varchar(30));");
|
||||
try {
|
||||
MetaDataExporter exporter = new MetaDataExporter("Q", "com.mysema.query.sql.domain", "", "", "target");
|
||||
st.execute("drop table survey if exists");
|
||||
st.execute("create table survey (id int,name varchar(30));");
|
||||
MetaDataExporter exporter = new MetaDataExporter("Q", "com.mysema.query.sql.domain", null, null, "target");
|
||||
exporter.export(conn.getMetaData());
|
||||
} finally {
|
||||
st.close();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user