Package org.internna.iwebmvc.utils

Source Code of org.internna.iwebmvc.utils.ClassUtils

/*
* Copyright 2009-2010 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.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import javassist.util.proxy.ProxyObject;
import net.sf.cglib.proxy.Enhancer;

import org.internna.iwebmvc.IWebMvcException;
import org.internna.iwebmvc.model.DomainEntity;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Utility methods 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() {
        throw new AssertionError("Do not try to instantiate utility class");
    }

    /**
     * Tries to obtain an instance of a class from the class name.
     *
     * @param className any
     * @return any
     */
    public static final Object instantiateClass(final String className) throws ClassNotFoundException {
        return StringUtils.hasText(className) ? BeanUtils.instantiateClass(forName(className)) : null;
    }

    /**
     * Tries to invoke the default constructor of a class.
     *
     * @param <T> the resulting object type
     * @param clazz any
     * @return any
     */
    @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;
    }

    /**
     * Obtains all annotated fields in a class or superclass.
     *
     * @param clazz a non null instance
     * @param annotation a non null instance
     * @return
     */
    public final static List<Field> getAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotation) {
        Collection<Field> all = getFields(clazz);
        List<Field> annotated = new ArrayList<Field>();
        for (Field field : all)
            if (field.isAnnotationPresent(annotation))
               annotated.add(field);
        all.clear();
        return annotated;
    }

    /**
     * Gets a field from a class or superclass.
     *
     * @param clazz a non null instance
     * @param name a non null instance
     * @return null unless the field actually exists
     */
    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 (no proxies or CGLIB) class of the object.
     *
     * @param obj any
     * @return any
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> getActualClass(T obj) {
      Class<?> clazz = null;
      if (obj != null) {
            clazz = Class.class.isInstance(obj) ? (Class<?>) obj : obj.getClass();
            if (ProxyObject.class.isInstance(obj)) clazz = clazz.getSuperclass();
            else if (Advised.class.isInstance(obj)) clazz = ((Advised) obj).getTargetClass();
                clazz = getActualClass(clazz);
      }
      return (Class<T>) clazz;
    }

  protected static Class<?> getActualClass(Class<?> clazz) {
        Class<?> actual = clazz;
        if (Enhancer.isEnhanced(clazz)) actual = clazz.getSuperclass();
        return actual;
    }

    /**
     * TRIES to obtain the generic declaration of a type. Usually this can only be done
     * when the field is declared like: List<String> field;
     *
     * @param field any
     * @return any
     */
    public static Class<?> getGenericType(Field field) {
        Class<?> clazz = null;
        if (field != null) {
            Type t = field.getGenericType();
            if (t instanceof ParameterizedType) {
                ParameterizedType type = (ParameterizedType) t;
                if ((type != null) && (type.getActualTypeArguments() != null) && (type.getActualTypeArguments().length > 0)) clazz = (Class<?>) type.getActualTypeArguments()[0];
            } else if (t instanceof Class) clazz = (Class<?>) t;
        }
        return clazz;
    }

    /**
     * Tries to obtain a method from a class or superclass and makes it accessible.
     *
     * @param clazz a non null instance
     * @param name a non empty string
     * @param paramTypes any
     * @return any
     */
    public static Method getMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
        try {
            Method m = clazz.getDeclaredMethod(name, paramTypes);
            m.setAccessible(true);
            return m;
        } catch (NoSuchMethodException ex) {
            Class<?> superClass = clazz.getSuperclass();
            return superClass != null ? getMethod(superClass, name, paramTypes) : null;
        }
    }

    /**
     * Invokes a method. The first argument is the method name. The first vararg must be the target object (not null).
     *
     * @param method a non empty instance
     * @param args a non null non empty instance
     * @return the return of the method invoked
     */
    public static Object call(String method, Object... args) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Assert.hasText(method);
        Assert.notEmpty(args);
        Class<?>[] classes = new Class<?>[args.length - 1];
        for (int index = 0; index < classes.length; index++)
            classes[index] = args[index + 1].getClass();
        Method m = getMethod(args[0].getClass(), method, classes);
        return m.invoke(args[0], Arrays.copyOfRange(args, 1, args.length));
    }

    /**
     * Gets the enum given an ordinal that matches a (nested) path of a class.
     *
     * @param clazz a non null instance
     * @param path a non empty string
     * @param position a positive (>= 0) integer
     * @return the
     */
    public static Object getEnumValue(Class<? extends DomainEntity> clazz, String path, int position) {
      Assert.hasText(path);
      Assert.notNull(clazz);
      Assert.isTrue(position >= 0);
      try {
            Class<?> type = clazz;
            String[] parts = path.split("\\.");
            for (int index = 0; index < parts.length - 1; index++)
                type = new BeanWrapperImpl(type.newInstance()).getPropertyType(parts[index]);
            Field field = getField(type, parts[parts.length - 1]);
            Class<?> enumType = field.getType();
            return enumType.isEnum() && (position < enumType.getEnumConstants().length) ? enumType.getEnumConstants()[position] : null;
      } catch (Exception ex) {
            throw new IWebMvcException("Could not get enum value from [" + clazz.getName() + "] with position [" + position + "]", ex);
      }
    }

}
TOP

Related Classes of org.internna.iwebmvc.utils.ClassUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.