@Test(expected = IllegalArgumentException.class) public void createTestNoConstructor() throws Exception { StringValueObjectFactory.create(Object.class, ""); }
public static <T> StringValueObjectFactory<T> create(final Class<T> clazz, final String templateString) { final Constructor<T> stringConstructor; try { stringConstructor = clazz.getConstructor(String.class); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a String constructor", clazz), e); } final T template; try { template = stringConstructor.newInstance(templateString); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { throw new IllegalArgumentException(String.format("Failed to instantiate template %s for '%s'", clazz, templateString), e); } final Constructor<T> copyConstructor; try { copyConstructor = clazz.getConstructor(clazz); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a copy constructor", clazz), e); } final Field f = findValueField(clazz); final StringValueObjectFactory<T> ret; try { ret = new StringValueObjectFactory<>(template, LOOKUP.unreflectConstructor(copyConstructor).asType(CONSTRUCTOR_METHOD_TYPE), LOOKUP.unreflectSetter(f).asType(SETTER_METHOD_TYPE)); } catch (IllegalAccessException e) { throw new IllegalStateException("Failed to instantiate method handles", e); } final Throwable t = new Throwable("Invocation stack"); t.fillInStackTrace(); if (matchesPackage(clazz.getPackage().getName(), t.getStackTrace())) { LOG.info("Instantiated factory for {}", clazz); } else { LOG.warn("Instantiated factory for {} outside its package", clazz, t); } return ret; }
StringValueObjectFactory { public static <T> StringValueObjectFactory<T> create(final Class<T> clazz, final String templateString) { final Constructor<T> stringConstructor; try { stringConstructor = clazz.getConstructor(String.class); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a String constructor", clazz), e); } final T template; try { template = stringConstructor.newInstance(templateString); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { throw new IllegalArgumentException(String.format("Failed to instantiate template %s for '%s'", clazz, templateString), e); } final Constructor<T> copyConstructor; try { copyConstructor = clazz.getConstructor(clazz); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a copy constructor", clazz), e); } final Field f = findValueField(clazz); final StringValueObjectFactory<T> ret; try { ret = new StringValueObjectFactory<>(template, LOOKUP.unreflectConstructor(copyConstructor).asType(CONSTRUCTOR_METHOD_TYPE), LOOKUP.unreflectSetter(f).asType(SETTER_METHOD_TYPE)); } catch (IllegalAccessException e) { throw new IllegalStateException("Failed to instantiate method handles", e); } final Throwable t = new Throwable("Invocation stack"); t.fillInStackTrace(); if (matchesPackage(clazz.getPackage().getName(), t.getStackTrace())) { LOG.info("Instantiated factory for {}", clazz); } else { LOG.warn("Instantiated factory for {} outside its package", clazz, t); } return ret; } }
StringValueObjectFactory { public static <T> StringValueObjectFactory<T> create(final Class<T> clazz, final String templateString) { final Constructor<T> stringConstructor; try { stringConstructor = clazz.getConstructor(String.class); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a String constructor", clazz), e); } final T template; try { template = stringConstructor.newInstance(templateString); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { throw new IllegalArgumentException(String.format("Failed to instantiate template %s for '%s'", clazz, templateString), e); } final Constructor<T> copyConstructor; try { copyConstructor = clazz.getConstructor(clazz); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a copy constructor", clazz), e); } final Field f = findValueField(clazz); final StringValueObjectFactory<T> ret; try { ret = new StringValueObjectFactory<>(template, LOOKUP.unreflectConstructor(copyConstructor).asType(CONSTRUCTOR_METHOD_TYPE), LOOKUP.unreflectSetter(f).asType(SETTER_METHOD_TYPE)); } catch (IllegalAccessException e) { throw new IllegalStateException("Failed to instantiate method handles", e); } final Throwable t = new Throwable("Invocation stack"); t.fillInStackTrace(); if (matchesPackage(clazz.getPackage().getName(), t.getStackTrace())) { LOG.info("Instantiated factory for {}", clazz); } else { LOG.warn("Instantiated factory for {} outside its package", clazz, t); } return ret; } private  StringValueObjectFactory(final T template, final MethodHandle constructor, final MethodHandle setter); }
StringValueObjectFactory { public static <T> StringValueObjectFactory<T> create(final Class<T> clazz, final String templateString) { final Constructor<T> stringConstructor; try { stringConstructor = clazz.getConstructor(String.class); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a String constructor", clazz), e); } final T template; try { template = stringConstructor.newInstance(templateString); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { throw new IllegalArgumentException(String.format("Failed to instantiate template %s for '%s'", clazz, templateString), e); } final Constructor<T> copyConstructor; try { copyConstructor = clazz.getConstructor(clazz); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a copy constructor", clazz), e); } final Field f = findValueField(clazz); final StringValueObjectFactory<T> ret; try { ret = new StringValueObjectFactory<>(template, LOOKUP.unreflectConstructor(copyConstructor).asType(CONSTRUCTOR_METHOD_TYPE), LOOKUP.unreflectSetter(f).asType(SETTER_METHOD_TYPE)); } catch (IllegalAccessException e) { throw new IllegalStateException("Failed to instantiate method handles", e); } final Throwable t = new Throwable("Invocation stack"); t.fillInStackTrace(); if (matchesPackage(clazz.getPackage().getName(), t.getStackTrace())) { LOG.info("Instantiated factory for {}", clazz); } else { LOG.warn("Instantiated factory for {} outside its package", clazz, t); } return ret; } private  StringValueObjectFactory(final T template, final MethodHandle constructor, final MethodHandle setter); static StringValueObjectFactory<T> create(final Class<T> clazz, final String templateString); @SuppressWarnings("checkstyle:illegalCatch") T newInstance(final String string); T getTemplate(); }
StringValueObjectFactory { public static <T> StringValueObjectFactory<T> create(final Class<T> clazz, final String templateString) { final Constructor<T> stringConstructor; try { stringConstructor = clazz.getConstructor(String.class); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a String constructor", clazz), e); } final T template; try { template = stringConstructor.newInstance(templateString); } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { throw new IllegalArgumentException(String.format("Failed to instantiate template %s for '%s'", clazz, templateString), e); } final Constructor<T> copyConstructor; try { copyConstructor = clazz.getConstructor(clazz); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format("%s does not have a copy constructor", clazz), e); } final Field f = findValueField(clazz); final StringValueObjectFactory<T> ret; try { ret = new StringValueObjectFactory<>(template, LOOKUP.unreflectConstructor(copyConstructor).asType(CONSTRUCTOR_METHOD_TYPE), LOOKUP.unreflectSetter(f).asType(SETTER_METHOD_TYPE)); } catch (IllegalAccessException e) { throw new IllegalStateException("Failed to instantiate method handles", e); } final Throwable t = new Throwable("Invocation stack"); t.fillInStackTrace(); if (matchesPackage(clazz.getPackage().getName(), t.getStackTrace())) { LOG.info("Instantiated factory for {}", clazz); } else { LOG.warn("Instantiated factory for {} outside its package", clazz, t); } return ret; } private  StringValueObjectFactory(final T template, final MethodHandle constructor, final MethodHandle setter); static StringValueObjectFactory<T> create(final Class<T> clazz, final String templateString); @SuppressWarnings("checkstyle:illegalCatch") T newInstance(final String string); T getTemplate();  }