private static Class loadClass(String name, ClassLoader extensionClassloader) { try { return ClassUtils.loadClass(name, extensionClassloader); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(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 Class<? extends Service> resolveContract() { try { return loadClass(contractClassName, FileSystemServiceProviderDiscoverer.class); } catch (Exception e) { throw new MuleRuntimeException(e); } } }
private Class<?> getDelegateType(ExtensionLoadingContext context, ClassLoader classLoader) { String type = context.<String>getParameter(TYPE_PROPERTY_NAME) .orElseThrow(() -> new IllegalArgumentException(format("Property '%s' has not been specified", TYPE_PROPERTY_NAME))); if (isBlank(type)) { throw new IllegalArgumentException(format("'%s' property cannot be blank", TYPE_PROPERTY_NAME)); } try { return loadClass(type, classLoader); } catch (ClassNotFoundException e) { throw new RuntimeException(format("Class '%s' cannot be loaded", type), e); } } }
/** * Returns the {@link Class} object that is associated to the {@code typeElement} * * @param typeElement a {@link TypeElement} which represents a {@link Class} * @param processingEnvironment the current {@link ProcessingEnvironment} * @param <T> the generic type of the returned {@link Class} * @return the {@link Class} represented by {@code typeElement} */ public <T> Optional<Class<T>> classFor(TypeElement typeElement, ProcessingEnvironment processingEnvironment) { try { return of(loadClass(getClassName(typeElement, processingEnvironment), typeElement.getClass())); } catch (ClassNotFoundException e) { return empty(); } }
/** * Disposes the {@link ClassLoader} by closing all the resources opened by this {@link ClassLoader}. See * {@link URLClassLoader#close()}. */ @Override public void dispose() { try { // Java 7 added support for closing a URLClassLoader, it will close any resources opened by this classloader close(); } catch (IOException e) { // ignore } try { // fix groovy compiler leaks http://www.mulesoft.org/jira/browse/MULE-5125 final Class clazz = ClassUtils.loadClass("org.codehaus.groovy.transform.ASTTransformationVisitor", getClass()); final Field compUnit = clazz.getDeclaredField("compUnit"); compUnit.setAccessible(true); // static field compUnit.set(null, null); } catch (Throwable t) { // ignore } }
private ExtensionElement getExtensionType(ExtensionLoadingContext context) { return context.<ExtensionElement>getParameter(EXTENSION_TYPE).orElseGet(() -> { String type = (String) context.getParameter("type").get(); try { ClassLoader extensionClassLoader = context.getExtensionClassLoader(); return new ExtensionTypeWrapper<>(loadClass(type, extensionClassLoader), new DefaultExtensionsTypeLoaderFactory().createTypeLoader(extensionClassLoader)); } catch (ClassNotFoundException e) { throw new RuntimeException(format("Class '%s' cannot be loaded", type), e); } }); }
@Test public void testLoadPrimitiveClass() throws Exception { assertSame(ClassUtils.loadClass("boolean", getClass()), Boolean.TYPE); assertSame(ClassUtils.loadClass("byte", getClass()), Byte.TYPE); assertSame(ClassUtils.loadClass("char", getClass()), Character.TYPE); assertSame(ClassUtils.loadClass("double", getClass()), Double.TYPE); assertSame(ClassUtils.loadClass("float", getClass()), Float.TYPE); assertSame(ClassUtils.loadClass("int", getClass()), Integer.TYPE); assertSame(ClassUtils.loadClass("long", getClass()), Long.TYPE); assertSame(ClassUtils.loadClass("short", getClass()), Short.TYPE); }
@Override public List<ServiceAssembly> discover() throws ServiceResolutionError { List<ServiceAssembly> locators = new LinkedList<>(); MuleServiceModelJsonSerializer serializer = new MuleServiceModelJsonSerializer(); for (Object serviceArtifactClassLoader : serviceArtifactClassLoaders) { try { ClassLoader classLoader = (ClassLoader) serviceArtifactClassLoader.getClass().getMethod("getClassLoader").invoke(serviceArtifactClassLoader); MuleServiceModel serviceModel; try (InputStream descriptor = classLoader.getResourceAsStream("META-INF/mule-artifact/mule-artifact.json")) { serviceModel = serializer.deserialize(IOUtils.toString(descriptor)); } for (MuleServiceContractModel contract : serviceModel.getContracts()) { ServiceProvider serviceProvider = instantiateServiceProvider(classLoader, contract.getServiceProviderClassName()); locators.add(new ImmutableServiceAssembly(serviceModel.getName(), serviceProvider, classLoader, loadClass(contract.getContractClassName(), getClass().getClassLoader()))); } } catch (Exception e) { throw new IllegalStateException("Couldn't discover service from class loader: " + serviceArtifactClassLoader, e); } } return locators; }
@Test public void testLoadClassOfType() throws Exception { Class<? extends Exception> clazz = ClassUtils.loadClass("java.lang.IllegalArgumentException", getClass(), Exception.class); assertNotNull(clazz); assertEquals(clazz.getName(), "java.lang.IllegalArgumentException"); try { ClassUtils.loadClass("java.lang.UnsupportedOperationException", getClass(), String.class); fail("IllegalArgumentException should be thrown since class is not of expected type"); } catch (IllegalArgumentException e) { // expected } }
@Test public void testLoadClass() throws Exception { Class clazz = ClassUtils.loadClass("java.lang.String", getClass()); assertNotNull(clazz); assertEquals(clazz.getName(), "java.lang.String"); try { ClassUtils.loadClass("java.lang.Bing", getClass()); fail("ClassNotFoundException should be thrown"); } catch (ClassNotFoundException e) { // expected } }
/** * Resolves the types of each component model when possible. */ public void resolveComponentTypes() { // TODO MULE-13894 enable this once changes are completed and no componentBuildingDefinition is needed // checkState(componentBuildingDefinitionRegistry.isPresent(), // "ApplicationModel was created without a " + ComponentBuildingDefinitionProvider.class.getName()); componentBuildingDefinitionRegistry.ifPresent(buildingDefinitionRegistry -> { executeOnEveryComponentTree(componentModel -> { Optional<ComponentBuildingDefinition<?>> buildingDefinition = buildingDefinitionRegistry.getBuildingDefinition(componentModel.getIdentifier()); buildingDefinition.map(definition -> { ObjectTypeVisitor typeDefinitionVisitor = new ObjectTypeVisitor(componentModel); definition.getTypeDefinition().visit(typeDefinitionVisitor); componentModel.setType(typeDefinitionVisitor.getType()); return definition; }).orElseGet(() -> { String classParameter = componentModel.getParameters().get(CLASS_ATTRIBUTE); if (classParameter != null) { try { componentModel.setType(ClassUtils.loadClass(classParameter, Thread.currentThread().getContextClassLoader())); } catch (ClassNotFoundException e) { throw new RuntimeConfigurationException(createStaticMessage(e.getMessage()), e); } } return null; }); }); }); }
@Test @SuppressWarnings({"rawtypes", "unchecked"}) public void increaseBufferSizeViaSystemProperty() throws Exception { final int newBufferSize = 8 * 1024; testWithSystemProperty(MuleProperties.MULE_STREAMING_BUFFER_SIZE, Integer.toString(newBufferSize), () -> { InputStream in = new ByteArrayInputStream(new byte[newBufferSize]); OutputStream out = mock(OutputStream.class); ClassLoader contextClassLoader = currentThread().getContextClassLoader(); URLClassLoader newClassLoader = new URLClassLoader(((URLClassLoader) contextClassLoader).getURLs(), null); Class clazz = loadClass(IOUtils.class.getCanonicalName(), newClassLoader); try { currentThread().setContextClassLoader(newClassLoader); clazz.getMethod("copyLarge", InputStream.class, OutputStream.class).invoke(clazz, in, out); } finally { currentThread().setContextClassLoader(contextClassLoader); } // With 8KB buffer define via system property only 1 read is required for 8KB // input stream verify(out, times(1)).write(any(byte[].class), anyInt(), anyInt()); }); }
final Class<?> classParameter; try { classParameter = loadClass(classParameterValue, currentThread().getContextClassLoader()); } catch (ClassNotFoundException e) { throw new RuntimeConfigurationException(createStaticMessage(e.getMessage()), e);
private static void addToConsumableClasses(String className) { try { consumableClasses.add(ClassUtils.loadClass(className, Message.class)); } catch (ClassNotFoundException e) { // ignore } }
private static Class loadClass(String name, ClassLoader extensionClassloader) { try { return ClassUtils.loadClass(name, extensionClassloader); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } }
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); }
@Test public void testLoadPrimitiveClass() throws Exception { assertSame(ClassUtils.loadClass("boolean", getClass()), Boolean.TYPE); assertSame(ClassUtils.loadClass("byte", getClass()), Byte.TYPE); assertSame(ClassUtils.loadClass("char", getClass()), Character.TYPE); assertSame(ClassUtils.loadClass("double", getClass()), Double.TYPE); assertSame(ClassUtils.loadClass("float", getClass()), Float.TYPE); assertSame(ClassUtils.loadClass("int", getClass()), Integer.TYPE); assertSame(ClassUtils.loadClass("long", getClass()), Long.TYPE); assertSame(ClassUtils.loadClass("short", getClass()), Short.TYPE); }
@Test public void testLoadClassOfType() throws Exception { Class<? extends Exception> clazz = ClassUtils.loadClass("java.lang.IllegalArgumentException", getClass(), Exception.class); assertNotNull(clazz); assertEquals(clazz.getName(), "java.lang.IllegalArgumentException"); try { ClassUtils.loadClass("java.lang.UnsupportedOperationException", getClass(), String.class); fail("IllegalArgumentException should be thrown since class is not of expected type"); } catch (IllegalArgumentException e) { // expected } }
@Test public void testLoadClass() throws Exception { Class clazz = ClassUtils.loadClass("java.lang.String", getClass()); assertNotNull(clazz); assertEquals(clazz.getName(), "java.lang.String"); try { ClassUtils.loadClass("java.lang.Bing", getClass()); fail("ClassNotFoundException should be thrown"); } catch (ClassNotFoundException e) { // expected } }