Search in sources :

Example 1 with JavaGeneratorException

use of io.fabric8.java.generator.exceptions.JavaGeneratorException in project kubernetes-client by fabric8io.

the class JObject method generateJava.

@Override
public GeneratorResult generateJava() {
    CompilationUnit cu = new CompilationUnit();
    if (!this.pkg.isEmpty()) {
        cu.setPackageDeclaration(this.pkg);
    }
    ClassOrInterfaceDeclaration clz = cu.addClass(this.className);
    clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonInclude"), new NameExpr("com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL")));
    clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonPropertyOrder"), new NameExpr(getSortedFieldsAsParam(this.fields.keySet()))));
    clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.databind.annotation.JsonDeserialize"), new NameExpr("using = com.fasterxml.jackson.databind.JsonDeserializer.None.class")));
    if (config.isObjectExtraAnnotations()) {
        addExtraAnnotations(clz);
    }
    clz.addImplementedType("io.fabric8.kubernetes.api.model.KubernetesResource");
    List<GeneratorResult.ClassResult> buffer = new ArrayList<>(this.fields.size() + 1);
    List<String> sortedKeys = this.fields.keySet().stream().sorted().collect(Collectors.toList());
    for (String k : sortedKeys) {
        AbstractJSONSchema2Pojo prop = this.fields.get(k);
        boolean isRequired = this.required.contains(k);
        GeneratorResult gr = prop.generateJava();
        // For now the inner types are only for enums
        if (!gr.getInnerClasses().isEmpty()) {
            for (GeneratorResult.ClassResult enumCR : gr.getInnerClasses()) {
                Optional<EnumDeclaration> ed = enumCR.getCompilationUnit().getEnumByName(enumCR.getName());
                if (ed.isPresent()) {
                    clz.addMember(ed.get());
                }
            }
        }
        buffer.addAll(gr.getTopLevelClasses());
        String originalFieldName = k;
        String fieldName = AbstractJSONSchema2Pojo.sanitizeString(k);
        String fieldType = prop.getType();
        try {
            FieldDeclaration objField = clz.addField(fieldType, fieldName, Modifier.Keyword.PRIVATE);
            objField.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonProperty"), new StringLiteralExpr(originalFieldName)));
            if (isRequired) {
                objField.addAnnotation("javax.validation.constraints.NotNull");
            }
            objField.createGetter();
            objField.createSetter();
            if (Utils.isNotNullOrEmpty(prop.getDescription())) {
                objField.setJavadocComment(prop.getDescription());
                objField.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonPropertyDescription"), new StringLiteralExpr(prop.getDescription().replace("\"", "\\\""))));
            }
        } catch (Exception cause) {
            throw new JavaGeneratorException("Error generating field " + fieldName + " with type " + prop.getType(), cause);
        }
    }
    if (this.preserveUnknownFields || config.isAlwaysPreserveUnknownFields()) {
        ClassOrInterfaceType mapType = new ClassOrInterfaceType().setName(Keywords.JAVA_UTIL_MAP).setTypeArguments(new ClassOrInterfaceType().setName("String"), new ClassOrInterfaceType().setName("Object"));
        FieldDeclaration objField = clz.addField(mapType, Keywords.ADDITIONAL_PROPERTIES, Modifier.Keyword.PRIVATE);
        objField.setVariables(new NodeList<>(new VariableDeclarator().setName(Keywords.ADDITIONAL_PROPERTIES).setType(mapType).setInitializer("new java.util.HashMap<>()")));
        objField.addAnnotation("com.fasterxml.jackson.annotation.JsonIgnore");
        objField.createGetter().addAnnotation("com.fasterxml.jackson.annotation.JsonAnyGetter");
        objField.createSetter().addAnnotation("com.fasterxml.jackson.annotation.JsonAnySetter");
    }
    buffer.add(new GeneratorResult.ClassResult(this.className, cu));
    return new GeneratorResult(buffer);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException)

Example 2 with JavaGeneratorException

use of io.fabric8.java.generator.exceptions.JavaGeneratorException in project kubernetes-client by fabric8io.

the class WritableCRCompilationUnit method writeAllJavaClasses.

public void writeAllJavaClasses(File basePath, String basePackage) {
    try {
        createFolders(basePackage, basePath);
        for (GeneratorResult.ClassResult cr : this.classResults) {
            String pkg = cr.getCompilationUnit().getPackageDeclaration().map(NodeWithName::getNameAsString).orElse(null);
            File path = createFolders(pkg, basePath);
            writeToFile(path.toPath().resolve(cr.getName() + ".java").toFile(), cr.getCompilationUnit().toString());
        }
    } catch (Exception e) {
        throw new JavaGeneratorException(e);
    }
}
Also used : GeneratorResult(io.fabric8.java.generator.nodes.GeneratorResult) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException) File(java.io.File) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException) IOException(java.io.IOException)

Example 3 with JavaGeneratorException

use of io.fabric8.java.generator.exceptions.JavaGeneratorException in project kubernetes-client by fabric8io.

the class CRGeneratorRunner method runOnSingleSource.

private void runOnSingleSource(File source, File basePath) {
    try (FileInputStream fis = new FileInputStream(source)) {
        List<HasMetadata> resources = new ArrayList<>();
        Object deserialized = Serialization.unmarshal(fis, Collections.emptyMap());
        if (deserialized instanceof List) {
            resources.addAll((List<HasMetadata>) deserialized);
        } else {
            resources.add((CustomResourceDefinition) deserialized);
        }
        resources.parallelStream().forEach(resource -> {
            if (resource.getKind().toLowerCase(Locale.ROOT).equals("customresourcedefinition")) {
                CustomResourceDefinition crd = (CustomResourceDefinition) resource;
                String pkg = getPackage(crd.getSpec().getGroup());
                List<WritableCRCompilationUnit> writables = generate(crd, pkg);
                writables.parallelStream().forEach(w -> w.writeAllJavaClasses(basePath, pkg));
            } else {
                LOGGER.warn("Not generating nothing for resource of kind: " + resource.getKind());
            }
        });
    } catch (FileNotFoundException e) {
        throw new JavaGeneratorException("File " + source.getAbsolutePath() + " not found", e);
    } catch (IOException e) {
        throw new JavaGeneratorException("Exception reading " + source.getAbsolutePath(), e);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JCRObject(io.fabric8.java.generator.nodes.JCRObject) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException)

Aggregations

JavaGeneratorException (io.fabric8.java.generator.exceptions.JavaGeneratorException)3 IOException (java.io.IOException)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 GeneratorResult (io.fabric8.java.generator.nodes.GeneratorResult)1 JCRObject (io.fabric8.java.generator.nodes.JCRObject)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1