Search in sources :

Example 6 with JavaWriter

use of com.squareup.javawriter.JavaWriter in project realm-java by realm.

the class RealmProxyMediatorGenerator method generate.

public void generate() throws IOException {
    String qualifiedGeneratedClassName = String.format("%s.%sMediator", REALM_PACKAGE_NAME, className);
    JavaFileObject sourceFile = processingEnvironment.getFiler().createSourceFile(qualifiedGeneratedClassName);
    JavaWriter writer = new JavaWriter(new BufferedWriter(sourceFile.openWriter()));
    writer.setIndent("    ");
    writer.emitPackage(REALM_PACKAGE_NAME);
    writer.emitEmptyLine();
    writer.emitImports("android.util.JsonReader", "java.io.IOException", "java.util.Collections", "java.util.HashSet", "java.util.List", "java.util.Map", "java.util.HashMap", "java.util.Set", "java.util.Iterator", "java.util.Collection", "io.realm.internal.ColumnInfo", "io.realm.internal.SharedRealm", "io.realm.internal.RealmObjectProxy", "io.realm.internal.RealmProxyMediator", "io.realm.internal.Row", "io.realm.internal.Table", "io.realm.RealmObjectSchema", "org.json.JSONException", "org.json.JSONObject");
    writer.emitEmptyLine();
    writer.emitAnnotation(RealmModule.class);
    writer.beginType(// full qualified name of the item to generate
    qualifiedGeneratedClassName, // the type of the item
    "class", // modifiers to apply
    Collections.<Modifier>emptySet(), // class to extend
    "RealmProxyMediator");
    writer.emitEmptyLine();
    emitFields(writer);
    emitCreateTableMethod(writer);
    emitCreateRealmObjectSchema(writer);
    emitValidateTableMethod(writer);
    emitGetFieldNamesMethod(writer);
    emitGetTableNameMethod(writer);
    emitNewInstanceMethod(writer);
    emitGetClassModelList(writer);
    emitCopyToRealmMethod(writer);
    emitInsertObjectToRealmMethod(writer);
    emitInsertListToRealmMethod(writer);
    emitInsertOrUpdateObjectToRealmMethod(writer);
    emitInsertOrUpdateListToRealmMethod(writer);
    emitCreteOrUpdateUsingJsonObject(writer);
    emitCreateUsingJsonStream(writer);
    emitCreateDetachedCopyMethod(writer);
    writer.endType();
    writer.close();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) JavaWriter(com.squareup.javawriter.JavaWriter) BufferedWriter(java.io.BufferedWriter)

Example 7 with JavaWriter

use of com.squareup.javawriter.JavaWriter in project paraphrase by JakeWharton.

the class ParaphraseWriter method write.

void write(String packageName, List<Phrase> phrases) throws IOException {
    String filePath = packageName.replace('.', File.separatorChar) + File.separator + PHRASE_CLASS + ".java";
    File file = new File(outputDir, filePath);
    file.getParentFile().mkdirs();
    FileWriter fileWriter = new FileWriter(file);
    JavaWriter writer = new JavaWriter(fileWriter);
    try {
        writer.emitSingleLineComment("Generated by Paraphrase plugin. Do not modify!");
        writer.emitPackage(packageName);
        writer.emitImports("android.content.Context");
        writer.emitImports("android.content.res.Resources");
        writer.emitImports("android.text.SpannableStringBuilder");
        writer.emitImports("android.view.View");
        writer.emitImports("java.util.Arrays");
        writer.emitEmptyLine();
        writer.beginType(PHRASE_CLASS, "class", EnumSet.of(PUBLIC, FINAL));
        // Factory method for each phrase.
        for (Phrase phrase : phrases) {
            String className = classNameOf(phrase);
            if (phrase.documentation != null) {
                writer.emitJavadoc(phrase.documentation);
            }
            writer.beginMethod(className, phrase.name, EnumSet.of(PUBLIC, STATIC));
            writer.emitStatement("return new %s()", className);
            writer.endMethod();
            writer.emitEmptyLine();
        }
        for (Phrase phrase : phrases) {
            writePhraseClass(writer, phrase);
            writer.emitEmptyLine();
        }
        writeAbstractPhraseClass(writer);
        writer.emitEmptyLine();
        writer.beginConstructor(EnumSet.of(PRIVATE));
        writer.emitStatement("throw new AssertionError(\"No instances.\")");
        writer.endConstructor();
        writer.endType();
    } finally {
        Closeables.close(writer, true);
    }
}
Also used : FileWriter(java.io.FileWriter) JavaWriter(com.squareup.javawriter.JavaWriter) File(java.io.File)

Example 8 with JavaWriter

use of com.squareup.javawriter.JavaWriter in project gce2retrofit by chiuki.

the class Generator method generateInterface.

private static void generateInterface(WriterFactory writerFactory, String packageName, String resourceName, JsonObject methods, EnumSet<MethodType> methodTypes, Map<String, String> packageMap) throws IOException {
    String capitalizedName = WordUtils.capitalizeFully(resourceName, '_');
    String className = capitalizedName.replaceAll("_", "");
    ClassInfo classInfo = new ClassInfo(packageName, className);
    classInfo.movePackage(packageMap);
    String path = StringUtil.getPath(classInfo.packageName, classInfo.className + ".java");
    Writer fileWriter = writerFactory.getWriter(path);
    JavaWriter javaWriter = new JavaWriter(fileWriter);
    javaWriter.emitPackage(packageName);
    javaWriter.emitImports(packageName + ".model.*");
    if (packageMap != null) {
        Set<String> models = getModels(methods);
        Set<String> subdirectories = new HashSet<>(models.size());
        for (String model : models) {
            String dir = packageMap.get(getPrefix(packageMap, model));
            if (dir != null) {
                subdirectories.add(dir);
            }
        }
        ArrayList<String> dirs = new ArrayList<>(subdirectories);
        Collections.sort(dirs);
        for (String dir : dirs) {
            javaWriter.emitImports(packageName + ".model." + dir + ".*");
        }
    }
    if (methodTypes.contains(MethodType.V2)) {
        javaWriter.emitEmptyLine().emitImports("retrofit2.Call", "retrofit2.http.GET", "retrofit2.http.POST", "retrofit2.http.PATCH", "retrofit2.http.PUT", "retrofit2.http.DELETE", "retrofit2.http.Body", "retrofit2.http.Path", "retrofit2.http.Query");
    } else {
        javaWriter.emitEmptyLine().emitImports("retrofit.Callback", "retrofit.client.Response", "retrofit.http.GET", "retrofit.http.POST", "retrofit.http.PATCH", "retrofit.http.PUT", "retrofit.http.DELETE", "retrofit.http.Body", "retrofit.http.Path", "retrofit.http.Query");
    }
    if (methodTypes.contains(MethodType.REACTIVE)) {
        javaWriter.emitImports("rx.Observable");
    }
    javaWriter.emitEmptyLine();
    javaWriter.beginType(packageName + "." + className, "interface", EnumSet.of(PUBLIC));
    for (Entry<String, JsonElement> entry : methods.entrySet()) {
        String methodName = entry.getKey();
        Method method = gson.fromJson(entry.getValue(), Method.class);
        for (MethodType methodType : methodTypes) {
            String prefix = methodType.equals(MethodType.V2) ? "" : "/";
            javaWriter.emitAnnotation(method.httpMethod, "\"" + prefix + method.path + "\"");
            emitMethodSignature(fileWriter, methodName, method, methodType, packageMap);
        }
    }
    javaWriter.endType();
    fileWriter.close();
}
Also used : JsonElement(com.google.gson.JsonElement) JavaWriter(com.squareup.javawriter.JavaWriter) ArrayList(java.util.ArrayList) JavaWriter(com.squareup.javawriter.JavaWriter) Writer(java.io.Writer) HashSet(java.util.HashSet)

Example 9 with JavaWriter

use of com.squareup.javawriter.JavaWriter in project gce2retrofit by chiuki.

the class Generator method generateModel.

private static void generateModel(WriterFactory writerFactory, String modelPackageName, JsonObject schema, Map<String, String> classMap, Map<String, String> packageMap) throws IOException {
    String id = schema.get("id").getAsString();
    ClassInfo classInfo = new ClassInfo(modelPackageName, id);
    classInfo.movePackage(packageMap);
    String path = StringUtil.getPath(classInfo.packageName, classInfo.className + ".java");
    Writer writer = writerFactory.getWriter(path);
    JavaWriter javaWriter = new JavaWriter(writer);
    javaWriter.emitPackage(classInfo.packageName);
    if (packageMap != null) {
        ArrayList<String> dirs = new ArrayList<>(packageMap.values());
        Collections.sort(dirs);
        if (!modelPackageName.equals(classInfo.packageName)) {
            javaWriter.emitImports(modelPackageName + ".*");
        }
        for (String dir : dirs) {
            String packageName = modelPackageName + "." + dir;
            if (!packageName.equals(classInfo.packageName)) {
                javaWriter.emitImports(packageName + ".*");
            }
        }
        if (!dirs.isEmpty()) {
            javaWriter.emitEmptyLine();
        }
    }
    javaWriter.emitImports("com.google.gson.annotations.SerializedName").emitEmptyLine().emitImports("java.util.List").emitEmptyLine();
    String type = schema.get("type").getAsString();
    if (type.equals("object")) {
        javaWriter.beginType(classInfo.packageName + "." + classInfo.className, "class", EnumSet.of(PUBLIC));
        generateObject(javaWriter, schema, classMap, modelPackageName, packageMap);
        javaWriter.endType();
    } else if (type.equals("string")) {
        javaWriter.beginType(classInfo.packageName + "." + classInfo.className, "enum", EnumSet.of(PUBLIC));
        generateEnum(javaWriter, schema);
        javaWriter.endType();
    }
    writer.close();
}
Also used : JavaWriter(com.squareup.javawriter.JavaWriter) ArrayList(java.util.ArrayList) JavaWriter(com.squareup.javawriter.JavaWriter) Writer(java.io.Writer)

Aggregations

JavaWriter (com.squareup.javawriter.JavaWriter)9 BufferedWriter (java.io.BufferedWriter)4 JavaFileObject (javax.tools.JavaFileObject)4 ArrayList (java.util.ArrayList)3 Writer (java.io.Writer)2 JsonElement (com.google.gson.JsonElement)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 VariableElement (javax.lang.model.element.VariableElement)1 Types (javax.lang.model.util.Types)1