@SuppressWarnings("unchecked") private void doTestCache() { for (int i = 0; i < toConstruct.length; i++) { Class cl = toConstruct[i]; Object x = ReflectionUtils.newInstance(cl, null); Object y = ReflectionUtils.newInstance(cl, null); assertEquals(cl, x.getClass()); assertEquals(cl, y.getClass()); } }
private static Object newInstance( final JBossServiceConfig serviceConfig, final List<ClassReflectionIndex> mBeanClassHierarchy, final ClassLoader deploymentClassLoader) throws DeploymentUnitProcessingException { // set TCCL so that the MBean instantiation happens in the deployment's classloader final ClassLoader oldTCCL = WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(deploymentClassLoader); try { final JBossServiceConstructorConfig constructorConfig = serviceConfig.getConstructorConfig(); final int paramCount = constructorConfig != null ? constructorConfig.getArguments().length : 0; final Class<?>[] types = new Class<?>[paramCount]; final Object[] params = new Object[paramCount]; if (constructorConfig != null) { final Argument[] arguments = constructorConfig.getArguments(); for (int i = 0; i < paramCount; i++) { final Argument argument = arguments[i]; types[i] = ReflectionUtils.getClass(argument.getType(), deploymentClassLoader); params[i] = newValue( ReflectionUtils.getClass(argument.getType(), deploymentClassLoader), argument.getValue()); } } final Constructor<?> constructor = mBeanClassHierarchy.get(0).getConstructor(types); final Object mBeanInstance = ReflectionUtils.newInstance(constructor, params); return mBeanInstance; } finally { // switch back the TCCL WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTCCL); } }
private void call(String operation, Map<String, String> args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { final String requestClassName = packageName + ".model." + operation + "Request"; final String operationMethodName = operation.substring(0, 1).toLowerCase() + operation.substring(1); Class<Object> requestClass = ReflectionUtils.loadClass(this.getClass(), requestClassName); Object requestObject = ReflectionUtils.newInstance(requestClass); if (args != null && !args.isEmpty()) { for (Map.Entry<String, String> entry : args.entrySet()) { String key = entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1); Object value = convertTo( ReflectionUtils.getParameterTypes(requestObject, Arrays.asList(key)), entry.getValue()); ReflectionUtils.setByPath(requestObject, value, Arrays.asList(key)); } } Method method = ReflectionUtils.findMethod(client, operationMethodName, requestClass); result = method.invoke(client, requestObject); }
@Test public void testCantCreate() { try { ReflectionUtils.newInstance(NoDefaultCtor.class, null); fail("invalid call should fail"); } catch (RuntimeException rte) { assertEquals(NoSuchMethodException.class, rte.getCause().getClass()); } }
@SuppressWarnings("unchecked") @Test public void testCacheDoesntLeak() throws Exception { int iterations = 9999; // very fast, but a bit less reliable - bigger numbers force GC for (int i = 0; i < iterations; i++) { URLClassLoader loader = new URLClassLoader(new URL[0], getClass().getClassLoader()); Class cl = Class.forName("org.apache.hadoop.util.TestReflectionUtils$LoadedInChild", false, loader); Object o = ReflectionUtils.newInstance(cl, null); assertEquals(cl, o.getClass()); } System.gc(); assertTrue(cacheSize() + " too big", cacheSize() < iterations); }
@Override public RecordWriter<K, V> getRecordWriter( FileSystem ignored, JobConf job, String name, Progressable progress) throws IOException { boolean isCompressed = getCompressOutput(job); if (!isCompressed) { Path file = FileOutputFormat.getTaskOutputPath(job, name); FileSystem fs = file.getFileSystem(job); FSDataOutputStream fileOut = fs.create(file, progress); return new LineRecordWriter<K, V>(fileOut); } else { Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job, GzipCodec.class); CompressionCodec codec = ReflectionUtils.newInstance(codecClass, job); Path file = FileOutputFormat.getTaskOutputPath(job, name + codec.getDefaultExtension()); FileSystem fs = file.getFileSystem(job); FSDataOutputStream fileOut = fs.create(file, progress); return new LineRecordWriter<K, V>(new DataOutputStream(codec.createOutputStream(fileOut))); } }
/** * Factory method for creating the varius JSF listener instances that may be referenced by <code> * type</code> or <code>binding</code>. * * <p>If <code>binding</code> is not <code>null</code> and the evaluation result is not <code>null * </code> return that instance. Otherwise try to instantiate an instances based on <code>type * </code>. * * @param type the <code>Listener</code> type * @param binding a <code>ValueExpression</code> which resolves to a <code>Listener</code> * instance * @return a <code>Listener</code> instance based off the provided <code>type</code> and <binding> */ public static Object getListenerInstance(ValueExpression type, ValueExpression binding) { FacesContext faces = FacesContext.getCurrentInstance(); Object instance = null; if (faces == null) { return null; } if (binding != null) { instance = binding.getValue(faces.getELContext()); } if (instance == null && type != null) { try { instance = ReflectionUtils.newInstance(((String) type.getValue(faces.getELContext()))); } catch (Exception e) { throw new AbortProcessingException(e.getMessage(), e); } if (binding != null) { binding.setValue(faces.getELContext(), instance); } } return instance; }
/** * @param cls Hadoop partitioner class. * @param conf Job configuration. */ public GridHadoopV1Partitioner(Class<? extends Partitioner> cls, Configuration conf) { part = (Partitioner<Object, Object>) ReflectionUtils.newInstance(cls, conf); }