public int run()

in lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java [49:197]


  public int run(InputStream in, PrintStream out, PrintStream err, List<String> origArgs) throws Exception {
    if (origArgs.size() < 3) {
      System.err
          .println("Usage: [-encoding <outputencoding>] [-string] [-bigDecimal] [-fieldVisibility <visibilityType>] "
              + "[-noSetters] [-nullSafeAnnotations] [-nullSafeAnnotationNullable <nullableAnnotation>] "
              + "[-nullSafeAnnotationNotNull <notNullAnnotation>] [-addExtraOptionalGetters] "
              + "[-optionalGetters <optionalGettersType>] [-templateDir <templateDir>] "
              + "(schema|protocol) input... outputdir");
      System.err.println(" input - input files or directories");
      System.err.println(" outputdir - directory to write generated java");
      System.err.println(" -encoding <outputencoding> - set the encoding of " + "output file(s)");
      System.err.println(" -string - use java.lang.String instead of Utf8");
      System.err.println(" -fieldVisibility [private|public] - use either and default private");
      System.err.println(" -noSetters - do not generate setters");
      System.err.println(" -nullSafeAnnotations - add @Nullable and @NotNull annotations");
      System.err.println(" -nullSafeAnnotationNullable - full package path of annotation to use for nullable fields");
      System.err
          .println(" -nullSafeAnnotationNotNull - full package path of annotation to use for non-nullable fields");
      System.err
          .println(" -addExtraOptionalGetters - generate extra getters with this format: 'getOptional<FieldName>'");
      System.err.println(
          " -optionalGetters [all_fields|only_nullable_fields]- generate getters returning Optional<T> for all fields or only for nullable fields");
      System.err
          .println(" -bigDecimal - use java.math.BigDecimal for " + "decimal type instead of java.nio.ByteBuffer");
      System.err.println(" -templateDir - directory with custom Velocity templates");
      return 1;
    }

    CompilerOptions compilerOpts = new CompilerOptions();
    compilerOpts.stringType = StringType.CharSequence;
    compilerOpts.useLogicalDecimal = false;
    compilerOpts.createSetters = true;
    compilerOpts.createNullSafeAnnotations = false;
    compilerOpts.nullSafeAnnotationNullable = Optional.empty();
    compilerOpts.nullSafeAnnotationNotNull = Optional.empty();
    compilerOpts.optionalGettersType = Optional.empty();
    compilerOpts.addExtraOptionalGetters = false;
    compilerOpts.encoding = Optional.empty();
    compilerOpts.templateDir = Optional.empty();
    compilerOpts.fieldVisibility = Optional.empty();

    List<String> args = new ArrayList<>(origArgs);
    int arg = 0;

    if (args.contains("-noSetters")) {
      compilerOpts.createSetters = false;
      args.remove(args.indexOf("-noSetters"));
    }

    if (args.contains("-nullSafeAnnotations")) {
      compilerOpts.createNullSafeAnnotations = true;
      args.remove(args.indexOf("-nullSafeAnnotations"));
    }

    if (args.contains("-nullSafeAnnotationNullable")) {
      arg = args.indexOf("-nullSafeAnnotationNullable") + 1;
      compilerOpts.nullSafeAnnotationNullable = Optional.of(args.get(arg));
      args.remove(arg);
      args.remove(arg - 1);
    }

    if (args.contains("-nullSafeAnnotationNotNull")) {
      arg = args.indexOf("-nullSafeAnnotationNotNull") + 1;
      compilerOpts.nullSafeAnnotationNotNull = Optional.of(args.get(arg));
      args.remove(arg);
      args.remove(arg - 1);
    }

    if (args.contains("-addExtraOptionalGetters")) {
      compilerOpts.addExtraOptionalGetters = true;
      args.remove(args.indexOf("-addExtraOptionalGetters"));
    }

    if (args.contains("-optionalGetters")) {
      arg = args.indexOf("-optionalGetters") + 1;
      try {
        compilerOpts.optionalGettersType = Optional
            .of(OptionalGettersType.valueOf(args.get(arg).toUpperCase(Locale.ENGLISH)));
      } catch (IllegalArgumentException | IndexOutOfBoundsException e) {
        System.err.println("Expected one of" + Arrays.toString(OptionalGettersType.values()));
        return 1;
      }
      args.remove(arg);
      args.remove(arg - 1);
    }

    if (args.contains("-encoding")) {
      arg = args.indexOf("-encoding") + 1;
      compilerOpts.encoding = Optional.of(args.get(arg));
      args.remove(arg);
      args.remove(arg - 1);
    }

    if (args.contains("-string")) {
      compilerOpts.stringType = StringType.String;
      args.remove(args.indexOf("-string"));
    }

    if (args.contains("-fieldVisibility")) {
      arg = args.indexOf("-fieldVisibility") + 1;
      try {
        compilerOpts.fieldVisibility = Optional.of(FieldVisibility.valueOf(args.get(arg).toUpperCase(Locale.ENGLISH)));
      } catch (IllegalArgumentException | IndexOutOfBoundsException e) {
        System.err.println("Expected one of" + Arrays.toString(FieldVisibility.values()));
        return 1;
      }
      args.remove(arg);
      args.remove(arg - 1);
    }
    if (args.contains("-templateDir")) {
      arg = args.indexOf("-templateDir") + 1;
      compilerOpts.templateDir = Optional.of(args.get(arg));
      args.remove(arg);
      args.remove(arg - 1);
    }

    arg = 0;
    if ("-bigDecimal".equalsIgnoreCase(args.get(arg))) {
      compilerOpts.useLogicalDecimal = true;
      arg++;
    }

    String method = args.get(arg);
    List<File> inputs = new ArrayList<>();
    File output = new File(args.get(args.size() - 1));

    for (int i = arg + 1; i < args.size() - 1; i++) {
      inputs.add(new File(args.get(i)));
    }

    if ("schema".equals(method)) {
      Schema.Parser parser = new Schema.Parser();
      for (File src : determineInputs(inputs, SCHEMA_FILTER)) {
        Schema schema = parser.parse(src);
        final SpecificCompiler compiler = new SpecificCompiler(schema);
        executeCompiler(compiler, compilerOpts, src, output);
      }
    } else if ("protocol".equals(method)) {
      for (File src : determineInputs(inputs, PROTOCOL_FILTER)) {
        Protocol protocol = Protocol.parse(src);
        final SpecificCompiler compiler = new SpecificCompiler(protocol);
        executeCompiler(compiler, compilerOpts, src, output);
      }
    } else {
      System.err.println("Expected \"schema\" or \"protocol\".");
      return 1;
    }
    return 0;
  }