Topic
  • No replies
amishera
amishera
1 Post

Pinned topic How does this thing work?

‏2008-06-30T08:30:00Z |
Hi,
Reading the following article I got some questions:
http://www.ibm.com/developerworks/java/library/j-jtp08305.html

The article mentions that:
"Because proxy classes do not have accessible names, they cannot have constructors, so they must instead be created by factories."

But in an example later, the author has done this:

If you wrap a HashSet with a logging proxy and execute the following simple test program:

Set s = newLoggingProxy(Set.class, new HashSet());
s.add("three");
if (!s.contains("four"))
s.add("four");
System.out.println(s);

It seems to create proxy using constructor only. Where is the code that generates proxy using factory method (where is the call of Proxy.newInstance())?

Moreover, in another example later the author has done something like this:

public class JavaBeanProxyFactory {

...

public static<T> T getProxy(Class<T> intf,
Map<String, Object> values) {
return (T) Proxy.newProxyInstance
(JavaBeanProxyFactory.class.getClassLoader(),
new Class[] { intf }, new JavaBeanProxy(values));
}
}

While in a previous example the author did this:

public class SetProxyFactory {

public static Set getSetProxy(final Set s) {
return (Set) Proxy.newProxyInstance
(s.getClass().getClassLoader(),
new Class[] { Set.class },
new InvocationHandler() {
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
return method.invoke(s, args);
}
});
}
}

Now the question is which classloader should we use in the newProxyInstance method? ProxyFactory's classloader or the classloader of the class to be proxy-ed?

Thanks in advance.