public static <T> T createInstance(final Class<?> cls, final Class<? extends T> defaultType, final Class<T> requiredType) { Assert.assertNotNull("Class to instantiate must be specified", cls); try { if (requiredType == null || requiredType.isAssignableFrom(cls)) { final Class<T> tClass = CastUtils.cast(cls); return tClass.newInstance(); } else { throw new InstanceCreationClassException("Class '" + cls.getName() + "' is not of type '" + requiredType + "'"); } } catch (final NoClassDefFoundError e) { throw new InstanceCreationClassException("Class '" + cls + "'found , but is missing a dependent class: " + e.getMessage(), e); } catch (final InstantiationException e) { throw new InstanceCreationException("Could not instantiate an object of class '" + cls.getName() + "'; " + e.getMessage()); } catch (final IllegalAccessException e) { throw new InstanceCreationException("Could not access the class '" + cls.getName() + "'; " + e.getMessage()); } }
public static <T> T createInstance(final String className, final String defaultTypeName, final Class<T> requiredType) { Class<? extends T> defaultType = null; if (defaultTypeName != null) { try { defaultType = CastUtils.cast(Thread.currentThread().getContextClassLoader().loadClass(defaultTypeName)); if (defaultType == null) { throw new InstanceCreationClassException("Failed to load default type '" + defaultTypeName + "'"); } } catch (final ClassNotFoundException e) { throw new UnavailableClassException("The default type '" + defaultTypeName + "' cannot be found"); } catch (final NoClassDefFoundError e) { throw new InstanceCreationClassException("Default type '" + defaultTypeName + "' found, but is missing a dependent class: " + e.getMessage(), e); } } return createInstance(className, defaultType, requiredType); }
public static <T> T createInstance(final Class<?> cls, final Class<? extends T> defaultType, final Class<T> requiredType) { Assert.assertNotNull("Class to instantiate must be specified", cls); try { if (requiredType == null || requiredType.isAssignableFrom(cls)) { final Class<T> tClass = CastUtils.cast(cls); return tClass.newInstance(); } else { throw new InstanceCreationClassException("Class '" + cls.getName() + "' is not of type '" + requiredType + "'"); } } catch (final NoClassDefFoundError e) { throw new InstanceCreationClassException("Class '" + cls + "'found , but is missing a dependent class: " + e.getMessage(), e); } catch (final InstantiationException e) { throw new InstanceCreationException("Could not instantiate an object of class '" + cls.getName() + "'; " + e.getMessage()); } catch (final IllegalAccessException e) { throw new InstanceCreationException("Could not access the class '" + cls.getName() + "'; " + e.getMessage()); } }
public static <T> T createInstance(final String className, final String defaultTypeName, final Class<T> requiredType) { Class<? extends T> defaultType = null; if (defaultTypeName != null) { try { defaultType = CastUtils.cast(Thread.currentThread().getContextClassLoader().loadClass(defaultTypeName)); if (defaultType == null) { throw new InstanceCreationClassException("Failed to load default type '" + defaultTypeName + "'"); } } catch (final ClassNotFoundException e) { throw new UnavailableClassException("The default type '" + defaultTypeName + "' cannot be found"); } catch (final NoClassDefFoundError e) { throw new InstanceCreationClassException("Default type '" + defaultTypeName + "' found, but is missing a dependent class: " + e.getMessage(), e); } } return createInstance(className, defaultType, requiredType); }
/** * Returns the provided {@link Facet facet} as an {@link ImperativeFacet} if it either is one or if it is * a {@link DecoratingFacet} that in turn wraps an {@link ImperativeFacet}. * * <p> * Otherwise, returns <tt>null</tt>. */ public static ImperativeFacet getImperativeFacet(final Facet facet) { if (facet instanceof ImperativeFacet) { return (ImperativeFacet) facet; } if (facet instanceof DecoratingFacet) { final DecoratingFacet<?> decoratingFacet = CastUtils.cast(facet); return getImperativeFacet(decoratingFacet.getDecoratedFacet()); } return null; }
/** * Returns the provided {@link Facet facet} as an {@link ImperativeFacet} if it either is one or if it is * a {@link DecoratingFacet} that in turn wraps an {@link ImperativeFacet}. * * <p> * Otherwise, returns <tt>null</tt>. */ public static ImperativeFacet getImperativeFacet(final Facet facet) { if (facet instanceof ImperativeFacet) { return (ImperativeFacet) facet; } if (facet instanceof DecoratingFacet) { final DecoratingFacet<?> decoratingFacet = CastUtils.cast(facet); return getImperativeFacet(decoratingFacet.getDecoratedFacet()); } return null; }
public static <R, T extends R> Class<T> loadClass( String className, Class<R> requiredType) { Assert.assertNotNull("Class to instantiate must be specified", className); try { Class<?> loadedClass = loadClass(className); if (requiredType != null && !requiredType.isAssignableFrom(loadedClass)) { throw new InstanceCreationClassException("Class '" + className + "' is not of type '" + requiredType + "'"); } return CastUtils.cast(loadedClass); } catch (final NoClassDefFoundError e) { throw new InstanceCreationClassException("Default type '" + className + "' found, but is missing a dependent class: " + e.getMessage(), e); } }
public static <R, T extends R> Class<T> loadClass( String className, Class<R> requiredType) { Assert.assertNotNull("Class to instantiate must be specified", className); try { Class<?> loadedClass = loadClass(className); if (requiredType != null && !requiredType.isAssignableFrom(loadedClass)) { throw new InstanceCreationClassException("Class '" + className + "' is not of type '" + requiredType + "'"); } return CastUtils.cast(loadedClass); } catch (final NoClassDefFoundError e) { throw new InstanceCreationClassException("Default type '" + className + "' found, but is missing a dependent class: " + e.getMessage(), e); } }
public static <T> T createInstance(final Class<?> cls, final String defaultTypeName, final Class<T> requiredType) { Class<? extends T> defaultType = null; if (defaultTypeName != null) { defaultType = loadClass(defaultTypeName, requiredType); try { defaultType = CastUtils.cast(Thread.currentThread().getContextClassLoader().loadClass(defaultTypeName)); if (defaultType == null) { throw new InstanceCreationClassException("Failed to load default type '" + defaultTypeName + "'"); } } catch (final ClassNotFoundException e) { throw new UnavailableClassException("The default type '" + defaultTypeName + "' cannot be found"); } catch (final NoClassDefFoundError e) { throw new InstanceCreationClassException("Default type '" + defaultTypeName + "' found, but is missing a dependent class: " + e.getMessage(), e); } } return createInstance(cls, defaultType, requiredType); }
public static <T> T createInstance(final Class<?> cls, final String defaultTypeName, final Class<T> requiredType) { Class<? extends T> defaultType = null; if (defaultTypeName != null) { defaultType = loadClass(defaultTypeName, requiredType); try { defaultType = CastUtils.cast(Thread.currentThread().getContextClassLoader().loadClass(defaultTypeName)); if (defaultType == null) { throw new InstanceCreationClassException("Failed to load default type '" + defaultTypeName + "'"); } } catch (final ClassNotFoundException e) { throw new UnavailableClassException("The default type '" + defaultTypeName + "' cannot be found"); } catch (final NoClassDefFoundError e) { throw new InstanceCreationClassException("Default type '" + defaultTypeName + "' found, but is missing a dependent class: " + e.getMessage(), e); } } return createInstance(cls, defaultType, requiredType); }
@SuppressWarnings("unchecked") public Installer getInstaller(final String implClassName) { try { Installer installer = CastUtils.cast(InstanceFactory.createInstance(implClassName)); if (installer != null) { mergeConfigurationFor(installer); injectDependenciesInto(installer); } return installer; } catch (final InstanceCreationException e) { throw new InstanceCreationException("Specification error in " + INSTALLER_REGISTRY_FILE, e); } catch (final UnavailableClassException e) { return null; } }