org.deephacks.tools4j.config.internal.core.runtime.ClassToSchemaConverter Maven / Gradle / Ivy
/**
* 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