@Test void objectMethods() { assertEquals(translate, translate); assertEquals(translate.hashCode(), translate.hashCode()); final Translate other = new InternationalizationServiceFactory(Locale::getDefault) .create(Translate.class, Translate.class.getClassLoader()); assertNotSame(translate, other); }
public <T> T create(final Class<T> api, final ClassLoader loader) { if (Mode.mode != Mode.UNSAFE) { if (!api.isInterface()) { throw new IllegalArgumentException(api + " is not an interface"); } if (Stream .of(api.getMethods()) .filter(m -> m.getDeclaringClass() != Object.class) .anyMatch(m -> m.getReturnType() != String.class)) { throw new IllegalArgumentException(api + " methods must return a String"); } if (Stream .of(api.getMethods()) .flatMap(m -> Stream.of(m.getParameters())) .anyMatch(p -> p.isAnnotationPresent(Language.class) && p.getType() != Locale.class)) { throw new IllegalArgumentException("@Language can only be used with Locales"); } } final String pck = api.getPackage().getName(); return api .cast(Proxy .newProxyInstance(loader, new Class<?>[] { api }, new InternationalizedHandler(api.getName() + '.', api.getSimpleName() + '.', (pck == null || pck.isEmpty() ? "" : (pck + '.')) + "Messages", localeSupplier))); }
InternationalizationServiceFactory { public <T> T create(final Class<T> api, final ClassLoader loader) { if (Mode.mode != Mode.UNSAFE) { if (!api.isInterface()) { throw new IllegalArgumentException(api + " is not an interface"); } if (Stream .of(api.getMethods()) .filter(m -> m.getDeclaringClass() != Object.class) .anyMatch(m -> m.getReturnType() != String.class)) { throw new IllegalArgumentException(api + " methods must return a String"); } if (Stream .of(api.getMethods()) .flatMap(m -> Stream.of(m.getParameters())) .anyMatch(p -> p.isAnnotationPresent(Language.class) && p.getType() != Locale.class)) { throw new IllegalArgumentException("@Language can only be used with Locales"); } } final String pck = api.getPackage().getName(); return api .cast(Proxy .newProxyInstance(loader, new Class<?>[] { api }, new InternationalizedHandler(api.getName() + '.', api.getSimpleName() + '.', (pck == null || pck.isEmpty() ? "" : (pck + '.')) + "Messages", localeSupplier))); } }
InternationalizationServiceFactory { public <T> T create(final Class<T> api, final ClassLoader loader) { if (Mode.mode != Mode.UNSAFE) { if (!api.isInterface()) { throw new IllegalArgumentException(api + " is not an interface"); } if (Stream .of(api.getMethods()) .filter(m -> m.getDeclaringClass() != Object.class) .anyMatch(m -> m.getReturnType() != String.class)) { throw new IllegalArgumentException(api + " methods must return a String"); } if (Stream .of(api.getMethods()) .flatMap(m -> Stream.of(m.getParameters())) .anyMatch(p -> p.isAnnotationPresent(Language.class) && p.getType() != Locale.class)) { throw new IllegalArgumentException("@Language can only be used with Locales"); } } final String pck = api.getPackage().getName(); return api .cast(Proxy .newProxyInstance(loader, new Class<?>[] { api }, new InternationalizedHandler(api.getName() + '.', api.getSimpleName() + '.', (pck == null || pck.isEmpty() ? "" : (pck + '.')) + "Messages", localeSupplier))); }  }
InternationalizationServiceFactory { public <T> T create(final Class<T> api, final ClassLoader loader) { if (Mode.mode != Mode.UNSAFE) { if (!api.isInterface()) { throw new IllegalArgumentException(api + " is not an interface"); } if (Stream .of(api.getMethods()) .filter(m -> m.getDeclaringClass() != Object.class) .anyMatch(m -> m.getReturnType() != String.class)) { throw new IllegalArgumentException(api + " methods must return a String"); } if (Stream .of(api.getMethods()) .flatMap(m -> Stream.of(m.getParameters())) .anyMatch(p -> p.isAnnotationPresent(Language.class) && p.getType() != Locale.class)) { throw new IllegalArgumentException("@Language can only be used with Locales"); } } final String pck = api.getPackage().getName(); return api .cast(Proxy .newProxyInstance(loader, new Class<?>[] { api }, new InternationalizedHandler(api.getName() + '.', api.getSimpleName() + '.', (pck == null || pck.isEmpty() ? "" : (pck + '.')) + "Messages", localeSupplier))); }  T create(final Class<T> api, final ClassLoader loader); }
InternationalizationServiceFactory { public <T> T create(final Class<T> api, final ClassLoader loader) { if (Mode.mode != Mode.UNSAFE) { if (!api.isInterface()) { throw new IllegalArgumentException(api + " is not an interface"); } if (Stream .of(api.getMethods()) .filter(m -> m.getDeclaringClass() != Object.class) .anyMatch(m -> m.getReturnType() != String.class)) { throw new IllegalArgumentException(api + " methods must return a String"); } if (Stream .of(api.getMethods()) .flatMap(m -> Stream.of(m.getParameters())) .anyMatch(p -> p.isAnnotationPresent(Language.class) && p.getType() != Locale.class)) { throw new IllegalArgumentException("@Language can only be used with Locales"); } } final String pck = api.getPackage().getName(); return api .cast(Proxy .newProxyInstance(loader, new Class<?>[] { api }, new InternationalizedHandler(api.getName() + '.', api.getSimpleName() + '.', (pck == null || pck.isEmpty() ? "" : (pck + '.')) + "Messages", localeSupplier))); }  T create(final Class<T> api, final ClassLoader loader);  }