Search in sources :

Example 6 with FinalModifierException

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);
    }
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) FinalModifierException(org.apache.aries.proxy.FinalModifierException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) FinalModifierException(org.apache.aries.proxy.FinalModifierException)

Example 7 with FinalModifierException

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));
    }
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) FinalModifierException(org.apache.aries.proxy.FinalModifierException) Test(org.junit.Test)

Aggregations

FinalModifierException (org.apache.aries.proxy.FinalModifierException)7 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)3 Bundle (org.osgi.framework.Bundle)3 UnableToProxyException (org.apache.aries.proxy.UnableToProxyException)2 Method (java.lang.reflect.Method)1 MethodVisitor (org.objectweb.asm.MethodVisitor)1 Type (org.objectweb.asm.Type)1 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)1 Method (org.objectweb.asm.commons.Method)1 BundleWiring (org.osgi.framework.wiring.BundleWiring)1