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

The following examples show how to use com.squareup.javawriter.JavaWriter#beginControlFlow() . 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: MapSaveStatementWriter.java    From postman with MIT License 6 votes vote down vote up
@Override
public void writeFieldWriteStatement(VariableElement field, JavaWriter writer)
        throws IOException {
    DeclaredType type = (DeclaredType) field.asType();
    List<? extends TypeMirror> typeArguments = type.getTypeArguments();
    TypeMirror keyType = typeArguments.get(0);
    TypeMirror valueType = typeArguments.get(1);

    writer.beginControlFlow("if (object.%s != null)", field.getSimpleName());
    writer.emitStatement(
            "%1$s.writeMapToBundle(object.%2$s, bundle, %3$s.class, %4$s.class, \"%2$s\")",
            MapBundler.class.getCanonicalName(),
            field.getSimpleName(),
            keyType,
            valueType);
    writer.endControlFlow();
}
 
Example 2
Source File: CollectionSaveStatementWriter.java    From postman with MIT License 6 votes vote down vote up
@Override
public void writeFieldReadStatement(VariableElement field,
                                    Collection<ExecutableElement> postCreateChildMethods,
                                    JavaWriter writer) throws IOException {
    DeclaredType type = (DeclaredType) field.asType();
    TypeMirror itemType = type.getTypeArguments().get(0);
    TypeMirror itemTypeErasure = processingEnv.getTypeUtils().erasure(itemType);

    String collectionInitializer;
    try {
        collectionInitializer = initializers.findCollectionInitializer(type);
    } catch (InvalidTypeException e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage(), field);
        collectionInitializer = "null";
    }
    writer.beginControlFlow("if (bundle.containsKey(\"%s\"))", field.getSimpleName());
    writer.emitStatement("object.%s = %s", field.getSimpleName(), collectionInitializer);
    writer.emitStatement(
            "%1$s.readCollectionFromBundle(object.%2$s, bundle, %3$s.class, \"%2$s\")",
            CollectionBundler.class.getCanonicalName(),
            field.getSimpleName(),
            itemTypeErasure);

    writePostCreateChildMethodCalls(field, itemType, postCreateChildMethods, writer);
    writer.endControlFlow();
}
 
Example 3
Source File: SettingsBuilder.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
private static void writeStringArrGetterFuncBody(JavaWriter writer, String prefName,
                                                 String defValue, Map properties)
        throws IOException {
    String separator = (String) properties.get("separator");
    writer.emitStatement("String s = getPreferences().getString(%s, null)",
            prefName);
    writer.beginControlFlow("if (s == null || s.length() == 0)");
    writer.emitStatement("return %s", defValue);
    writer.endControlFlow();
    writer.emitStatement("return s.split(String.valueOf(%s))", separator);
}
 
Example 4
Source File: EnumSaveStatementWriter.java    From postman with MIT License 5 votes vote down vote up
@Override
public void writeFieldReadStatement(VariableElement field,
                                    Collection<ExecutableElement> postCreateChildMethods,
                                    JavaWriter writer)
        throws IOException {
    String enumType = field.asType().toString();
    Name fieldName = field.getSimpleName();
    String fieldNameVariable = fieldName + "Name";
    writer.emitStatement("String %s = bundle.getString(\"%s\")", fieldNameVariable, fieldName);
    writer.beginControlFlow(String.format("if (%s != null)", fieldNameVariable));
    writer.emitStatement("object.%s = %s.valueOf(%s)", fieldName, enumType, fieldNameVariable);
    writer.endControlFlow();
}
 
Example 5
Source File: EnumSaveStatementWriter.java    From postman with MIT License 5 votes vote down vote up
@Override
public void writeFieldWriteStatement(VariableElement field, JavaWriter writer)
        throws IOException {
    Name fieldName = field.getSimpleName();
    writer.beginControlFlow(String.format("if (object.%s != null)", fieldName));
    writer.emitStatement("bundle.putString(\"%s\", object.%s.name())", fieldName, fieldName);
    writer.endControlFlow();
}
 
Example 6
Source File: CollectionSaveStatementWriter.java    From postman with MIT License 5 votes vote down vote up
@Override
public void writeFieldWriteStatement(VariableElement field, JavaWriter writer)
        throws IOException {
    DeclaredType type = (DeclaredType) field.asType();
    TypeMirror itemType = type.getTypeArguments().get(0);
    TypeMirror itemTypeErasure = processingEnv.getTypeUtils().erasure(itemType);

    writer.beginControlFlow("if (object.%s != null)", field.getSimpleName());
    writer.emitStatement(
            "%1$s.writeCollectionToBundle(object.%2$s, bundle, %3$s.class, \"%2$s\")",
            CollectionBundler.class.getCanonicalName(),
            field.getSimpleName(),
            itemTypeErasure);
    writer.endControlFlow();
}
 
Example 7
Source File: CollectionSaveStatementWriter.java    From postman with MIT License 5 votes vote down vote up
private void writePostCreateChildMethodCalls(VariableElement field,
                                             TypeMirror itemType,
                                             Collection<ExecutableElement>
                                                     postCreateChildMethods,
                                             JavaWriter writer) throws IOException {

    if (!postCreateChildMethods.isEmpty() && metaTypes.isSubtype(itemType, Names.PARCELABLE)) {
        writer.beginControlFlow("for (Object child : object.%s)", field.getSimpleName());
        for (ExecutableElement method : postCreateChildMethods) {
            writer.emitStatement("object.%s(child)", method.getSimpleName());
        }
        writer.endControlFlow();
    }
}
 
Example 8
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();
}