Search in sources :

Example 11 with Parameter

use of com.querydsl.codegen.utils.model.Parameter in project querydsl by querydsl.

the class ScalaTypeDump method test.

@Test
@Ignore
public void test() throws IOException {
    List<Class<?>> classes = new ArrayList<Class<?>>();
    classes.add(SimpleExpression.class);
    classes.add(ComparableExpression.class);
    classes.add(BooleanExpression.class);
    classes.add(StringExpression.class);
    classes.add(TemporalExpression.class);
    classes.add(TimeExpression.class);
    classes.add(DateTimeExpression.class);
    classes.add(DateExpression.class);
    classes.add(EnumExpression.class);
    classes.add(NumberExpression.class);
    StringWriter w = new StringWriter();
    ScalaWriter writer = new ScalaWriter(w);
    writer.packageDecl("com.querydsl.scala");
    writer.imports(Expression.class.getPackage());
    for (Class<?> cl : classes) {
        Type type = new ClassType(cl);
        Type superClass = new ClassType(cl.getSuperclass());
        writer.beginClass(type, superClass);
        for (Method m : cl.getDeclaredMethods()) {
            List<Parameter> params = new ArrayList<Parameter>();
            for (Class<?> paramType : m.getParameterTypes()) {
                params.add(new Parameter("arg" + params.size(), new ClassType(paramType)));
            }
            Type returnType = new ClassType(m.getReturnType());
            writer.beginPublicMethod(returnType, ":" + m.getName(), params.toArray(new Parameter[0]));
            writer.end();
        }
        writer.end();
    }
    System.out.println(w);
}
Also used : Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) StringWriter(java.io.StringWriter) Expression(com.querydsl.core.types.Expression) ArrayList(java.util.ArrayList) Parameter(com.querydsl.codegen.utils.model.Parameter) ScalaWriter(com.querydsl.codegen.utils.ScalaWriter) Method(java.lang.reflect.Method) ClassType(com.querydsl.codegen.utils.model.ClassType) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with Parameter

use of com.querydsl.codegen.utils.model.Parameter in project querydsl by querydsl.

the class TypeElementHandler method transformParams.

public List<Parameter> transformParams(List<? extends VariableElement> params) {
    List<Parameter> parameters = new ArrayList<Parameter>(params.size());
    for (VariableElement param : params) {
        Type paramType = getType(param);
        parameters.add(new Parameter(param.getSimpleName().toString(), paramType));
    }
    return parameters;
}
Also used : QueryType(com.querydsl.core.annotations.QueryType) EntityType(com.querydsl.codegen.EntityType) Type(com.querydsl.codegen.utils.model.Type) PropertyType(com.querydsl.core.annotations.PropertyType) ArrayList(java.util.ArrayList) Parameter(com.querydsl.codegen.utils.model.Parameter) VariableElement(javax.lang.model.element.VariableElement)

Example 13 with Parameter

use of com.querydsl.codegen.utils.model.Parameter in project querydsl by querydsl.

the class BeanSerializer method serialize.

@Override
public void serialize(EntityType model, SerializerConfig serializerConfig, CodeWriter writer) throws IOException {
    String simpleName = model.getSimpleName();
    // package
    if (!model.getPackageName().isEmpty()) {
        writer.packageDecl(model.getPackageName());
    }
    // imports
    Set<String> importedClasses = getAnnotationTypes(model);
    for (Type iface : interfaces) {
        importedClasses.add(iface.getFullName());
    }
    importedClasses.add(generatedAnnotationClass.getName());
    if (model.hasLists()) {
        importedClasses.add(List.class.getName());
    }
    if (model.hasCollections()) {
        importedClasses.add(Collection.class.getName());
    }
    if (model.hasSets()) {
        importedClasses.add(Set.class.getName());
    }
    if (model.hasMaps()) {
        importedClasses.add(Map.class.getName());
    }
    if (addToString && model.hasArrays()) {
        importedClasses.add(Arrays.class.getName());
    }
    writer.importClasses(importedClasses.toArray(new String[0]));
    // javadoc
    writer.javadoc(simpleName + javadocSuffix);
    // header
    for (Annotation annotation : model.getAnnotations()) {
        writer.annotation(annotation);
    }
    writer.line("@", generatedAnnotationClass.getSimpleName(), "(\"", getClass().getName(), "\")");
    if (!interfaces.isEmpty()) {
        Type superType = null;
        if (printSupertype && model.getSuperType() != null) {
            superType = model.getSuperType().getType();
        }
        Type[] ifaces = interfaces.toArray(new Type[0]);
        writer.beginClass(model, superType, ifaces);
    } else if (printSupertype && model.getSuperType() != null) {
        writer.beginClass(model, model.getSuperType().getType());
    } else {
        writer.beginClass(model);
    }
    bodyStart(model, writer);
    if (addFullConstructor) {
        addFullConstructor(model, writer);
    }
    // fields
    for (Property property : model.getProperties()) {
        if (propertyAnnotations) {
            for (Annotation annotation : property.getAnnotations()) {
                writer.annotation(annotation);
            }
        }
        writer.privateField(property.getType(), property.getEscapedName());
    }
    // accessors
    for (Property property : model.getProperties()) {
        String propertyName = property.getEscapedName();
        // getter
        writer.beginPublicMethod(property.getType(), "get" + BeanUtils.capitalize(propertyName));
        writer.line("return ", propertyName, ";");
        writer.end();
        // setter
        Parameter parameter = new Parameter(propertyName, property.getType());
        writer.beginPublicMethod(Types.VOID, "set" + BeanUtils.capitalize(propertyName), parameter);
        writer.line("this.", propertyName, " = ", propertyName, ";");
        writer.end();
    }
    if (addToString) {
        addToString(model, writer);
    }
    bodyEnd(model, writer);
    writer.end();
}
Also used : Type(com.querydsl.codegen.utils.model.Type) ClassType(com.querydsl.codegen.utils.model.ClassType) Set(java.util.Set) HashSet(java.util.HashSet) Collection(java.util.Collection) Parameter(com.querydsl.codegen.utils.model.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) Arrays(java.util.Arrays) Map(java.util.Map) Annotation(java.lang.annotation.Annotation)

Example 14 with Parameter

use of com.querydsl.codegen.utils.model.Parameter in project querydsl by querydsl.

the class ScalaWriterTest method Primitive.

@Test
public void Primitive() throws IOException {
    writer.beginClass(testType);
    writer.beginConstructor(new Parameter("a", Types.INT));
    writer.end();
    writer.end();
    System.out.println(w);
    assertTrue(w.toString().contains("def this(a: Int) {"));
}
Also used : Parameter(com.querydsl.codegen.utils.model.Parameter) Test(org.junit.Test)

Example 15 with Parameter

use of com.querydsl.codegen.utils.model.Parameter in project querydsl by querydsl.

the class ScalaWriterTest method Arrays.

@Test
public void Arrays() throws IOException {
    // def main(args: Array[String]) {
    writer.beginClass(new SimpleType("Main"));
    writer.field(Types.STRING.asArrayType(), "stringArray");
    writer.beginPublicMethod(Types.VOID, "main", new Parameter("args", Types.STRING.asArrayType()));
    writer.line("//");
    writer.end();
    writer.beginPublicMethod(Types.VOID, "main2", new Parameter("args", new ClassType(TypeCategory.ARRAY, String[].class)));
    writer.line("//");
    writer.end();
    writer.end();
    System.out.println(w);
    assertTrue(w.toString().contains("var stringArray: Array[String]"));
    assertTrue(w.toString().contains("def main(args: Array[String])"));
    assertTrue(w.toString().contains("def main2(args: Array[String])"));
}
Also used : SimpleType(com.querydsl.codegen.utils.model.SimpleType) Parameter(com.querydsl.codegen.utils.model.Parameter) ClassType(com.querydsl.codegen.utils.model.ClassType) Test(org.junit.Test)

Aggregations

Parameter (com.querydsl.codegen.utils.model.Parameter)19 Test (org.junit.Test)11 Type (com.querydsl.codegen.utils.model.Type)7 ClassType (com.querydsl.codegen.utils.model.ClassType)5 SimpleType (com.querydsl.codegen.utils.model.SimpleType)5 ArrayList (java.util.ArrayList)4 StringWriter (java.io.StringWriter)3 Constructor (com.querydsl.codegen.utils.model.Constructor)2 PropertyType (com.querydsl.core.annotations.PropertyType)2 QueryType (com.querydsl.core.annotations.QueryType)2 Annotation (java.lang.annotation.Annotation)2 Map (java.util.Map)2 EntityType (com.querydsl.codegen.EntityType)1 JavaWriter (com.querydsl.codegen.utils.JavaWriter)1 ScalaWriter (com.querydsl.codegen.utils.ScalaWriter)1 DOT (com.querydsl.codegen.utils.Symbols.DOT)1 QueryDelegate (com.querydsl.core.annotations.QueryDelegate)1 Expression (com.querydsl.core.types.Expression)1 Writer (java.io.Writer)1 Method (java.lang.reflect.Method)1