use of com.google.gwt.core.ext.typeinfo.JClassType in project che by eclipse.
the class ExtensionRegistryGenerator method generateImports.
/**
* Inject imports
*
* @param extensions
* @param composerFactory
*/
private void generateImports(List<JClassType> extensions, ClassSourceFileComposerFactory composerFactory) {
// imports
composerFactory.addImport(GWT.class.getCanonicalName());
composerFactory.addImport(Extension.class.getCanonicalName());
composerFactory.addImport(ExtensionRegistry.class.getCanonicalName());
composerFactory.addImport(Inject.class.getCanonicalName());
composerFactory.addImport(Provider.class.getCanonicalName());
composerFactory.addImport(List.class.getCanonicalName());
composerFactory.addImport(ArrayList.class.getCanonicalName());
composerFactory.addImport(Map.class.getCanonicalName());
composerFactory.addImport(HashMap.class.getCanonicalName());
// import for extensions
for (JClassType jClassType : extensions) {
composerFactory.addImport(jClassType.getQualifiedSourceName());
}
}
use of com.google.gwt.core.ext.typeinfo.JClassType in project rstudio by rstudio.
the class JavaScriptSerializerGenerator method generate.
@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
TypeOracle oracle = context.getTypeOracle();
List<JClassType> classes = new ArrayList<JClassType>();
// locate all the types annotated with JavaScriptSerializable
for (JClassType classType : oracle.getTypes()) {
if (isAnnotatedSerializable(classType))
classes.add(classType);
}
ClassSourceFileComposerFactory sourceFile = new ClassSourceFileComposerFactory(genPackageName, genClassName);
sourceFile.addImplementedInterface(JavaScriptSerializer.class.getCanonicalName());
sourceFile.addImport("com.google.gwt.core.client.JavaScriptObject");
sourceFile.addImport("org.rstudio.core.client.js.JsObject;");
PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName);
if (printWriter != null) {
SourceWriter sourceWriter = sourceFile.createSourceWriter(context, printWriter);
sourceWriter.println(genClassName + "() {");
sourceWriter.println("}");
printSerializers(classes, sourceWriter);
printDeserializers(classes, sourceWriter);
sourceWriter.commit(logger);
}
return sourceFile.getCreatedClassName();
}
use of com.google.gwt.core.ext.typeinfo.JClassType in project rstudio by rstudio.
the class JavaScriptSerializerGenerator method printSerializers.
private void printSerializers(List<JClassType> classes, SourceWriter w) {
// print the method that dispatches to the appropriate serializer
w.println("public <T> JavaScriptObject serialize(T source)");
w.println("{");
w.indent();
for (JClassType classType : classes) {
if (classType.isAbstract())
continue;
w.println();
w.println("if (source.getClass().getName() == " + classType.getQualifiedSourceName() + ".class.getName())");
w.println("{");
w.indent();
w.println("return serializeJso((" + classType.getQualifiedSourceName() + ") source);");
w.outdent();
w.println("}");
w.println();
}
w.println("return null;");
w.outdent();
w.println("}");
// print individual serializers
for (JClassType classType : classes) {
w.print("private final native JavaScriptObject serializeJso(");
w.println(classType.getQualifiedSourceName() + " source) /*-{");
w.indent();
w.println("return {");
w.indent();
w.println("\"class_name\":\"" + classType.getQualifiedSourceName() + "\",");
w.println("\"class_data\": {");
w.indent();
JField[] fields = classType.getFields();
for (int i = 0; i < fields.length; i++) {
JField field = fields[i];
if (!field.isStatic()) {
w.print("\"" + field.getName() + "\": ");
if (isAnnotatedSerializable(field)) {
w.print("this.@" + genPackageName + "." + genClassName + "::serializeJso(L");
w.print(field.getType().getQualifiedSourceName().replace(".", "/"));
w.print(";)(");
}
w.println("source.@" + classType.getQualifiedSourceName() + "::" + field.getName());
if (isAnnotatedSerializable(field)) {
w.print(")");
}
if (i < (fields.length - 1))
w.print(", ");
w.println();
}
}
w.outdent();
w.println("}");
w.outdent();
w.println("};");
w.outdent();
w.println("}-*/;");
w.println();
}
}
use of com.google.gwt.core.ext.typeinfo.JClassType in project rstudio by rstudio.
the class JavaScriptSerializerGenerator method printDeserializers.
private void printDeserializers(List<JClassType> classes, SourceWriter w) {
w.println("private final native String classFromJso(" + "JavaScriptObject jso) /*-{");
w.indent();
w.println("return jso.class_name;");
w.outdent();
w.println("}-*/;");
w.println();
// print the method that dispatches to the appropriate deserializer
w.println("public <T> T deserialize (JavaScriptObject jso)");
w.println("{");
w.indent();
for (JClassType classType : classes) {
// ignore abstract classes
if (classType.isAbstract())
continue;
// determine class name from string
w.println();
w.println("if (classFromJso(jso) == \"" + classType.getQualifiedSourceName() + "\")");
w.println("{");
w.indent();
w.println(classType.getQualifiedSourceName() + " ret = new " + classType.getQualifiedSourceName() + "();");
w.println("deserializeJso(ret, jso);");
w.println("return (T) ret;");
w.outdent();
w.println("}");
w.println();
}
w.println("return null;");
w.outdent();
w.println("}");
// emit individual deserializer methods (overloads)
for (JClassType classType : classes) {
if (classType.isAbstract())
continue;
w.println();
w.println("private final native void deserializeJso(" + classType.getQualifiedSourceName() + " dest, " + "JavaScriptObject source) /*-{");
w.indent();
for (JField field : classType.getFields()) {
if (!field.isStatic()) {
w.print("dest.@" + classType.getQualifiedSourceName() + "::");
w.print(field.getName() + " = ");
if (isAnnotatedSerializable(field)) {
w.print("this.@" + genPackageName + "." + genClassName + "::deserialize(");
w.print("Lcom/google/gwt/core/client/JavaScriptObject;)(");
}
w.print("source.class_data[\"" + field.getName() + "\"]");
if (isAnnotatedSerializable(field))
w.print(")");
w.println(";");
}
}
w.outdent();
w.println("}-*/;");
}
}
use of com.google.gwt.core.ext.typeinfo.JClassType in project libgdx by libgdx.
the class ReflectionCacheGenerator method generate.
@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
TypeOracle oracle = context.getTypeOracle();
assert (oracle != null);
JClassType type = oracle.findType(typeName);
if (type == null) {
logger.log(ERROR, "Couldn't find type '" + typeName + "'");
throw new UnableToCompleteException();
}
if (type.isInterface() == null) {
logger.log(ERROR, "Type '" + typeName + "' must be an interface");
throw new UnableToCompleteException();
}
ReflectionCacheSourceCreator source = new ReflectionCacheSourceCreator(logger, context, type);
return source.create();
}
Aggregations