public static Class<?> tryToLoadClassForName(String proxyClassName, Class<?> targetClass) { if (MAGIC_IN_PROGRESS.get()) { return null; } return org.apache.deltaspike.core.util.ClassUtils.tryToLoadClassForName(proxyClassName, targetClass); }
/** * Indicates whether the {@link BeanManagerProvider} has been initialized. Usually it's not necessary to call this * method in application code. It's useful e.g. for other frameworks to check if DeltaSpike and the CDI container in * general have been started. * * @return true if the BeanManagerProvider is ready to be used */ public static boolean isActive() { return bmpSingleton != null && bmpSingleton.bmInfos.containsKey(ClassUtils.getClassLoader(null)); }
private NameBuilder getExtensionBuilder(View view) { NameBuilder extensionBuilder; if (DefaultExtensionBuilder.class.equals(view.extensionBuilder())) { String customDefaultExtensionBuilderClassName = JsfBaseConfig.ViewConfigCustomization.CUSTOM_DEFAULT_EXTENSION_BUILDER; if (customDefaultExtensionBuilderClassName != null) { extensionBuilder = (NameBuilder)ClassUtils.tryToInstantiateClassForName(customDefaultExtensionBuilderClassName); } else { extensionBuilder = new DefaultExtensionBuilder(); } } else { extensionBuilder = ClassUtils.tryToInstantiateClass(view.extensionBuilder()); } return extensionBuilder; }
@Override public void restoreState(FacesContext context, Object state) { Object[] wrappedState = (Object[]) state; if (this.wrapped == null) //fallback for full state-saving { Class wrappedClass = ClassUtils.tryToLoadClassForName((String)wrappedState[0]); T resolvedInstance = resolveInstanceForClass(context, wrappedClass); //TODO re-visit logic for multiple (custom) wrappers if (resolvedInstance instanceof AbstractContextualReferenceWrapper) { resolvedInstance = ((AbstractContextualReferenceWrapper<T>)resolvedInstance).getWrapped(); } this.wrapped = resolvedInstance; } if (this.wrapped == null) { this.wrapped = (T)ClassUtils.tryToInstantiateClassForName((String)wrappedState[0]); BeanProvider.injectFields(this.wrapped); } if (this.wrapped instanceof StateHolder) { ((StateHolder) this.wrapped).restoreState(context, wrappedState[1]); } }
private boolean needsTransaction(EntityViewCdiQueryInvocationContext context) { boolean requiresTx = false; Method method = context.getMethod(); if (ClassUtils.containsMethod(EntityRepositoryHandler.class, method)) { Method executed = ClassUtils.extractMethod(EntityRepositoryHandler.class, method); requiresTx = executed.isAnnotationPresent(RequiresTransaction.class); } return requiresTx || context.getRepositoryMethod().requiresTransaction(); } }
ClassUtils.tryToLoadClassForName(customPreProcessorClassName, ConfigPreProcessor.class); ConfigPreProcessor preProcessor = ClassUtils.tryToInstantiateClass(preProcessorClass);
/** * Checks if the given class has a method with the same signature, taking in to account generic types * @param targetClass * @param method * @return if it contains a method with the same signature. */ public static boolean containsPossiblyGenericMethod(Class<?> targetClass, Method method) { return extractPossiblyGenericMethod(targetClass, method) != null; }
public CdiAwareJobFactory() { String defaultJobFactoryName = SchedulerBaseConfig.JobCustomization.DEFAULT_JOB_FACTORY_CLASS_NAME; defaultFactory = ClassUtils.tryToInstantiateClassForName(defaultJobFactoryName, JobFactory.class); }
private boolean isDeactivated(Exclude exclude, Class defaultExpressionInterpreterClass) { String expressions = exclude.onExpression(); Class<? extends ExpressionInterpreter> interpreterClass = exclude.interpretedBy(); if (interpreterClass.equals(ExpressionInterpreter.class)) { interpreterClass = defaultExpressionInterpreterClass; } ExpressionInterpreter<String, Boolean> expressionInterpreter = ClassUtils.tryToInstantiateClass(interpreterClass); if (expressionInterpreter == null) { if (LOG.isLoggable(Level.WARNING)) { LOG.warning("can't instantiate " + interpreterClass.getClass().getName()); } return true; } return expressionInterpreter.evaluate(expressions); }
private EntityViewDelegateQueryHandler lookup(EntityViewCdiQueryInvocationContext context) { Bean<EntityViewDelegateQueryHandler> selectedBean = lookupCache.get(context.getMethod()); if (selectedBean == null) { Set<Bean<EntityViewDelegateQueryHandler>> beans = BeanProvider .getBeanDefinitions(EntityViewDelegateQueryHandler.class, true, true); for (Bean<EntityViewDelegateQueryHandler> bean : beans) { if (ClassUtils.containsPossiblyGenericMethod(bean.getBeanClass(), context.getMethod())) { selectedBean = bean; } } if (selectedBean != null) { lookupCache.put(context.getMethod(), selectedBean); } } if (selectedBean != null) { CreationalContext<EntityViewDelegateQueryHandler> cc = beanManager.createCreationalContext(selectedBean); EntityViewDelegateQueryHandler instance = (EntityViewDelegateQueryHandler) beanManager.getReference( selectedBean, EntityViewDelegateQueryHandler.class, cc); if (selectedBean.getScope().equals(Dependent.class)) { context.addDestroyable(new BeanDestroyable<EntityViewDelegateQueryHandler>(selectedBean, instance, cc)); } return instance; } return null; }
/** * Tries to load a class based on the given name * @param name name of the class * @return loaded class or <code>null</code> if it isn't in the classpath */ public static Class tryToLoadClassForName(String name) { try { return loadClassForName(name); } catch (ClassNotFoundException e) { //do nothing - it's just a try return null; } }
/** * Extracts a method matching the source method, allowing generic type parameters to be substituted as * long as they are properly castable. * * @param clazz The target class * @param sourceMethod The source method. * @return the extracted method or <code>null</code> */ public static Method extractPossiblyGenericMethod(Class<?> clazz, Method sourceMethod) { Method exactMethod = extractMethod(clazz, sourceMethod); if (exactMethod == null) { String methodName = sourceMethod.getName(); Class<?>[] parameterTypes = sourceMethod.getParameterTypes(); for (Method method : clazz.getMethods()) { if (method.getName().equals(methodName) && allSameType(method.getParameterTypes(), parameterTypes)) { return method; } } return null; } else { return exactMethod; } }
/** * Checks if the given class contains a method with the same signature. * * @param targetClass The class to check * @param method The source method * @return if it contains a method with the same signature. */ public static boolean containsMethod(Class<?> targetClass, Method method) { return extractMethod(targetClass, method) != null; }
/** * Clear all ConfigSources for the current ClassLoader */ public static synchronized void freeConfigSources() { configSources.remove(ClassUtils.getClassLoader(null)); }
private NameBuilder getBasePathBuilder(View view) { NameBuilder basePathBuilder; if (DefaultBasePathBuilder.class.equals(view.basePathBuilder())) { String customDefaultBasePathBuilderClassName = JsfBaseConfig.ViewConfigCustomization.CUSTOM_DEFAULT_BASE_PATH_BUILDER; if (customDefaultBasePathBuilderClassName != null) { basePathBuilder = (NameBuilder)ClassUtils.tryToInstantiateClassForName(customDefaultBasePathBuilderClassName); } else { basePathBuilder = new DefaultBasePathBuilder(); } } else { basePathBuilder = ClassUtils.tryToInstantiateClass(view.basePathBuilder()); } return basePathBuilder; }
@Override public void restoreState(FacesContext context, Object state) { Object[] wrappedState = (Object[]) state; if (this.wrapped == null) //fallback for full state-saving { Class wrappedClass = ClassUtils.tryToLoadClassForName((String)wrappedState[0]); T resolvedInstance = resolveInstanceForClass(context, wrappedClass); //TODO re-visit logic for multiple (custom) wrappers if (resolvedInstance instanceof AbstractContextualReferenceWrapper) { resolvedInstance = ((AbstractContextualReferenceWrapper<T>)resolvedInstance).getWrapped(); } this.wrapped = resolvedInstance; } if (this.wrapped == null) { this.wrapped = (T)ClassUtils.tryToInstantiateClassForName((String)wrappedState[0]); BeanProvider.injectFields(this.wrapped); } if (this.wrapped instanceof StateHolder) { ((StateHolder) this.wrapped).restoreState(context, wrappedState[1]); } }
private boolean needsTransaction(EntityViewCdiQueryInvocationContext context) { boolean requiresTx = false; Method method = context.getMethod(); if (ClassUtils.containsMethod(EntityRepositoryHandler.class, method)) { Method executed = ClassUtils.extractMethod(EntityRepositoryHandler.class, method); requiresTx = executed.isAnnotationPresent(RequiresTransaction.class); } return requiresTx || context.getRepositoryMethod().requiresTransaction(); } }
ClassUtils.tryToLoadClassForName(customPreProcessorClassName, ConfigPreProcessor.class); ConfigPreProcessor preProcessor = ClassUtils.tryToInstantiateClass(preProcessorClass);
/** * Checks if the given class has a method with the same signature, taking in to account generic types * @param targetClass * @param method * @return if it contains a method with the same signature. */ public static boolean containsPossiblyGenericMethod(Class<?> targetClass, Method method) { return extractPossiblyGenericMethod(targetClass, method) != null; }
/** * Tries to instantiate a class for the given name and type via the default constructor * @param className name of the class * @param targetType target type * @param <T> current type * @return created instance or null if the instantiation failed */ public static <T> T tryToInstantiateClassForName(String className, Class<T> targetType) { Object result = tryToInstantiateClassForName(className); //noinspection unchecked return result != null ? (T) result : null; }