List of usage examples for java.lang.reflect Method setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:com.puppycrawl.tools.checkstyle.CheckerTest.java
private static Method getFireAuditStartedMethod() throws NoSuchMethodException { final Class<Checker> checkerClass = Checker.class; final Method fireAuditStarted = checkerClass.getDeclaredMethod("fireAuditStarted"); fireAuditStarted.setAccessible(true); return fireAuditStarted; }
From source file:com.github.dozermapper.core.util.ReflectionUtils.java
public static Object invoke(Method method, Object obj, Object[] args) { Object result = null;/*from w w w.j a v a 2s. c om*/ try { method.setAccessible(true); result = method.invoke(obj, args); } catch (IllegalArgumentException e) { if (e.getMessage().equals(IAE_MESSAGE)) { MappingUtils.throwMappingException(prepareExceptionMessage(method, args), e); } MappingUtils.throwMappingException(e); } catch (IllegalAccessException e) { MappingUtils.throwMappingException(e); } catch (InvocationTargetException e) { MappingUtils.throwMappingException(e); } return result; }
From source file:com.puppycrawl.tools.checkstyle.CheckerTest.java
private static Method getFireAuditFinished() throws NoSuchMethodException { final Class<Checker> checkerClass = Checker.class; final Method fireAuditFinished = checkerClass.getDeclaredMethod("fireAuditFinished"); fireAuditFinished.setAccessible(true); return fireAuditFinished; }
From source file:com.ecmoho.common.util.ReflectionUtils.java
/** * , private/protected./*from w ww . j a v a2 s .c o m*/ */ public static Object invokeMethod(final Object object, final String methodName, final Object[] parameters) { try { Class<?>[] parameterTypes = null; if (parameters != null) { parameterTypes = new Class[parameters.length]; for (int i = 0; i < parameters.length; i++) { parameterTypes[i] = parameters[i].getClass(); int idex = parameterTypes[i].getName().indexOf("_$$_"); if (idex > -1) { parameterTypes[i] = Class.forName(parameterTypes[i].getName().substring(0, idex)); } } } Method method = getDeclaredMethod(object, methodName, parameterTypes); if (method == null) { throw new IllegalArgumentException( "Could not find method [" + methodName + "] on target [" + object + "]"); } method.setAccessible(true); return method.invoke(object, parameters); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } }
From source file:com.sun.socialsite.business.impl.JPAListenerManagerImpl.java
/** * Calls any appropriately-annotated methods in listeners which * have registered to receive lifecycle events for a class to * which the specified entity belongs./*from ww w . ja v a2 s . c o m*/ * @param entity the entity experiencing a lifecycle event. * @param annotationClass the annotation class corresponding to the event. */ private static void notifyListeners(Object entity, Class<? extends Annotation> annotationClass) { //log.trace(String.format("notifyListeners(%s, %s)", entity, annotationClass.getCanonicalName())); Collection<Object> listeners = getListeners(entity); for (Object listener : listeners) { Method[] methods = listener.getClass().getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getAnnotation(annotationClass) != null) { try { log.debug(String.format("calling %s.%s(%s)", listener, method.getName(), entity)); // Handle cases where the listener is a nested class if (Modifier.isPublic(method.getModifiers())) method.setAccessible(true); method.invoke(listener, entity); } catch (Throwable t) { // TODO: Catch and Handle individual Exception types log.error("Exception", t); } } } } }
From source file:com.startechup.tools.ModelParser.java
/** * Invokes the setter method that was link to the json key. * * @param method The setter method to be invoked. * @param classInstance The instance of the container class. * @param key The json key serving as the reference of the value in the json object * @param jsonObject The API response object. *//*ww w . jav a 2 s .c om*/ private static void initMethodInvocation(Method method, Object classInstance, String key, JSONObject jsonObject) { Object value = getValueFromJsonObject(jsonObject, key, method.getName()); // Only invoke the method when the value is not null if (value != null) { method.setAccessible(true); Object castedObject = value; if (value instanceof Number) { castedObject = castNumberObject(method.getParameterTypes()[0], value); } else if (value instanceof JSONArray) { if (method.getParameterTypes()[0].isArray()) { //TODO find a way to genetically convert json array to array, for now throw our custom exception throwException("Cannot parse " + JSONArray.class + " to " + method.getParameterTypes()[0]); } else { Object parameterInstance = getNewInstance(method.getParameterTypes()[0]); if (parameterInstance instanceof Collection) { ParameterizedType parameterizedType = (ParameterizedType) method .getGenericParameterTypes()[0]; Class<?> classType = (Class<?>) parameterizedType.getActualTypeArguments()[0]; castedObject = parse(classType, (JSONArray) value); } else { //TODO find a way to genetically convert json array to the other parameter class, for now throw our custom exception throwException("Cannot parse " + JSONArray.class + " to " + method.getParameterTypes()[0]); } } } else if (value instanceof JSONObject) { castedObject = parse(method.getParameterTypes()[0], ((JSONObject) value)); } // Finally invoke the method after casting the values into the method parameter type invoke(method, classInstance, castedObject); } }
From source file:com.google.code.siren4j.util.ReflectionUtils.java
/** * Sets the fields value, first by attempting to call the setter method if it exists and then * falling back to setting the field directly. * * @param obj the object instance to set the value on, cannot be <code>null</code>. * @param info the fields reflected info object, cannot be <code>null</code>. * @param value the value to set on the field, may be <code>null</code>. * @throws Siren4JException upon reflection error. *//*from ww w . ja va2 s . c o m*/ public static void setFieldValue(Object obj, ReflectedInfo info, Object value) throws Siren4JException { if (obj == null) { throw new IllegalArgumentException("obj cannot be null"); } if (info == null) { throw new IllegalArgumentException("info cannot be null"); } if (info.getSetter() != null) { Method setter = info.getSetter(); setter.setAccessible(true); try { setter.invoke(obj, new Object[] { value }); } catch (Exception e) { throw new Siren4JException(e); } } else { // No setter set field directly try { info.getField().set(obj, value); } catch (Exception e) { throw new Siren4JException(e); } } }
From source file:com.zc.util.refelect.Reflector.java
/** * ?methodannotationClass/*w w w . j ava2s.c o m*/ * * @param method * method * @param annotationClass * annotationClass * * @return {@link java.lang.annotation.Annotation} */ public static <T extends Annotation> T getAnnotation(Method method, Class annotationClass) { method.setAccessible(true); if (method.isAnnotationPresent(annotationClass)) { return (T) method.getAnnotation(annotationClass); } return null; }
From source file:com.asakusafw.workflow.executor.TaskExecutors.java
static Method findMethod(String className, String methodName, Class<?>... parameterTypes) { try {//w w w .j ava 2s.com Class<?> aClass = Class.forName(className, false, TaskExecutors.class.getClassLoader()); Method method = aClass.getDeclaredMethod(methodName, parameterTypes); method.setAccessible(true); return method; } catch (Exception e) { LOG.trace("failed to activate method: {}#{}", className, methodName, e); return null; } }
From source file:org.lendingclub.mercator.docker.SwarmScanner.java
/** * The Docker java client is significantly behind the server API. Rather * than try to fork/patch our way to success, we just implement a bit of * magic to get access to the underlying jax-rs WebTarget. * //from w w w. j a va 2 s . co m * Docker should just expose this as a public method. * * @param c * @return */ public static WebTarget extractWebTarget(DockerClient c) { try { for (Field m : DockerClientImpl.class.getDeclaredFields()) { if (DockerCmdExecFactory.class.isAssignableFrom(m.getType())) { m.setAccessible(true); JerseyDockerCmdExecFactory f = (JerseyDockerCmdExecFactory) m.get(c); Method method = f.getClass().getDeclaredMethod("getBaseResource"); method.setAccessible(true); return (WebTarget) method.invoke(f); } } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new IllegalStateException("could not obtain WebTarget", e); } throw new IllegalStateException("could not obtain WebTarget"); }