Java Reference
In-Depth Information
else
{
System.out.println("<--- return");
}
return result;
}
catch(Throwable t)
{
System.out.println("<--- exception: " + t.getMessage());
return null;
}
}
}
public class Main
{
private static void useCounter(Counter counter)
{
//wie zuvor:
...
}
public static void main(String[] args)
{
CounterImpl counterImpl = new CounterImpl();
try
{
Class<?>[] implInterfaces = new Class<?>[1];
implInterfaces[0] = Counter.class;
Class<?> dynProxyClass = Proxy.getProxyClass(
ClassLoader.getSystemClassLoader(),
implInterfaces);
Class<?>[] formalArgs = new Class<?>[1];
formalArgs[0] = InvocationHandler.class;
Constructor<?> constructor =
dynProxyClass.getConstructor(formalArgs);
Object[] actualArgs = new Object[1];
actualArgs[0] = new MyInvocationHandler(counterImpl);
Counter dynamicProxy =
(Counter) constructor.newInstance(actualArgs);
useCounter(dynamicProxy);
}
catch(Exception e)
{
System.out.println("exception: " + e.getMessage());
}
System.out.println("-------------------------");
try
{
Counter dynamicProxy = (Counter) Proxy.newProxyInstance(
ClassLoader.getSystemClassLoader(),
new Class<?>[] {Counter.class},
new MyInvocationHandler(counterImpl));
useCounter(dynamicProxy);
}
catch(Exception e)
 
Search WWH ::




Custom Search