Created
September 21, 2011 15:00
-
-
Save jmchilton/1232269 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStream; | |
import java.lang.reflect.InvocationHandler; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Proxy; | |
import java.util.Properties; | |
import com.google.gwt.i18n.client.Constants; | |
public class ConstantProxies { | |
private static final ConstantInvocationHandler HANDLER = new ConstantInvocationHandler(); | |
public static <T extends Constants> T getProxy(final Class<T> forClass) { | |
@SuppressWarnings("unchecked") | |
T proxy = (T) Proxy.newProxyInstance(forClass.getClassLoader(), | |
new Class[] {forClass}, | |
HANDLER); | |
return proxy; | |
} | |
private static class ConstantInvocationHandler implements InvocationHandler { | |
public Object invoke(final Object proxy, | |
final Method method, | |
final Object[] args) throws Throwable { | |
@SuppressWarnings("unchecked") | |
final Class<? extends Constants> declaringClass = | |
(Class<? extends Constants>) method.getDeclaringClass(); | |
final String propertiesFileName = declaringClass.getSimpleName() + ".properties"; | |
final InputStream inputStream = declaringClass.getResourceAsStream(propertiesFileName); | |
final Properties properties = new Properties(); | |
properties.load(inputStream); | |
return properties.get(method.getName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment