Search in sources :

Example 6 with JMethod

use of com.google.gwt.core.ext.typeinfo.JMethod in project rstudio by rstudio.

the class ImageResourceInfo method emitCommandFields.

private void emitCommandFields(SourceWriter writer) throws UnableToCompleteException {
    // Declare the fields for the commands
    for (JMethod method : commandMethods_) {
        String name = method.getName();
        writer.println("private AppCommand " + name + "_;");
    }
}
Also used : JMethod(com.google.gwt.core.ext.typeinfo.JMethod)

Example 7 with JMethod

use of com.google.gwt.core.ext.typeinfo.JMethod in project rstudio by rstudio.

the class ImageResourceInfo method generateImageBundle.

private ImageResourceInfo generateImageBundle() {
    String className = bundleType_.getSimpleSourceName() + "__AutoGenResources";
    String pathToInstance = packageName_ + "." + className + ".INSTANCE";
    ImageResourceInfo iri = new ImageResourceInfo(pathToInstance);
    PrintWriter printWriter = context_.tryCreate(logger_, packageName_, className);
    if (printWriter == null)
        return null;
    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName_, className);
    factory.addImport("com.google.gwt.core.client.GWT");
    factory.addImport("com.google.gwt.resources.client.*");
    factory.makeInterface();
    factory.addImplementedInterface("ClientBundle");
    SourceWriter writer = factory.createSourceWriter(context_, printWriter);
    Set<String> resourceNames = context_.getResourcesOracle().getPathNames();
    for (JMethod method : commandMethods_) {
        String commandId = method.getName();
        String key = packageName_.replace('.', '/') + "/" + commandId;
        if (resourceNames.contains(key + ".png")) {
            writer.println("ImageResource " + commandId + "();");
            iri.addImage(commandId);
        }
        if (resourceNames.contains(key + "_2x.png")) {
            writer.println("@Source(\"" + commandId + "_2x.png\")");
            writer.println("ImageResource " + commandId + "2x();");
            iri.addImage(commandId + "2x");
        }
    }
    writer.println("public static final " + className + " INSTANCE = " + "(" + className + ")GWT.create(" + className + ".class);");
    writer.outdent();
    writer.println("}");
    context_.commit(logger_, printWriter);
    return iri;
}
Also used : ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) JMethod(com.google.gwt.core.ext.typeinfo.JMethod) SourceWriter(com.google.gwt.user.rebind.SourceWriter) PrintWriter(java.io.PrintWriter)

Example 8 with JMethod

use of com.google.gwt.core.ext.typeinfo.JMethod in project GwtMobile by dennisjzh.

the class EntityGenerator method AddVariables.

private void AddVariables() {
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("Persistence.define(\"%s\", new HashMap<String, String> () {{\n", requestedClassName));
    for (JMethod getter : getters) {
        String propertyName = getter.getName().substring(3);
        String propertyType = utils.getSQLiteType(getter.getReturnType());
        sb.append(String.format("\tput(\"%s\", \"%s\");\n", propertyName, propertyType));
    }
    sb.append("}});");
    utils.addVariable("private static", "JavaScriptObject", "nativeEntity", sb.toString());
    for (JMethod hasManyRel : hasManyRels) {
        String hasManyRelName = hasManyRel.getName().substring(3);
        String hasManyRelEntity = utils.getGenericTypeShortName(hasManyRel.getReturnType().getParameterizedQualifiedSourceName());
        utils.addVariable("private static", "EntityInternal<" + hasManyRelEntity + ">", "hasMany" + hasManyRelName, "GWT.create(" + hasManyRelEntity + ".class)");
    }
    for (JMethod hasOneRel : hasOneRels) {
        String hasOneRelColName = hasOneRel.getName().substring(3);
        String hasOneRelEntity = hasOneRel.getReturnType().getSimpleSourceName();
        utils.addVariable("private static", "EntityInternal<" + hasOneRelEntity + ">", "hasOne" + hasOneRelColName, "GWT.create(" + hasOneRelEntity + ".class)");
    }
}
Also used : JMethod(com.google.gwt.core.ext.typeinfo.JMethod)

Example 9 with JMethod

use of com.google.gwt.core.ext.typeinfo.JMethod in project GwtMobile by dennisjzh.

the class InstanceGenerator method generateClass.

@Override
public void generateClass() {
    utils.generateMethod("public", null, requestedClassName + "Impl", new String[][] { { "JavaScriptObject", "nativeObject" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("this.nativeObject = nativeObject;");
        }
    });
    utils.generateMethod("public", "JavaScriptObject", "getNativeObject", null, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("return nativeObject;");
        }
    });
    utils.generateMethod("public", "String", "getId", null, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("return getId(nativeObject);");
        }
    });
    utils.generateNativeMethod("private", "String", "getId", new String[][] { { "JavaScriptObject", "nativeObject" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("return nativeObject.id;");
        }
    });
    utils.generateMethod("public", "<T extends Persistable> void", "fetch", new String[][] { { "Transaction", "transaction" }, { "Entity<T>", "entity" }, { "ScalarCallback<T>", "callback" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("EntityInternal<T> entityInternal = (EntityInternal<T>)entity;");
            utils.println("fetch(transaction, entityInternal.getEntityName(), entityInternal, callback, this, nativeObject);");
        }
    });
    utils.generateMethod("public", "<T extends Persistable> void", "fetch", new String[][] { { "Entity<T>", "entity" }, { "ScalarCallback<T>", "callback" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("fetch(null, entity, callback);");
        }
    });
    utils.generateNativeMethod("private", " <T extends Persistable> void", "fetch", new String[][] { { "Transaction", "transaction" }, { "String", "property" }, { "EntityInternal<T>", "entity" }, { "ScalarCallback<T>", "callback" }, { requestedClassName + "Impl", "self" }, { "JavaScriptObject", "nativeObject" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("nativeObject.fetch($wnd.persistence, transaction, property, function(result) {");
            utils.sw().indent();
            utils.println("self.@%s.%s.%sImpl::processFetchCallback(Lcom/google/gwt/core/client/JavaScriptObject;Lcom/gwtmobile/persistence/client/EntityInternal;Lcom/gwtmobile/persistence/client/ScalarCallback;)(result, entity, callback);", utils.getPackageName(requestedClassName), generatedClassName, requestedClassName);
            utils.sw().outdent();
            utils.println("});");
        }
    });
    utils.generateMethod("private", "<T extends Persistable> void", "processFetchCallback", new String[][] { { "JavaScriptObject", "result" }, { "EntityInternal<T>", "entity" }, { "ScalarCallback<T>", "callback" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("callback.onSuccess(entity.newInstance(result));");
        }
    });
    utils.generateMethod("public", "void", "selectJSON", new String[][] { { "Transaction", "transaction" }, { "String[]", "propertySpec" }, { "ScalarCallback<String>", "callback" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("JsArrayString jsArray = null;");
            utils.println("if (propertySpec != null) {");
            utils.sw().indent();
            utils.println("jsArray = (JsArrayString) JavaScriptObject.createArray();");
            utils.println("for (int i = 0; i < propertySpec.length; i++) {");
            utils.println("String spec = propertySpec[i];");
            utils.println("jsArray.set(i, spec);");
            utils.sw().outdent();
            utils.println("}");
            utils.sw().outdent();
            utils.println("}");
            utils.println("selectJSON(transaction, jsArray, callback, this, nativeObject);");
        }
    });
    utils.generateMethod("public", "void", "selectJSON", new String[][] { { "String[]", "propertySpec" }, { "ScalarCallback<String>", "callback" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("selectJSON(null, propertySpec, callback);");
        }
    });
    utils.generateNativeMethod("private", "void", "selectJSON", new String[][] { { "Transaction", "transaction" }, { "JsArrayString", "propertySpec" }, { "ScalarCallback<String>", "callback" }, { requestedClassName + "Impl", "self" }, { "JavaScriptObject", "nativeObject" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("nativeObject.selectJSON(transaction, propertySpec, function(result) {");
            utils.sw().indent();
            utils.println("var resultJson = $wnd.JSON.stringify(result);");
            utils.println("self.@%s.%s.%sImpl::processStringCallback(Ljava/lang/String;Lcom/gwtmobile/persistence/client/ScalarCallback;)(resultJson, callback);", utils.getPackageName(requestedClassName), generatedClassName, requestedClassName);
            utils.sw().outdent();
            utils.println("});");
        }
    });
    utils.generateMethod("private", "void", "processStringCallback", new String[][] { { "String", "result" }, { "ScalarCallback<String>", "callback" } }, new MethodGenerator() {

        @Override
        public void generateMethod() {
            utils.println("callback.onSuccess(result);");
        }
    });
    for (final JMethod getter : getters) {
        //TODO: is getter method always public?
        final String returnTypeName = getter.getReturnType().getSimpleSourceName();
        final boolean isDateReturnType = returnTypeName.equals("Date");
        final boolean isPrimitiveReturnType = getter.getReturnType().isPrimitive() != null;
        final boolean isCharReturnType = returnTypeName.equals("char");
        final boolean isJsonReturnType = utils.isSubclassOf(getter.getReturnType(), "JSONValue");
        utils.generateMethod("public", returnTypeName, getter.getName(), null, new MethodGenerator() {

            @Override
            public void generateMethod() {
                if (isDateReturnType) {
                    utils.println("long value = (long)" + getter.getName() + "(nativeObject);");
                    //TODO: assume that the actual date is not "the epoch" for now.
                    utils.println("return (value == 0) ? null : new Date(value);");
                } else if (isJsonReturnType) {
                    utils.println("String value = " + getter.getName() + "(nativeObject);");
                    utils.println("return (value == null) ? null : (%s) JSONParser.parse(value);", returnTypeName);
                } else {
                    utils.println("return " + getter.getName() + "(nativeObject);");
                }
            }
        });
        utils.generateNativeMethod("private", isDateReturnType ? "double" : isJsonReturnType ? "String" : returnTypeName, getter.getName(), new String[][] { { "JavaScriptObject", "nativeObject" } }, new MethodGenerator() {

            @Override
            public void generateMethod() {
                utils.println("var value = nativeObject." + getter.getName().substring(3) + ";");
                //CHAR in SQLite has TEXT affinity.  
                if (isCharReturnType) {
                    utils.println("return (value == null || value.length == 0) ? 0 : value.charCodeAt(0);");
                } else if (isPrimitiveReturnType) {
                    utils.println("return (value == null) ? 0 : value;");
                } else if (isDateReturnType) {
                    //return Date.getTime() as double. Can't pass long to Java.
                    utils.println("return (value == null) ? 0 : value.getTime();");
                } else if (isJsonReturnType) {
                    utils.println("return (value == null) ? null : JSON.stringify(value);");
                } else {
                    utils.println("return value;");
                }
            }
        });
        utils.generateMethod("public", "void", "set" + getter.getName().substring(3), new String[][] { { returnTypeName, "value" } }, new MethodGenerator() {

            @Override
            public void generateMethod() {
                if (isDateReturnType) {
                    utils.println("set" + getter.getName().substring(3) + "(value == null ? 0 : value.getTime(), nativeObject);");
                } else if (isJsonReturnType) {
                    utils.println("set" + getter.getName().substring(3) + "(value == null ? null : value.toString(), nativeObject);");
                } else {
                    utils.println("set" + getter.getName().substring(3) + "(value, nativeObject);");
                }
            }
        });
        utils.generateNativeMethod("private", "void", "set" + getter.getName().substring(3), new String[][] { { isDateReturnType ? "double" : isJsonReturnType ? "String" : returnTypeName, "value" }, { "JavaScriptObject", "nativeObject" } }, new MethodGenerator() {

            @Override
            public void generateMethod() {
                if (isDateReturnType) {
                    utils.println("nativeObject." + getter.getName().substring(3) + " = (value == 0) ? null : new Date(value);");
                } else if (isJsonReturnType) {
                    utils.println("nativeObject." + getter.getName().substring(3) + " = (value == null) ? null : JSON.parse(value);");
                } else if (isCharReturnType) {
                    utils.println("nativeObject." + getter.getName().substring(3) + " = String.fromCharCode(value);");
                } else {
                    utils.println("nativeObject." + getter.getName().substring(3) + " = value;");
                }
            }
        });
    }
    for (final JMethod hasManyRel : hasManyRels) {
        utils.generateMethod("public", hasManyRel.getReturnType().getParameterizedQualifiedSourceName(), hasManyRel.getName(), null, new MethodGenerator() {

            @Override
            public void generateMethod() {
                utils.println("return hasMany" + hasManyRel.getName().substring(3) + ".newCollection(" + hasManyRel.getName() + "(nativeObject));");
            }
        });
        utils.generateNativeMethod("private", "JavaScriptObject", hasManyRel.getName(), new String[][] { { "JavaScriptObject", "nativeObject" } }, new MethodGenerator() {

            @Override
            public void generateMethod() {
                utils.println("return nativeObject." + hasManyRel.getName().substring(3) + ";");
            }
        });
    }
    for (final JMethod hasOneRel : hasOneRels) {
        utils.generateMethod("public", hasOneRel.getReturnType().getSimpleSourceName(), hasOneRel.getName(), null, new MethodGenerator() {

            @Override
            public void generateMethod() {
                utils.println("return hasOne" + hasOneRel.getName().substring(3) + ".newInstance(" + hasOneRel.getName() + "(nativeObject));");
            }
        });
        utils.generateNativeMethod("private", "JavaScriptObject", hasOneRel.getName(), new String[][] { { "JavaScriptObject", "nativeObject" } }, new MethodGenerator() {

            @Override
            public void generateMethod() {
                utils.println("return nativeObject." + hasOneRel.getName().substring(3) + ";");
            }
        });
        utils.generateMethod("public", "void", "set" + hasOneRel.getName().substring(3), new String[][] { { hasOneRel.getReturnType().getSimpleSourceName(), "value" } }, new MethodGenerator() {

            @Override
            public void generateMethod() {
                utils.println("set" + hasOneRel.getName().substring(3) + "(((PersistableInternal)(value)).getNativeObject(), nativeObject);");
            }
        });
        utils.generateNativeMethod("private", "void", "set" + hasOneRel.getName().substring(3), new String[][] { { "JavaScriptObject", "value" }, { "JavaScriptObject", "nativeObject" } }, new MethodGenerator() {

            @Override
            public void generateMethod() {
                utils.println("nativeObject." + hasOneRel.getName().substring(3) + " = value;");
            }
        });
    }
}
Also used : JMethod(com.google.gwt.core.ext.typeinfo.JMethod)

Aggregations

JMethod (com.google.gwt.core.ext.typeinfo.JMethod)9 UnableToCompleteException (com.google.gwt.core.ext.UnableToCompleteException)2 Element (org.w3c.dom.Element)2 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)1 JType (com.google.gwt.core.ext.typeinfo.JType)1 ClassSourceFileComposerFactory (com.google.gwt.user.rebind.ClassSourceFileComposerFactory)1 SourceWriter (com.google.gwt.user.rebind.SourceWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 NodeList (org.w3c.dom.NodeList)1