/** * 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); } } }
withParametersCount(0), withModifier(Modifier.PUBLIC )); withParametersCount(0), withModifier(Modifier.PUBLIC ));
private Constructor<? extends Resource> lookingForConstructor(Class<? extends Resource> resourceClass) { LOGGER.debug("Looking for a constructor to inject resource configuration"); Constructor <? extends Resource> constructor = null; Set<Constructor> resourceConstructors = ReflectionUtils.getConstructors(resourceClass, withModifier(Modifier.PUBLIC), withParametersAssignableFrom(ResourceConfiguration.class), withParametersCount(1)); if (resourceConstructors.isEmpty()) { LOGGER.debug("No configuration can be injected for {} because there is no valid constructor. " + "Using default empty constructor.", resourceClass.getName()); try { constructor = resourceClass.getConstructor(); } catch (NoSuchMethodException nsme) { LOGGER.error("Unable to find default empty constructor for {}", resourceClass.getName(), nsme); } } else if (resourceConstructors.size() == 1) { constructor = resourceConstructors.iterator().next(); } else { LOGGER.info("Too much constructors to instantiate resource {}", resourceClass.getName()); } return constructor; }
private <T extends ServiceDiscovery> Constructor<T> lookingForConstructor(Class<T> serviceDiscoveryClass) { Constructor constructor = constructors.get(serviceDiscoveryClass); if (constructor == null) { LOGGER.debug("Looking for a constructor to inject service discovery configuration"); Set<Constructor> serviceDiscoveryConstructors = ReflectionUtils.getConstructors(serviceDiscoveryClass, withModifier(Modifier.PUBLIC), withParametersAssignableFrom(ServiceDiscoveryConfiguration.class), withParametersCount(1)); if (serviceDiscoveryConstructors.isEmpty()) { LOGGER.debug("No configuration can be injected for {} because there is no valid constructor. " + "Using default empty constructor.", serviceDiscoveryClass.getName()); try { constructor = serviceDiscoveryClass.getConstructor(); } catch (NoSuchMethodException nsme) { LOGGER.error("Unable to find default empty constructor for {}", serviceDiscoveryClass.getName(), nsme); } } else if (serviceDiscoveryConstructors.size() == 1) { constructor = serviceDiscoveryConstructors.iterator().next(); } else { LOGGER.info("Too much constructors to instantiate service discovery {}", serviceDiscoveryClass.getName()); } constructors.put(serviceDiscoveryClass, constructor); } return constructor; }
private Constructor<?> lookingForConstructor(Class<?> policyClass) { Constructor constructor = constructors.get(policyClass); if (constructor == null) { LOGGER.debug("Looking for a constructor to inject policy configuration"); Set<Constructor> policyConstructors = ReflectionUtils.getConstructors(policyClass, withModifier(Modifier.PUBLIC), withParametersAssignableFrom(PolicyConfiguration.class), withParametersCount(1)); if (policyConstructors.isEmpty()) { LOGGER.debug("No configuration can be injected for {} because there is no valid constructor. " + "Using default empty constructor.", policyClass.getName()); try { constructor = policyClass.getConstructor(); } catch (NoSuchMethodException nsme) { LOGGER.error("Unable to find default empty constructor for {}", policyClass.getName(), nsme); } } else if (policyConstructors.size() == 1) { constructor = policyConstructors.iterator().next(); } else { LOGGER.info("Too much constructors to instantiate policy {}", policyClass.getName()); } constructors.put(policyClass, constructor); } return constructor; }
@SuppressWarnings("unchecked") public static <T extends Enum> boolean hasDocumentation(Class<? extends T> documentedEnumClass) { return !ReflectionUtils.getMethods(documentedEnumClass, ReflectionUtils.withModifier(Modifier.PUBLIC), ReflectionUtils.withModifier(Modifier.STATIC), ReflectionUtils.withName("getDocumentation")).isEmpty(); }
@SuppressWarnings("unchecked") public static <T extends Enum<?>> boolean hasDocumentation(Class<? extends T> documentedEnumClass) { return !ReflectionUtils.getMethods(documentedEnumClass, ReflectionUtils.withModifier(Modifier.PUBLIC), ReflectionUtils.withModifier(Modifier.STATIC), ReflectionUtils.withName("getDocumentation")) .isEmpty(); }
/** * Get all public fields of this type, cached. * <p> * Excludes static. */ @SuppressWarnings("unchecked") public static Set<Field> getAllPublicFields(Class type) { Set<Field> result = allPublicFields.get(type); if (result == null) { result = getAllFields(type, withModifier(Modifier.PUBLIC), withoutModifier(Modifier.STATIC)); allPublicFields.put(type, result); } return result; }
public Map<Class<? extends Annotation>, Method> resolve(Class<?> policyClass) { Map<Class<? extends Annotation>, Method> methods = new HashMap<>(); for(Class<? extends Annotation> annot : RESOLVABLE_ANNOTATIONS) { Set<Method> resolved = ReflectionUtils.getMethods( policyClass, withModifier(Modifier.PUBLIC), withAnnotation(annot)); if (! resolved.isEmpty()) { methods.put(annot, resolved.iterator().next()); } } return methods; } }
public Map<Class<? extends Annotation>, Method> resolve(Class<?> policyClass) { Map<Class<? extends Annotation>, Method> methods = new HashMap<>(); for(Class<? extends Annotation> annot : RESOLVABLE_ANNOTATIONS) { Set<Method> resolved = ReflectionUtils.getMethods( policyClass, withModifier(Modifier.PUBLIC), withAnnotation(annot)); if (! resolved.isEmpty()) { methods.put(annot, resolved.iterator().next()); } } return methods; } }
/** * Get all public fields of this type, cached. * <p> * Excludes static. */ @SuppressWarnings("unchecked") public static Set<Field> getAllPublicFields(Class type) { Set<Field> result = allPublicFields.get(type); if (result == null) { result = getAllFields(type, withModifier(Modifier.PUBLIC), withoutModifier(Modifier.STATIC)); allPublicFields.put(type, result); } return result; }
private Constructor<? extends Notifier> lookingForConstructor(Class<? extends Notifier> notifierClass) { LOGGER.debug("Looking for a constructor to inject notifier configuration"); Constructor <? extends Notifier> constructor = null; Set<Constructor> notifierConstructors = ReflectionUtils.getConstructors(notifierClass, withModifier(Modifier.PUBLIC), withParametersAssignableFrom(NotificationConfiguration.class), withParametersCount(1)); if (notifierConstructors.isEmpty()) { LOGGER.debug("No configuration can be injected for {} because there is no valid constructor. " + "Using default empty constructor.", notifierClass.getName()); try { constructor = notifierClass.getConstructor(); } catch (NoSuchMethodException nsme) { LOGGER.error("Unable to find default empty constructor for {}", notifierClass.getName(), nsme); } } else if (notifierConstructors.size() == 1) { constructor = notifierConstructors.iterator().next(); } else { LOGGER.info("Too much constructors to instantiate notifier {}", notifierClass.getName()); } return constructor; }
private Constructor<?> lookingForConstructor(Class<?> policyClass) { Constructor constructor = constructors.get(policyClass); if (constructor == null) { LOGGER.debug("Looking for a constructor to inject policy configuration"); Set<Constructor> policyConstructors = ReflectionUtils.getConstructors(policyClass, withModifier(Modifier.PUBLIC), withParametersAssignableFrom(PolicyConfiguration.class), withParametersCount(1)); if (policyConstructors.isEmpty()) { LOGGER.debug("No configuration can be injected for {} because there is no valid constructor. " + "Using default empty constructor.", policyClass.getName()); try { constructor = policyClass.getConstructor(); } catch (NoSuchMethodException nsme) { LOGGER.error("Unable to find default empty constructor for {}", policyClass.getName(), nsme); } } else if (policyConstructors.size() == 1) { constructor = policyConstructors.iterator().next(); } else { LOGGER.info("Too much constructors to instantiate policy {}", policyClass.getName()); } constructors.put(policyClass, constructor); } return constructor; }
private Constructor<? extends Resource> lookingForConstructor(Class<? extends Resource> resourceClass) { LOGGER.debug("Looking for a constructor to inject resource configuration"); Constructor <? extends Resource> constructor = null; Set<Constructor> resourceConstructors = ReflectionUtils.getConstructors(resourceClass, withModifier(Modifier.PUBLIC), withParametersAssignableFrom(ResourceConfiguration.class), withParametersCount(1)); if (resourceConstructors.isEmpty()) { LOGGER.debug("No configuration can be injected for {} because there is no valid constructor. " + "Using default empty constructor.", resourceClass.getName()); try { constructor = resourceClass.getConstructor(); } catch (NoSuchMethodException nsme) { LOGGER.error("Unable to find default empty constructor for {}", resourceClass.getName(), nsme); } } else if (resourceConstructors.size() == 1) { constructor = resourceConstructors.iterator().next(); } else { LOGGER.info("Too much constructors to instantiate resource {}", resourceClass.getName()); } return constructor; }
public static Collection<Method> getOperationMethods(Class<?> declaringClass) { return getAllMethods(declaringClass, withAnnotation(org.mule.extension.annotations.Operation.class), withModifier(Modifier.PUBLIC)); }
private <T extends ServiceDiscovery> Constructor<T> lookingForConstructor(Class<T> serviceDiscoveryClass) { Constructor constructor = constructors.get(serviceDiscoveryClass); if (constructor == null) { LOGGER.debug("Looking for a constructor to inject service discovery configuration"); Set<Constructor> serviceDiscoveryConstructors = ReflectionUtils.getConstructors(serviceDiscoveryClass, withModifier(Modifier.PUBLIC), withParametersAssignableFrom(ServiceDiscoveryConfiguration.class), withParametersCount(1)); if (serviceDiscoveryConstructors.isEmpty()) { LOGGER.debug("No configuration can be injected for {} because there is no valid constructor. " + "Using default empty constructor.", serviceDiscoveryClass.getName()); try { constructor = serviceDiscoveryClass.getConstructor(); } catch (NoSuchMethodException nsme) { LOGGER.error("Unable to find default empty constructor for {}", serviceDiscoveryClass.getName(), nsme); } } else if (serviceDiscoveryConstructors.size() == 1) { constructor = serviceDiscoveryConstructors.iterator().next(); } else { LOGGER.info("Too much constructors to instantiate service discovery {}", serviceDiscoveryClass.getName()); } constructors.put(serviceDiscoveryClass, constructor); } return constructor; }
/** * 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; }
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(); }