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(); } }
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(); } } } }