Hallo,
habe folgendes Problem mit Annotationen in Verbindung mit der java.lang.reflect.Proxy -Klasse:
die Methode Proxy.newProxyInstance(...) liefert mir hier folgende Exception:
füge ich ein Interface hinzu und ändere die Instanzierung des Objektes folgendermassen
bekomme ich war keine ClassCastException mehr, allerdings liefert obj.getClass() class $Proxy0.
ich annotiere die Methode foo() um in Folge darauf zu reflektieren (z.B. DI)
ist die Methode foo() zwar gelistet,
die erwartete Exception bleibt allerdings aus...
1. Frage: warum benötige ich ein Interface, warum reicht die Implementierung nicht aus?
2. Frage: Was mach ich hier falsch, dass die Annotation nicht gefunden wird, wenn ich das
Object hinter einem Proxy verstecke?
Danke und mfG,
Christian
habe folgendes Problem mit Annotationen in Verbindung mit der java.lang.reflect.Proxy -Klasse:
Java:
public class ProxyTest implements InvocationHandler
{
private Object obj;
private ProxyTest(Object obj) {
this.obj = obj;
}
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(obj, args);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static public class AnyClassImpl
{
public void foo() {
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public static void main(String[] args) throws Exception
{
AnyClassImpl obj = new AnyClassImpl();
obj = (AnyClassImpl) Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),
new ProxyTest(obj));
}
}
die Methode Proxy.newProxyInstance(...) liefert mir hier folgende Exception:
Code:
Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to ioc.ProxyTest$AnyClassImpl
füge ich ein Interface hinzu und ändere die Instanzierung des Objektes folgendermassen
Java:
public class ProxyTest implements InvocationHandler
{
private Object obj;
private ProxyTest(Object obj) {
this.obj = obj;
}
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(obj, args);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public interface AnyClass
{
void foo();
}
static public class AnyClassImpl implements AnyClass
{
public void foo() {
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public static void main(String[] args) throws Exception
{
AnyClass obj = new AnyClassImpl();
obj = (AnyClass) Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),
new ProxyTest(obj));
System.err.println(obj.getClass());
}
}
bekomme ich war keine ClassCastException mehr, allerdings liefert obj.getClass() class $Proxy0.
ich annotiere die Methode foo() um in Folge darauf zu reflektieren (z.B. DI)
Java:
...
static public class AnyClassImpl implements AnyClass
{
@AnyAnnotation
public void foo() {
}
}
...
final Class clazz = obj.getClass();
for(Method m : clazz.getDeclaredMethods()) {
System.err.println(m.getName());
if(m.isAnnotationPresent(AnyAnnotation.class)) {
throw new Exception("AnyAnnotation present");
}
}
...
Code:
foo
equals
toString
hashCode
die erwartete Exception bleibt allerdings aus...
1. Frage: warum benötige ich ein Interface, warum reicht die Implementierung nicht aus?
2. Frage: Was mach ich hier falsch, dass die Annotation nicht gefunden wird, wenn ich das
Object hinter einem Proxy verstecke?
Danke und mfG,
Christian