/** * Determine whether the given class has a public constructor with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * @param clazz the clazz to analyze * @param paramTypes the parameter types of the method * @return whether the class has a corresponding constructor * @see Class#getMethod */ public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) { return (getConstructorIfAvailable(clazz, paramTypes) != null); }
@Nullable private static Constructor<?> determineFactoryConstructor(Class<?> targetClass, Class<?> sourceClass) { return ClassUtils.getConstructorIfAvailable(targetClass, sourceClass); }
/** * Determine whether the given class has a public constructor with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * @param clazz the clazz to analyze * @param paramTypes the parameter types of the method * @return whether the class has a corresponding constructor * @see Class#getMethod */ public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) { return (getConstructorIfAvailable(clazz, paramTypes) != null); }
@Nullable private static Constructor<?> determineFactoryConstructor(Class<?> targetClass, Class<?> sourceClass) { return ClassUtils.getConstructorIfAvailable(targetClass, sourceClass); }
/** * Attempt to create a copy of the supplied {@code TestContext} using its * <em>copy constructor</em>. */ private static TestContext copyTestContext(TestContext testContext) { Constructor<? extends TestContext> constructor = ClassUtils.getConstructorIfAvailable(testContext.getClass(), testContext.getClass()); if (constructor != null) { try { ReflectionUtils.makeAccessible(constructor); return constructor.newInstance(testContext); } catch (Exception ex) { if (logger.isInfoEnabled()) { logger.info(String.format("Failed to invoke copy constructor for [%s]; " + "concurrent test execution is therefore likely not supported.", testContext), ex); } } } // Fallback to original instance return testContext; }
@SuppressWarnings({ "rawtypes", "unchecked" }) private CommandOutput<?, ?, ?> instanciateCommandOutput(Class<? extends CommandOutput> type) { Assert.notNull(type, "Cannot create instance for 'null' type."); Constructor<CommandOutput> constructor = CONSTRUCTORS.get(type); if (constructor == null) { constructor = (Constructor<CommandOutput>) ClassUtils.getConstructorIfAvailable(type, RedisCodec.class); CONSTRUCTORS.put(type, constructor); } return BeanUtils.instantiateClass(constructor, CODEC); } }
private OAuth2ProtectedResourceDetails creatResource(Object target, OAuth2ContextConfiguration contextLoader) { Class<? extends OAuth2ProtectedResourceDetails> type = contextLoader.value(); if (type == OAuth2ProtectedResourceDetails.class) { type = contextLoader.resource(); } Constructor<? extends OAuth2ProtectedResourceDetails> constructor = ClassUtils .getConstructorIfAvailable(type, TestAccounts.class); if (constructor != null && testAccounts != null) { return BeanUtils.instantiateClass(constructor, testAccounts); } constructor = ClassUtils.getConstructorIfAvailable(type, Environment.class); if (constructor != null && environment != null) { return BeanUtils.instantiateClass(constructor, environment); } constructor = ClassUtils.getConstructorIfAvailable(type, Object.class); if (constructor != null) { return BeanUtils.instantiateClass(constructor, target); } // Fallback to default constructor return BeanUtils.instantiate(type); }
@Test public void constructorWhenAuthorizationRequestResolverIsNullThenThrowIllegalArgumentException() { Constructor<OAuth2AuthorizationRequestRedirectFilter> constructor = ClassUtils.getConstructorIfAvailable( OAuth2AuthorizationRequestRedirectFilter.class, OAuth2AuthorizationRequestResolver.class); assertThatThrownBy(() -> constructor.newInstance(null)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void constructorWhenClientRegistrationRepositoryIsNullThenThrowIllegalArgumentException() { Constructor<OAuth2AuthorizationRequestRedirectFilter> constructor = ClassUtils.getConstructorIfAvailable( OAuth2AuthorizationRequestRedirectFilter.class, ClientRegistrationRepository.class); assertThatThrownBy(() -> constructor.newInstance(null)) .isInstanceOf(IllegalArgumentException.class); }
/** * Determine whether the given class has a public constructor with the given signature. * <p>Essentially translates <code>NoSuchMethodException</code> to "false". * @param clazz the clazz to analyze * @param paramTypes the parameter types of the method * @return whether the class has a corresponding constructor * @see java.lang.Class#getMethod */ public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) { return (getConstructorIfAvailable(clazz, paramTypes) != null); }
private static Constructor<?> getConstructor(Class<?> clazz, Class<?> sourceParameterType) { return ClassUtils.getConstructorIfAvailable(clazz, sourceParameterType); }
/** * Determine whether the given class has a public constructor with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * @param clazz the clazz to analyze * @param paramTypes the parameter types of the method * @return whether the class has a corresponding constructor * @see Class#getMethod */ public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) { return (getConstructorIfAvailable(clazz, paramTypes) != null); }
/** * Determine whether the given class has a public constructor with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * @param clazz the clazz to analyze * @param paramTypes the parameter types of the method * @return whether the class has a corresponding constructor * @see Class#getMethod */ public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) { return (getConstructorIfAvailable(clazz, paramTypes) != null); }
/** * Create {@link CoreContainer} via its constructor (Solr 3.6.0 - 4.3.1) * * @param solrHomeDirectory * @param solrXmlFile * @return */ private CoreContainer createCoreContainerViaConstructor(String solrHomeDirectory, File solrXmlFile) { Constructor<CoreContainer> constructor = ClassUtils.getConstructorIfAvailable(CoreContainer.class, String.class, File.class); return BeanUtils.instantiateClass(constructor, solrHomeDirectory, solrXmlFile); }
@SuppressWarnings({ "rawtypes", "unchecked" }) private CommandOutput<?, ?, ?> instanciateCommandOutput(Class<? extends CommandOutput> type) { Assert.notNull(type, "Cannot create instance for 'null' type."); Constructor<CommandOutput> constructor = CONSTRUCTORS.get(type); if (constructor == null) { constructor = (Constructor<CommandOutput>) ClassUtils.getConstructorIfAvailable(type, RedisCodec.class); CONSTRUCTORS.put(type, constructor); } return BeanUtils.instantiateClass(constructor, CODEC); } }
public Object convertSourceToTargetClass(Object source, Class<?> targetClass) throws Exception { if (targetClass.equals(DataModel.class)) { targetClass = OneSelectionTrackingListDataModel.class; } Constructor<?> emptyConstructor = ClassUtils.getConstructorIfAvailable(targetClass); DataModel<?> model = (DataModel<?>) emptyConstructor.newInstance(new Object[] {}); model.setWrappedData(source); return model; }
public Object convertSourceToTargetClass(Object source, Class targetClass) throws Exception { if (targetClass.equals(DataModel.class)) { targetClass = OneSelectionTrackingListDataModel.class; } Constructor emptyConstructor = ClassUtils.getConstructorIfAvailable(targetClass, new Class[] {}); DataModel model = (DataModel) emptyConstructor.newInstance(new Object[] {}); model.setWrappedData(source); return model; }
public Object convertSourceToTargetClass(Object source, Class<?> targetClass) throws Exception { if (targetClass.equals(DataModel.class)) { targetClass = OneSelectionTrackingListDataModel.class; } Constructor<?> emptyConstructor = ClassUtils.getConstructorIfAvailable(targetClass); DataModel<?> model = (DataModel<?>) emptyConstructor.newInstance(new Object[] {}); model.setWrappedData(source); return model; }
@Override public MimeMessage createMimeMessage() { // We have to use reflection as SmartMimeMessage is not package-private if (ClassUtils.isPresent(SMART_MIME_MESSAGE_CLASS_NAME, ClassUtils.getDefaultClassLoader())) { Class<?> smartMimeMessage = ClassUtils.resolveClassName(SMART_MIME_MESSAGE_CLASS_NAME, ClassUtils.getDefaultClassLoader()); Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(smartMimeMessage, Session.class, String.class, FileTypeMap.class); if (constructor != null) { Object mimeMessage = BeanUtils.instantiateClass(constructor, getSession(), this.defaultEncoding, this.defaultFileTypeMap); return (MimeMessage) mimeMessage; } } return new MimeMessage(getSession()); }
public Query<?> createQuery(ParametersParameterAccessor accessor) { PartTree tree = new PartTree(getQueryMethod().getName(), getQueryMethod().getEntityInformation().getJavaType()); Constructor<? extends AbstractQueryCreator<?, ?>> constructor = (Constructor<? extends AbstractQueryCreator<?, ?>>) ClassUtils .getConstructorIfAvailable(queryCreator, PartTree.class, ParameterAccessor.class); return (Query<?>) BeanUtils.instantiateClass(constructor, tree, accessor).createQuery(); }