Java reflection: implement interface and pass to another class - java

I'm not sure if this is possible with Java, but I'm trying to implement an interface that's unavailable at compile time** and pass it to another class as an object of that interface. Let's say I have an interface like:
public interface MyInterface {
void onReceive(int i);
}
and another class like:
public void MyClass {
ArrayList<MyInterface> listenerList = new ArrayList<MyInterface>();
public void add(MyInterface m) {
listenerList.add(m);
}
}
If they were available at compile time, I would be using them like:
blah = new MyInterface() {
public void onReceive(int i) {
System.out.println("BLAH");
}
}
MyClass mc = new MyClass();
myClass.add(blah);
I'm wondering if there is a way to write code that does the same as above if the first two classes are only available at runtime.
Thanks in advance!
**I'm trying to use a framework library from Android's ROM, but it is in dalvik bytecode so I can't use it for compilation.
UPDATE: Here's some sample code I used to test the solution:
File a/IIMSListener.java
// Excerpt from decompiled class
public interface IIMSListener
{
void onReceive(int p0, int p1/*, IMSParameter p2*/);
}
File a/IMSRemoteListenerStub.java
// Excerpt from decompiled class
import java.util.concurrent.*;
import java.util.*;
public class IMSRemoteListenerStub
{
public List<IIMSListener> mListenerList = new CopyOnWriteArrayList<IIMSListener>();
public boolean addListener(final IIMSListener iimsListener) {
if (iimsListener != null && !this.mListenerList.contains(iimsListener)) {
this.mListenerList.add(iimsListener);
return true;
}
return false;
}
public boolean removeListener(final IIMSListener iimsListener) {
if (iimsListener != null && this.mListenerList.contains(iimsListener)) {
this.mListenerList.remove(iimsListener);
return true;
}
return false;
}
}
File b/test.java
import java.lang.reflect.;
import java.util.;
public class test {
public static void main(String[] args) throws IllegalAccessException,
IllegalArgumentException,
InvocationTargetException,
NoSuchMethodException,
SecurityException,
ClassNotFoundException {
// Implement interface
Class<?> IIMSListener = Class.forName("IIMSListener");
Object listenerInstance = Proxy.newProxyInstance(IIMSListener.getClassLoader(), new Class<?>[]{IIMSListener}, new InvocationHandler() {
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().equals("onReceive")){
System.out.println("ARGS: " + (Integer)args[0] + ", " + (Integer)args[1]);
return 1;
}
else return -1;
}
});
// Test
Method onReceive = listenerInstance.getClass().getDeclaredMethod("onReceive", new Class[] { int.class, int.class });
onReceive.invoke(listenerInstance, new Object[] { 1, 2 });
try {
// Pass to another class
Class IMSRemoteListenerStub = Class.forName("IMSRemoteListenerStub");
Constructor ctor = IMSRemoteListenerStub.getConstructor();
Object stubInstance = ctor.newInstance(new Object[] {});
Method addListener = stubInstance.getClass().getDeclaredMethod("addListener", new Class[] { IIMSListener });
addListener.invoke(stubInstance, new Object[] { listenerInstance });
// Test
Field mListenerList = IMSRemoteListenerStub.getField("mListenerList");
List<?> list = (List<?>)mListenerList.get(stubInstance);
onReceive.invoke(list.get(0), new Object[] { 3, 4 });
}
catch (InstantiationException e) {}
catch (NoSuchFieldException e) {}
}
}
Execution:
$ cd b
$ CLASSPATH=".:../a" java test
ARGS: 1, 2
ARGS: 3, 4

If it is going to be same interface then use Dynamic Proxies
//Loading the class at runtime
public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException {
Class<?> someInterface = Class.forName("SomeInterface");
Object instance = Proxy.newProxyInstance(someInterface.getClassLoader(), new Class<?>[]{someInterface}, new InvocationHandler() {
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//Handle the invocations
if(method.getName().equals("someMethod")){
return 1;
}
else return -1;
}
});
System.out.println(instance.getClass().getDeclaredMethod("someMethod", (Class<?>[])null).invoke(instance, new Object[]{}));
}

Related

I am having trouble in using jdk dynamic proxy

Before using spring aop and cglib, Now I replaced a simple example.I found that executing method sayHello1 () and sayHello2 () both output "before" and "after" Oh my god, it's very difficult, Do you understand what I am talking about? I am going crazy now. T.T
public interface HelloWorld {
void sayHello1(String say);
void sayHello2(String say);
}
public static class HelloWorldImpl implements HelloWorld {
#Override
public void sayHello1(String say) { System.out.println(say); }
#Override
public void sayHello2(String say) { System.out.println(say); }
}
public static class Invocation implements InvocationHandler {
private final Object target;
public Invocation(Object target) { this.target = target; }
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before..."); // TODO method before
Object object = method.invoke(target, args);
System.out.println("after..."); // TODO method after
return object;
}
}
public static void main(String[] args) {
HelloWorld helloWorld = (HelloWorld) Proxy.newProxyInstance(
ClassLoader.getSystemClassLoader(),
new Class[] { HelloWorld.class },
new Invocation(new HelloWorldImpl())
);
helloWorld.sayHello1("Hello World1 ...");
helloWorld.sayHello2("Hello World2 ...");
}
You mean you want something like this?
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Instrumenting " + method);
Object result;
switch (method.getName()) {
case "sayHello1":
System.out.println("before A");
result = method.invoke(target, args);
System.out.println("after A");
break;
case "sayHello2":
System.out.println("before B");
// Let's change the argument just for fun
args[0] = "changed argument";
result = method.invoke(target, args);
System.out.println("after B");
break;
default:
result = method.invoke(target, args);
}
return result;
}
That would yield the following console log:
Instrumenting public abstract void de.scrum_master.spring.q62001911.HelloWorld.sayHello1(java.lang.String)
before A
Hello World1 ...
after A
Instrumenting public abstract void de.scrum_master.spring.q62001911.HelloWorld.sayHello2(java.lang.String)
before B
changed argument
after B
Of course you could print further information or differentiate overloaded methods with the same names by parameter types. Try things like
method.getParameterTypes();
method.getParameterCount();
method.getReturnType();
Is this tedious? Yeah, it is, but still straightforward. And it being tedious is the reason why AspectJ or Spring AOP with their elegant pointcut + advice model are so much easier to use because they did the work already and hide the inner complexity from you.

Creating a parser of Class name + String value to a typed value

I am trying to write a method that can take in a String classname and a String value, and return the value represented as that String.
Example inputs:
parse("java.lang.String", "abc") -> String "ABC"
parse("java.lang.Boolean", "FALSE") -> Boolean FALSE
parse("java.lang.Integer", "123") -> Integer 123
parse("com.me.Color", "RED") -> enum Color.RED
I have found that if I use an if block containing assignableFrom calls, I can achieve this. But would prefer writing something more extendable, so it isn't as difficult to add a new parser tomorrow.
This is what I have now:
String stringClassName = //stringified full class name
String value = //value to parse
Class<?> fieldType = Class.forName(stringClassName)
if (fieldType.isAssignableFrom(String.class)) {
return value;
} else if (fieldType.isAssignableFrom(Boolean.class)) {
return Util.toBoolean(value);
} else if (fieldType.isEnum()) {
return Util.toEnum(fieldType, value);
} else {
// throw exception
}
There are multiple ways to do this. For example:
You could have an interface called Parser
package example;
public interface Parser {
boolean canParse(String fullQualifiedClassName);
Object parse(String fullQualifiedClassName, String value) throws ParseException;
class ParseException extends Exception {
public ParseException(String msg) {
super(msg);
}
public ParseException(Exception cause) {
super(cause);
}
}
}
And all your Default-Implementations in an Enum or statically defined in another way:
package example;
public enum DefaultParser implements Parser {
STRING {
#Override
public boolean canParse(String fullQualifiedClassName) {
return isClassAssignableFromClassName(fullQualifiedClassName, String.class);
}
#Override
public Object parse(String fullQualifiedClassName, String value) throws ParseException {
return value;
}
},
ENUM {
#Override
public boolean canParse(String fullQualifiedClassName) {
return isClassAssignableFromClassName(fullQualifiedClassName, Enum.class);
}
#Override
public Object parse(String fullQualifiedClassName, String value) throws ParseException {
final Class<? extends Enum> clazz;
try {
clazz = (Class<? extends Enum>) Class.forName(fullQualifiedClassName);
} catch (ClassNotFoundException e) {
throw new ParseException(e);
}
return Enum.valueOf(clazz, value);
}
},
BOOLEAN {
#Override
public boolean canParse(String fullQualifiedClassName) {
return isClassAssignableFromClassName(fullQualifiedClassName, Boolean.class);
}
#Override
public Object parse(String fullQualifiedClassName, String value) throws ParseException {
return value.toLowerCase().equals("true");
}
};
private static boolean isClassAssignableFromClassName(String fullQualifiedClassName, Class<?> clazz) {
try {
return clazz.isAssignableFrom(Class.forName(fullQualifiedClassName));
} catch (ClassNotFoundException e) {
return false;
}
}
}
And a ParentParser Implementation that combines multiple Parsers into one:
package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class ParentParser implements Parser {
private final List<Parser> parsers;
public ParentParser() {
this.parsers = new ArrayList<>();
this.parsers.addAll(Arrays.asList(DefaultParser.values()));
}
public void register(Parser parser) {
this.parsers.add(parser);
}
#Override
public boolean canParse(String fullQualifiedClassName) {
return findParser(fullQualifiedClassName).isPresent();
}
#Override
public Object parse(String fullQualifiedClassName, String value) throws ParseException {
return findParser(fullQualifiedClassName)
.orElseThrow(() -> new ParseException("no registered parser found for class=" + fullQualifiedClassName))
.parse(fullQualifiedClassName, value);
}
private Optional<Parser> findParser(String fullQualifiedClassName) {
return this.parsers.stream().filter(parser -> parser.canParse(fullQualifiedClassName)).findAny();
}
}
Which you can then use like this:
package example;
import example.Parser.ParseException;
public class Example {
public static void main(String[] args) throws ParseException {
final ParentParser parser = new ParentParser();
System.out.println(parser.parse("java.lang.String", "hello world"));
System.out.println(parser.parse("java.lang.Boolean", "true"));
System.out.println(parser.parse("java.time.DayOfWeek", "TUESDAY"));
}
}
And you could add more parsers, for example a parser using Jackson (JSON):
package example;
import com.fasterxml.jackson.databind.ObjectMapper;
import example.Parser.ParseException;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws ParseException {
final ParentParser parser = new ParentParser();
System.out.println(parser.parse("java.lang.String", "hello world"));
System.out.println(parser.parse("java.lang.Boolean", "true"));
System.out.println(parser.parse("java.time.DayOfWeek", "TUESDAY"));
parser.register(new JacksonParser());
System.out.println(parser.parse("java.util.Map", "{\"key\":\"value\"}"));
}
private static class JacksonParser implements Parser {
private static final ObjectMapper MAPPER = new ObjectMapper();
#Override
public boolean canParse(String fullQualifiedClassName) {
final Class<?> clazz;
try {
clazz = Class.forName(fullQualifiedClassName);
} catch (ClassNotFoundException e) {
return false;
}
return MAPPER.canDeserialize(MAPPER.constructType(clazz));
}
#Override
public Object parse(String fullQualifiedClassName, String value) throws ParseException {
try {
return MAPPER.readValue(value, Class.forName(fullQualifiedClassName));
} catch (ClassNotFoundException | IOException e) {
throw new ParseException(e);
}
}
}
}
Note that this can of course be optimized depending on your needs.
If your Parser-Implementations can only parse a static List of Types and there is only one Parser-Implementation per Class, you should change the List<Parser> to Map<Class<?>, Parser> and change the register-Method to register(Class<?> clazz, Parser parser) for example
You can write a generic solution using reflection apis in java.
That would reduce a lot amount of code and would be more extensible.
Also not there is a separate processing required for enum types.
I have covered the basic cases in the code shown below.
public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException {
Object instance1 = parse("java.lang.String", "abc", false);
Object instance2 = parse("java.lang.Boolean", "FALSE", false);
Object instance3 = parse("java.lang.Integer", "123", false);
Object instance4 = parse("com.me.Color", "RED", true);
}
private static Object parse(String className, String argument, boolean isEnum) throws NoSuchMethodException, ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
if (isEnum) {
Object value = Enum.valueOf((Class<? extends Enum>) Class.forName(className), argument);
//System.out.println(value);
return value;
} else {
return parse(className, new Object[]{argument}, isEnum);
}
}
private static Object parse(String className, Object[] arguments, boolean isEnum) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> clazz = Class.forName(className);
Constructor<?> ctor = clazz.getConstructor(String.class);
Object object = ctor.newInstance(arguments);
//System.out.println(object);
return object;
}

How to create chain of dynamic proxies?

I create two InvocationHandler, one for logging purpose and the other one for measuring time. Each on works but I do not know how to create a chain of these two, so that both will be executed. I thought it would be enough that for example the LoggingInvocationHandler extends the TimerInvocationHandler
public class DynamicProxyMain {
public static void main(String[] args) {
System.out.println("Starting dynamic proxy sample");
SubjectInterface timerProxy = (SubjectInterface) Proxy.newProxyInstance(SubjectInterface.class.getClassLoader(),
new Class<?>[]{SubjectInterface.class},
new TimerInvocationHandler(new SubjectInterfaceImpl()));
SubjectInterface logginProxy = (SubjectInterface) Proxy.newProxyInstance(SubjectInterface.class.getClassLoader(),
new Class<?>[]{SubjectInterface.class},
new LoggingInvocationHandler(new SubjectInterfaceImpl()));
timerProxy.methodA("a");
timerProxy.methodB("test b");
timerProxy.methodC(1, "test c");
}
}
public class LoggingInvocationHandler implements InvocationHandler {
Object impl;
String CLASSNAME = this.getClass().getCanonicalName();
public LoggingInvocationHandler(Object impl){
this.impl = impl;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object retVal;
System.out.println("LoggingHandler:" + this.getClass().getName() + " has been called");
retVal = method.invoke(impl, args);
System.out.println("LoggingHandler:" + this.getClass().getName() + " has ended");
return retVal;
}
}
public class TimerInvocationHandler extends LoggingInvocationHandler implements InvocationHandler{
private Object impl;
public TimerInvocationHandler(Object impl) {
super(impl);
this.impl = impl;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object retVal = null;
System.out.println("getting duration time for method " + method.getName());
long duration = -System.currentTimeMillis();
retVal = super.invoke(proxy,method,args);
duration += System.currentTimeMillis();
System.out.println("it took " + duration + " milliseconds");
System.out.println("duration time handler has ended");
return retVal;
}
}
Actually I solved it, so that both InvocationHandlers will be called. I edited my post with the currently working code
The idea bears a similarity to Intercepting Filter, I'll give your an implementation of it, which slightly modified in order to work with DynamicProxyHandler, if you're interested and want more details, you should read the link thoroughly.
Participants:
InvocationChain - which is responsible for dispatching invocations.
Invocation - where you should put things like logging and timer.
DynamicProxyHanlder - that simply delegates the request to InvocationChain.
Implementaiton:
DynamicProxyHandler.java
public class DynamicProxyHandler implements InvocationHandler {
private Object proxied;
InvocationChain chain = new InvocationChainImp();
DynamicProxyHandler(Object proxied) {
this.proxied = proxied;
}
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return chain.invoke(proxied, method, args);
}
}
Invocation.java
public interface Invocation {
Object invoke(Object callee, Method method, Object[] args, InvocationChain chain);
}
InvocationChain.java
public interface InvocationChain {
public Object invoke(Object callee, Method method, Object[] args);
}
InvocationChainImp.java
public class InvocationChainImp implements InvocationChain {
List<Invocation> list = new ArrayList<>();
Object result;
Iterator<Invocation> tasks;
InvocationChainImp() {
list.add(new LoggingInvocation());
list.add(new TimerInvocation());
list.add(new FinalInvocation());
tasks = list.iterator();
}
#Override
public Object invoke(Object callee, Method method, Object[] args) {
if (tasks.hasNext()) {
Object result = tasks.next().invoke(callee, method, args, this);
this.result = (this.result == null ? result : this.result);
}
return this.result;
}
Last not least, we want to define some custom classes that must be confined to Invocation interface for logging, timer, etc.
LoggingInvocation.java
public class LoggingInvocation implements Invocation {
#Override
public Object invoke(Object callee, Method method, Object[] args, InvocationChain chain) {
chain.invoke(callee, method, args);
Logger.getLogger(this.getClass().getCanonicalName()).info(method.getName() + "() execution logged!");
return null;
}
}
TimerInvocation.java
public class TimerInvocation implements Invocation {
#Override
public Object invoke(Object callee, Method method, Object[] args, InvocationChain chain) {
long start_time = System.nanoTime();
chain.invoke(callee, method, args);
long end_time = System.nanoTime();
System.out.println("Timer: excution took " + (end_time - start_time) / 1e6 + "ms");
return null;
}
}
FinalInvocation.java where the request is finally invoked on the proxied instance.
public class FinalInvocation implements Invocation {
#Override
public Object invoke(Object callee, Method method, Object[] args, InvocationChain chain) {
try {
return method.invoke(callee, args);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
}
Rest of code is trivial, as it's just used to prove that the implementation works.
You can stop reading now if want to write your own.
SubjectInterface.java
public interface SubjectInterface {
String hello();
}
SubjectInterfaceImp.java
public class SubjectInterfaceImp implements SubjectInterface {
#Override
public String hello() {
System.out.println("in SubjectInterfaceImp: Greeting!");
return "hello";
}
}
Main.java
public class Main {
public static void main(String[] args) throws Exception {
SubjectInterface subject = (SubjectInterface) Proxy.newProxyInstance(
SubjectInterface.class.getClassLoader(),
new Class[] { SubjectInterface.class }, new DynamicProxyHandler(new SubjectInterfaceImp()));
System.out.println("in Main: subject.hello() = " + subject.hello());
}
}
Okay, we have enough of code, it's show time, let's see we got, voila!
in SubjectInterfaceImp: Greeting!
Timer: excution took 0.532198ms
九月 02, 2016 12:37:36 下午 LoggingInvocation invoke
信息: hello() execution logged!
in Main: subject.hello() = hello
This is not the natural way to implement it.TimerInvocationHandler has nothing to do with LoggingInvocationHandler.
define a decorator which is a InvocationHandler and wraps a another InovovationHandler
https://en.wikipedia.org/wiki/Decorator_pattern
EDIT: since one comment ask me to provide a sample implementation,following part is added, but this is the not exact decorator pattern, but i think others can understand the solution. in this case TimeInvocationHandler is not limited to measure the login time
public class TimerInvocationHandler implements InvocationHandler
{
protected InvocationHandler invocationHandler;
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
//do whatever you want
Object result = invocationHandler.invoke(proxy, method, args);
// do what ever you want
return result;
}
}

Create an exception-safe wrapper of a class

I have a legacy class C1, implementing interface I, that may throw some exceptions.
I want to create a class C2, also implementing interface I, that is based on an instance of C1, but catches all exceptions and does something useful about them.
Currently my implementation looks like this:
class C2 implements I {
C1 base;
#Override void func1() {
try {
base.func1();
} catch (Exception e) {
doSomething(e);
}
}
#Override void func2() {
try {
base.func2();
} catch (Exception e) {
doSomething(e);
}
}
...
}
(Note: I could also make C2 extend C1. This does not matter for the current question).
The interface contains many functions, so I have to write the same try... catch block again and again.
Is there a way to reduce the amount of code duplication here?
You can make a Proxy, it could actually be generic
interface I1 {
void test();
}
class C1 implements I1 {
public void test() {
System.out.println("test");
throw new RuntimeException();
}
}
class ExceptionHandler implements InvocationHandler {
Object obj;
ExceptionHandler(Object obj) {
this.obj = obj;
}
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
return method.invoke(obj, args);
} catch (Exception e) {
// need a workaround for primitive return types
return null;
}
}
static <T> T proxyFor(Object obj, Class<T> i) {
return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader(), new Class[] { i },
new ExceptionHandler(obj));
}
}
public class Test2 {
public static void main(String[] args) throws Exception {
I1 i1 = ExceptionHandler.proxyFor(new C1(), I1.class);
i1.test();
}
}

How to build a function on the fly in java?

I'm parsing a text file that is being mapped to some java code like such:
public void eval(Node arg)
{
if(arg.data.equals("rand"))
{
moveRandomly();
}
else if(arg.data.equals("home"))
{
goHome();
}
else if(arg.data.equals("iffood"))
{
ifFoodHere(arg.left, arg.right);
}//snip..
This is going to need to be re-evaluated about a thousand times and I'd rather not have to traverse the whole thing every time. Is there any way to make this traversal once and then have it be a function that is called every other time?
You could make a Map of Runnables:
Map<String, Runnable> methods = new HashMap<String, Runnable>();
methods.put("rand", new Runnable()
{
public void run()
{
moveRandomly();
}
});
...
then in your method
public void eval(Node arg)
{
Runnable command = methods.get(arg.data);
command.run();
}
Create an anonymous inner class.
Something like:
public Callable<Void> eval(Node arg)
{
if(arg.data.equals("rand"))
{
return new Callable<Void>{ public Void call() { moveRandomly(); return null; } };
}
...
}
Callable<Void> f = eval(a);
f.call();
If you know all the arguments/commands you can expect, i might do it like this:
enum Args {
home, rand, iffood;
private Method method;
private Args () {
try {
this.method = Commands.class.getMethod(this.name(), Node.class);
} catch (final Exception e) {}
}
public void invoke (final Node args)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
this.method.invoke(null, args);
}
public static Args valueOf (final Node arg) {
return valueOf(arg.data);
}
public static void eval (final Node arg)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
valueOf(arg).invoke(arg);
}
}
Command implementations are:
class Commands {
public static void home (final Node arg) {
goHome(); // Call the implementation
// or simply make these bodies the implementations.
}
public static void iffood (final Node arg) {
ifFoodHere(arg.left, arg.right);
}
public static void rand (final Node arg) {
moveRandom();
}
//...
}
your eval() then becomes, simply:
try {
Args.eval(arg);
} catch (IllegalArgumentException e) {
// Handle unknown arg.data
}

Categories