Last active
August 29, 2015 14:13
-
-
Save pmauduit/239c188deb0264ab2149 to your computer and use it in GitHub Desktop.
Fiddling with java.lang.reflect.Proxy
This file contains hidden or 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.lang.reflect.Proxy; | |
import java.lang.reflect.InvocationHandler; | |
import java.lang.Throwable; | |
import java.lang.reflect.Method; | |
import java.lang.Object; | |
public class Main { | |
interface IA {}; | |
/* Same conditions as GeoServer: | |
interface CatalogInfo | |
interface ResourceInfo extends CatalogInfo | |
class ResourceInfoImpl implements ResourceInfo | |
interface FeatureTypeInfo extends ResourceInfo | |
abstract class DecoratingFeatureTypeInfo extends AbstractDecorator<FeatureTypeInfo> implements FeatureTypeInfo | |
class SecuredFeatureTypeInfo extends DecoratingFeatureTypeInfo | |
class FeatureTypeInfoImpl extends ResourceInfoImpl implements FeatureTypeInfo | |
Observed: SecuredFeatureTypeInfo is not recognized as FeatureTypeInfo implementation | |
*/ | |
class A implements IA, InvocationHandler { | |
public Object invoke(Object proxy, Method method, Object[] args) | |
throws Throwable { | |
System.out.println("I did something dynamically"); | |
return null; | |
} | |
}; | |
abstract class B extends A {}; | |
class C extends B {}; | |
class D extends A {}; | |
private A a; | |
private C c; | |
private D d; | |
public Main() { | |
a = new A(); | |
c = new C(); | |
d = new D(); | |
System.out.println(a instanceof IA); | |
System.out.println(c instanceof IA); | |
System.out.println(d instanceof IA); | |
IA pa = (IA) Proxy.newProxyInstance(IA.class.getClassLoader(), | |
new Class[] { IA.class }, | |
a); | |
IA pc = (IA) Proxy.newProxyInstance(IA.class.getClassLoader(), | |
new Class[] { IA.class }, | |
c); | |
IA pd = (IA) Proxy.newProxyInstance(IA.class.getClassLoader(), | |
new Class[] { IA.class }, | |
d); | |
System.out.println(pa.getClass().getName()); | |
System.out.println(pa instanceof IA); | |
System.out.println(pc instanceof IA); | |
System.out.println(pd instanceof IA); | |
} | |
public static void main(String[] args) { | |
Main m = new Main(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment