List of usage examples for java.lang.reflect Method toString
public String toString()
From source file:org.apache.brooklyn.util.javalang.MethodAccessibleReflections.java
static boolean trySetAccessible(Method method) { try {/*from w w w.j a va 2 s . c om*/ method.setAccessible(true); if (SET_ACCESSIBLE_SUCCEEDED_LOGGED_METHODS.add(method.toString())) { LOG.warn("Discouraged use of setAccessible, called for method " + method); } else { if (LOG.isTraceEnabled()) LOG.trace("Discouraged use of setAccessible, called for method " + method); } return true; } catch (SecurityException e) { boolean added = SET_ACCESSIBLE_FAILED_LOGGED_METHODS.add(method.toString()); if (added) { LOG.warn("Problem setting accessible for method " + method, e); } else { if (LOG.isTraceEnabled()) LOG.trace("Problem setting accessible for method " + method, e); } return false; } }
From source file:org.jdal.annotation.AnnotatedElementAccessor.java
/** * Try to set a value on AnnotatedElement. * @param element the annotated element. * @param value value to set./*from w ww . j ava 2 s . c om*/ */ public static void setValue(AnnotatedElement element, Object target, Object value) { if (element instanceof Field) { Field field = (Field) element; ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, target, value); } else if (element instanceof Method) { Method method = (Method) element; try { method.invoke(target, new Object[] { value }); } catch (Exception e) { log.error("Cannot set value on method [" + method.toString() + "]"); } } }
From source file:org.apache.brooklyn.util.javalang.MethodAccessibleReflections.java
/** * @see {@link Reflections#findAccessibleMethod(Method)} *///from w w w . java 2s.c o m // Similar to org.apache.commons.lang3.reflect.MethodUtils.getAccessibleMethod // We could delegate to that, but currently brooklyn-utils-common doesn't depend on commons-lang3; maybe it should? static Maybe<Method> findAccessibleMethod(Method method) { if (!isAccessible(method)) { String err = "Method is not public, so not normally accessible for " + method; if (FIND_ACCESSIBLE_FAILED_LOGGED_METHODS.add(method.toString())) { LOG.warn(err + "; usage may subsequently fail"); } return Maybe.absent(err); } boolean declaringClassAccessible = isAccessible(method.getDeclaringClass()); if (declaringClassAccessible) { return Maybe.of(method); } // reflectively calling a public method on a private class can fail (unless one first // calls setAccessible). Check if this overrides a public method on a public super-type // that we can call instead! if (Modifier.isStatic(method.getModifiers())) { String err = "Static method not declared on a public class, so not normally accessible for " + method; if (FIND_ACCESSIBLE_FAILED_LOGGED_METHODS.add(method.toString())) { LOG.warn(err + "; usage may subsequently fail"); } return Maybe.absent(err); } Maybe<Method> altMethod = tryFindAccessibleEquivalent(method); if (altMethod.isPresent()) { LOG.debug("Switched method for publicly accessible equivalent: method={}; origMethod={}", altMethod.get(), method); return altMethod; } else { String err = "No accessible (overridden) method found in public super-types for method " + method; if (FIND_ACCESSIBLE_FAILED_LOGGED_METHODS.add(method.toString())) { LOG.warn(err); } return Maybe.absent(err); } }
From source file:org.blocks4j.reconf.client.elements.ConfigurationItemElement.java
private static void checkAnnotations(Method method) { if (!(method.isAnnotationPresent(ConfigurationItem.class) || method.isAnnotationPresent(UpdateConfigurationRepository.class))) { throw new ReConfInitializationError(msg.format("error.not.configured.method", method.toString())); }//from w w w .ja v a 2 s . co m }
From source file:org.eclipse.gemini.blueprint.config.internal.adapter.CustomListenerAdapterUtils.java
private static Map<Class<?>, List<Method>> doDetermineCustomMethods(final Class<?> target, final String methodName, final Class<?>[] possibleArgumentTypes, final boolean onlyPublic) { final Map<Class<?>, List<Method>> methods = new LinkedHashMap<Class<?>, List<Method>>(3); final boolean trace = log.isTraceEnabled(); org.springframework.util.ReflectionUtils.doWithMethods(target, new org.springframework.util.ReflectionUtils.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { if (!method.isBridge() && methodName.equals(method.getName())) { if (onlyPublic && !Modifier.isPublic(method.getModifiers())) { if (trace) log.trace("Only public methods are considered; ignoring " + method); return; }/* w w w .j a v a2 s . co m*/ // take a look at the variables Class<?>[] args = method.getParameterTypes(); if (args != null) { // Properties can be ignored if (args.length == 1) { addMethod(args[0], method, methods); } // or passed as Map, Dictionary else if (args.length == 2) { Class<?> propType = args[1]; for (int i = 0; i < possibleArgumentTypes.length; i++) { Class<?> clazz = possibleArgumentTypes[i]; if (clazz.isAssignableFrom(propType)) { addMethod(args[0], method, methods); } } } } } } private void addMethod(Class<?> key, Method mt, Map<Class<?>, List<Method>> methods) { if (trace) log.trace("discovered custom method [" + mt.toString() + "] on " + target); List<Method> mts = methods.get(key); if (mts == null) { mts = new ArrayList<Method>(2); methods.put(key, mts); org.springframework.util.ReflectionUtils.makeAccessible(mt); mts.add(mt); return; } // add a method only if there is still space if (mts.size() == 1) { Method m = mts.get(0); if (m.getParameterTypes().length == mt.getParameterTypes().length) { if (trace) log.trace("Method w/ signature " + methodSignature(m) + " has been already discovered; ignoring it"); } else { org.springframework.util.ReflectionUtils.makeAccessible(mt); mts.add(mt); } } } private String methodSignature(Method m) { StringBuilder sb = new StringBuilder(); int mod = m.getModifiers(); if (mod != 0) { sb.append(Modifier.toString(mod) + " "); } sb.append(m.getReturnType() + " "); sb.append(m.getName() + "("); Class<?>[] params = m.getParameterTypes(); for (int j = 0; j < params.length; j++) { sb.append(params[j]); if (j < (params.length - 1)) sb.append(","); } sb.append(")"); return sb.toString(); } }); return methods; }
From source file:org.flite.cach3.aop.UpdateMultiCacheAdvice.java
public static List<Object> getKeyObjects(final int keyIndex, final Object returnValue, final JoinPoint jp, final Method methodToCache) throws Exception { final Object keyObject = keyIndex == -1 ? validateReturnValueAsKeyObject(returnValue, methodToCache) : getIndexObject(keyIndex, jp.getArgs(), methodToCache.toString()); if (verifyTypeIsList(keyObject.getClass())) { return (List<Object>) keyObject; }// w ww. ja v a2s .c o m throw new InvalidAnnotationException(String.format( "The parameter object found at dataIndex [%s] is not a [%s] but is of type [%s]. " + "[%s] does not fulfill the requirements.", UpdateMultiCache.class.getName(), List.class.getName(), keyObject.getClass().getName(), methodToCache.toString())); }
From source file:org.flite.cach3.aop.ReadThroughMultiCacheAdvice.java
public static List<Object> getKeyObjectList(final int keyIndex, final JoinPoint jp, final Method method) throws Exception { final Object keyObjects = getIndexObject(keyIndex, jp.getArgs(), method.toString()); if (verifyTypeIsList(keyObjects.getClass())) { return (List<Object>) keyObjects; }//from ww w.j av a2s .c o m throw new InvalidAnnotationException(String.format( "The parameter object found at keyIndex [%s] is not a [%s], but is of type [%s]. " + "[%s] does not fulfill the requirements.", keyIndex, List.class.getName(), keyObjects.getClass().getName(), method.toString())); }
From source file:org.flite.cach3.aop.L2UpdateMultiCacheAdvice.java
protected static void updateCache(final List<String> cacheKeys, final List<Object> returnList, final Method methodToCache, final Duration window, final LogicalCacheIF cache) { if (returnList.size() != cacheKeys.size()) { throw new InvalidAnnotationException(String.format( "The key generation objects, and the resulting objects do not match in size for [%s].", methodToCache.toString())); }//from www . j a v a 2s . c o m final ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder(); for (int ix = 0; ix < returnList.size(); ix++) { final Object resultObject = returnList.get(ix); boolean cacheable = true; if (resultObject instanceof CacheConditionally) { cacheable = ((CacheConditionally) resultObject).isCacheable(); } if (cacheable) { builder.put(cacheKeys.get(ix), resultObject); } } cache.setBulk(builder.build(), window); }
From source file:autocorrelator.apps.SDFGroovy.java
private static void exitWithHelp(String msg, Options options) { System.err.println(msg);/*from ww w . j a v a 2 s . c o m*/ HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(120); formatter.printHelp(INTROText, options); StringBuilder sb = new StringBuilder(); Class<SDFGroovyHelper> helper = SDFGroovyHelper.class; for (Method m : helper.getMethods()) { if ((m.getModifiers() & Modifier.STATIC) == 0 || (m.getModifiers() & Modifier.PUBLIC) == 0) continue; String meth = m.toString(); meth = meth.replaceAll("public static final ", ""); meth = meth.replaceAll("java\\.lang\\.", ""); meth = meth.replaceAll("java\\.math\\.", ""); meth = meth.replaceAll("autocorrelator\\.apps\\.SDFGroovyHelper.", ""); sb.append('\t').append(meth).append('\n'); } System.out.println("Imported internal functions:"); System.out.println(sb); System.exit(1); }
From source file:reconf.client.elements.ConfigurationItemElement.java
public static List<ConfigurationItemElement> from(ConfigurationRepositoryElement repository) { List<ConfigurationItemElement> result = new ArrayList<ConfigurationItemElement>(); for (Method method : repository.getInterfaceClass().getMethods()) { ConfigurationItem ann = method.getAnnotation(ConfigurationItem.class); if (ann == null) { if (method.isAnnotationPresent(UpdateConfigurationRepository.class)) { continue; }//from ww w .j a v a2 s. co m throw new ReConfInitializationError(msg.format("error.not.configured.method", method.toString())); } ConfigurationItemElement resultItem = null; for (ConfigurationItemElement item : repository.getConfigurationItems()) { if (StringUtils.equals(item.getMethodName(), method.getName())) { resultItem = item; } } if (resultItem == null) { resultItem = new ConfigurationItemElement(); resultItem.setMethod(method.getName()); resultItem.setAdapter(ann.adapter()); resultItem.setValue(ann.value()); resultItem.setQualifier(ann.qualifier()); } resultItem.setMethod(method); defineItemProductComponentOverride(repository, resultItem, ann); result.add(resultItem); } return result; }