List of utility methods to do Class New Instance
Object | newInstanceHard(String name) new Instance Hard try { return Class.forName(name).newInstance(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); ... |
T | newInstanceOf(Class new Instance Of try { if (clazz.equals(byte[].class)) { return (T) new byte[] {}; return clazz.newInstance(); } catch (Exception e) { throw new RuntimeException(e); |
Object | newInstanceOf(String className) new Instance Of Class<?> c = Class.forName(className);
Constructor<?> constructor = c.getConstructor();
return constructor.newInstance();
|
T | newInstanceOrThrow(final Class new Instance Or Throw try { return clazz.newInstance(); } catch (final Exception e) { throw new RuntimeException("Could not instantiate class:" + clazz.getSimpleName(), e); |
List | newInstancesViaMetaAnnotation(Class> declarator, Class extends Annotation> metaAnnotationClass, Class new Instances Via Meta Annotation List<T> result = Lists.newArrayList(); for (Annotation annotation : declarator.getAnnotations()) { Annotation metaAnnotation = annotation.annotationType().getAnnotation(metaAnnotationClass); T r = newInstanceViaAnnotation(declarator, metaAnnotation, expected, annotation); if (r != null) result.add(r); return result; ... |
T | newInstanceViaAnnotation(Class> declarator, Annotation annotation, Class new Instance Via Annotation if (annotation != null) for (Method f : annotation.annotationType().getDeclaredMethods()) if (f.getReturnType() == Class.class) { try { Object objtype = f.invoke(annotation); if (objtype instanceof Class<?> && expected.isAssignableFrom((Class<?>) objtype)) { T result = newInstance((Class<T>) objtype, declarator, parameter); if (result != null) ... |
T | newInstanceWithDefaults(Class new Instance With Defaults Object proxy = Proxy.newProxyInstance(annotationType.getClassLoader(), new Class<?>[] { annotationType }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.getDefaultValue(); }); return annotationType.cast(proxy); |
Object | newInstanceWithFill(Class> componentType, int length, Object filledValue) new Instance With Fill Object array = Array.newInstance(componentType, length); for (int i = 0; i < Array.getLength(array); i++) { Array.set(array, i, filledValue); return array; |
T | newInstanceWithoutInit(Class new Instance Without Init try { ReflectionFactory rf = ReflectionFactory.getReflectionFactory(); Constructor objDef = Object.class.getDeclaredConstructor(); Constructor intConstr = rf.newConstructorForSerialization(clazz, objDef); return clazz.cast(intConstr.newInstance()); } catch (RuntimeException e) { throw e; } catch (Exception e) { ... |
T | newInstanceWithParameterTypes(Constructor new Instance With Parameter Types Constructor paramConstructor = getConstructorWithNoParams(constructor.getParameterTypes()[0]); if (paramConstructor == null) return null; setAccessible(constructor); try { return constructor.newInstance(paramConstructor.newInstance()); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); ... |