use of org.apache.aries.proxy.FinalModifierException in project aries by apache.
the class InterfaceProxyGenerator method getProxyInstance.
/**
* Generate a new proxy instance implementing the supplied interfaces and using the supplied
* dispatcher and listener
* @param client the bundle that is trying to generate this proxy (can be null)
* @param superclass The superclass to use (or null for Object)
* @param ifaces The set of interfaces to implement (may be empty if superclass is non null)
* @param dispatcher
* @param listener
* @return
* @throws UnableToProxyException
*/
public static Object getProxyInstance(Bundle client, Class<?> superclass, Collection<Class<?>> ifaces, Callable<Object> dispatcher, InvocationListener listener) throws UnableToProxyException {
if (superclass != null && (superclass.getModifiers() & Modifier.FINAL) != 0)
throw new FinalModifierException(superclass);
ProxyClassLoader pcl = null;
SortedSet<Class<?>> interfaces = createSet(ifaces);
synchronized (cache) {
BundleWiring wiring = client == null ? null : (BundleWiring) client.adapt(BundleWiring.class);
WeakReference<ProxyClassLoader> ref = cache.get(wiring);
if (ref != null)
pcl = ref.get();
if (pcl != null && pcl.isInvalid(interfaces)) {
pcl = null;
cache.remove(wiring);
}
if (pcl == null) {
pcl = new ProxyClassLoader(client);
cache.put(wiring, new WeakReference<ProxyClassLoader>(pcl));
}
}
Class<?> c = pcl.createProxyClass(superclass, interfaces);
try {
Constructor<?> con = c.getDeclaredConstructor(Callable.class, InvocationListener.class);
con.setAccessible(true);
return con.newInstance(dispatcher, listener);
} catch (Exception e) {
throw new UnableToProxyException(ifaces.iterator().next(), e);
}
}
use of org.apache.aries.proxy.FinalModifierException in project aries by apache.
the class BasicProxyTest method checkProxydefaultMethodInterface.
/**
* This method checks that we correctly proxy an interface with default methods on java 8
*/
@Test
public void checkProxydefaultMethodInterface() throws UnableToProxyException {
Bundle b = bundleContext.getBundle();
Callable<Object> c = new TestCallable();
Collection<Class<?>> classes = new ArrayList<Class<?>>();
// proxy an interface with a default methods (on Java 8).
classes.add(java.lang.CharSequence.class);
try {
mgr.createDelegatingProxy(b, classes, c, null);
} catch (FinalModifierException e) {
String msg = e.getMessage();
assertEquals("The message didn't look right", "The class " + TestCallable.class.getName() + " is final.", msg);
assertTrue("The message didn't appear in the toString", e.toString().endsWith(msg));
}
}
Aggregations