Search in sources :

Example 1 with GeneratorResult

use of io.fabric8.java.generator.nodes.GeneratorResult 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 GeneratorResult

use of io.fabric8.java.generator.nodes.GeneratorResult in project kubernetes-client by fabric8io.

the class GeneratorTest method testObjectOfObjectsWithAlwaysPrefix.

@Test
void testObjectOfObjectsWithAlwaysPrefix() {
    // Arrange
    Config config = new Config(null, Config.Prefix.ALWAYS, null, null, null, null);
    Map<String, JSONSchemaProps> props = new HashMap<>();
    JSONSchemaProps newObj = new JSONSchemaProps();
    newObj.setType("object");
    props.put("o1", newObj);
    JObject obj = new JObject(null, "t", props, null, false, "My", "", config, null);
    // Act
    GeneratorResult res = obj.generateJava();
    // Assert
    assertEquals(2, res.getTopLevelClasses().size());
    assertEquals("MyO1", res.getTopLevelClasses().get(0).getName());
    assertEquals("MyT", res.getTopLevelClasses().get(1).getName());
}
Also used : JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) Test(org.junit.jupiter.api.Test)

Example 3 with GeneratorResult

use of io.fabric8.java.generator.nodes.GeneratorResult in project kubernetes-client by fabric8io.

the class GeneratorTest method testObjectOfPrimitives.

@Test
void testObjectOfPrimitives() {
    // Arrange
    Map<String, JSONSchemaProps> props = new HashMap<>();
    JSONSchemaProps newBool = new JSONSchemaProps();
    newBool.setType("boolean");
    props.put("o1", newBool);
    JObject obj = new JObject("v1alpha1", "t", props, null, false, "", "", defaultConfig, null);
    // Act
    GeneratorResult res = obj.generateJava();
    // Assert
    assertEquals("v1alpha1.T", obj.getType());
    assertEquals(1, res.getTopLevelClasses().size());
    assertEquals("T", res.getTopLevelClasses().get(0).getName());
    Optional<ClassOrInterfaceDeclaration> clz = res.getTopLevelClasses().get(0).getCompilationUnit().getClassByName("T");
    assertTrue(clz.isPresent());
    assertEquals(1, clz.get().getFields().size());
    assertTrue(clz.get().getFieldByName("o1").isPresent());
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) Test(org.junit.jupiter.api.Test)

Example 4 with GeneratorResult

use of io.fabric8.java.generator.nodes.GeneratorResult in project kubernetes-client by fabric8io.

the class GeneratorTest method testDefaultEnum.

@Test
void testDefaultEnum() {
    // Arrange
    Map<String, JSONSchemaProps> props = new HashMap<>();
    JSONSchemaProps newEnum = new JSONSchemaProps();
    newEnum.setType("string");
    List<JsonNode> enumValues = new ArrayList<>();
    enumValues.add(new TextNode("foo"));
    enumValues.add(new TextNode("bar"));
    enumValues.add(new TextNode("baz"));
    props.put("e1", newEnum);
    JEnum enu = new JEnum("t", enumValues, defaultConfig, null);
    // Act
    GeneratorResult res = enu.generateJava();
    // Assert
    assertEquals("T", enu.getType());
    assertEquals(1, res.getInnerClasses().size());
    assertEquals("T", res.getInnerClasses().get(0).getName());
    Optional<EnumDeclaration> en = res.getInnerClasses().get(0).getCompilationUnit().getEnumByName("T");
    assertTrue(en.isPresent());
    assertEquals(3, en.get().getEntries().size());
    assertEquals("FOO", en.get().getEntries().get(0).getName().asString());
    assertEquals("BAR", en.get().getEntries().get(1).getName().asString());
    assertEquals("BAZ", en.get().getEntries().get(2).getName().asString());
}
Also used : JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) Test(org.junit.jupiter.api.Test)

Example 5 with GeneratorResult

use of io.fabric8.java.generator.nodes.GeneratorResult in project kubernetes-client by fabric8io.

the class GeneratorTest method testClassNamesDisambiguationWithPackageNesting.

@Test
void testClassNamesDisambiguationWithPackageNesting() {
    // Arrange
    Map<String, JSONSchemaProps> props = new HashMap<>();
    JSONSchemaProps newObj1 = new JSONSchemaProps();
    JSONSchemaProps newObj2 = new JSONSchemaProps();
    newObj1.setType("object");
    newObj2.setType("object");
    Map<String, JSONSchemaProps> obj2Props = new HashMap<>();
    obj2Props.put("o1", newObj1);
    obj2Props.put("o2", newObj1);
    obj2Props.put("o3", newObj1);
    newObj2.setProperties(obj2Props);
    props.put("o1", newObj1);
    props.put("o2", newObj2);
    JObject obj = new JObject("v1alpha1", "t", props, null, false, "", "", defaultConfig, null);
    // Act
    GeneratorResult res = obj.generateJava();
    // Assert
    assertEquals(6, res.getTopLevelClasses().size());
    // The order here is not important
    assertEquals("O1", res.getTopLevelClasses().get(0).getName());
    assertEquals("O1", res.getTopLevelClasses().get(1).getName());
    assertEquals("O2", res.getTopLevelClasses().get(2).getName());
    assertEquals("O3", res.getTopLevelClasses().get(3).getName());
    assertEquals("O2", res.getTopLevelClasses().get(4).getName());
    assertEquals("T", res.getTopLevelClasses().get(5).getName());
    CompilationUnit cuT = res.getTopLevelClasses().get(5).getCompilationUnit();
    Optional<ClassOrInterfaceDeclaration> clzT = cuT.getClassByName("T");
    assertTrue(clzT.isPresent());
    assertEquals(2, clzT.get().getFields().size());
    assertTrue(clzT.get().getFieldByName("o1").isPresent());
    assertEquals("v1alpha1", cuT.getPackageDeclaration().get().getNameAsString());
    assertEquals("v1alpha1.t.O1", clzT.get().getFieldByName("o1").get().getElementType().toString());
    CompilationUnit cuO1 = res.getTopLevelClasses().get(0).getCompilationUnit();
    Optional<ClassOrInterfaceDeclaration> clzO1 = cuO1.getClassByName("O1");
    assertTrue(clzO1.isPresent());
    assertEquals("v1alpha1.t", cuO1.getPackageDeclaration().get().getNameAsString());
    CompilationUnit cuO2 = res.getTopLevelClasses().get(4).getCompilationUnit();
    Optional<ClassOrInterfaceDeclaration> clzO2 = cuO2.getClassByName("O2");
    assertTrue(clzO2.isPresent());
    assertTrue(clzO2.get().getFieldByName("o1").isPresent());
    assertEquals("v1alpha1.t", cuO2.getPackageDeclaration().get().getNameAsString());
    assertEquals("v1alpha1.t.o2.O1", clzO2.get().getFieldByName("o1").get().getElementType().toString());
    assertTrue(clzO2.get().getFieldByName("o2").isPresent());
    assertTrue(clzO2.get().getFieldByName("o3").isPresent());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) Test(org.junit.jupiter.api.Test)

Aggregations

JSONSchemaProps (io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps)8 Test (org.junit.jupiter.api.Test)8 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 TextNode (com.fasterxml.jackson.databind.node.TextNode)2 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 JavaGeneratorException (io.fabric8.java.generator.exceptions.JavaGeneratorException)1