/*
* Copyright 2002-2007 the original author or authors.
*
* 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.internna.iwebmvc.utils;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import net.sf.cglib.proxy.Enhancer;
import org.springframework.beans.BeanUtils;
/**
* Utility mehods for working with classes.
*
* @author Jose Noheda
* @since 1.0
*/
public final class ClassUtils extends org.springframework.util.ClassUtils {
/**
* Default private constructor for a Utility class.
*/
private ClassUtils() { }
public static final Object instantiateClass(final String className) throws ClassNotFoundException {
return StringUtils.hasText(className) ? BeanUtils.instantiateClass(forName(className)) : null;
}
@SuppressWarnings("unchecked")
public static final <T> T instantiateClass(Class<T> clazz) {
return clazz != null ? (T) BeanUtils.instantiateClass(clazz) : null;
}
/**
* Returns all the fields (public & private) declared in the whole class
* hierachy of a given class.
*
* @param clazz any class
* @return
*/
public final static Collection<Field> getFields(Class<?> clazz) {
Assert.notNull(clazz);
Class<?> currentClass = clazz;
Collection<Field> fields = new ArrayList<Field>();
do {
fields.addAll(Arrays.asList(currentClass.getDeclaredFields()));
currentClass = currentClass.getSuperclass();
} while (currentClass != null);
return fields;
}
public final static Field getField(Class<?> clazz, String name) {
Assert.notNull(clazz);
Assert.notNull(name);
Class<?> currentClass = clazz;
Field field = null;
while ((field == null) & (currentClass != null)) {
try {
field = currentClass.getDeclaredField(name);
} catch (Exception e) {
// Just omit
}
currentClass = currentClass.getSuperclass();
}
return field;
}
/**
* Returns the real (non CGLIB) class of the object
* @param obj
* @return
*/
public static Class<?> getActualClass(Object obj) {
return obj != null ? getActualClass(obj.getClass()) : null;
}
public static Class<?> getActualClass(Class<?> clazz) {
Class<?> actual = clazz;
if (Enhancer.isEnhanced(clazz)) actual = clazz = clazz.getSuperclass();
return actual;
}
public static Class<?> getGenericType(Field field) {
Class<?> clazz = null;
if (field != null) {
ParameterizedType type = (ParameterizedType) field.getGenericType();
if ((type != null) && (type.getActualTypeArguments() != null) && (type.getActualTypeArguments().length > 0)) clazz = (Class<?>) type.getActualTypeArguments()[0];
}
return clazz;
}
}