private Object createNewDelegate() { try { return ReflectionUtils.accessibleConstructor(this.defaultImplType).newInstance(); } catch (Throwable ex) { throw new IllegalArgumentException("Cannot create default implementation for '" + this.interfaceType.getName() + "' mixin (" + this.defaultImplType.getName() + "): " + ex); } }
public void setCustomSqlExceptionTranslatorClass(@Nullable Class<? extends SQLExceptionTranslator> customTranslatorClass) { if (customTranslatorClass != null) { try { this.customSqlExceptionTranslator = ReflectionUtils.accessibleConstructor(customTranslatorClass).newInstance(); } catch (Throwable ex) { throw new IllegalStateException("Unable to instantiate custom translator", ex); } } else { this.customSqlExceptionTranslator = null; } }
@SuppressWarnings("unchecked") private static <T> T instantiateFactory(String instanceClassName, Class<T> factoryClass, ClassLoader classLoader) { try { Class<?> instanceClass = ClassUtils.forName(instanceClassName, classLoader); if (!factoryClass.isAssignableFrom(instanceClass)) { throw new IllegalArgumentException( "Class [" + instanceClassName + "] is not assignable to [" + factoryClass.getName() + "]"); } return (T) ReflectionUtils.accessibleConstructor(instanceClass).newInstance(); } catch (Throwable ex) { throw new IllegalArgumentException("Unable to instantiate factory class: " + factoryClass.getName(), ex); } }
@SuppressWarnings("unchecked") private Object instantiateUserDefinedStrategy( String className, Class<?> strategyType, @Nullable ClassLoader classLoader) { Object result; try { result = ReflectionUtils.accessibleConstructor(ClassUtils.forName(className, classLoader)).newInstance(); } catch (ClassNotFoundException ex) { throw new IllegalArgumentException("Class [" + className + "] for strategy [" + strategyType.getName() + "] not found", ex); } catch (Throwable ex) { throw new IllegalArgumentException("Unable to instantiate class [" + className + "] for strategy [" + strategyType.getName() + "]: a zero-argument constructor is required", ex); } if (!strategyType.isAssignableFrom(result.getClass())) { throw new IllegalArgumentException("Provided class name must be an implementation of " + strategyType); } return result; }
/** * Create a Map of the given type, with the given * initial capacity (if supported by the Map type). * @param mapType a sub-interface of Map * @param initialCapacity the initial capacity * @return the new Map instance */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected Map<Object, Object> createMap(Class<? extends Map> mapType, int initialCapacity) { if (!mapType.isInterface()) { try { return ReflectionUtils.accessibleConstructor(mapType).newInstance(); } catch (Throwable ex) { throw new IllegalArgumentException( "Could not instantiate map class: " + mapType.getName(), ex); } } else if (SortedMap.class == mapType) { return new TreeMap<>(); } else { return new LinkedHashMap<>(initialCapacity); } }
private static RequestUpgradeStrategy initUpgradeStrategy() { String className; if (tomcatPresent) { className = "TomcatRequestUpgradeStrategy"; } else if (jettyPresent) { className = "JettyRequestUpgradeStrategy"; } else if (undertowPresent) { className = "UndertowRequestUpgradeStrategy"; } else if (reactorNettyPresent) { // As late as possible (Reactor Netty commonly used for WebClient) className = "ReactorNettyRequestUpgradeStrategy"; } else { throw new IllegalStateException("No suitable default RequestUpgradeStrategy found"); } try { className = "org.springframework.web.reactive.socket.server.upgrade." + className; Class<?> clazz = ClassUtils.forName(className, HandshakeWebSocketService.class.getClassLoader()); return (RequestUpgradeStrategy) ReflectionUtils.accessibleConstructor(clazz).newInstance(); } catch (Throwable ex) { throw new IllegalStateException( "Failed to instantiate RequestUpgradeStrategy: " + className, ex); } }
/** * Create a Collection of the given type, with the given initial capacity * (if supported by the Collection type). * @param collectionClass the type of Collection to instantiate * @return the created Collection instance */ @SuppressWarnings("unchecked") protected T createCollection(Class<?> collectionClass) { if (!collectionClass.isInterface()) { try { return (T) ReflectionUtils.accessibleConstructor(collectionClass).newInstance(); } catch (Throwable ex) { throw new IllegalArgumentException( "Could not instantiate collection class: " + collectionClass.getName(), ex); } } else if (List.class == collectionClass) { return (T) new ArrayList(); } else if (SortedSet.class == collectionClass) { return (T) new TreeSet(); } else { return (T) new LinkedHashSet(); } }
return (RequestUpgradeStrategy) ReflectionUtils.accessibleConstructor(clazz).newInstance();
/** * Create a Collection of the given type, with the given * initial capacity (if supported by the Collection type). * @param collectionType a sub-interface of Collection * @param initialCapacity the initial capacity * @return the new Collection instance */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected Collection<Object> createCollection(Class<? extends Collection> collectionType, int initialCapacity) { if (!collectionType.isInterface()) { try { return ReflectionUtils.accessibleConstructor(collectionType).newInstance(); } catch (Throwable ex) { throw new IllegalArgumentException( "Could not instantiate collection class: " + collectionType.getName(), ex); } } else if (List.class == collectionType) { return new ArrayList<>(initialCapacity); } else if (SortedSet.class == collectionType) { return new TreeSet<>(); } else { return new LinkedHashSet<>(initialCapacity); } }
/** * Create an instance of the specified job class. * <p>Can be overridden to post-process the job instance. * @param bundle the TriggerFiredBundle from which the JobDetail * and other info relating to the trigger firing can be obtained * @return the job instance * @throws Exception if job instantiation failed */ protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { Class<?> jobClass = bundle.getJobDetail().getJobClass(); return ReflectionUtils.accessibleConstructor(jobClass).newInstance(); }
/** * Initialize the wrapped Servlet instance. * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig) */ @Override public void afterPropertiesSet() throws Exception { if (this.servletClass == null) { throw new IllegalArgumentException("'servletClass' is required"); } if (this.servletName == null) { this.servletName = this.beanName; } this.servletInstance = ReflectionUtils.accessibleConstructor(this.servletClass).newInstance(); this.servletInstance.init(new DelegatingServletConfig()); }
/** * Instantiate a new TransformerFactory for this view. * <p>The default implementation simply calls * {@link javax.xml.transform.TransformerFactory#newInstance()}. * If a {@link #setTransformerFactoryClass "transformerFactoryClass"} * has been specified explicitly, the default constructor of the * specified class will be called instead. * <p>Can be overridden in subclasses. * @param transformerFactoryClass the specified factory class (if any) * @return the new TransactionFactory instance * @see #setTransformerFactoryClass * @see #getTransformerFactory() */ protected TransformerFactory newTransformerFactory( @Nullable Class<? extends TransformerFactory> transformerFactoryClass) { if (transformerFactoryClass != null) { try { return ReflectionUtils.accessibleConstructor(transformerFactoryClass).newInstance(); } catch (Exception ex) { throw new TransformerFactoryConfigurationError(ex, "Could not instantiate TransformerFactory"); } } else { return TransformerFactory.newInstance(); } }
/** * Create a new BeanShell-scripted object from the given script source. * <p>The script may either be a simple script that needs a corresponding proxy * generated (implementing the specified interfaces), or declare a full class * or return an actual instance of the scripted object (in which case the * specified interfaces, if any, need to be implemented by that class/instance). * @param scriptSource the script source text * @param scriptInterfaces the interfaces that the scripted Java object is * supposed to implement (may be {@code null} or empty if the script itself * declares a full class or returns an actual instance of the scripted object) * @param classLoader the ClassLoader to use for evaluating the script * @return the scripted Java object * @throws EvalError in case of BeanShell parsing failure */ public static Object createBshObject(String scriptSource, @Nullable Class<?>[] scriptInterfaces, @Nullable ClassLoader classLoader) throws EvalError { Object result = evaluateBshScript(scriptSource, scriptInterfaces, classLoader); if (result instanceof Class) { Class<?> clazz = (Class<?>) result; try { return ReflectionUtils.accessibleConstructor(clazz).newInstance(); } catch (Throwable ex) { throw new IllegalStateException("Could not instantiate script class: " + clazz.getName(), ex); } } else { return result; } }
return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
@Override public final Object getAspectInstance() { try { return ReflectionUtils.accessibleConstructor(this.aspectClass).newInstance(); } catch (NoSuchMethodException ex) { throw new AopConfigException( "No default constructor on aspect class: " + this.aspectClass.getName(), ex); } catch (InstantiationException ex) { throw new AopConfigException( "Unable to instantiate aspect class: " + this.aspectClass.getName(), ex); } catch (IllegalAccessException ex) { throw new AopConfigException( "Could not access aspect constructor: " + this.aspectClass.getName(), ex); } catch (InvocationTargetException ex) { throw new AopConfigException( "Failed to invoke aspect constructor: " + this.aspectClass.getName(), ex.getTargetException()); } }
@Override public E createElement(int index) { try { return ReflectionUtils.accessibleConstructor(this.elementClass).newInstance(); } catch (NoSuchMethodException ex) { throw new ElementInstantiationException( "No default constructor on element class: " + this.elementClass.getName(), ex); } catch (InstantiationException ex) { throw new ElementInstantiationException( "Unable to instantiate element class: " + this.elementClass.getName(), ex); } catch (IllegalAccessException ex) { throw new ElementInstantiationException( "Could not access element constructor: " + this.elementClass.getName(), ex); } catch (InvocationTargetException ex) { throw new ElementInstantiationException( "Failed to invoke element constructor: " + this.elementClass.getName(), ex.getTargetException()); } } }
/** * Attempt compilation of the supplied expression. A check is made to see * if it is compilable before compilation proceeds. The check involves * visiting all the nodes in the expression Ast and ensuring enough state * is known about them that bytecode can be generated for them. * @param expression the expression to compile * @return an instance of the class implementing the compiled expression, * or {@code null} if compilation is not possible */ @Nullable public CompiledExpression compile(SpelNodeImpl expression) { if (expression.isCompilable()) { if (logger.isDebugEnabled()) { logger.debug("SpEL: compiling " + expression.toStringAST()); } Class<? extends CompiledExpression> clazz = createExpressionClass(expression); if (clazz != null) { try { return ReflectionUtils.accessibleConstructor(clazz).newInstance(); } catch (Throwable ex) { throw new IllegalStateException("Failed to instantiate CompiledExpression", ex); } } } if (logger.isDebugEnabled()) { logger.debug("SpEL: unable to compile " + expression.toStringAST()); } return null; }
private void growCollectionIfNecessary() { if (this.index >= this.collection.size()) { if (!this.growCollection) { throw new SpelEvaluationException(getStartPosition(), SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, this.collection.size(), this.index); } if (this.index >= this.maximumSize) { throw new SpelEvaluationException(getStartPosition(), SpelMessage.UNABLE_TO_GROW_COLLECTION); } if (this.collectionEntryDescriptor.getElementTypeDescriptor() == null) { throw new SpelEvaluationException( getStartPosition(), SpelMessage.UNABLE_TO_GROW_COLLECTION_UNKNOWN_ELEMENT_TYPE); } TypeDescriptor elementType = this.collectionEntryDescriptor.getElementTypeDescriptor(); try { Constructor<?> ctor = ReflectionUtils.accessibleConstructor(elementType.getType()); int newElements = this.index - this.collection.size(); while (newElements >= 0) { this.collection.add(ctor.newInstance()); newElements--; } } catch (Throwable ex) { throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.UNABLE_TO_GROW_COLLECTION); } } }
return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
fbProxy = ReflectionUtils.accessibleConstructor(fbClass).newInstance();