Android Open Source - DKO Gson Generator






From Project

Back to project page DKO.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project DKO listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.kered.dko.ant;
/*  ww w .ja  v  a  2 s  .  co  m*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

class GsonGenerator {

  public static void go(final String dir, final String pkg, final String pkgName, final Map<String, String> tableToClassNames) throws IOException {
    final String pkgDir = Util.join("/", pkg.split("[.]"));
    File f = new File(dir, pkgDir);
    f = new File(f, pkgName);
    f = new File(f, "_Gson.java");
    System.out.println("writing: "+ f.getAbsolutePath());

    final BufferedWriter br = new BufferedWriter(new FileWriter(f));

    br.write("package "+ pkg +"."+ pkgName +";\n");
    br.write("\n");
    br.write("import java.io.*;\n");
    br.write("import java.lang.reflect.*;\n");
    br.write("import org.kered.dko.*;\n");
    br.write("import com.google.gson.*;\n");
    br.write("import com.google.gson.reflect.*;\n");
    br.write("import com.google.gson.stream.*;\n");
    br.write("\n");
    br.write("public class _Gson {\n");
    br.write("\n");
    br.write("\tfinal static TypeAdapter<Class> _class = new TypeAdapter<Class>() {\n");
    br.write("\t\t@Override\n");
    br.write("\t\tpublic Class read(final JsonReader r) throws IOException {\n");
    br.write("\t\t\ttry {\n");
    br.write("\t\t\t\treturn this.getClass().getClassLoader().loadClass(r.nextString());\n");
    br.write("\t\t\t} catch (final ClassNotFoundException e) {\n");
    br.write("\t\t\t\tthrow new RuntimeException(e);\n");
    br.write("\t\t\t}\n");
    br.write("\t\t}\n");
    br.write("\t\t@Override\n");
    br.write("\t\tpublic void write(final JsonWriter w, final Class cls) throws IOException {\n");
    br.write("\t\t\tw.value(cls.getName());\n");
    br.write("\t\t}\n");
    br.write("\t};\n");
    br.write("\n");
    br.write("\t\tfinal static InstanceCreator<Query<?>> _query = new InstanceCreator<Query<?>>() {\n");
    br.write("\t\t\t@SuppressWarnings({ \"unchecked\", \"rawtypes\" })\n");
    br.write("\t\t\t@Override\n");
    br.write("\t\t\tpublic Query<?> createInstance(final Type type) {\n");
    br.write("\t\t\t\tfinal Type[] typeParameters = ((ParameterizedType)type).getActualTypeArguments();\n");
    br.write("\t\t\t\tfinal Type idType = typeParameters[0];\n");
    br.write("\t\t\t\treturn QueryFactory.IT.getQuery((Class)idType);\n");
    br.write("\t\t\t}\n");
    br.write("\t\t};\n");
    br.write("\n");

    br.write("\tpublic static GsonBuilder registerAllDKOs(GsonBuilder builder) {\n");
    br.write("\t\tbuilder = builder.registerTypeHierarchyAdapter(Class.class, _class);\n");
    for (final Entry<String, String> e : tableToClassNames.entrySet()) {
      br.write("\t\tbuilder = builder.registerTypeAdapter(new TypeToken<Query<"+ e.getValue() +">>(){}.getType(), _query);\n");
    }

    br.write("\t\treturn builder;\n");
    br.write("\t}\n");
    br.write("\n");
    br.write("}\n");

    br.close();
  }

}




Java Source Code List

.HelloWorld.java
org.kered.contactlensfinder.DB.java
org.kered.contactlensfinder.MainActivity.java
org.kered.contactlensfinder.ViewManufacturersActivity.java
org.kered.contactlensfinder.ViewProductsActivity.java
org.kered.contactlensfinder.ViewPropertiesActivity.java
org.kered.dko.AbstractQuery.java
org.kered.dko.Bulk.java
org.kered.dko.CSV.java
org.kered.dko.ClosableIterator.java
org.kered.dko.Condition.java
org.kered.dko.Constants.java
org.kered.dko.Context.java
org.kered.dko.DBQuery.java
org.kered.dko.DBRowIterator.java
org.kered.dko.Diff.java
org.kered.dko.DualIterator.java
org.kered.dko.Expression.java
org.kered.dko.Field.java
org.kered.dko.FilteringQuery.java
org.kered.dko.Function.java
org.kered.dko.InMemoryQuery.java
org.kered.dko.Join.java
org.kered.dko.LazyCacheIterable.java
org.kered.dko.LocalJoin.java
org.kered.dko.M.java
org.kered.dko.Main.java
org.kered.dko.MatryoshkaQuery.java
org.kered.dko.PeekableClosableIterator.java
org.kered.dko.PeekableIterator.java
org.kered.dko.QueryAddField.java
org.kered.dko.QueryFactory.java
org.kered.dko.QuerySnapshot.java
org.kered.dko.Query.java
org.kered.dko.SQLFunction.java
org.kered.dko.SelectAsMapIterable.java
org.kered.dko.SelectFromOAI.java
org.kered.dko.SelectSingleColumn.java
org.kered.dko.SoftJoinUtil.java
org.kered.dko.SoftJoin.java
org.kered.dko.SqlContext.java
org.kered.dko.Statistics.java
org.kered.dko.SubQueryField.java
org.kered.dko.TableInfo.java
org.kered.dko.TableWrapper.java
org.kered.dko.Table.java
org.kered.dko.TemporaryTableFactory.java
org.kered.dko.TmpTableBuilder.java
org.kered.dko.Tuple.java
org.kered.dko.UsageMonitor.java
org.kered.dko.UsageStats.java
org.kered.dko.Util.java
org.kered.dko.ant.ClassGenerator.java
org.kered.dko.ant.CodeGeneratorBase.java
org.kered.dko.ant.CodeGenerator.java
org.kered.dko.ant.DataSourceGenerator.java
org.kered.dko.ant.GsonGenerator.java
org.kered.dko.ant.JoinGenerator.java
org.kered.dko.ant.Main.java
org.kered.dko.ant.SchemaExtractorBase.java
org.kered.dko.ant.SchemaExtractor.java
org.kered.dko.ant.Util.java
org.kered.dko.datasource.CheapConnectionPoolingDataSource.java
org.kered.dko.datasource.ConnectionCountingDataSource.java
org.kered.dko.datasource.JDBCDriverDataSource.java
org.kered.dko.datasource.MatryoshkaDataSource.java
org.kered.dko.datasource.MirroredDataSource.java
org.kered.dko.datasource.ReflectedDataSource.java
org.kered.dko.datasource.SingleConnectionDataSource.java
org.kered.dko.datasource.SingleThreadedDataSource.java
org.kered.dko.datasource.UnClosableConnection.java
org.kered.dko.datasource.Util.java
org.kered.dko.json.CDL.java
org.kered.dko.json.CookieList.java
org.kered.dko.json.Cookie.java
org.kered.dko.json.HTTPTokener.java
org.kered.dko.json.HTTP.java
org.kered.dko.json.JSONArray.java
org.kered.dko.json.JSONException.java
org.kered.dko.json.JSONML.java
org.kered.dko.json.JSONObject.java
org.kered.dko.json.JSONString.java
org.kered.dko.json.JSONStringer.java
org.kered.dko.json.JSONTokener.java
org.kered.dko.json.JSONWriter.java
org.kered.dko.json.Pickle.java
org.kered.dko.json.XMLTokener.java
org.kered.dko.json.XML.java
org.kered.dko.junk.DerbyLoadTestSchema.java
org.kered.dko.junk.OracleCreateTestUser.java
org.kered.dko.junk.OracleLoadTestSchema.java
org.kered.dko.persistence.Util.java
org.kered.dko.util.DumpDatabase.java
sakila.Example0.java
sakila.Example1.java
sakila.Example2.java
sakila.Util.java