Skip to content

Commit

Permalink
resolve WELD-634
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Aug 25, 2010
1 parent 5653f7d commit 44a3588
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 2 deletions.
29 changes: 29 additions & 0 deletions impl/src/main/java/org/jboss/weld/bean/proxy/DecoratorProxy.java
@@ -0,0 +1,29 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.bean.proxy;

/**
* Marker interface to identify Decorator proxies
*
* @author Stuart Douglas
*
*/
public interface DecoratorProxy
{

}
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.Set;

import javassist.NotFoundException;
import javassist.bytecode.AccessFlag;
Expand Down Expand Up @@ -72,6 +73,12 @@ private void addHandlerInitializerMethod(ClassFile proxyClassType) throws Except
proxyClassType.addMethod(MethodUtils.makeMethod(Modifier.PRIVATE, void.class, "_initMH", new Class[] { Object.class }, new Class[] {}, createMethodHandlerInitializerBody(proxyClassType), proxyClassType.getConstPool()));
}

@Override
protected void addAdditionalInterfaces(Set<Class<?>> interfaces)
{
interfaces.add(DecoratorProxy.class);
}

/**
* calls _initMH on the method handler and then stores the result in the
* methodHandler field as then new methodHandler
Expand Down
10 changes: 10 additions & 0 deletions impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java
Expand Up @@ -274,13 +274,23 @@ private void addDefaultAdditionalInterfaces()
additionalInterfaces.add(Serializable.class);
}

/**
* Sub classes may override to specify additional interfaces the proxy should
* implement
*/
protected void addAdditionalInterfaces(Set<Class<?>> interfaces)
{

}

@SuppressWarnings("unchecked")
private Class<T> createProxyClass(String proxyClassName) throws Exception
{
ArraySet<Class<?>> specialInterfaces = new ArraySet<Class<?>>(3);
specialInterfaces.add(LifecycleMixin.class);
specialInterfaces.add(TargetInstanceProxy.class);
specialInterfaces.add(ProxyObject.class);
addAdditionalInterfaces(specialInterfaces);
// Remove special interfaces from main set (deserialization scenario)
additionalInterfaces.removeAll(specialInterfaces);

Expand Down
Expand Up @@ -36,6 +36,7 @@
import javax.inject.Inject;

import org.jboss.interceptor.util.InterceptionUtils;
import org.jboss.weld.bean.proxy.DecoratorProxy;
import org.jboss.weld.exceptions.IllegalStateException;
import org.jboss.weld.exceptions.InvalidObjectException;
import org.jboss.weld.introspector.ForwardingWeldField;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void inject(Object declaringInstance, BeanManagerImpl manager, Creational
try
{
Object instanceToInject = declaringInstance;
if (!isDelegate())
if (!(instanceToInject instanceof DecoratorProxy))
{
// if declaringInstance is a proxy, unwrap it
instanceToInject = InterceptionUtils.getRawInstance(declaringInstance);
Expand All @@ -131,7 +132,7 @@ public void inject(Object declaringInstance, Object value)
try
{
Object instanceToInject = declaringInstance;
if (!isDelegate())
if (!(instanceToInject instanceof DecoratorProxy))
{
// if declaringInstance is a proxy, unwrap it
instanceToInject = InterceptionUtils.getRawInstance(declaringInstance);
Expand Down
@@ -0,0 +1,45 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.tests.decorators.abstractDecorator;

import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.inject.Inject;

/**
* @author <a href="mailto:mariusb@redhat.com">Marius Bogoevici</a>
*/
@Decorator
public abstract class FrameWithPrivateFieldInjectedDelegateAndInjectionIntoDecorator implements Window
{

static boolean drawn;

@Inject @Delegate
private Window window;

@Inject
WindowPane pane;

public void draw()
{
drawn = true;
window.draw();
}

}
@@ -0,0 +1,55 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.tests.decorators.abstractDecorator;

import static org.jboss.weld.tests.decorators.abstractDecorator.AbstractDecoratorTestHelper.resetAll;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* @author <a href="mailto:mariusb@redhat.com">Marius Bogoevici</a>
*/
@RunWith(Arquillian.class)
public class SimpleAbstractDecoratorWithPrivateDelegateTest
{
@Deployment
public static Archive<?> deploy()
{
return ShrinkWrap.create(BeanArchive.class)
.decorate(FrameWithPrivateFieldInjectedDelegateAndInjectionIntoDecorator.class)
.addPackage(SimpleAbstractDecoratorWithPrivateDelegateTest.class.getPackage());
}

@Test
public void testAbstractDecoratorApplied(WindowImpl window)
{
resetAll();

window.draw();
Assert.assertTrue(WindowImpl.drawn);
Assert.assertTrue(FrameWithPrivateFieldInjectedDelegateAndInjectionIntoDecorator.drawn);
}

}
Expand Up @@ -16,12 +16,17 @@
*/
package org.jboss.weld.tests.decorators.abstractDecorator;

import javax.inject.Inject;

/**
*
* @author Marius Bogoevici
*/
public class WindowImpl implements Window
{
@Inject
WindowPane pane;

static boolean drawn;

static boolean moved;
Expand Down
@@ -0,0 +1,26 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.decorators.abstractDecorator;

/**
*
* @author Stuart Douglas
*/
public class WindowPane
{

}

0 comments on commit 44a3588

Please sign in to comment.