/** get all constructors of given {@code type}, up the super class hierarchy, optionally filtered by {@code predicates} */ public static Set<Constructor> getAllConstructors(final Class<?> type, Predicate<? super Constructor>... predicates) { Set<Constructor> result = Sets.newHashSet(); for (Class<?> t : getAllSuperTypes(type)) { result.addAll(getConstructors(t, predicates)); } return result; }
/** get all constructors of given {@code type}, up the super class hierarchy, optionally filtered by {@code predicates} */ public static Set<Constructor> getAllConstructors(final Class<?> type, Predicate<? super Constructor>... predicates) { Set<Constructor> result = Sets.newHashSet(); for (Class<?> t : getAllSuperTypes(type)) { result.addAll(getConstructors(t, predicates)); } return result; }
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(); }
final Set<Constructor> constructors = getConstructors(beanFactoryId.type); for (final Constructor<T> constructor : constructors) {
/** * Creates a copy of this Rule with a new instance of the generated rule class if present. * * This prevents sharing instances across threads, which is not supported for performance reasons. * Otherwise the generated code would need to be thread safe, adding to the runtime overhead. * Instead we buy speed by spending more memory. * * @param functionRegistry the registered functions of the system * @return a copy of this rule with a new instance of its generated code */ public Rule invokableCopy(FunctionRegistry functionRegistry) { final Builder builder = toBuilder(); final Class<? extends GeneratedRule> ruleClass = generatedRuleClass(); if (ruleClass != null) { try { //noinspection unchecked final Set<Constructor> constructors = ReflectionUtils.getConstructors(ruleClass); final Constructor onlyElement = Iterables.getOnlyElement(constructors); final GeneratedRule instance = (GeneratedRule) onlyElement.newInstance(functionRegistry); builder.generatedRule(instance); } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { LOG.warn("Unable to generate code for rule {}: {}", id(), e); } } return builder.build(); }
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; }
public static Set<Constructor> getAllConstructors(final Class<?> type , Predicate<Constructor>... predicates) { Set<Constructor> result = new HashSet(); for (Class<?> t : getAllSuperTypes(type)) { result.addAll(getConstructors(t , predicates)); } return result; }
/** get all constructors of given {@code type}, up the super class hierarchy, optionally filtered by {@code predicates} */ public static Set<Constructor> getAllConstructors(final Class<?> type, Predicate<? super Constructor>... predicates) { Set<Constructor> result = Sets.newHashSet(); for (Class<?> t : getAllSuperTypes(type)) { result.addAll(getConstructors(t, predicates)); } return result; }
/** get all constructors of given {@code type}, up the super class hierarchy, optionally filtered by {@code predicates} */ public static Set<Constructor> getAllConstructors(final Class<?> type, Predicate<? super Constructor>... predicates) { Set<Constructor> result = Sets.newHashSet(); for (Class<?> t : getAllSuperTypes(type)) { result.addAll(getConstructors(t, predicates)); } return result; }
public static <T> T createWith( Class<T> type, List<Class> types, List<Object> arguments ) { Class[] typeArray = types.toArray( new Class[ types.size() ] ); Set<Constructor> constructors = getConstructors( type, withParametersAssignableTo( typeArray ) ); if( constructors.size() != 1 ) throw new FluidException( "could not find constructor for: " + type + ", with: " + types ); Constructor constructor = constructors.iterator().next(); Object[] argsArray = arguments.toArray( new Object[ arguments.size() ] ); return newInstance( constructor, argsArray ); }
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 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 <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 <T extends Exception> ConstructorDelegate<T> selectMostCompleteConstructor(Class<T> exceptionType) { Collection<Constructor> candidate = getConstructors(exceptionType, withParameters(ValidationResult.class, MuleEvent.class)); if (!CollectionUtils.isEmpty(candidate)) { return new ValidationResultAndEventConstructorDelegate<>((Constructor<T>) candidate.iterator().next()); } candidate = getConstructors(exceptionType, withParameters(ValidationResult.class)); if (!CollectionUtils.isEmpty(candidate)) { return new ValidationResultConstructorDelegate((Constructor<T>) candidate.iterator().next()); } candidate = getConstructors(exceptionType, withParameters(String.class)); if (CollectionUtils.isEmpty(candidate)) { throw new IllegalArgumentException( String.format( "Exception type %s was expected to contain at least one accessible constructor with eia single String argument but a matching constructor " + "could not be found.", exceptionType.getCanonicalName())); } return new DirectMessageConstructorDelegate((Constructor<T>) candidate.iterator().next()); }