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.
/*
* Copyright 2015-2019 Futeh Kao
*
* 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 net.e6tech.elements.common.reflection;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import net.e6tech.elements.common.logging.Logger;
import net.e6tech.elements.common.util.SystemException;
import java.io.IOException;
import java.lang.reflect.*;
import java.util.Collection;
import java.util.Iterator;
/**
* Created by futeh.
*/
@SuppressWarnings("unchecked")
public class ObjectConverter {
public static final ObjectMapper mapper;
static {
mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
public static Class loadClass(ClassLoader loader, String name) throws ClassNotFoundException {
if (Primitives.isPrimitive(name))
return Primitives.get(name);
return loader.loadClass(name);
}
public Object convert(Object from, Method getterOrSetter, InstanceCreationListener listener) throws IOException {
Type returnType = getterOrSetter.getGenericReturnType();
if (!returnType.equals(Void.TYPE)) {
// getter
if (getterOrSetter.getParameterTypes().length != 0) {
throw new IllegalArgumentException("Method " + getterOrSetter.getName() + " must be a getter");
}
return convert(from, returnType, listener);
} else {
Type[] parameters = getterOrSetter.getGenericParameterTypes();
if (parameters.length != 1)
throw new IllegalArgumentException("Method " + getterOrSetter.getName() + " must be a setter");
Type argumentType = parameters[0];
return convert(from, argumentType, listener);
}
}
public Object convert(Object from, Field field, InstanceCreationListener listener) throws IOException {
return convert(from, field.getGenericType(), listener);
}
@SuppressWarnings({"squid:S134", "squid:S3776"})
public Object convert(Object from, Type toType, InstanceCreationListener listener) throws IOException {
Object converted;
if (toType instanceof Class) {
converted = convert(from, (Class) toType, listener);
} else {
ParameterizedType parametrized = (ParameterizedType) toType;
Class enclosedType = (Class) parametrized.getRawType();
Type type = parametrized.getActualTypeArguments()[0];
if (type instanceof Class) {
// for now, we limit ourselves to detecting one level. A counter example would be
// List>> or List