Skip to content

Instantly share code, notes, and snippets.

@code-twister
Created February 2, 2018 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-twister/01b11cc4bb6dd8a2c33792488b9b9d17 to your computer and use it in GitHub Desktop.
Save code-twister/01b11cc4bb6dd8a2c33792488b9b9d17 to your computer and use it in GitHub Desktop.
An example showing how to create dynamic proxies for interfaces.
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class ProxyExample {
public static void main(String[] args) {
// Setup... this is where the magic happens...
ApiInterface service = (ApiInterface) Proxy.newProxyInstance(
ApiInterface.class.getClassLoader(),
new Class[]{ ApiInterface.class },
(proxy, method, args1) -> {
// we control what happens when the method is called (invoked), we get all the necessary information
System.out.println("invoking method "+method.getName()+" with arguments "+ Arrays.toString(args1));
// this next part is just to show how you can manipulate return value
if (args1.length > 0 && args1[0] instanceof String) {
return ((String) args1[0]).toUpperCase();
}
return null;
}
);
// CLIENT CODE we call both methods in the ApiInterface
System.out.println("returned: " + service.getSomeStringFromServer("Hello"));
service.doSomeOtherThing(4.5d);
}
interface ApiInterface {
String getSomeStringFromServer(String parameter);
void doSomeOtherThing(double otherParam);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment