Force an object expose an interface at runtime, through a generated proxy class. It must implement all required methods.
Requires BCEL.
UPDATED: does verification at bridge generation time.
expose(Object, Class) will search for the correct public target type to use.
UPDATED: performance hacks.
Example usage:
public class BridgeTest {
public interface CharList {
public int length();
public char charAt(int i);
}
void print(CharList l) {
for(int i=0; i
Bridge.java:
/* Make an object expose an interface at runtime */
import org.apache.bcel.generic.*;
import org.apache.bcel.Constants;
import java.lang.reflect.Method;
import java.util.*;
public abstract class Bridge
{
protected C __target; // the implementation of the interface
private final void __init(Object target) { this.__target = (C)target; }
// cache already-generated bridges
private static HashMap
> cache
= new HashMap
>();
// allow injection of classes from byte arrays
private static InjectingClassLoader loader = new InjectingClassLoader();
private static final void cacheSet(Class key1, Class key2, Class value) {
HashMap
intermediate = cache.get(key1);
if(intermediate == null)
cache.put(key1, intermediate = new HashMap
());
intermediate.put(key2, value);
}
private static final Class cacheGet(Class key1, Class key2) {
HashMap
intermediate = cache.get(key1);
if(intermediate == null)
return null;
return intermediate.get(key2);
}
// returns [ [ifaceMethods...] [fromMethods...] ]
private static Method[][] getMapping(Class from, Class iface) throws NoSuchMethodException,IllegalAccessException {
if(!iface.isInterface())
throw new IllegalArgumentException(iface.getName()+" is not an interface");
if(!java.lang.reflect.Modifier.isPublic(from.getModifiers()))
throw new IllegalAccessException(from.getName()+" is not public");
java.lang.reflect.Method[][] map = new java.lang.reflect.Method[2][];
map[0] = iface.getMethods();
map[1] = new java.lang.reflect.Method[map[0].length];
for(int i=0;i
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}