예제 #1
0
 protected void write(JavaFile file) {
   try {
     file.writeTo(filer);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
예제 #2
0
 public void create() {
   final JavaFile javaFile =
       JavaFile.builder(packageName, createTerminalType())
           .indent("    ")
           .skipJavaLangImports(true)
           .build();
   Misc.writeJavaFile(javaFile, javaFileInfo.projectInfo().srcMainJavaDir());
   final TerminalTestWriter terminalTestWriter = new TerminalTestWriter(javaFileInfo);
   terminalTestWriter.create();
 }
예제 #3
0
 public void write(Filer filer) throws IOException {
   TypeSpec clazz =
       TypeSpec.classBuilder(bindingClass.getHelperClassName())
           .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
           .addType(createBuilderClass(bindingClass))
           .addMethod(createCallBuilderMethod(bindingClass))
           .addMethod(createBindMethod(bindingClass))
           .addMethod(createBindWithSourceMethod(bindingClass))
           .addMethod(createPackMethod(bindingClass))
           .build();
   JavaFile.builder(bindingClass.getPackageName(), clazz).build().writeTo(filer);
 }
  @Override
  public String getContents() {
    StrBuilder strBuilder = new StrBuilder();

    try {
      javaFile.writeTo(strBuilder);
    } catch (IOException e) {
      return null;
    }

    return strBuilder.build();
  }
예제 #5
0
 @Override
 protected JavaFile compose(ScopeSpec spec) {
   TypeSpec typeSpec = build(spec);
   return JavaFile.builder(spec.getClassName().packageName(), typeSpec).build();
 }
예제 #6
0
  private void processGenerated(Set annotations, RoundEnvironment roundEnv) {
    List<GeneratedModel> generatedModels = new ArrayList<>();

    for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(Generated.class)) {
      if (annotatedElement.getKind() != ElementKind.CLASS) {
        log(
            Diagnostic.Kind.NOTE,
            String.format(
                "Only Classes annotated with %1s(\"org.jsonschema2pojo\") annotation will be included",
                Generated.class.getSimpleName()));
      } else {
        log(
            Diagnostic.Kind.NOTE,
            String.format(
                "Generated class value %1s",
                annotatedElement.getAnnotation(Generated.class).value()[0]));
        if (annotatedElement
            .getAnnotation(Generated.class)
            .value()[0]
            .equals("org.jsonschema2pojo")) {

          GeneratedModelGenerator generatedModelGenerator =
              new GeneratedModelGenerator(
                  (TypeElement) annotatedElement, getLogger(), getElementUtils());

          try {
            generatedModelGenerator.initialize();
            generatedModels.add(generatedModelGenerator.generate());

          } catch (BaseGenerator.InitializationException e) {
            log(
                Diagnostic.Kind.ERROR,
                String.format(
                    "Failed to initialize %1s due to %2s",
                    GeneratedModelGenerator.class.getSimpleName(), e.getMessage()));
            e.printStackTrace();
          } catch (BaseGenerator.GenerationException e) {
            log(
                Diagnostic.Kind.ERROR,
                String.format(
                    "Code generator %1s failed due to %2s",
                    GeneratedModelGenerator.class.getSimpleName(), e.getMessage()));
            e.printStackTrace();
          }
        }
      }
    }

    try {
      List<List<GeneratedModel>> matches = PlugInUtils.findMatchingGroups(generatedModels);
      log(Diagnostic.Kind.NOTE, String.format("Found %1s groups.", matches.size()));
      for (List<GeneratedModel> group : matches) {
        log(Diagnostic.Kind.NOTE, String.format("Can group %1s generated objects.", group.size()));
        for (GeneratedModel item : group) {
          log(Diagnostic.Kind.NOTE, item.getClassElement().getSimpleName().toString());
        }
      }
      GeneratedModelConverter generatedModelConverter =
          new GeneratedModelConverter(getLogger(), getTypeUtils(), getElementUtils(), matches);
      List<Pair<TypeSpec, GeneratedModel>> typeSpecList =
          generatedModelConverter.convert(generatedModels);
      for (Pair<TypeSpec, GeneratedModel> pair : typeSpecList) {
        log(Diagnostic.Kind.NOTE, "Writing " + pair.first.name + " ...");
        JavaFile.builder(pair.second.getPackageName(), pair.first).build().writeTo(getFiler());
      }
    } catch (BaseModelConverter.ConversionException e) {
      log(
          Diagnostic.Kind.ERROR,
          String.format(
              "Failed to convert %1s due to %2s",
              GeneratedModelConverter.class.getSimpleName(), e.getMessage()));
      e.printStackTrace();
    } catch (IOException e) {
      log(
          Diagnostic.Kind.ERROR,
          String.format(
              "Failed to generate output file for %1s",
              GeneratedModelGenerator.class.getSimpleName()));
      e.printStackTrace();
    }
  }
예제 #7
0
 private void processEndpoint(
     Set annotations,
     RoundEnvironment roundEnv,
     HashMap<String, ValidatorModel> validatorModelHashMap) {
   for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(Endpoint.class)) {
     if (annotatedElement.getKind() != ElementKind.CLASS) {
       log(
           Diagnostic.Kind.ERROR,
           String.format(
               "Only Classes can be annotated with @%1s annotation",
               Endpoint.class.getSimpleName()));
     } else {
       TypeElement te = (TypeElement) annotatedElement;
       RequestModelGenerator requestModelGenerator =
           new RequestModelGenerator(te, getTypeUtils(), getElementUtils(), getLogger());
       JsonCodeWriter codeWriter = new JsonCodeWriter(getFiler(), getLogger());
       RequestModelConverter modelConverter =
           new RequestModelConverter(
               getLogger(), getTypeUtils(), getElementUtils(), validatorModelHashMap);
       try {
         requestModelGenerator.initialize();
         RequestModel model = requestModelGenerator.generate();
         String packageName = getElementUtils().getPackageOf(te).toString();
         String responsePackageName =
             getElementUtils().getPackageOf(te).toString()
                 + "."
                 + te.getSimpleName().toString().toLowerCase()
                 + "response";
         String responseClassName = te.getSimpleName() + "ApiResponse";
         model.setPackageName(packageName);
         model.setResponseClassName(responseClassName);
         model.setResponsePackageName(responsePackageName);
         JCodeModel responseCodeModel =
             JsonParser.parseJsonToModel(
                 responsePackageName, responseClassName, model.getExampleJson(), getLogger());
         log(Diagnostic.Kind.NOTE, "Writing JSON Java model to file...");
         responseCodeModel.build(codeWriter);
         log(Diagnostic.Kind.NOTE, "Writing JSON Java model to file done.");
         List<TypeSpec> typeSpecList = modelConverter.convert(model);
         log(Diagnostic.Kind.NOTE, "Writing Endpoint request objects to file...");
         if (!isBaseGenerated) {
           typeSpecList.add(
               RequestModelConverter
                   .getRequestSuperClass()); // must be generated once or Filer will throw
           isBaseGenerated = true;
         }
         for (TypeSpec typeSpec : typeSpecList) {
           log(Diagnostic.Kind.NOTE, "Writing " + typeSpec.name + "...");
           JavaFile.builder(packageName, typeSpec).build().writeTo(getFiler());
         }
         log(Diagnostic.Kind.NOTE, "Writing request objects to file done.");
         log(Diagnostic.Kind.NOTE, "==================Finished ==================");
       } catch (BaseGenerator.InitializationException e) {
         log(
             Diagnostic.Kind.ERROR,
             String.format(
                 "Failed to initialize %1s due to %2s",
                 RequestModelGenerator.class.getSimpleName(), e.getMessage()));
         e.printStackTrace();
       } catch (BaseGenerator.GenerationException e) {
         log(
             Diagnostic.Kind.ERROR,
             String.format(
                 "Code generator %1s failed due to %2s",
                 RequestModelGenerator.class.getSimpleName(), e.getMessage()));
         e.printStackTrace();
       } catch (BaseModelConverter.ConversionException e) {
         log(
             Diagnostic.Kind.ERROR,
             String.format(
                 "Failed to convert %1s due to %2s",
                 RequestModelConverter.class.getSimpleName(), e.getMessage()));
         e.printStackTrace();
       } catch (JsonParser.JsonParserException e) {
         log(Diagnostic.Kind.ERROR, String.format("Failed to parse sample JSON"));
         e.printStackTrace();
       } catch (IOException e) {
         log(
             Diagnostic.Kind.ERROR,
             String.format(
                 "Failed to generate output file for %1s",
                 RequestModelGenerator.class.getSimpleName()));
         e.printStackTrace();
       }
     }
   }
 }