package translations; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.Map; public class MessagePoolFactory { private Class messageModuleClass; private Map> translations = new HashMap>(); public MessagePoolFactory(Class messagePoolInterface) { if (!messagePoolInterface.isInterface()){ throw new IllegalArgumentException("MessagePoolInterface must be an interface."); } this.messageModuleClass = messagePoolInterface; for (Method method : messagePoolInterface.getDeclaredMethods()){ Translations translationsAnnotation = method.getAnnotation(Translations.class); if (translationsAnnotation == null) continue; Map map = new HashMap(); for (Entry entry : translationsAnnotation.entries()){ map.put(entry.key(), entry.value()); } translations.put(method, map); } } public T getLanguageSource(final String language) { return (T) Proxy.newProxyInstance(messageModuleClass.getClassLoader(), new Class[]{messageModuleClass}, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return String.format(translations.get(method).get(language), args); } }); } }