List of usage examples for org.springframework.util Assert isAssignable
public static void isAssignable(Class<?> superType, Class<?> subType)
From source file:io.gravitee.common.spring.factory.SpringFactoriesLoader.java
@SuppressWarnings("unchecked") private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, ClassLoader classLoader, Object[] args, Set<String> names) { List<T> instances = new ArrayList<>(names.size()); for (String name : names) { try {//ww w . j ava 2 s . c o m Class<?> instanceClass = ClassUtils.forName(name, classLoader); Assert.isAssignable(type, instanceClass); Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes); T instance = (T) BeanUtils.instantiateClass(constructor, args); ((AbstractApplicationContext) applicationContext).getBeanFactory().autowireBean(instance); if (instance instanceof ApplicationContextAware) { ((ApplicationContextAware) instance).setApplicationContext(applicationContext); } instances.add(instance); } catch (Throwable ex) { throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex); } } return instances; }
From source file:org.codehaus.grepo.query.jpa.repository.JpaRepositoryFactoryBean.java
/** * {@inheritDoc}/*from w w w. j a v a2 s. c om*/ */ @Override protected void validateTargetClass() { Assert.notNull(getTargetClass(), "targetClass must not be null"); Assert.isAssignable(DefaultJpaRepository.class, getTargetClass()); }
From source file:org.wso2.msf4j.spring.MSF4JSpringApplication.java
private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, ClassLoader classLoader, Object[] args, Set<String> names) { List<T> instances = new ArrayList<T>(names.size()); for (String name : names) { try {// w w w . ja v a 2 s . c o m Class<?> instanceClass = ClassUtils.forName(name, classLoader); Assert.isAssignable(type, instanceClass); Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes); T instance = (T) BeanUtils.instantiateClass(constructor, args); instances.add(instance); } catch (Throwable ex) { throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex); } } return instances; }
From source file:org.codehaus.grepo.query.hibernate.repository.HibernateRepositoryFactoryBean.java
/** * {@inheritDoc}// www . j a v a 2 s. c om */ @Override protected void validateTargetClass() { Assert.notNull(getTargetClass(), "targetClass must not be null"); Assert.isAssignable(DefaultHibernateRepository.class, getTargetClass()); }
From source file:net.eusashead.spring.gaecache.GaeCache.java
@Override public void evict(Object key) { Assert.notNull(key);/*from w w w . j av a 2s . co m*/ Assert.isAssignable(GaeCacheKey.class, key.getClass()); GaeCacheKey cacheKey = GaeCacheKey.class.cast(key); Integer namespaceKey = getNamespaceKey(); String nsKey = getKey(namespaceKey, cacheKey); log.fine(String.format("Deleting key %s (%s) from namespace %s (namespace key: %s)", cacheKey.hashValue(), cacheKey.rawValue(), this.name, namespaceKey)); syncCache.delete(nsKey); }
From source file:net.eusashead.spring.gaecache.GaeCache.java
@Override public ValueWrapper get(Object key) { Assert.notNull(key);//www. j av a2 s.c om Assert.isAssignable(GaeCacheKey.class, key.getClass()); GaeCacheKey cacheKey = GaeCacheKey.class.cast(key); Integer namespaceKey = getNamespaceKey(); String nsKey = getKey(namespaceKey, cacheKey); Object value = syncCache.get(nsKey); log.fine(String.format("Retrieving key %s (%s) from namespace %s (namespace key: %s), got %s", cacheKey.hashValue(), cacheKey.rawValue(), this.name, namespaceKey, value)); return (value != null ? new SimpleValueWrapper(value) : null); }
From source file:net.eusashead.spring.gaecache.GaeCache.java
@Override public void put(Object key, Object value) { Assert.notNull(key);/* ww w . j a v a 2 s . co m*/ Assert.isAssignable(GaeCacheKey.class, key.getClass()); GaeCacheKey cacheKey = GaeCacheKey.class.cast(key); Integer namespaceKey = getNamespaceKey(); String nsKey = getKey(namespaceKey, cacheKey); log.fine(String.format("Caching key %s (%s) from namespace %s (namespace key: %s), with %s", cacheKey.hashValue(), cacheKey.rawValue(), this.name, namespaceKey, value)); this.syncCache.put(nsKey, value, expiration); }
From source file:org.eclipse.gemini.blueprint.context.support.OsgiPropertyEditorRegistrar.java
@SuppressWarnings("unchecked") private void createEditors(Properties configuration) { boolean trace = log.isTraceEnabled(); // load properties using this class class loader ClassLoader classLoader = getClass().getClassLoader(); for (Map.Entry<Object, Object> entry : configuration.entrySet()) { // key represents type Class<?> key;//from w w w . j a v a 2s. co m // value represents property editor Class<?> editorClass; try { key = classLoader.loadClass((String) entry.getKey()); editorClass = classLoader.loadClass((String) entry.getValue()); } catch (ClassNotFoundException ex) { throw (RuntimeException) new IllegalArgumentException("Cannot load class").initCause(ex); } Assert.isAssignable(PropertyEditor.class, editorClass); if (trace) log.trace("Adding property editor[" + editorClass + "] for type[" + key + "]"); editors.put(key, (Class<? extends PropertyEditor>) editorClass); } }
From source file:org.springframework.beans.BeanUtils.java
/** * Instantiate a class using its no-arg constructor and return the new instance * as the specified assignable type./*from ww w. j ava 2 s. co m*/ * <p>Useful in cases where the type of the class to instantiate (clazz) is not * available, but the type desired (assignableTo) is known. * <p>Note that this method tries to set the constructor accessible if given a * non-accessible (that is, non-public) constructor. * @param clazz class to instantiate * @param assignableTo type that clazz must be assignableTo * @return the new instance * @throws BeanInstantiationException if the bean cannot be instantiated * @see Constructor#newInstance */ @SuppressWarnings("unchecked") public static <T> T instantiateClass(Class<?> clazz, Class<T> assignableTo) throws BeanInstantiationException { Assert.isAssignable(assignableTo, clazz); return (T) instantiateClass(clazz); }
From source file:org.springframework.boot.SpringApplication.java
@SuppressWarnings("unchecked") private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {/*from w w w . j a va 2 s . c o m*/ ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // Use names and ensure unique to protect against duplicates Set<String> names = new LinkedHashSet<String>(SpringFactoriesLoader.loadFactoryNames(type, classLoader)); List<T> instances = new ArrayList<T>(names.size()); // Create instances from the names for (String name : names) { try { Class<?> instanceClass = ClassUtils.forName(name, classLoader); Assert.isAssignable(type, instanceClass); Constructor<?> constructor = instanceClass.getConstructor(parameterTypes); T instance = (T) constructor.newInstance(args); instances.add(instance); } catch (Throwable ex) { throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex); } } AnnotationAwareOrderComparator.sort(instances); return instances; }