@Override public Object call(Object[] parameters, BindingContext context) { return withContextClassLoader(extensionClassLoader, () -> invokeMethod(method, componentInstance, parametersResolver.apply(parameters))); }
private <R> R instantiateOrFail(Class<R> clazz) { try { return ClassUtils.instantiateClass(clazz); } catch (Exception e) { throw new IllegalArgumentException("Error instantiating class: [" + clazz + "].", e); } } }
private static Class loadClass(String name, ClassLoader extensionClassloader) { try { return ClassUtils.loadClass(name, extensionClassloader); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } }
private T instantiateResolver(Class<T> factoryType) { try { return ClassUtils.instantiateClass(factoryType); } catch (Exception e) { throw new MuleRuntimeException(createStaticMessage("Could not create instance of type " + getClassName(factoryType)), e); } } }
private ServiceProvider instantiateServiceProvider(ClassLoader classLoader, String className) throws ServiceResolutionError { Object reflectedObject; try { reflectedObject = withContextClassLoader(classLoader, () -> { try { return ClassUtils.instantiateClass(className); } catch (Exception e) { throw new MuleRuntimeException(createStaticMessage("Unable to create service from class: " + className), e); } }); } catch (RuntimeException e) { throw new ServiceResolutionError(e.getMessage(), e); } if (!(reflectedObject instanceof ServiceProvider)) { throw new ServiceResolutionError(format("Provided service class '%s' does not implement '%s'", className, ServiceProvider.class.getName())); } return (ServiceProvider) reflectedObject; }
List<MuleCoreExtension> result = new LinkedList<>(); Enumeration<?> e = ClassUtils.getResources(CORE_EXTENSION_RESOURCE_NAME, getClass().getClassLoader()); List<Properties> extensions = new LinkedList<Properties>(); String extClass = (String) entry.getValue(); try { MuleCoreExtension extension = (MuleCoreExtension) ClassUtils.instantiateClass(extClass); extension.setContainerClassLoader(containerClassLoader); result.add(extension);
public static Object instantiateClass(String name, Object[] constructorArgs, Class<?> callingClass) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> clazz = loadClass(name, callingClass); return instantiateClass(clazz, constructorArgs); }
public MuleArtifactClassLoaderTestCase() { apiLocation = getResource("com/organization/test-artifact/1.0.0/test-artifact-1.0.0-raml.zip", this.getClass()); apiLibraryLocation = getResource("com/organization/test-artifact/1.0.0/test-artifact-1.0.0-raml-fragment.zip", this.getClass()); jarLocation = getResource("com/organization/test-artifact/1.0.0/test-artifact-1.0.0.jar", this.getClass()); testsJarLocation = getResource("com/organization/test-artifact/1.0.0/test-artifact-1.0.0-tests.jar", this.getClass()); }
private List<ConfigurationListener> getListeners(Configuration configuration) { try { return ClassUtils.getFieldValue(configuration, "listeners", true); } catch (Exception e) { throw new MuleRuntimeException(createStaticMessage("Could not get listeners. Unexpected configuration type"), e); } }
private Set<Class> getRegisteredServiceContracts() { return serviceRepository.getServices().stream() .flatMap(s -> stream(findImplementedInterfaces(s.getClass())) .filter(type -> Service.class.isAssignableFrom(type))) .collect(toSet()); }
private <T> T instantiateResolver(Class<?> factoryType) { try { return (T) ClassUtils.instantiateClass(factoryType); } catch (Exception e) { throw new MuleRuntimeException(createStaticMessage("Could not create NamedTypeResolver of type " + getClassName(factoryType)), e); } } }
private URL getParentResource() { return ClassUtils.getResource("classloader-test-hello.jar", this.getClass()); }
@Test public void getFieldValue() throws Exception { final int hash = hashCode(); HashBlob blob = new HashBlob(hash); assertThat(hash, equalTo(ClassUtils.getFieldValue(blob, "hash", false))); }
/** * Creates a proxy for the provided object instance. * * @param object object to wrap. Non null. * @return a new proxy instance. */ public static <T> T createLifecycleFilterProxy(T object) { checkArgument(object != null, "object cannot be null"); InvocationHandler handler = new LifecycleFilterProxy(object); return (T) newProxyInstance(object.getClass().getClassLoader(), findImplementedInterfaces(object.getClass()), handler); } }
/** * Executes the given {@code callable} using the {@link ClassLoader} associated to the {@code extensionModel} * * @param extensionModel a {@link ExtensionModel} * @param callable a {@link Callable} * @param <T> the generic type of the {@code callable}'s return type * @return the value returned by the {@code callable} * @throws Exception if the {@code callable} fails to execute */ public static <T> T withExtensionClassLoader(ExtensionModel extensionModel, Callable<T> callable) throws Exception { return withContextClassLoader(getClassLoader(extensionModel), callable); }
@Override public Source createSource() { try { return ClassUtils.instantiateClass(sourceType); } catch (Exception e) { throw new RuntimeException("Exception found trying to instantiate source type " + sourceType.getName(), e); } } }
/** * {@inheritDoc} */ @Override public Optional<String> packageFor(String className) { try { Class aClass = loadClass(className, Thread.currentThread().getContextClassLoader()); return ofNullable(aClass.getPackage().getName()); } catch (ClassNotFoundException e) { return empty(); } } }
private T instantiateResolver(Class<T> factoryType) { try { return ClassUtils.instantiateClass(factoryType); } catch (Exception e) { throw new MuleRuntimeException(createStaticMessage("Could not create instance of type " + getClassName(factoryType)), e); } } }
private URL getChildFileResource() { return ClassUtils.getResource("classloader-test-bye.jar", this.getClass()); }
@Test public void getFieldValueRecursive() throws Exception { final int hash = hashCode(); HashBlob blob = new ExtendedHashBlob(hash); assertThat(hash, equalTo(ClassUtils.getFieldValue(blob, "hash", true))); }