Package com.squareup.javawriter

Examples of com.squareup.javawriter.JavaWriter


      System.out.println( "Created output directory " + directory );
    }

    String fileName = directory + "/" + className + ".java";
    System.out.println( "Writing generated code to " + fileName );
    return new JavaWriter( new OutputStreamWriter( new FileOutputStream( fileName ), UTF_8 ) );
  }
View Full Code Here


  }

  /** Create the java functions required for the internal class */
  void brewJava(Writer writer) throws IOException {
    logger.d("brewJava");
    JavaWriter javaWriter = new JavaWriter(writer);
    javaWriter.setCompressingTypes(false);

    javaWriter.emitSingleLineComment("Generated code from Shillelagh. Do not modify!")
        .emitPackage(classPackage)
        /* Knows nothing of android types */
        .emitImports("android.content.ContentValues", "android.database.Cursor",
            "android.database.DatabaseUtils", "android.database.sqlite.SQLiteDatabase")
        .emitImports(ByteArrayInputStream.class, ByteArrayOutputStream.class, IOException.class,
            ObjectInputStream.class, ObjectOutputStream.class, LinkedList.class, Date.class,
            List.class)
        .beginType(className, "class", EnumSet.of(PUBLIC, FINAL));

    if (this.isChildTable) {
      emitParentInsert(javaWriter);
      emitSelectAll(javaWriter);
    }
    emitInsert(javaWriter);
    emitOneToOneInsert(javaWriter);
    emitGetId(javaWriter);
    emitCreateTable(javaWriter);
    emitDropTable(javaWriter);
    emitUpdate(javaWriter);
    emitUpdateColumnId(javaWriter);
    emitDeleteWithId(javaWriter);
    emitDeleteWithObject(javaWriter);
    emitMapCursorToObject(javaWriter);
    emitSelectById(javaWriter);
    emitByteArraySerialization(javaWriter);
    javaWriter.endType();
  }
View Full Code Here

  }

  /** Create the java functions required for the internal class */
  void brewJava(Writer writer) throws IOException {
    logger.d("brewJava");
    JavaWriter javaWriter = new JavaWriter(writer);
    javaWriter.setCompressingTypes(false);

    javaWriter.emitSingleLineComment("Generated code from Shillelagh. Do not modify!")
        .emitPackage(classPackage)
        /* Knows nothing of android types */
        .emitImports("android.content.ContentValues", "android.database.Cursor",
            "android.database.DatabaseUtils", "android.database.sqlite.SQLiteDatabase")
        .emitImports(ByteArrayInputStream.class, ByteArrayOutputStream.class, IOException.class,
            ObjectInputStream.class, ObjectOutputStream.class, LinkedList.class, Date.class,
            List.class)
        .beginType(className, "class", EnumSet.of(PUBLIC, FINAL));

    if (this.isChildTable) {
      emitParentInsert(javaWriter);
      emitSelectAll(javaWriter);
    }
    emitInsert(javaWriter);
    emitOneToOneInsert(javaWriter);
    emitGetId(javaWriter);
    emitCreateTable(javaWriter);
    emitDropTable(javaWriter);
    emitUpdate(javaWriter);
    emitUpdateColumnId(javaWriter);
    emitDeleteWithId(javaWriter);
    emitDeleteWithObject(javaWriter);
    emitMapCursorToObject(javaWriter);
    emitSingleMap(javaWriter);
    emitSelectById(javaWriter);
    emitByteArraySerialization(javaWriter);
    javaWriter.endType();
  }
View Full Code Here

  private static final Joiner argumentJoiner = Joiner.on(", ");

  void writeFactory(final FactoryDescriptor descriptor)
      throws IOException {
    JavaFileObject sourceFile = filer.createSourceFile(descriptor.name());
    JavaWriter writer = new JavaWriter(sourceFile.openWriter());
    String packageName = getPackage(descriptor.name()).toString();
    writer.emitPackage(packageName)
        .emitImports("javax.annotation.Generated");

    writer.emitImports("javax.inject.Inject");
    if (!descriptor.providerNames().isEmpty()) {
      writer.emitImports("javax.inject.Provider");
    }

    for (String implementingType : descriptor.implementingTypes()) {
      String implementingPackageName = getPackage(implementingType).toString();
      if (!"java.lang".equals(implementingPackageName)
          && !packageName.equals(implementingPackageName)) {
        writer.emitImports(implementingType);
      }
    }

    String[] implementedClasses = FluentIterable.from(descriptor.implementingTypes())
        .transform(new Function<String, String>() {
          @Override public String apply(String implemetingClass) {
            return getSimpleName(implemetingClass).toString();
          }
        })
        .toSortedSet(Ordering.natural())
        .toArray(new String[0]);

    String factoryName = getSimpleName(descriptor.name()).toString();
    writer.emitAnnotation(Generated.class,
        ImmutableMap.of("value", "\"" + AutoFactoryProcessor.class.getName() + "\""));
    EnumSet<Modifier> modifiers = EnumSet.of(FINAL);
    if (descriptor.publicType()) {
      modifiers.add(PUBLIC);
    }
    writer.beginType(factoryName, "class", modifiers,
        Object.class.getName().equals(descriptor.extendingType())
            ? null : descriptor.extendingType(),
        implementedClasses);

    ImmutableList.Builder<String> constructorTokens = ImmutableList.builder();
    for (Entry<Key, String> entry : descriptor.providerNames().entrySet()) {
      Key key = entry.getKey();
      String providerName = entry.getValue();
      writer.emitField("Provider<" + key.getType() + ">", providerName, EnumSet.of(PRIVATE, FINAL));
      Optional<String> qualifier = key.getQualifier();
      String qualifierPrefix = qualifier.isPresent() ? "@" + qualifier.get() + " " : "";
      constructorTokens.add(qualifierPrefix + "Provider<" + key.getType() + ">").add(providerName);
    }

    writer.emitAnnotation("Inject");
    writer.beginMethod(null, factoryName,
        descriptor.publicType() ? EnumSet.of(PUBLIC) : EnumSet.noneOf(Modifier.class),
        constructorTokens.build().toArray(new String[0]));

    for (String providerName : descriptor.providerNames().values()) {
      writer.emitStatement("this.%1$s = %1$s", providerName);
    }

    writer.endMethod();

    for (final FactoryMethodDescriptor methodDescriptor : descriptor.methodDescriptors()) {
      writer.beginMethod(methodDescriptor.returnType(), methodDescriptor.name(),
          methodDescriptor.publicMethod() ? EnumSet.of(PUBLIC) : EnumSet.noneOf(Modifier.class),
          parameterTokens(methodDescriptor.passedParameters()));
      FluentIterable<String> creationParameterNames =
          FluentIterable.from(methodDescriptor.creationParameters())
              .transform(new Function<Parameter, String>() {
                @Override public String apply(Parameter parameter) {
                  return methodDescriptor.passedParameters().contains(parameter)
                      ? parameter.name()
                      : descriptor.providerNames().get(parameter.asKey()) + ".get()";
                }
              });
      writer.emitStatement("return new %s(%s)", writer.compressType(methodDescriptor.returnType()),
          argumentJoiner.join(creationParameterNames));
      writer.endMethod();
    }

    for (ImplemetationMethodDescriptor methodDescriptor
        : descriptor.implementationMethodDescriptors()) {
      writer.emitAnnotation(Override.class);
      writer.beginMethod(methodDescriptor.returnType(), methodDescriptor.name(),
          methodDescriptor.publicMethod() ? EnumSet.of(PUBLIC) : EnumSet.noneOf(Modifier.class),
          parameterTokens(methodDescriptor.passedParameters()));
      FluentIterable<String> creationParameterNames =
          FluentIterable.from(methodDescriptor.passedParameters())
              .transform(new Function<Parameter, String>() {
                @Override public String apply(Parameter parameter) {
                  return parameter.name();
                }
              });
      writer.emitStatement("return create(%s)", argumentJoiner.join(creationParameterNames));
      writer.endMethod();
    }

    writer.endType();
    writer.close();
  }
View Full Code Here

        }

        File buildConfigJava = new File(pkgFolder, BUILD_CONFIG_NAME);
        FileWriter out = new FileWriter(buildConfigJava);

        JavaWriter writer = new JavaWriter(out);

        Set<Modifier> publicFinal = EnumSet.of(Modifier.PUBLIC, Modifier.FINAL);
        Set<Modifier> publicFinalStatic = EnumSet.of(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC);

        writer.emitJavadoc("Automatically generated file. DO NOT MODIFY")
                .emitPackage(mBuildConfigPackageName)
                .beginType("BuildConfig", "class", publicFinal);

        for (ClassField field : mFields) {
            writer.emitField(
                    field.getType(),
                    field.getName(),
                    publicFinalStatic,
                    field.getValue());
        }

        for (Object item : mItems) {
            if (item instanceof ClassField) {
                ClassField field = (ClassField)item;
                writer.emitField(
                        field.getType(),
                        field.getName(),
                        publicFinalStatic,
                        field.getValue());

            } else if (item instanceof String) {
                writer.emitSingleLineComment((String) item);
            }
        }

        writer.endType();

        out.close();
    }
View Full Code Here

  BuilderSourceGenerator underTest;

  @Before
  public void init() {
    out = new StringWriter();
    writer = new JavaWriter(out);
    underTest = new BuilderSourceGenerator(writer);
  }
View Full Code Here

  BuilderSourceGenerator underTest;

  @Before
  public void init() {
    out = new StringWriter();
    writer = new JavaWriter(out);
    underTest = new BuilderSourceGenerator(writer);
  }
View Full Code Here

  BuilderSourceGenerator underTest;

  @Before
  public void init() {
    out = new StringWriter();
    writer = new JavaWriter(out);
    underTest = new BuilderSourceGenerator(writer);
  }
View Full Code Here

  ManualBuilderSourceGenerator underTest;

  @Before
  public void init() {
    out = new StringWriter();
    writer = new JavaWriter(out);
    underTest = new ManualBuilderSourceGenerator(writer);
  }
View Full Code Here

  BuilderSourceGenerator underTest;

  @Before
  public void init() {
    out = new StringWriter();
    writer = new JavaWriter(out);
    underTest = new BuilderSourceGenerator(writer);
  }
View Full Code Here

TOP

Related Classes of com.squareup.javawriter.JavaWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.