/** * Returns the list of {@link Path} annotated methods. */ private static List<Method> requestMappingMethods(Object object) { return getAllMethods(object.getClass(), withModifier(Modifier.PUBLIC)) .stream() // Lookup super classes just in case if the object is a proxy. .filter(m -> getAnnotations(m, FindOption.LOOKUP_SUPER_CLASSES) .stream() .map(Annotation::annotationType) .anyMatch(a -> a == Path.class || HTTP_METHOD_MAP.containsKey(a))) .sorted(Comparator.comparingInt(AnnotatedHttpServiceFactory::order)) .collect(toImmutableList()); }
/** * Registers all available command methods annotated with {@link org.terasology.logic.console.commandSystem.annotations.Command}. */ public static void registerAvailable(Object provider, Console console, Context context) { Predicate<? super Method> predicate = Predicates.<Method>and(ReflectionUtils.withModifier(Modifier.PUBLIC), ReflectionUtils.withAnnotation(Command.class)); Set<Method> commandMethods = ReflectionUtils.getAllMethods(provider.getClass(), predicate); for (Method method : commandMethods) { if (!hasSenderAnnotation(method)) { logger.error("Command {} provided by {} contains a EntityRef without @Sender annotation, may cause a NullPointerException", method.getName(), provider.getClass().getSimpleName()); } logger.debug("Registering command method {} in class {}", method.getName(), method.getDeclaringClass().getCanonicalName()); try { SpecificAccessibleObject<Method> specificMethod = new SpecificAccessibleObject<>(method, provider); MethodCommand command = referringTo(specificMethod, context); console.registerCommand(command); logger.debug("Registered command method {} in class {}", method.getName(), method.getDeclaringClass().getCanonicalName()); } catch (RuntimeException t) { logger.error("Failed to load command method {} in class {}", method.getName(), method.getDeclaringClass().getCanonicalName(), t); } } }
final Set<Method> methods = getAllMethods(beanFactoryId.type); for (final Method method : methods) { final RequestConverter[] converters = method.getAnnotationsByType(RequestConverter.class);
Set<Method> beforeClassMethods = getAllMethods(testClass, withAnnotation(BeforeClass.class), withReturnType(Void.TYPE), withParametersCount(0), withModifier(Modifier.PUBLIC Set<Method> allBeforeClassMethods = getAllMethods(testClass, withAnnotation(BeforeClass.class)); allBeforeClassMethods.removeAll(beforeClassMethods); if (!allBeforeClassMethods.isEmpty()) { Set<Method> afterClassMethods = getAllMethods(testClass, withAnnotation(AfterClass.class), withReturnType(Void.TYPE), withParametersCount(0), withModifier(Modifier.PUBLIC Set<Method> allAfterClassMethods = getAllMethods(testClass, withAnnotation(AfterClass.class)); allAfterClassMethods.removeAll(afterClassMethods); if (!allAfterClassMethods.isEmpty()) {
private boolean lookupUpdateConfigurationMethod() { if (updateConfigurationMethod == null) { Collection<Method> candidateMethods = getAllMethods(originalLogger.getClass(), withName("updateConfiguration"), withParameters(Configuration.class)); if (candidateMethods.size() == 1) { updateConfigurationMethod = candidateMethods.iterator().next(); updateConfigurationMethod.setAccessible(true); } } return updateConfigurationMethod != null; }
for (Method method : getAllMethods(object.getClass(), withAnnotation(Inject.class))) { if (method.getParameters().length == 1) { Class<?> dependencyType = method.getParameterTypes()[0];
public DefaultLifecyclePhase(String name, Class<?> lifecycleClass, String oppositeLifecyclePhase) { this.name = name; this.lifecycleClass = lifecycleClass; Set<Method> lifecycleMethodsCandidate = getAllMethods(lifecycleClass, withName(name)); lifecycleMethod = lifecycleMethodsCandidate.isEmpty() ? null : lifecycleMethodsCandidate.iterator().next(); this.oppositeLifecyclePhase = oppositeLifecyclePhase; }
/** * Given the class targetClass (not an interface) the method will return all matched methods * for method. Methods will be reached in extended super classes or implemented interfaces. * * @param method from which the search starts. * @return a set of methods grabbed from parent classes and interfaces. */ public static Set<Method> methodsFromAncestors(final Method method) { // Fetching annotations from method (we fetch all methods in super and // interfaces) Class<?> targetClass = method.getDeclaringClass(); Predicate<? super Method> predicate = new Predicate<Method>() { @Override public boolean apply(Method input) { return !method.equals(input) && methodsAreEquivalent(method, input); } }; Set<Method> methods = new HashSet<Method>(); methods.add(method); methods.addAll(ReflectionUtils.getAllMethods(targetClass, predicate)); return methods; }
private boolean lookupUpdateConfigurationMethod() { if (updateConfigurationMethod == null) { Collection<Method> candidateMethods = getAllMethods(originalLogger.getClass(), withName("updateConfiguration"), withParameters(Configuration.class)); if (candidateMethods.size() == 1) { updateConfigurationMethod = candidateMethods.iterator().next(); updateConfigurationMethod.setAccessible(true); } } return updateConfigurationMethod != null; }
private boolean lookupUpdateConfigurationMethod() { if (updateConfigurationMethod == null) { Collection<Method> candidateMethods = getAllMethods(originalLogger.getClass(), withName("updateConfiguration"), withParameters(Configuration.class)); if (candidateMethods.size() == 1) { updateConfigurationMethod = candidateMethods.iterator().next(); updateConfigurationMethod.setAccessible(true); } } return updateConfigurationMethod != null; }
private Transactional deepGetAnnotation(final Method method, Class<?> targetClass) { Transactional transaction = method.getAnnotation(Transactional.class); // Fetching annotations from method (we fetch all methods in super and interfaces) if (transaction == null) { Predicate<? super Method> predicate = new Predicate<Method>() { @Override public boolean apply(Method input) { return (!method.equals(input)) && methodIsEqual(method, input); } }; Set<Method> methods = ReflectionUtils.getAllMethods(targetClass, predicate); for (Method method2 : methods) { transaction = method2.getAnnotation(Transactional.class); if (transaction != null) { break; } } } // Fetching annotation from class if (transaction == null) { transaction = SeedReflectionUtils.getMetaAnnotationFromAncestors(targetClass, Transactional.class); } return transaction; }
/** * The logic for processing a collection of children * * @param iface The lifecycle interface to be called * @param objects An iterator over all children that must also be called * @throws LifecycleException if any fail */ private static void processAllNoRetry(Class<? extends Initialisable> iface, Iterator<? extends Initialisable> objects) throws LifecycleException { if (!iface.isAssignableFrom(Lifecycle.class)) { throw new IllegalArgumentException("Not a Lifecycle interface: " + iface); } Set<Method> lifecycleMethodsCandidate = getAllMethods(iface, withName(Initialisable.PHASE_NAME)); Method method = lifecycleMethodsCandidate.isEmpty() ? null : lifecycleMethodsCandidate.iterator().next(); // some interfaces have a single exception, others none boolean hasException = method.getExceptionTypes().length > 0; Class<?> exception = hasException ? method.getExceptionTypes()[0] : null; while (objects.hasNext()) { Object target = objects.next(); processSingleNoRetry(target, method, exception, iface); } }
public static Collection<Method> getOperationMethods(Class<?> declaringClass) { return getAllMethods(declaringClass, withAnnotation(org.mule.extension.annotations.Operation.class), withModifier(Modifier.PUBLIC)); }
@Override public void subscribe(Object eventHandler) { Set<Method> methods = ReflectionUtils.getAllMethods(eventHandler.getClass(), ReflectionUtils.withAnnotation(Handles.class)); for (Method method : methods) { Handles annotation = method.getAnnotation(Handles.class); subscribeInternal(eventHandler, annotation.value(), method); } }
@Override public Object process(Object object) { for (Field field : getAllFields(object.getClass(), withAnnotation(Inject.class), withType(Registry.class))) { try { field.setAccessible(true); field.set(object, new DefaultRegistry(context)); } catch (Exception e) { throw new RuntimeException(format("Could not inject dependency on field %s of type %s", field.getName(), object.getClass().getName()), e); } } for (Method method : getAllMethods(object.getClass(), withAnnotation(Inject.class), withParameters(Registry.class))) { try { method.invoke(object, new DefaultRegistry(context)); } catch (Exception e) { throw new RuntimeException(format("Could not inject dependency on method %s of type %s", method.getName(), object.getClass().getName()), e); } } return object; } }
/** * Get all public methods of type, cached. * <p> * Excludes static, abstract. */ @SuppressWarnings("unchecked") public static Set<Method> getAllPublicMethods(Class type) { Set<Method> result = allPublicMethods.get(type); if (result == null) { result = getAllMethods(type, withModifier(Modifier.PUBLIC), withoutModifier(Modifier.ABSTRACT), withoutModifier(Modifier.STATIC), withoutModifier(Modifier.VOLATILE)); allPublicMethods.put(type, result); } return result; }
/** * Get all public methods of type, cached. * <p> * Excludes static, abstract. */ @SuppressWarnings("unchecked") public static Set<Method> getAllPublicMethods(Class type) { Set<Method> result = allPublicMethods.get(type); if (result == null) { result = getAllMethods(type, withModifier(Modifier.PUBLIC), withoutModifier(Modifier.ABSTRACT), withoutModifier(Modifier.STATIC), withoutModifier(Modifier.VOLATILE)); allPublicMethods.put(type, result); } return result; }
@Override public Result<Object> execute(final String name, Object... args) throws ExtractException { String[] prefixes = new String[]{ "get", "is", "has" }; Set<Method> methods = getAllMethods(context.getClass(), methodMatcher(equalToIgnoringCase(name), args.length)); int i = 0; while (methods.isEmpty() && i < prefixes.length) { methods = getAllMethods(context.getClass(), methodMatcher(equalToIgnoringCase(prefixes[i++] + name), args.length)); } if (methods.isEmpty()) return new Result<Object>(); else { Iterator<Method> iterator = methods.iterator(); while (iterator.hasNext()) { try { return new Result<Object>(iterator.next().invoke(context, args)); } catch (InvocationTargetException e) { throw new ExtractException(e); } catch (IllegalAccessException e) { // do nothing } } } return new Result<Object>(); } };
public static Method getOperationMethod(Class<?> declaringClass, Operation operation) { Class<?>[] parameterTypes; if (operation.getParameters().isEmpty()) { parameterTypes = org.apache.commons.lang.ArrayUtils.EMPTY_CLASS_ARRAY; } else { parameterTypes = new Class<?>[operation.getParameters().size()]; int i = 0; for (Parameter parameter : operation.getParameters()) { parameterTypes[i++] = parameter.getType().getRawType(); } } Collection<Method> methods = getAllMethods(declaringClass, withAnnotation(org.mule.extension.annotations.Operation.class), withModifier(Modifier.PUBLIC), withName(operation.getName()), withParameters(parameterTypes)); checkArgument(!methods.isEmpty(), String.format("Could not find method %s in class %s", operation.getName(), declaringClass.getName())); checkArgument(methods.size() == 1, String.format("More than one matching method was found in class %s for operation %s", declaringClass.getName(), operation.getName())); return methods.iterator().next(); }
@Override protected void applyContractTest(Class<?> entityType) { final Set<Method> injectMethods = ReflectionUtils.getAllMethods(entityType, ReflectionUtils.withPrefix("inject")); for (Method injectMethod : injectMethods) { try { final String desc = desc(entityType, injectMethod); out.println("processing " + desc); out.incrementIndent(); process(entityType, injectMethod); } finally { out.decrementIndent(); } } }