Example usage for org.springframework.util ReflectionUtils handleReflectionException

List of usage examples for org.springframework.util ReflectionUtils handleReflectionException

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils handleReflectionException.

Prototype

public static void handleReflectionException(Exception ex) 

Source Link

Document

Handle the given reflection exception.

Usage

From source file:egovframework.rte.bat.core.reflection.EgovReflectionSupport.java

/**
 * sqlType?  ?   //  w  w  w  .j a  va2 s  .  c om
 */
public String[] getSqlTypeArray(String[] params, Object item) {

    String[] sqlTypes = new String[params.length];

    for (int i = 0; i < params.length; i++) {

        try {
            sqlTypes[i] = item.getClass().getDeclaredField(params[i]).getType().getSimpleName().toString();

        } catch (SecurityException e) {
            ReflectionUtils.handleReflectionException(e);
        } catch (NoSuchFieldException e) {
            ReflectionUtils.handleReflectionException(e);
        }
    }
    return sqlTypes;

}

From source file:com.googlecode.ehcache.annotations.key.AbstractDeepCacheKeyGenerator.java

/**
 * Calls {@link #shouldReflect(Object)} to determine if the object needs to be reflected on to
 * generate a good key. If so {@link AccessibleObject#setAccessible(AccessibleObject[], boolean)} is
 * used to enable access to private, protected and default fields. Each non-transient, non-static field
 * has {@link #deepHashCode(Object, Object)} called on it.
 *//*from   w w  w . ja  v a 2  s .c o m*/
protected final void reflectionDeepHashCode(G generator, final Object element) {
    //Special objects which shouldn't be reflected on due to lack of interesting fields
    if (element instanceof Class<?>) {
        this.append(generator, element);
        return;
    }

    //Determine if the element should be reflected on
    if (!this.shouldReflect(element)) {
        this.append(generator, element);
        return;
    }

    //Accumulate the data that makes up the object being reflected on so it can be recursed on as a single grouping of data
    final List<Object> reflectiveObject = new LinkedList<Object>();

    //Write out the target class so that two classes with the same fields can't collide
    reflectiveObject.add(element.getClass());

    try {
        for (Class<?> targetClass = element.getClass(); targetClass != null; targetClass = targetClass
                .getSuperclass()) {
            final Field[] fields = targetClass.getDeclaredFields();
            AccessibleObject.setAccessible(fields, true);

            for (int i = 0; i < fields.length; i++) {
                final Field field = fields[i];
                final int modifiers = field.getModifiers();

                //Ignore static and transient fields
                if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) {
                    final Object fieldValue = field.get(element);
                    reflectiveObject.add(fieldValue);
                }
            }
        }
    } catch (IllegalAccessException exception) {
        ReflectionUtils.handleReflectionException(exception);
    }

    this.deepHashCode(generator, reflectiveObject);
}

From source file:com.ett.common.util.ReflectUtil.java

/**
 * @param object//from   ww w .j a  va2s .c  om
 * @param methodName
 * @param params
 * @return
 * @throws NoSuchMethodException
 */
public static Object invokePrivateMethod(Object object, String methodName, Object... params)
        throws NoSuchMethodException {
    Assert.notNull(object);
    Assert.hasText(methodName);
    Class[] types = new Class[params.length];
    for (int i = 0; i < params.length; i++) {
        types[i] = params[i].getClass();
    }

    Class clazz = object.getClass();
    Method method = null;
    for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
        try {
            method = superClass.getDeclaredMethod(methodName, types);
            break;
        } catch (NoSuchMethodException e) {
            // ??17,?
        }
    }

    if (method == null)
        throw new NoSuchMethodException("No Such Method:" + clazz.getSimpleName() + methodName);

    boolean accessible = method.isAccessible();
    method.setAccessible(true);
    Object result = null;
    try {
        result = method.invoke(object, params);
    } catch (Exception e) {
        ReflectionUtils.handleReflectionException(e);
    }
    method.setAccessible(accessible);
    return result;
}

From source file:com.austin.base.commons.util.ReflectUtil.java

/**
 * @param object/*  w ww  .j  a va  2 s.c om*/
 * @param methodName
 * @param params
 * @return
 * @throws NoSuchMethodException
 */
public static Object invokePrivateMethod(Object object, String methodName, Object... params)
        throws NoSuchMethodException {
    Assert.notNull(object);
    Assert.hasText(methodName);
    Class[] types = new Class[params.length];
    for (int i = 0; i < params.length; i++) {
        types[i] = params[i].getClass();
    }

    Class clazz = object.getClass();
    Method method = null;
    for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
        try {
            method = superClass.getDeclaredMethod(methodName, types);
            break;
        } catch (NoSuchMethodException e) {
            // ??17,?
        }
    }

    if (method == null)
        throw new NoSuchMethodException("No Such Method:" + clazz.getSimpleName() + methodName);

    boolean accessible = method.isAccessible();
    method.setAccessible(true);
    Object result = null;
    try {
        result = method.invoke(object, params);
    } catch (Exception e) {
        ReflectionUtils.handleReflectionException(e);
    }
    method.setAccessible(accessible);
    return result;
}

From source file:com.siberhus.tdfl.mapping.BeanWrapLineMapper.java

private Object getPropertyValue(Object bean, String nestedName) {
    BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);
    Object nestedValue = wrapper.getPropertyValue(nestedName);
    if (nestedValue == null) {
        try {//  w  w w .  j  a va  2  s.c o  m
            nestedValue = wrapper.getPropertyType(nestedName).newInstance();
            wrapper.setPropertyValue(nestedName, nestedValue);
        } catch (InstantiationException e) {
            ReflectionUtils.handleReflectionException(e);
        } catch (IllegalAccessException e) {
            ReflectionUtils.handleReflectionException(e);
        }
    }
    return nestedValue;
}

From source file:grails.plugin.springsecurity.web.access.intercept.AnnotationFilterInvocationDefinition.java

protected Closure<?> newInstance(final Class<?> closureClass) {
    try {/* w w w.  jav a  2  s  .c  o  m*/
        Constructor<?> constructor = closureClass.getConstructor(Object.class, Object.class);
        ReflectionUtils.makeAccessible(constructor);
        return (Closure<?>) constructor.newInstance(this, this);
    } catch (NoSuchMethodException e) {
        ReflectionUtils.handleReflectionException(e);
    } catch (InstantiationException e) {
        ReflectionUtils.handleReflectionException(e);
    } catch (IllegalAccessException e) {
        ReflectionUtils.handleReflectionException(e);
    } catch (InvocationTargetException e) {
        ReflectionUtils.handleInvocationTargetException(e);
    }
    return null;
}

From source file:com.hihsoft.baseclass.web.controller.BaseController.java

public static <T> T getOrCreateRequestAttribute(HttpServletRequest request, String key, Class<T> clazz) {
    Object value = request.getAttribute(key);
    if (value == null) {
        try {/* w w  w  .jav a2s .  c om*/
            value = clazz.newInstance();
        } catch (Exception e) {
            ReflectionUtils.handleReflectionException(e);
        }
        request.setAttribute(key, value);
    }
    return clazz.cast(value);
}

From source file:com.wwinsoft.modules.utils.PropertiesUtils.java

public static Map toParameterMap(Object parameter) {
    if (parameter instanceof Map) {
        return (Map) parameter;
    } else {/*from w  w w . j  av a  2  s. com*/
        try {
            return PropertyUtils.describe(parameter);
        } catch (Exception e) {
            ReflectionUtils.handleReflectionException(e);
            return new HashMap();
        }
    }
}

From source file:dstrelec.nats.annotation.NatsListenerAnnotationBeanPostProcessor.java

private Method checkProxy(Method methodArg, Object bean) {
    Method method = methodArg;/*  w  ww.j a v  a  2  s.c o m*/
    if (AopUtils.isJdkDynamicProxy(bean)) {
        try {
            // Found a @NatsListener method on the target class for this JDK proxy ->
            // is it also present on the proxy itself?
            method = bean.getClass().getMethod(method.getName(), method.getParameterTypes());
            Class<?>[] proxiedInterfaces = ((Advised) bean).getProxiedInterfaces();
            for (Class<?> iface : proxiedInterfaces) {
                try {
                    method = iface.getMethod(method.getName(), method.getParameterTypes());
                    break;
                } catch (NoSuchMethodException noMethod) {
                }
            }
        } catch (SecurityException ex) {
            ReflectionUtils.handleReflectionException(ex);
        } catch (NoSuchMethodException ex) {
            throw new IllegalStateException(String.format(
                    "@NatsListener method '%s' found on bean target class '%s', "
                            + "but not found in any interface(s) for bean JDK proxy. Either "
                            + "pull the method up to an interface or switch to subclass (CGLIB) "
                            + "proxies by setting proxy-target-class/proxyTargetClass " + "attribute to 'true'",
                    method.getName(), method.getDeclaringClass().getSimpleName()), ex);
        }
    }
    return method;
}

From source file:org.acegisecurity.context.HttpSessionContextIntegrationFilter.java

/**
 * Gets the security context from the session (if available) and returns it.
 * <p/>// w  ww  .j ava2 s  . c o m
 * If the session is null, the context object is null or the context object stored in the session
 * is not an instance of SecurityContext it will return null.
 * <p/>
 * If <tt>cloneFromHttpSession</tt> is set to true, it will attempt to clone the context object
 * and return the cloned instance.
 *
 * @param httpSession the session obtained from the request.
 */
private SecurityContext readSecurityContextFromSession(HttpSession httpSession) {
    if (httpSession == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("No HttpSession currently exists");
        }

        return null;
    }

    // Session exists, so try to obtain a context from it.

    Object contextFromSessionObject = httpSession.getAttribute(ACEGI_SECURITY_CONTEXT_KEY);

    if (contextFromSessionObject == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("HttpSession returned null object for ACEGI_SECURITY_CONTEXT");
        }

        return null;
    }

    // We now have the security context object from the session.

    // Clone if required (see SEC-356)
    if (cloneFromHttpSession) {
        Assert.isInstanceOf(Cloneable.class, contextFromSessionObject,
                "Context must implement Clonable and provide a Object.clone() method");
        try {
            Method m = contextFromSessionObject.getClass().getMethod("clone", new Class[] {});
            if (!m.isAccessible()) {
                m.setAccessible(true);
            }
            contextFromSessionObject = m.invoke(contextFromSessionObject, new Object[] {});
        } catch (Exception ex) {
            ReflectionUtils.handleReflectionException(ex);
        }
    }

    if (!(contextFromSessionObject instanceof SecurityContext)) {
        if (logger.isWarnEnabled()) {
            logger.warn("ACEGI_SECURITY_CONTEXT did not contain a SecurityContext but contained: '"
                    + contextFromSessionObject + "'; are you improperly modifying the HttpSession directly "
                    + "(you should always use SecurityContextHolder) or using the HttpSession attribute "
                    + "reserved for this class?");
        }

        return null;
    }

    // Everything OK. The only non-null return from this method.

    return (SecurityContext) contextFromSessionObject;
}