Example usage for java.lang.reflect Method setAccessible

List of usage examples for java.lang.reflect Method setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Method setAccessible.

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

private static boolean writePseudoPropertyValueNamed0(String propertyName, Object bean, Object newValue) {
    try {//ww w.j a  va 2  s  .  c o m
        PropertyDescriptor pd = findPropertyDescriptorFor(bean.getClass(), propertyName);
        // ???????
        if (pd != null) {
            Method writer = pd.getWriteMethod();
            if (writer != null) {
                writer.setAccessible(true);
                writer.invoke(bean, newValue);
                return true;
            } else {
                if (L.isDebugEnabled()) {
                    Map<String, Object> replace = new HashMap<String, Object>(1);
                    replace.put("property", propertyName);
                    L.debug(Strings.substitute(R.getString("D-UTIL#0009"), replace));
                }
                return false;
            }
            // ???????
        } else {
            Field f = bean.getClass().getField(propertyName);
            if (f != null) {
                f.setAccessible(true);
                f.set(bean, newValue);
                return true;
            } else {
                if (L.isDebugEnabled()) {
                    Map<String, Object> replace = new HashMap<String, Object>(1);
                    replace.put("property", propertyName);
                    L.debug(Strings.substitute(R.getString("D-UTIL#0010"), replace));
                }
                return false;
            }
        }
    } catch (SecurityException e) {
        throw new InternalException(Beans.class, "E-UTIL#0010", e);
    } catch (NoSuchFieldException e) {
        Map<String, String> replace = new HashMap<String, String>(1);
        replace.put("property", propertyName);
        throw new InternalException(Beans.class, "E-UTIL#0011", replace, e);
    } catch (IllegalArgumentException e) {
        Map<String, String> replace = new HashMap<String, String>(1);
        replace.put("property", propertyName);
        throw new InternalException(Beans.class, "E-UTIL#0012", replace, e);
    } catch (IllegalAccessException e) {
        throw new InternalException(Beans.class, "E-UTIL#0013", e);
    } catch (InvocationTargetException e) {
        Map<String, String> replace = new HashMap<String, String>(1);
        replace.put("property", propertyName);
        replace.put("class", bean.getClass().getName());
        throw new InternalException(Beans.class, "E-UTIL#0014", replace, e);
    }
}

From source file:info.papdt.blacklight.support.Utility.java

public static void bindOnClick(final Object obj, Object... viewsAndMethod) {
    final Class<?> clazz = obj.getClass();
    String method = viewsAndMethod[viewsAndMethod.length - 1].toString();
    try {//from   w  w w . j  a v  a2  s  .com
        final Method m = findMethod(clazz, method);
        m.setAccessible(true);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    m.invoke(obj);
                } catch (InvocationTargetException e) {

                } catch (IllegalAccessException e) {

                }
            }
        };

        for (Object o : viewsAndMethod) {
            if (o instanceof View) {
                ((View) o).setOnClickListener(listener);
            }
        }
    } catch (NoSuchMethodException e) {

    }
}

From source file:info.papdt.blacklight.support.Utility.java

public static void bindOnLongClick(final Object obj, Object... viewsAndMethod) {
    final Class<?> clazz = obj.getClass();
    String method = viewsAndMethod[viewsAndMethod.length - 1].toString();
    try {//from w  w w .  j  a v a 2 s . c om
        final Method m = findMethod(clazz, method);
        m.setAccessible(true);
        View.OnLongClickListener listener = new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                try {
                    return Boolean.parseBoolean(m.invoke(obj).toString());
                } catch (InvocationTargetException e) {

                } catch (IllegalAccessException e) {

                }

                return false;
            }
        };

        for (Object o : viewsAndMethod) {
            if (o instanceof View) {
                ((View) o).setOnLongClickListener(listener);
            }
        }
    } catch (NoSuchMethodException e) {

    }
}

From source file:co.nubetech.apache.hadoop.OracleDBRecordReader.java

/**
 * Set session time zone/* w ww  .  j  av a  2 s  . c  o  m*/
 * 
 * @param conf
 *            The current configuration. We read the
 *            'oracle.sessionTimeZone' property from here.
 * @param conn
 *            The connection to alter the timezone properties of.
 */
public static void setSessionTimeZone(Configuration conf, Connection conn) throws SQLException {
    // need to use reflection to call the method setSessionTimeZone on
    // the OracleConnection class because oracle specific java libraries are
    // not accessible in this context.
    Method method;
    try {
        method = conn.getClass().getMethod("setSessionTimeZone", new Class[] { String.class });
    } catch (Exception ex) {
        LOG.error("Could not find method setSessionTimeZone in " + conn.getClass().getName(), ex);
        // rethrow SQLException
        throw new SQLException(ex);
    }

    // Need to set the time zone in order for Java
    // to correctly access the column "TIMESTAMP WITH LOCAL TIME ZONE".
    // We can't easily get the correct Oracle-specific timezone string
    // from Java; just let the user set the timezone in a property.
    String clientTimeZone = conf.get(SESSION_TIMEZONE_KEY, "GMT");
    try {
        method.setAccessible(true);
        method.invoke(conn, clientTimeZone);
        LOG.info("Time zone has been set to " + clientTimeZone);
    } catch (Exception ex) {
        LOG.warn("Time zone " + clientTimeZone + " could not be set on Oracle database.");
        LOG.warn("Setting default time zone: GMT");
        try {
            // "GMT" timezone is guaranteed to exist.
            method.invoke(conn, "GMT");
        } catch (Exception ex2) {
            LOG.error("Could not set time zone for oracle connection", ex2);
            // rethrow SQLException
            throw new SQLException(ex);
        }
    }
}

From source file:com.github.dactiv.common.utils.ReflectionUtils.java

/**
 * ?, ?DeclaredMethod,?. ?Object?, null.
 * /*from  ww w. java  2 s.c  o  m*/
 * ?. ?Method,?Method.invoke(Object obj, Object...
 * args)
 * 
 * @param targetClass
 *            Class
 * @param parameterTypes
 *            ?
 * 
 * @return {@link Method}
 */
public static Method getAccessibleMethod(final Class targetClass, final String methodName,
        Class<?>... parameterTypes) {
    Assert.notNull(targetClass, "targetClass?");
    Assert.notNull(methodName, "methodName?");

    for (Class<?> superClass = targetClass; superClass != Object.class; superClass = superClass
            .getSuperclass()) {
        try {
            Method method = superClass.getDeclaredMethod(methodName, parameterTypes);
            method.setAccessible(true);
            return method;
        } catch (NoSuchMethodException e) {
        }
    }
    return null;
}

From source file:br.com.lucasisrael.regra.reflections.TratamentoReflections.java

/**
 * Make the given method accessible, explicitly setting it accessible if
 * necessary. The {@code setAccessible(true)} method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active)./* ww w  .  ja v a  2  s. c o m*/
 * @param method the method to make accessible
 * @see java.lang.reflect.Method#setAccessible
 */
public static void makeAccessible(Method method) {
    if ((!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
        method.setAccessible(true);
    }
}

From source file:info.guardianproject.netcipher.web.WebkitProxy.java

private static void resetProxyForICS() throws Exception {
    try {//from w  w w . j a  v  a 2  s. com
        Class webViewCoreClass = Class.forName("android.webkit.WebViewCore");
        Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
        if (webViewCoreClass != null && proxyPropertiesClass != null) {
            Method m = webViewCoreClass.getDeclaredMethod("sendStaticMessage", Integer.TYPE, Object.class);

            if (m != null) {
                m.setAccessible(true);

                // android.webkit.WebViewCore.EventHub.PROXY_CHANGED = 193;
                m.invoke(null, 193, null);
            }
        }
    } catch (Exception e) {
        Log.e("ProxySettings",
                "Exception setting WebKit proxy through android.net.ProxyProperties: " + e.toString());
        throw e;
    } catch (Error e) {
        Log.e("ProxySettings",
                "Exception setting WebKit proxy through android.webkit.Network: " + e.toString());
        throw e;
    }
}

From source file:com.helpinput.utils.Utils.java

public static Method accessible(Method method) {
    if (method != null) {
        if (!method.isAccessible())
            method.setAccessible(true);
    }//from w  ww  .java 2 s  . com
    return method;
}

From source file:com.helpinput.core.Utils.java

@SuppressWarnings("unchecked")
public static <T extends Member> T setAccess(final Member theMember) {
    if (theMember instanceof Method) {
        Method real = (Method) theMember;
        if (!real.isAccessible())
            real.setAccessible(true);
    } else if (theMember instanceof Constructor) {
        @SuppressWarnings("rawtypes")
        Constructor real = (Constructor) theMember;
        if (!real.isAccessible())
            real.setAccessible(true);/*w  w w  .j ava2  s. co m*/
    } else if (theMember instanceof Field) {
        Field real = (Field) theMember;
        if (!real.isAccessible())
            real.setAccessible(true);
    }
    return (T) theMember;
}

From source file:com.github.dactiv.common.utils.ReflectionUtils.java

/**
 * ?methodannotationClass//from   ww w  . ja v  a  2  s .c  o  m
 * 
 * @param method
 *            method
 * @param annotationClass
 *            annotationClass
 * 
 * @return {@link Annotation}
 */
public static <T extends Annotation> T getAnnotation(Method method, Class annotationClass) {

    Assert.notNull(method, "method?");
    Assert.notNull(annotationClass, "annotationClass?");

    method.setAccessible(true);
    if (method.isAnnotationPresent(annotationClass)) {
        return (T) method.getAnnotation(annotationClass);
    }
    return null;
}