Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.Id;
import org.deephacks.tools4j.config.internal.core.Reflections;
import org.deephacks.tools4j.config.model.Bean;
import org.deephacks.tools4j.config.model.Bean.BeanId;
import org.deephacks.tools4j.config.model.Schema;
import org.deephacks.tools4j.config.model.Schema.SchemaProperty;
import org.deephacks.tools4j.config.model.Schema.SchemaPropertyList;
import org.deephacks.tools4j.config.model.Schema.SchemaPropertyRef;
import org.deephacks.tools4j.config.model.Schema.SchemaPropertyRefList;
import org.deephacks.tools4j.config.model.Schema.SchemaPropertyRefMap;
import org.deephacks.tools4j.config.spi.Conversion;
import org.deephacks.tools4j.config.spi.Conversion.Converter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public final class BeanToObjectConverter implements Converter {
private Conversion conversion = Conversion.get();
private ConcurrentHashMap> classCache = new ConcurrentHashMap>();
@Override
public Object convert(Bean source, Class specificType) {
final Object instance;
try {
instance = newInstance(specificType);
} catch (Exception e) {
throw new UnsupportedOperationException(e);
}
Map valuesToInject = new HashMap<>();
Map instanceCache = new HashMap<>();
return convert(source, instance, valuesToInject, instanceCache);
}
private Object convert(Bean source, Object instance, Map valuesToInject,
Map instanceCache) {
instanceCache.put(source.getId(), instance);
convertProperty(source, valuesToInject);
convertPropertyList(source, valuesToInject);
convertPropertyRef(source, valuesToInject, instanceCache);
convertPropertyRefList(source, valuesToInject, instanceCache);
convertPropertyRefMap(source, valuesToInject, instanceCache);
Schema schema = source.getSchema();
if (!schema.getId().isSingleton()) {
// do not try to inject get id: the field is static final
valuesToInject.put(getIdField(instance.getClass()), source.getId().getInstanceId());
}
inject(instance, valuesToInject);
return instance;
}
private void inject(Object instance, Map values) {
List fields = findFields(instance.getClass());
for (Field field : fields) {
field.setAccessible(true);
Object value = values.get(field.getName());
if (value == null) {
continue;
}
try {
field.set(instance, value);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new UnsupportedOperationException(e);
}
}
}
private void convertPropertyRefMap(Bean source, Map values,
Map instanceCache) {
for (SchemaPropertyRefMap prop : source.getSchema().get(SchemaPropertyRefMap.class)) {
List beans = source.getReference(prop.getName());
if (beans == null) {
continue;
}
Map