
la.serendipity.RESTy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resty Show documentation
Show all versions of resty Show documentation
Declarative REST mapper for Java.
package la.serendipity;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.ProxyFactory;
import la.serendipity.resty.core.InvocationHandlerImplementation;
public class RESTy {
public static T matelialize(final Class target) {
return matelialize("", target);
}
public static T matelialize(String urlPrefix, final Class target) {
if (Modifier.isInterface(target.getModifiers())) {
return createInstanceFromInterfaceViaJavaLangReflectProxy(target);
} else {
return createInstanceFromAbstractViaJavassist(target);
}
}
@SuppressWarnings("unchecked")
private static T createInstanceFromInterfaceViaJavaLangReflectProxy(final Class target) {
return (T) Proxy.newProxyInstance(getContextClassLoader(), new Class>[] {target},
new InvocationHandlerImplementation(target, ""));
}
private static T createInstanceFromAbstractViaJavassist(final Class target) {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setSuperclass(target);
proxyFactory.setFilter(new MethodFilter() {
@Override
public boolean isHandled(Method m) {
if (Modifier.isAbstract(m.getModifiers())) {
return true;
}
if (m.getName().equals("finalize")) {
return false;
}
return false;
}
});
@SuppressWarnings("unchecked")
Class generatedClass = proxyFactory.createClass();
final T foo;
try {
foo = generatedClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
((javassist.util.proxy.Proxy) foo).setHandler(new InvocationHandlerImplementation(target, ""));
return foo;
}
private static ClassLoader getContextClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy