Java Code Examples for com.squareup.javawriter.JavaWriter#emitField()

The following examples show how to use com.squareup.javawriter.JavaWriter#emitField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AdapterHolderWriter.java    From Ollie with Apache License 2.0 6 votes vote down vote up
private void writeCollections(JavaWriter writer) throws IOException {
	writer.emitField(
			"List<Migration>",
			"MIGRATIONS",
			CONSTANT_MODIFIERS,
			"new ArrayList<Migration>()"
	);
	writer.emitField(
			"Map<Class<? extends Model>, ModelAdapter>",
			"MODEL_ADAPTERS",
			CONSTANT_MODIFIERS,
			"new HashMap<Class<? extends Model>, ModelAdapter>()"
	);
	writer.emitField(
			"Map<Class, TypeAdapter>",
			"TYPE_ADAPTERS",
			CONSTANT_MODIFIERS,
			"new HashMap<Class, TypeAdapter>()"
	);
	writer.emitEmptyLine();
}
 
Example 2
Source File: BuildConfigGenerator.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static void emitClassField(JavaWriter writer, ClassField field) throws IOException {
    String documentation = field.getDocumentation();
    if (!documentation.isEmpty()) {
        writer.emitJavadoc(documentation);
    }
    for (String annotation : field.getAnnotations()) {
        writer.emitAnnotation(annotation);
    }
    writer.emitField(
            field.getType(),
            field.getName(),
            PUBLIC_STATIC_FINAL,
            field.getValue());
}
 
Example 3
Source File: StringFogClassGenerator.java    From StringFog with Apache License 2.0 4 votes vote down vote up
public static void generate(File outputFile, String packageName, String className, String key,
                            String implementation) throws IOException {
    File outputDir = outputFile.getParentFile();
    if (!outputDir.exists() && !outputDir.mkdirs()) {
        throw new IOException("Can not mkdirs the dir: " + outputDir);
    }

    int lastIndexOfDot = implementation.lastIndexOf(".");
    String implementationSimpleClassName = lastIndexOfDot == -1 ? implementation :
            implementation.substring(implementation.lastIndexOf(".") + 1);

    JavaWriter javaWriter = new JavaWriter(new FileWriter(outputFile));
    javaWriter.emitPackage(packageName);
    javaWriter.emitEmptyLine();
    javaWriter.emitImports(implementation);
    javaWriter.emitEmptyLine();

    javaWriter.emitJavadoc("Generated code from StringFog gradle plugin. Do not modify!");
    javaWriter.beginType(className, "class", ImmutableSet.of(Modifier.PUBLIC,
            Modifier.FINAL));

    javaWriter.emitField(implementationSimpleClassName, "IMPL",
            ImmutableSet.of(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL),
            "new " + implementationSimpleClassName + "()");

    javaWriter.emitEmptyLine();
    javaWriter.beginMethod(String.class.getSimpleName(), "encrypt",
            ImmutableSet.of(Modifier.PUBLIC, Modifier.STATIC),
            String.class.getSimpleName(), "value");
    javaWriter.emitStatement("return " + "IMPL.encrypt(value, \"" + key + "\")");
    javaWriter.endMethod();

    javaWriter.emitEmptyLine();
    javaWriter.beginMethod(String.class.getSimpleName(), "decrypt",
            ImmutableSet.of(Modifier.PUBLIC, Modifier.STATIC),
            String.class.getSimpleName(), "value");
    javaWriter.emitStatement("return " + "IMPL.decrypt(value, \"" + key + "\")");
    javaWriter.endMethod();

    javaWriter.emitEmptyLine();
    javaWriter.beginMethod(boolean.class.getSimpleName(), "overflow",
            ImmutableSet.of(Modifier.PUBLIC, Modifier.STATIC),
            String.class.getSimpleName(), "value");
    javaWriter.emitStatement("return " + "IMPL.overflow(value, \"" + key + "\")");
    javaWriter.endMethod();

    javaWriter.emitEmptyLine();
    javaWriter.endType();

    javaWriter.close();
}
 
Example 4
Source File: ParaphraseWriter.java    From paraphrase with Apache License 2.0 4 votes vote down vote up
private static void writeAbstractPhraseClass(JavaWriter writer) throws IOException {
  writer.beginType(ABSTRACT_PHRASE_CLASS, "class", EnumSet.of(PUBLIC, STATIC, ABSTRACT));

  writer.emitField("int", "resId", EnumSet.of(PRIVATE, FINAL));
  writer.emitField("String[]", "keys", EnumSet.of(PRIVATE, FINAL));
  writer.emitField("CharSequence[]", "values", EnumSet.of(FINAL));
  writer.emitEmptyLine();

  writer.beginConstructor(EnumSet.of(PRIVATE), "int", "resId", "String...", "keys");
  writer.emitStatement("this.resId = resId");
  writer.emitStatement("this.keys = keys");
  writer.emitStatement("this.values = new String[keys.length]");
  writer.endConstructor();
  writer.emitEmptyLine();

  writer.emitAnnotation(SuppressWarnings.class, "\"ConstantConditions\"");
  writer.beginMethod("CharSequence", "build", EnumSet.of(PUBLIC, FINAL), "View", "view");
  writer.emitStatement("return build(view.getContext().getResources())");
  writer.endMethod();
  writer.emitEmptyLine();

  writer.beginMethod("CharSequence", "build", EnumSet.of(PUBLIC, FINAL), "Context", "context");
  writer.emitStatement("return build(context.getResources())");
  writer.endMethod();
  writer.emitEmptyLine();

  writer.beginMethod("CharSequence", "build", EnumSet.of(PUBLIC, FINAL), "Resources", "res");
  writer.emitStatement(
      "SpannableStringBuilder text = new SpannableStringBuilder(res.getText(resId))");
  writer.emitStatement("int token = 0");
  writer.beginControlFlow("while ((token = nextToken(text, token)) != -1)");
  writer.emitStatement("String key = tokenAt(text, token)");
  writer.beginControlFlow("if (key == null)");
  writer.emitSingleLineComment("Skip this and next character to avoid '{{' instances.");
  writer.emitStatement("token += 2");
  writer.emitStatement("continue");
  writer.endControlFlow();
  writer.emitStatement("int index = Arrays.binarySearch(keys, key)");
  writer.beginControlFlow("if (index < 0)");
  writer.emitStatement(
      "throw new AssertionError(\"Unknown key '\" + key + \"' in: \" + Arrays.toString(keys))");
  writer.endControlFlow();
  writer.emitStatement("CharSequence value = values[index]");
  writer.emitStatement("text.replace(token, token + key.length() + 2, value)");
  writer.emitStatement("token += value.length()");
  writer.endControlFlow();
  writer.emitStatement("return text");
  writer.endMethod();
  writer.emitEmptyLine();

  writer.beginMethod("int", "nextToken", EnumSet.of(PRIVATE, STATIC), "SpannableStringBuilder",
      "b", "int", "start");
  writer.beginControlFlow("for (int i = start + 1, end = b.length(); i < end; i++)");
  writer.beginControlFlow("if (b.charAt(i) == '{')");
  writer.emitStatement("return i");
  writer.endControlFlow();
  writer.endControlFlow();
  writer.emitStatement("return -1");
  writer.endMethod();
  writer.emitEmptyLine();

  writer.beginMethod("String", "tokenAt", EnumSet.of(PRIVATE, STATIC), "SpannableStringBuilder",
      "b", "int", "start");
  writer.beginControlFlow("for (int i = start + 1, end = b.length(); i < end; i++)");
  writer.emitStatement("char current = b.charAt(i)");
  writer.beginControlFlow("if (current == '}')");
  writer.emitStatement("return b.subSequence(start + 1, i).toString()");
  writer.endControlFlow();
  writer.beginControlFlow("if (current < 'a' || current > 'z')");
  writer.emitStatement("break");
  writer.endControlFlow();
  writer.endControlFlow();
  writer.emitStatement("return null");
  writer.endMethod();

  writer.endType();
}
 
Example 5
Source File: Generator.java    From gce2retrofit with Apache License 2.0 4 votes vote down vote up
private static void generateObject(
    JavaWriter javaWriter, JsonObject schema,
    Map<String, String> classMap, String packageName, Map<String, String> packageMap,
    Map<String, List<AnnotationType>> roomAnnnotationMap)
    throws IOException {
  JsonElement element = schema.get("properties");
  if (element == null) {
    return;
  }
  String id = schema.get("id").getAsString();
  JsonObject properties = element.getAsJsonObject();
  for (Entry<String, JsonElement> entry : properties.entrySet()) {
    String key = entry.getKey();
    String variableName = key;
    if (StringUtil.isReservedWord(key)) {
      javaWriter.emitAnnotation("SerializedName(\"" + key + "\")");
      variableName += "_";
    }
    final String annotationKey = id + "." + key;
    if (roomAnnnotationMap != null && roomAnnnotationMap.containsKey(annotationKey)) {
      final List<AnnotationType> annotations = roomAnnnotationMap.get(annotationKey);
      for (AnnotationType annotationType : annotations) {
        if (annotationType.attributes == null) {
          javaWriter.emitAnnotation(annotationType.annotation);
        } else {
          javaWriter.emitAnnotation(annotationType.annotation, annotationType.attributes);
        }
      }
    }
    PropertyType propertyType = gson.fromJson(
        entry.getValue(), PropertyType.class);
    String javaType = propertyType.toJavaType();
    if (classMap != null && classMap.containsKey(key)) {
      javaType = classMap.get(key);
    }

    boolean isList = javaType.startsWith("List<");
    if (isList) {
      javaType = javaType.substring(5, javaType.length() - 1);
    }
    ClassInfo classInfo = new ClassInfo(packageName, javaType);
    classInfo.movePackage(packageMap);
    javaWriter.emitField(isList ? "List<" + classInfo.className + ">" : classInfo.className,
        variableName, EnumSet.of(PUBLIC));
  }
}
 
Example 6
Source File: MementoProcessor.java    From memento with Apache License 2.0 4 votes vote down vote up
private void emitFields(Set<? extends Element> elements, JavaWriter writer) throws IOException {
    writer.emitEmptyLine();
    for (Element element : elements) {
        writer.emitField(element.asType().toString(), element.getSimpleName().toString());
    }
}