@Test public void createsNewInstanceFromClassNameAndReturnsSupertype() { final CharSequence charSequence = ReflectionHelper.newInstance("java.lang.String", CharSequence.class); assertThat(charSequence).isEqualTo(""); }
public static <T> T newInstance(final Class<T> clazz) { try { Constructor<T> defaultConstructor = clazz.getDeclaredConstructor(); defaultConstructor.setAccessible(true); return defaultConstructor.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new HibersapException(MSG_CAN_NOT_CREATE_INSTANCE + clazz.getName(), e); } catch (NoSuchMethodException e) { throw new HibersapException("Class does not have a default constructor: " + clazz.getName(), e); } catch (InvocationTargetException e) { throw new HibersapException("Default constructor threw an exception: " + clazz.getName(), e); } }
ReflectionHelper { public static <T> T newInstance(final Class<T> clazz) { try { Constructor<T> defaultConstructor = clazz.getDeclaredConstructor(); defaultConstructor.setAccessible(true); return defaultConstructor.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new HibersapException(MSG_CAN_NOT_CREATE_INSTANCE + clazz.getName(), e); } catch (NoSuchMethodException e) { throw new HibersapException("Class does not have a default constructor: " + clazz.getName(), e); } catch (InvocationTargetException e) { throw new HibersapException("Default constructor threw an exception: " + clazz.getName(), e); } } }
ReflectionHelper { public static <T> T newInstance(final Class<T> clazz) { try { Constructor<T> defaultConstructor = clazz.getDeclaredConstructor(); defaultConstructor.setAccessible(true); return defaultConstructor.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new HibersapException(MSG_CAN_NOT_CREATE_INSTANCE + clazz.getName(), e); } catch (NoSuchMethodException e) { throw new HibersapException("Class does not have a default constructor: " + clazz.getName(), e); } catch (InvocationTargetException e) { throw new HibersapException("Default constructor threw an exception: " + clazz.getName(), e); } } private  ReflectionHelper(); }
ReflectionHelper { public static <T> T newInstance(final Class<T> clazz) { try { Constructor<T> defaultConstructor = clazz.getDeclaredConstructor(); defaultConstructor.setAccessible(true); return defaultConstructor.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new HibersapException(MSG_CAN_NOT_CREATE_INSTANCE + clazz.getName(), e); } catch (NoSuchMethodException e) { throw new HibersapException("Class does not have a default constructor: " + clazz.getName(), e); } catch (InvocationTargetException e) { throw new HibersapException("Default constructor threw an exception: " + clazz.getName(), e); } } private  ReflectionHelper(); static Class<?> getClassForName(final String className); static Class<?> getArrayType(final Class<?> type); static Field getDeclaredField(final Object bean, final String fieldName); static Object getFieldValue(final Object bean, final String fieldName); static Class<?> getGenericType(final Field field); static T newCollectionInstance(final Class<T> clazz); static T newInstance(final Class<T> clazz); static T newInstance(final String fullyQualifiedClassName, final Class<T> superType); static List<T> createInstances(final List<String> fullyQualifiedClassNames, final Class<T> superType); static void setFieldValue(final Object bean, final String fieldName, final Object value); static Set<Field> getDeclaredFieldsWithAnnotationRecursively(final Class<?> clazz,
                                                                        final Class<? extends Annotation> annotationClass); static T[] newArrayFromCollection(final Collection<?> collection, final Class<T> elementType); }
ReflectionHelper { public static <T> T newInstance(final Class<T> clazz) { try { Constructor<T> defaultConstructor = clazz.getDeclaredConstructor(); defaultConstructor.setAccessible(true); return defaultConstructor.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new HibersapException(MSG_CAN_NOT_CREATE_INSTANCE + clazz.getName(), e); } catch (NoSuchMethodException e) { throw new HibersapException("Class does not have a default constructor: " + clazz.getName(), e); } catch (InvocationTargetException e) { throw new HibersapException("Default constructor threw an exception: " + clazz.getName(), e); } } private  ReflectionHelper(); static Class<?> getClassForName(final String className); static Class<?> getArrayType(final Class<?> type); static Field getDeclaredField(final Object bean, final String fieldName); static Object getFieldValue(final Object bean, final String fieldName); static Class<?> getGenericType(final Field field); static T newCollectionInstance(final Class<T> clazz); static T newInstance(final Class<T> clazz); static T newInstance(final String fullyQualifiedClassName, final Class<T> superType); static List<T> createInstances(final List<String> fullyQualifiedClassNames, final Class<T> superType); static void setFieldValue(final Object bean, final String fieldName, final Object value); static Set<Field> getDeclaredFieldsWithAnnotationRecursively(final Class<?> clazz,
                                                                        final Class<? extends Annotation> annotationClass); static T[] newArrayFromCollection(final Collection<?> collection, final Class<T> elementType);  }