private static <T> T getInstance0(Class<? extends T> clazz) throws Exception { @SuppressWarnings("unchecked") final Constructor<? extends T> constructor = Iterables.getFirst(getConstructors(clazz, withParametersCount(0)), null); assert constructor != null : "No default constructor is found from " + clazz.getName(); constructor.setAccessible(true); return constructor.newInstance(); }
private static <T extends Annotation> void collectAnnotations( Builder<T> builder, Annotation annotation, Class<T> annotationType, @Nullable Class<? extends Annotation> containerType) { final Class<? extends Annotation> type = annotation.annotationType(); if (type == annotationType) { builder.add(annotationType.cast(annotation)); return; } if (containerType == null || type != containerType) { return; } final Method method = Iterables.getFirst( getMethods(containerType, withName("value"), withParametersCount(0)), null); if (method == null) { return; } method.setAccessible(true); try { @SuppressWarnings("unchecked") final T[] values = (T[]) method.invoke(annotation); Arrays.stream(values).forEach(builder::add); } catch (Exception e) { // Should not reach here. throw new Error("Failed to invoke 'value' method of the repeatable annotation: " + containerType.getName(), e); } }
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 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 <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<? 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; }
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; }
Predicates.and(withConcreteMethodNamed(getterMethod), ReflectionUtils.withParametersCount(0), ReflectUtils.withReturnTypeAssignableFrom(p.entityType))); assertThat(p.descRel(c) +": could not locate getter " + getterMethod + "() returning supertype of " + p.entityType.getSimpleName(), getterMethods.size(), is(1)); c.getMethod = CollectUtils.firstIn(getterMethods); Predicates.and(withConcreteMethodNamed(modifyMethod), ReflectionUtils.withParametersCount(1), ReflectUtils.withParametersAssignableFrom(p.entityType))); if(modifyMethods.size() != 1) { Predicates.and(withConcreteMethodNamed(clearMethod), ReflectionUtils.withParametersCount(0))); if(clearMethods.size() != 1) {
Predicates.and(withConcreteMethodNamed(addToMethod), ReflectionUtils.withParametersCount(1), ReflectUtils.withEntityParameter())); if(addToMethods.size() != 1) { Predicates.and(withConcreteMethodNamed(removeFromMethod), ReflectionUtils.withParametersCount(1), ReflectUtils.withEntityParameter())); if(removeFromMethods.size() != 1) { Predicates.and(withConcreteMethodNamed(modifyMethod), ReflectionUtils.withParametersCount(1), ReflectUtils.withEntityParameter())); if(modifyMethods.size() != 1) { Predicates.and(withConcreteMethodNamed(clearMethod), ReflectionUtils.withParametersCount(0))); if(clearMethods.size() != 1) {
builder.getClass(), ReflectionUtils.withPrefix("set"), ReflectionUtils.withParametersCount(1), method -> !Iterable.class.isAssignableFrom(method.getParameterTypes()[0]), method -> !(Optional.class.isAssignableFrom(method.getParameterTypes()[0]) &&