Search in sources :

Example 1 with FieldModel

use of org.platformlayer.model.FieldModel in project platformlayer by platformlayer.

the class GwtCodegenFileVisitor method processClassJso.

private void processClassJso(Class<?> clazz) throws MojoExecutionException {
    Map<String, Object> model = new HashMap<String, Object>();
    // ClassModel classModel = new ClassModel();
    // classModel.className = clazz.getSimpleName();
    // classModel.proxyClassName = classModel.className + "Proxy";
    // classModel.serviceClassName = classModel.className + "GwtService";
    // classModel.editorClassName = classModel.className + "Editor";
    List<FieldModel> fields = Lists.newArrayList();
    List<String> warnings = Lists.newArrayList();
    String jsoClassName = clazz.getSimpleName();
    String jsoBaseClassName = "com.google.gwt.core.client.JavaScriptObject";
    Set<String> skipFields = Sets.newHashSet();
    {
        Class<?> c = clazz;
        while (c != null) {
            if (c.getName().equals("org.platformlayer.core.model.ItemBase")) {
                jsoBaseClassName = "org.platformlayer.core.model.ItemBaseJs";
                skipFields.add("key");
                skipFields.add("tags");
                break;
            }
            if (c.getName().equals("org.platformlayer.core.model.Action")) {
                jsoBaseClassName = "org.platformlayer.core.model.ActionJs";
                skipFields.add("type");
                break;
            }
            if (c == Object.class) {
                break;
            } else {
                c = c.getSuperclass();
            }
        }
    }
    for (Field field : clazz.getFields()) {
        FieldModel fieldModel = new FieldModel();
        Class<?> type = field.getType();
        String fieldName = field.getName();
        if (skipFields.contains(fieldName)) {
            // These come in from the base class
            continue;
        }
        String beanName = Utils.capitalize(fieldName);
        Class<?> accessorType = type;
        if (accessorType.isPrimitive()) {
            accessorType = Utils.getBoxedType(accessorType);
        }
        if (type == Long.class || type.equals(long.class)) {
            warnings.add("JSNI cannot map 'long " + fieldName + "'");
            continue;
        }
        String mapped = null;
        if (!isNativeType(type)) {
            mapped = mapSpecialType(type);
            if (mapped == null) {
                String get = null;
                String set = null;
                if (type.equals(List.class)) {
                    Type genericType = field.getGenericType();
                    if (genericType instanceof ParameterizedType) {
                        ParameterizedType pt = (ParameterizedType) genericType;
                        Type[] actualTypeArguments = pt.getActualTypeArguments();
                        if (actualTypeArguments != null && actualTypeArguments.length == 1) {
                            Class<?> itemClass = (Class<?>) actualTypeArguments[0];
                            mapped = "java.util.List<" + itemClass.getName() + ">";
                            // public final
                            // List<org.platformlayer.service.certificates.model.PurchaseCertificateExample>
                            // getExamples() {
                            // return
                            // List<org.platformlayer.service.certificates.model.PurchaseCertificateExample>Js.get(this,
                            // "examples");
                            // }
                            //
                            // public final void
                            // setExamples(List<org.platformlayer.service.certificates.model.PurchaseCertificateExample>
                            // newValue) {
                            // List<org.platformlayer.service.certificates.model.PurchaseCertificateExample>Js.set(this,
                            // "examples", newValue);
                            // }
                            // Add imports??
                            get = "";
                            get += "public final java.util.List<{itemClass}> get{beanName}() {\n";
                            if (itemClass.equals(String.class)) {
                                get += "	com.google.gwt.core.client.JsArrayString array0 = com.gwtreboot.client.JsHelpers.getObject0(this, \"{fieldName}\").cast();\n";
                                get += "	return org.platformlayer.core.model.JsStringArrayToList.wrap(array0);\n";
                            } else {
                                get += "	com.google.gwt.core.client.JsArray<{itemClass}> array0 = com.gwtreboot.client.JsHelpers.getObject0(this, \"{fieldName}\").cast();\n";
                                get += "	return org.platformlayer.core.model.JsArrayToList.wrap(array0);\n";
                            }
                            get += "}\n";
                            set = "";
                            get = get.replace("{itemClass}", itemClass.getName());
                            set = set.replace("{itemClass}", itemClass.getName());
                        }
                    }
                } else if (type.equals(String.class)) {
                    get = "";
                    get += "public final String get{beanName}() {\n";
                    get += "	return com.gwtreboot.client.JsHelpers.getString0(this, \"{fieldName}\");\n";
                    get += "}\n";
                    set = "";
                    set += "public final void set{beanName}(String v) {\n";
                    set += "	com.gwtreboot.client.JsHelpers.set0(this, \"{fieldName}\", v);\n";
                    set += "}\n";
                    mapped = "HACK";
                } else if (type.equals(Boolean.class)) {
                    get = "";
                    get += "public final Boolean is{beanName}() {\n";
                    get += "	return com.gwtreboot.client.JsHelpers.getBoolean0(this, \"{fieldName}\");\n";
                    get += "}\n";
                    set = "";
                    set += "public final void set{beanName}(String v) {\n";
                    set += "	com.gwtreboot.client.JsHelpers.set0(this, \"{fieldName}\", v);\n";
                    set += "}\n";
                    mapped = "HACK";
                } else if (type.equals(Float.class)) {
                    get = "";
                    get += "public final Float get{beanName}() {\n";
                    get += "	return com.gwtreboot.client.JsHelpers.getFloat0(this, \"{fieldName}\");\n";
                    get += "}\n";
                    set = "";
                    set += "public final void set{beanName}(String v) {\n";
                    set += "	com.gwtreboot.client.JsHelpers.set0(this, \"{fieldName}\", v);\n";
                    set += "}\n";
                    mapped = "HACK";
                } else if (type.equals(Date.class)) {
                    get = "";
                    get += "public final java.util.Date get{beanName}() {\n";
                    get += "	return com.gwtreboot.client.JsHelpers.getDate0(this, \"{fieldName}\");\n";
                    get += "}\n";
                    set = "";
                    set += "public final void set{beanName}(java.util.Date v) {\n";
                    set += "	com.gwtreboot.client.JsHelpers.set0(this, \"{fieldName}\", v);\n";
                    set += "}\n";
                    mapped = "HACK";
                } else {
                    boolean gwtSafe = false;
                    for (Annotation annotation : type.getAnnotations()) {
                        if (annotation.annotationType().getSimpleName().equals("GwtSafe")) {
                            gwtSafe = true;
                        }
                    }
                    if (gwtSafe) {
                        get = "";
                        get += "public final {field.type} get{beanName}() {\n";
                        get += "	return com.gwtreboot.client.JsHelpers.getObject0(this, \"{fieldName}\").cast();\n";
                        get += "}\n";
                        set = "";
                        mapped = "HACK";
                    }
                }
                if (get != null) {
                    get = get.replace("{beanName}", beanName);
                    get = get.replace("{fieldName}", fieldName);
                    get = get.replace("{field.type}", type.getName());
                    fieldModel.customGet = get;
                }
                if (set != null) {
                    set = set.replace("{beanName}", beanName);
                    set = set.replace("{fieldName}", fieldName);
                    set = set.replace("{field.type}", type.getName());
                    fieldModel.customSet = set;
                }
            }
        }
        if (mapped != null) {
        } else if (!isNativeType(type)) {
            warnings.add("JSNI cannot map '" + type.getSimpleName() + " " + fieldName + "'");
            continue;
        }
        fieldModel.type = mapped != null ? mapped : type.getName();
        fieldModel.accessorType = mapped != null ? mapped : accessorType.getName();
        fieldModel.beanName = beanName;
        fieldModel.methodNameGet = "get" + beanName;
        if (type.equals(boolean.class) || type.equals(Boolean.class)) {
            fieldModel.methodNameGet = "is" + beanName;
        }
        fieldModel.name = fieldName;
        fieldModel.custom = mapped != null;
        fields.add(fieldModel);
    }
    // model.put("className", classModel.className);
    model.put("jsoClassName", jsoClassName);
    model.put("jsoBaseClassName", jsoBaseClassName);
    // model.put("serviceClassName", classModel.serviceClassName);
    // model.put("editorClassName", classModel.editorClassName);
    model.put("fields", fields);
    if (gwtBasePathComponents == null) {
        throw new MojoExecutionException("Did not find .gwt.xml file above " + clazz);
    }
    // String gwtPackage = Joiner.on(".").join(gwtBasePathComponents);
    String outputPackage = clazz.getPackage().getName();
    model.put("package", outputPackage);
    // File outputDir = new File(outDir, Joiner.on("/").join(gwtBasePathComponents));
    // Utils.mkdirs(outputDir);
    String outputPath = Joiner.on(".").join(gwtBasePathComponents) + ".translatable." + outputPackage;
    File outputDir = new File(outDir, outputPath.replace('.', '/'));
    Utils.mkdirs(outputDir);
    model.put("warnings", warnings);
    // String modelPackage = Joiner.on(".").join(getPathComponents());
    // model.put("modelPackage", modelPackage);
    //
    // String editorPackage = gwtPackage + ".client";
    // model.put("editorPackage", editorPackage);
    runTemplate("jso/JsoObject.ftl", model, new File(outputDir, jsoClassName + ".java"));
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) FieldModel(org.platformlayer.model.FieldModel) File(java.io.File)

Example 2 with FieldModel

use of org.platformlayer.model.FieldModel in project platformlayer by platformlayer.

the class GwtCodegenFileVisitor method processClassRequestFactory.

private void processClassRequestFactory(Class<?> clazz) throws MojoExecutionException {
    Map<String, Object> model = new HashMap<String, Object>();
    ClassModel classModel = new ClassModel();
    classModel.className = clazz.getSimpleName();
    classModel.proxyClassName = classModel.className + "Proxy";
    classModel.serviceClassName = classModel.className + "GwtService";
    classModel.editorClassName = classModel.className + "Editor";
    for (Field field : clazz.getFields()) {
        FieldModel fieldModel = new FieldModel();
        Class<?> type = field.getType();
        if (!isNativeType(type)) {
            continue;
        }
        Class<?> accessorType = type;
        if (accessorType.isPrimitive()) {
            accessorType = Utils.getBoxedType(accessorType);
        }
        String fieldName = field.getName();
        String beanName = Utils.capitalize(fieldName);
        fieldModel.type = type.getName();
        fieldModel.accessorType = accessorType.getName();
        fieldModel.beanName = beanName;
        fieldModel.name = fieldName;
        classModel.fields.add(fieldModel);
    }
    model.put("className", classModel.className);
    model.put("proxyClassName", classModel.proxyClassName);
    model.put("serviceClassName", classModel.serviceClassName);
    model.put("editorClassName", classModel.editorClassName);
    model.put("fields", classModel.fields);
    if (gwtBasePathComponents == null) {
        throw new MojoExecutionException("Did not find .gwt.xml file above " + clazz);
    }
    File gwtOutDir = new File(outDir, Joiner.on("/").join(gwtBasePathComponents));
    Utils.mkdirs(gwtOutDir);
    String gwtPackage = Joiner.on(".").join(gwtBasePathComponents);
    model.put("gwtPackage", gwtPackage);
    String modelPackage = Joiner.on(".").join(getPathComponents());
    model.put("modelPackage", modelPackage);
    String editorPackage = gwtPackage + ".client";
    model.put("editorPackage", editorPackage);
    runTemplate("requestfactory/GwtProxy.ftl", model, new File(new File(gwtOutDir, "shared"), classModel.proxyClassName + ".java"));
    runTemplate("requestfactory/GwtRequestFactory.ftl", model, new File(new File(gwtOutDir, "shared"), classModel.className + "RequestFactory.java"));
    runTemplate("requestfactory/GwtService.ftl", model, new File(new File(gwtOutDir, "server"), classModel.serviceClassName + ".java"));
}
Also used : Field(java.lang.reflect.Field) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) ClassModel(org.platformlayer.model.ClassModel) FieldModel(org.platformlayer.model.FieldModel) File(java.io.File)

Aggregations

File (java.io.File)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 FieldModel (org.platformlayer.model.FieldModel)2 Annotation (java.lang.annotation.Annotation)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ClassModel (org.platformlayer.model.ClassModel)1