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

org.deephacks.tools4j.config.internal.core.runtime.ClassToSchemaConverter Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.deephacks.tools4j.config.internal.core.runtime;

import org.deephacks.tools4j.config.Config;
import org.deephacks.tools4j.config.Id;
import org.deephacks.tools4j.config.internal.core.runtime.ClassIntrospector.FieldWrap;
import org.deephacks.tools4j.config.model.Schema;
import org.deephacks.tools4j.config.model.Schema.AbstractSchemaProperty;
import org.deephacks.tools4j.config.model.Schema.SchemaId;
import org.deephacks.tools4j.config.spi.Conversion;
import org.deephacks.tools4j.config.spi.Conversion.Converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.deephacks.tools4j.config.model.Events.CFG102_NOT_CONFIGURABLE;
import static org.deephacks.tools4j.config.model.Events.CFG108_ILLEGAL_MODIFIERS;

public final class ClassToSchemaConverter implements Converter, Schema> {
    private Conversion conversion = Conversion.get();

    @Override
    public Schema convert(Class source, Class specificType) {
        ClassIntrospector introspector = new ClassIntrospector(source);
        Config config = introspector.getAnnotation(Config.class);
        if (config == null) {
            throw CFG102_NOT_CONFIGURABLE(source);
        }
        SchemaId schemaId = getId(introspector);
        if (schemaId == null) {
            // get instance does not have @Id annotations so we create
            // it from the @Config annotation
            schemaId = SchemaId.create(config.name(), config.desc(), true);
        }
        String schemaName = config.name();
        if (schemaName == null || "".equals(schemaName)) {
            schemaName = source.getName();
        }
        Schema schema = Schema.create(schemaId, introspector.getName(), schemaName, config.desc());
        Collection fields = new ArrayList<>();
        fields.addAll(introspector.getFieldList());
        Collection props = conversion.convert(fields,
                AbstractSchemaProperty.class);
        for (AbstractSchemaProperty abstractProp : props) {
            schema.add(abstractProp);
        }
        return schema;
    }

    private SchemaId getId(ClassIntrospector introspector) {
        List ids = introspector.getFieldList(Id.class);
        boolean isSingleton = false;
        if (ids == null || ids.size() == 0) {
            return null;
        } else {
            FieldWrap id = ids.get(0);
            Id anno = (Id) id.getAnnotation().get();
            if ((id.isStatic() && !id.isFinal()) || (id.isFinal() && !id.isStatic())) {
                throw CFG108_ILLEGAL_MODIFIERS(anno.name());
            }
            String name = anno.name();
            if (name == null || "".equals(name)) {
                name = id.getFieldName();
            }
            return SchemaId.create(name, anno.desc(), isSingleton);
        }
    }

    public static final class ConfigClass {
        public String name;
        public String desc;
    }
}