Search in sources :

Example 1 with AsmProxyManager

use of org.apache.aries.proxy.impl.AsmProxyManager in project aries by apache.

the class WovenProxyGeneratorTest method getProxyInstance.

@Override
protected Object getProxyInstance(Class<?> proxyClass) {
    try {
        if (proxyClass.getName().equals(ProxyTestClassAbstract.class.getName())) {
            Collection<Class<?>> coll = new ArrayList<Class<?>>();
            coll.add(proxyClass);
            return new AsmProxyManager().createNewProxy(null, coll, new Callable() {

                public Object call() throws Exception {
                    return null;
                }
            }, null);
        }
        return proxyClass.newInstance();
    } catch (Exception e) {
        return null;
    }
}
Also used : AsmProxyManager(org.apache.aries.proxy.impl.AsmProxyManager) ArrayList(java.util.ArrayList) BeforeClass(org.junit.BeforeClass) Callable(java.util.concurrent.Callable) FinalModifierException(org.apache.aries.proxy.FinalModifierException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException)

Example 2 with AsmProxyManager

use of org.apache.aries.proxy.impl.AsmProxyManager in project aries by apache.

the class WovenProxyGeneratorTest method testWovenClassPlusInterfaces.

/**
   * This test checks that we can add interfaces to classes that don't implement
   * them using dynamic subclassing. This is a little odd, but it came for
   * free with support for proxying abstract classes!
   * @throws Exception 
   */
@Test
public void testWovenClassPlusInterfaces() throws Exception {
    Bundle b = mock(Bundle.class);
    BundleWiring wiring = getWiring(weavingLoader);
    when(b.adapt(BundleWiring.class)).thenReturn(wiring);
    Object toCall = new AsmProxyManager().createDelegatingProxy(b, Arrays.asList(getProxyClass(ProxyTestClassAbstract.class), Callable.class), new Callable() {

        public Object call() throws Exception {
            return weavingLoader.loadClass(ProxyTestClassChildOfAbstract.class.getName()).newInstance();
        }
    }, null);
    //Should proxy the abstract method on the class
    Method m = getProxyClass(ProxyTestClassAbstract.class).getMethod("getMessage");
    assertEquals("Working", m.invoke(toCall));
    //Should be a callable too!
    assertEquals("Callable Works too!", ((Callable) toCall).call());
}
Also used : AsmProxyManager(org.apache.aries.proxy.impl.AsmProxyManager) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) Method(java.lang.reflect.Method) Callable(java.util.concurrent.Callable) FinalModifierException(org.apache.aries.proxy.FinalModifierException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) Test(org.junit.Test)

Example 3 with AsmProxyManager

use of org.apache.aries.proxy.impl.AsmProxyManager in project aries by apache.

the class ProxySubclassGeneratorTest method testClassLoaders.

@SuppressWarnings("unchecked")
@Test
public void testClassLoaders() throws Exception {
    ClassLoader clA = new LimitedClassLoader("org.apache.aries.proxy.test.a", null, null);
    ClassLoader clB = new LimitedClassLoader("org.apache.aries.proxy.test.b", "org.apache.aries.proxy.test.a", clA);
    ClassLoader clC = new LimitedClassLoader("org.apache.aries.proxy.test.c", "org.apache.aries.proxy.test.b", clB);
    Class<?> clazzA = clA.loadClass("org.apache.aries.proxy.test.a.ProxyTestClassA");
    Class<?> clazzB = clB.loadClass("org.apache.aries.proxy.test.b.ProxyTestClassB");
    Class<?> clazzC = clC.loadClass("org.apache.aries.proxy.test.c.ProxyTestClassC");
    final Object object = clazzC.getConstructor(String.class).newInstance("hello");
    o = new AsmProxyManager().createNewProxy(null, Arrays.asList(clazzA, clazzB, clazzC), constantly(object), null);
    generatedProxySubclass = o.getClass();
    Method m = generatedProxySubclass.getDeclaredMethod("hello", new Class[] {});
    Object returned = m.invoke(o);
    assertEquals("hello", returned);
}
Also used : AsmProxyManager(org.apache.aries.proxy.impl.AsmProxyManager) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

AsmProxyManager (org.apache.aries.proxy.impl.AsmProxyManager)3 Method (java.lang.reflect.Method)2 Callable (java.util.concurrent.Callable)2 FinalModifierException (org.apache.aries.proxy.FinalModifierException)2 UnableToProxyException (org.apache.aries.proxy.UnableToProxyException)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 BeforeClass (org.junit.BeforeClass)1 Bundle (org.osgi.framework.Bundle)1 BundleWiring (org.osgi.framework.wiring.BundleWiring)1