Search in sources :

Example 1 with WovenProxy

use of org.apache.aries.proxy.weaving.WovenProxy in project aries by apache.

the class AbstractWovenProxyAdapter method visit.

public final void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
    LOGGER.debug(Constants.LOG_ENTRY, "visit", new Object[] { version, access, name, signature, superName, interfaces });
    // always update to the most recent version of the JVM
    version = JAVA_CLASS_VERSION;
    superType = Type.getType("L" + superName + ";");
    try {
        // we only want to implement WovenProxy once in the hierarchy.
        // It's best to do this as high up as possible so we check the
        // super. By loading it we may end up weaving it, but that's a good thing!
        Class<?> superClass = Class.forName(superName.replace('/', '.'), false, loader);
        isSerializable = Serializable.class.isAssignableFrom(superClass) || Arrays.asList(interfaces).contains(Type.getInternalName(Serializable.class)) || checkInterfacesForSerializability(interfaces);
        if (!!!WovenProxy.class.isAssignableFrom(superClass)) {
            // We have found a type we need to add WovenProxy information to
            implementWovenProxy = true;
            if (superClass != Object.class) {
                //If our superclass isn't Object, it means we didn't weave all the way
                //to the top of the hierarchy. This means we need to override all the
                //methods defined on our parent so that they can be intercepted!
                nonObjectSupers.add(superClass);
                Class<?> nextSuper = superClass.getSuperclass();
                while (nextSuper != Object.class) {
                    nonObjectSupers.add(nextSuper);
                    nextSuper = nextSuper.getSuperclass();
                }
                //Don't use reflection - it can be dangerous
                superHasNoArgsConstructor = superHasNoArgsConstructor(superName, name);
            } else {
                superHasNoArgsConstructor = true;
            }
            // re-work the interfaces list to include WovenProxy
            String[] interfacesPlusWovenProxy = new String[interfaces.length + 1];
            System.arraycopy(interfaces, 0, interfacesPlusWovenProxy, 0, interfaces.length);
            interfacesPlusWovenProxy[interfaces.length] = WOVEN_PROXY_IFACE_TYPE.getInternalName();
            // Write the class header including WovenProxy.
            cv.visit(version, access, name, signature, superName, interfacesPlusWovenProxy);
        } else {
            // Already has a woven proxy parent, but we still need to write the
            // header!
            cv.visit(version, access, name, signature, superName, interfaces);
        }
    } catch (ClassNotFoundException e) {
        // If this happens we're about to hit bigger trouble on verify, so we
        // should stop weaving and fail. Make sure we don't cause the hook to
        // throw an error though.
        UnableToProxyException u = new UnableToProxyException(name, e);
        cannotLoadSuperClassException(superName, u);
    }
}
Also used : Serializable(java.io.Serializable) WovenProxy(org.apache.aries.proxy.weaving.WovenProxy) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException)

Example 2 with WovenProxy

use of org.apache.aries.proxy.weaving.WovenProxy in project aries by apache.

the class WeavingProxyTest method checkProxyController.

@Test(expected = FinalModifierException.class)
public void checkProxyController() throws Exception {
    bundleContext.registerService(ProxyWeavingController.class.getName(), new ProxyWeavingController() {

        public boolean shouldWeave(WovenClass arg0, WeavingHelper arg1) {
            return false;
        }
    }, null);
    Bundle b = bundleContext.getBundle();
    Callable<Object> c = new TestCallable();
    Collection<Class<?>> classes = new ArrayList<Class<?>>();
    // Don't use anonymous inner class in this test as IBM and Sun load it at a different time
    // For IBM JDK, the anonymous inner class will be loaded prior to the controller is registered.
    Callable<?> callable = new TestFinalDelegate();
    classes.add(callable.getClass());
    Object o = mgr.createDelegatingProxy(b, classes, c, callable);
    if (o instanceof WovenProxy)
        fail("Proxy should not have been woven!");
}
Also used : Bundle(org.osgi.framework.Bundle) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) ArrayList(java.util.ArrayList) WovenProxy(org.apache.aries.proxy.weaving.WovenProxy) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) ProxyWeavingController(org.apache.aries.proxy.weavinghook.ProxyWeavingController) WeavingHelper(org.apache.aries.proxy.weavinghook.WeavingHelper) Test(org.junit.Test)

Example 3 with WovenProxy

use of org.apache.aries.proxy.weaving.WovenProxy in project aries by apache.

the class WeavingProxyTest method checkProxyFinalClass.

/**
   * This test does two things. First of all it checks that we can proxy a final 
   * class. It also validates that the class implements WovenProxy, and that the
   * delegation still works
   */
@Test
public void checkProxyFinalClass() throws Exception {
    Bundle b = bundleContext.getBundle();
    TestCallable dispatcher = new TestCallable();
    TestCallable template = new TestCallable();
    Collection<Class<?>> classes = new ArrayList<Class<?>>();
    classes.add(TestCallable.class);
    @SuppressWarnings("unchecked") Callable<Object> o = (Callable<Object>) mgr.createDelegatingProxy(b, classes, dispatcher, template);
    if (!!!(o instanceof WovenProxy))
        fail("Proxy should be woven!");
    Object inner = new Integer(3);
    dispatcher.setReturn(new TestCallable());
    ((TestCallable) dispatcher.call()).setReturn(inner);
    assertSame("Should return the same object", inner, o.call());
}
Also used : WovenProxy(org.apache.aries.proxy.weaving.WovenProxy) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) WovenClass(org.osgi.framework.hooks.weaving.WovenClass) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 4 with WovenProxy

use of org.apache.aries.proxy.weaving.WovenProxy in project aries by apache.

the class WovenProxyPlusSubclassGeneratorTest method getProxyInstance.

@Override
protected Object getProxyInstance(Class<?> proxyClass, InvocationListener listener) {
    WovenProxy proxy = (WovenProxy) getProxyInstance(proxyClass);
    proxy = proxy.org_apache_aries_proxy_weaving_WovenProxy_createNewProxyInstance(new SingleInstanceDispatcher(proxy), listener);
    return proxy;
}
Also used : WovenProxy(org.apache.aries.proxy.weaving.WovenProxy) SingleInstanceDispatcher(org.apache.aries.proxy.impl.SingleInstanceDispatcher)

Example 5 with WovenProxy

use of org.apache.aries.proxy.weaving.WovenProxy in project aries by apache.

the class WovenProxyGeneratorTest method getProxyInstance.

@Override
protected Object getProxyInstance(Class<?> proxyClass, InvocationListener listener) {
    WovenProxy proxy = (WovenProxy) getProxyInstance(proxyClass);
    proxy = proxy.org_apache_aries_proxy_weaving_WovenProxy_createNewProxyInstance(new SingleInstanceDispatcher(proxy), listener);
    return proxy;
}
Also used : WovenProxy(org.apache.aries.proxy.weaving.WovenProxy) SingleInstanceDispatcher(org.apache.aries.proxy.impl.SingleInstanceDispatcher)

Aggregations

WovenProxy (org.apache.aries.proxy.weaving.WovenProxy)7 ArrayList (java.util.ArrayList)3 SingleInstanceDispatcher (org.apache.aries.proxy.impl.SingleInstanceDispatcher)3 Test (org.junit.Test)3 Bundle (org.osgi.framework.Bundle)3 WovenClass (org.osgi.framework.hooks.weaving.WovenClass)3 Serializable (java.io.Serializable)1 Callable (java.util.concurrent.Callable)1 UnableToProxyException (org.apache.aries.proxy.UnableToProxyException)1 ProxyWeavingController (org.apache.aries.proxy.weavinghook.ProxyWeavingController)1 WeavingHelper (org.apache.aries.proxy.weavinghook.WeavingHelper)1