Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Programmer > A problem about...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 52642 of 53788
Post > Topic >>

A problem about proxies

by ZelluX <zellux@[EMAIL PROTECTED] > May 10, 2008 at 09:24 PM

I'm reading the chapter about proxies on Core Java 2
The book give a TraceHandler class as an example, which will print the
name of the method to be called.

im****t java.lang.reflect.*;

public class TraceHandler implements InvocationHandler {
    private Object target;

    public TraceHandler(Object t) {
        target = t;
    }

    public Object invoke(Object proxy, Method m, Object[] args) throws
Throwable {
        System.out.println("Method " + m + "invoked.");
        return m.invoke(target, args);
    }
}

I wrote a test interface and a class whichi implements it:
public interface MyInterface {
    int factorial(int n);
    void empty();
    void printSomething();
}

public class TargetObject implements MyInterface {
    public int factorial(int n) {
        return n <= 1 ? 0 : n * factorial(n - 1);
    }

    public void empty() {
    }

    public void printSomething() {
        System.out.println("Hello world as always");
    }
}

and a test class
im****t java.lang.reflect.*;

public class Test {
    public static void main(String[] args) {
        Object target = new TargetObject();
        InvocationHandler handler = new TraceHandler(target);
        Class[] interfaces = target.getClass().getInterfaces();
        Object proxy = Proxy.newProxyInstance(null, interfaces,
handler);
    }
}

But when i ran this Test program, it throws an exception
Exception in thread "main" java.lang.IllegalArgumentException:
interface MyInterface is not visible from class loader
        at java.lang.reflect.Proxy.getProxyClass(Proxy.java:353)
        at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:581)
        at Test.main(Test.java:9)

How to deal with this problem?

many thanks
 




 5 Posts in Topic:
A problem about proxies
ZelluX <zellux@[EMAIL   2008-05-10 21:24:28 
Re: A problem about proxies
Steven Simpson <ss@[EM  2008-05-11 09:42:10 
Re: A problem about proxies
ZelluX <zellux@[EMAIL   2008-05-11 03:14:01 
Re: A problem about proxies
Roedy Green <see_websi  2008-05-11 11:56:52 
Re: A problem about proxies
Roedy Green <see_websi  2008-05-11 11:51:01 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 17:38:34 CDT 2008.