All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.araguacaima.commons.utils.jsonschema.RuleFactory Maven / Gradle / Ivy

Go to download

Common utilities is a set of java utilities for managing typical actions when working with enums, files, exceptions, zip/jar files, classes (via Reflection), maps, numbers and so on. Most of the utilities extend functionalities offered by amazing libraries such as: * commons-beanutils (apache) * commons-lang3 (apache) * commons-io (apache) * commons-math3 (apache) * commons-collections4 (apache) * jreversepro (akkumar)

There is a newer version: 1.5.17
Show newest version
package com.araguacaima.commons.utils.jsonschema;

import com.araguacaima.commons.utils.ReflectionUtils;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JPackage;
import com.sun.codemodel.JType;
import org.jsonschema2pojo.Annotator;
import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.SchemaStore;
import org.jsonschema2pojo.rules.Rule;
import org.jsonschema2pojo.util.ParcelableHelper;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class RuleFactory extends org.jsonschema2pojo.rules.RuleFactory {

    private static final ReflectionUtils reflectionUtils = ReflectionUtils.getInstance();
    private final String definitionsRoot;
    private final Map definitions;
    private final Map generatedTypes = new HashMap<>();

    public RuleFactory(GenerationConfig generationConfig, Annotator annotator, SchemaStore schemaStore, String definitionsRoot, Map definitions) throws NoSuchFieldException, IllegalAccessException {
        super(generationConfig, annotator, schemaStore);
        this.definitions = definitions;
        this.definitionsRoot = definitionsRoot;
        Field field = org.jsonschema2pojo.rules.RuleFactory.class.getDeclaredField("nameHelper");
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        int modifiers = modifiersField.getInt(field);

        // blank out the final bit in the modifiers int
        modifiers &= ~Modifier.FINAL;
        modifiersField.setInt(field, modifiers);
        field.set(this, new NameHelper(generationConfig, true));
    }

    /**
     * Provides a rule instance that should be applied when a property
     * declaration (child of the "properties" declaration) is found in the
     * schema.
     *
     * @return a schema rule that can handle a property declaration.
     */
    @Override
    public Rule getPropertyRule() {
        return new PropertyRule(this, definitionsRoot, definitions);
    }

    /**
     * Provides a rule instance that should be applied when an "array"
     * declaration is found in the schema.
     *
     * @return a schema rule that can handle the "array" declaration.
     */
    @Override
    public Rule getArrayRule() {
        return new ArrayRule(this, definitionsRoot, definitions);
    }

    /**
     * Provides a rule instance that should be applied when an "object"
     * declaration is found in the schema.
     *
     * @return a schema rule that can handle the "object" declaration.
     */
    @Override
    public Rule getObjectRule() {
        return new ObjectRule(this, new ParcelableHelper(), getReflectionHelper());
    }

    public Map getGeneratedTypes() {
        return generatedTypes;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy