Is it ok to pull out specific arguments from a Java dynamic proxy invocation?
Casting is never 'pretty'.... But, in a proxy situation like this (assuming you really need the proxy), it is possibly unavoidable.

I can recommend though that you do it defensively... you should really do:

if (args.length &gt; 0) {
    if (args[0] instanceof Token) {
        System.out.println("Token with id: "+((Token)args[0]).getId()+ " was passed");
    } else {
        System.out.println("Proxy for a method which does not have an initial token: " + args[0]);
    }
} else {
    System.out.println("Proxy for a method that is being passed no arguments.");
}


If your situation requires that these values are populated, then you should throw exceptions instead of the other println methods.

Also, you should consider checking that the number of arguments in the args matches the number of arguments expected in method.getParameterTypes(), but with var-args, that can be hard.

Additionally, this line:


throw new RuntimeException("unexpected invocation exception: " + e.getMessage());



Should incorporate the actual cause.... when you get an exception through this catch block, it will be one of those times when the cause is critical, and you are throwing the cause away! That exception should look like:

throw new RuntimeException("unexpected invocation exception: " + e.getMessage(), e);