diff --git a/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericProduct.java b/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericProduct.java index d209680..15f1bc8 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericProduct.java +++ b/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericProduct.java @@ -1,19 +1,3 @@ -/* - * 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.extensions.bean.generic; import static java.lang.annotation.ElementType.FIELD; @@ -25,7 +9,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.Target; -import javax.enterprise.util.AnnotationLiteral; import javax.inject.Qualifier; /** @@ -39,22 +22,13 @@ * */ @Retention(RUNTIME) +<<<<<<< HEAD @Target({ METHOD, FIELD, PARAMETER, TYPE }) +======= +@Target({ METHOD, FIELD, PARAMETER, TYPE}) +>>>>>>> stuart/master @Qualifier public @interface GenericProduct { - - public static final GenericProduct INSTANCE = new GenericProductLiteral(); - - class GenericProductLiteral extends AnnotationLiteral implements GenericProduct - { - - private static final long serialVersionUID = 2768505716290514234L; - - - - private GenericProductLiteral() {} - } - } diff --git a/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericProductLiteral.java b/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericProductLiteral.java new file mode 100644 index 0000000..adb3c1f --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericProductLiteral.java @@ -0,0 +1,15 @@ +package org.jboss.weld.extensions.bean.generic; + +import javax.enterprise.util.AnnotationLiteral; + + +public class GenericProductLiteral extends AnnotationLiteral implements GenericProduct +{ + + public static final GenericProduct INSTANCE = new GenericProductLiteral(); + + private static final long serialVersionUID = 2768505716290514234L; + + public GenericProductLiteral() {} + +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/core/CoreExtension.java b/impl/src/main/java/org/jboss/weld/extensions/core/CoreExtension.java index 510fa20..2901030 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/core/CoreExtension.java +++ b/impl/src/main/java/org/jboss/weld/extensions/core/CoreExtension.java @@ -39,6 +39,8 @@ import org.jboss.weld.extensions.annotated.AnnotationRedefiner; import org.jboss.weld.extensions.annotated.RedefinitionContext; import org.jboss.weld.extensions.bean.BeanBuilder; +import org.jboss.weld.extensions.literal.InjectLiteral; +import org.jboss.weld.extensions.literal.NamedLiteral; /** * Extension to install the "core" extensions. Core extensions are those that diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java new file mode 100644 index 0000000..d719e26 --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandler.java @@ -0,0 +1,69 @@ +/* + * 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.extensions.servicehandler; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + *

+ * Meta annotation that is used to specify an invocation handler for an + * automatically implmented bean. + *

+ * + *

+ * If the annotation that is annotated with this meta-annotation is applied to + * an interface or abstract class then the container will automatically provide + * a concrete implementation of the class/interface, and delegate all calls to + * abstract methods through the handler class specified by this annotations. + *

+ * + *

+ * The handler class must have a method with the following signature: + *

+ * + *
+ *  @AroundInvoke public Object aroundInvoke(final InvocationContext invocation) throws Exception
+ * 
+ * + *

+ * This is the same as an intercepter class. This handler can be injected into + * and use initializer methods, however @PreDestory methods are not available + *

+ * + *

+ * The annotation should have: + *

+ * + *
+ * @Retention(RUNTIME)
+ * @Target({ TYPE })
+ * 
+ * + * @author Stuart Douglas + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.ANNOTATION_TYPE) +@Documented +public @interface ServiceHandler +{ + Class value(); +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java new file mode 100644 index 0000000..450230d --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerBeanLifecycle.java @@ -0,0 +1,98 @@ +/* + * 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.extensions.servicehandler; + +import java.lang.reflect.Method; + +import javassist.util.proxy.MethodFilter; +import javassist.util.proxy.ProxyFactory; +import javassist.util.proxy.ProxyObject; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; + +import org.jboss.weld.extensions.bean.BeanLifecycle; + +/** + * Bean lifecycle for ServiceHandler beans + * + * @author Stuart Douglas + * + * @param + * @param + */ +public class ServiceHandlerBeanLifecycle implements BeanLifecycle +{ + private final ProxyFactory factory; + private final Class proxyClass; + private final ServiceHandlerManager handler; + + + public ServiceHandlerBeanLifecycle(Class classToImplement, Class handlerClass, BeanManager manager) + { + handler = new ServiceHandlerManager(handlerClass, manager); + + // create the proxy + factory = new ProxyFactory(); + if (classToImplement.isInterface()) + { + Class[] interfaces = new Class[1]; + interfaces[0] = classToImplement; + factory.setInterfaces(interfaces); + } + else + { + factory.setSuperclass(classToImplement); + } + factory.setFilter(new MethodFilter() + { + public boolean isHandled(Method m) + { + // ignore finalize() + return !m.getName().equals("finalize"); + } + }); + + this.proxyClass = ((Class) factory.createClass()).asSubclass(classToImplement); + } + + public T create(Bean bean, CreationalContext creationalContext) + { + try + { + // Make sure to pass the creational context along, allowing dependents to be cleaned up properly + @SuppressWarnings("unchecked") + H handlerInstance = handler.create((CreationalContext) creationalContext); + + ServiceHandlerMethodHandler methodHandler = new ServiceHandlerMethodHandler(handler, handlerInstance); + T instance = proxyClass.newInstance(); + ((ProxyObject) instance).setHandler(methodHandler); + return instance; + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } + + public void destroy(Bean bean, T instance, CreationalContext creationalContext) + { + // handler.dispose(instance); + } + +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java new file mode 100644 index 0000000..0526584 --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerExtension.java @@ -0,0 +1,70 @@ +/* + * 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.extensions.servicehandler; + +import static org.jboss.weld.extensions.util.AnnotationInspector.getMetaAnnotation; + +import java.util.HashSet; +import java.util.Set; + +import javax.enterprise.event.Observes; +import javax.enterprise.inject.spi.AfterBeanDiscovery; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.Extension; +import javax.enterprise.inject.spi.ProcessAnnotatedType; + +import org.jboss.weld.extensions.bean.BeanBuilder; + +/** + * This extension automatically implements interfaces and abstract classes. + * + * @author Stuart Douglas + * + */ +class ServiceHandlerExtension implements Extension +{ + Set> beans = new HashSet>(); + + void processAnnotatedType(@Observes ProcessAnnotatedType event, BeanManager beanManager) + { + ServiceHandler annotation = getMetaAnnotation(event.getAnnotatedType(), ServiceHandler.class); + if (annotation != null) + { + Class handlerClass = annotation.value(); + try + { + BeanBuilder builder = new BeanBuilder(beanManager); + builder.defineBeanFromAnnotatedType(event.getAnnotatedType()); + builder.setBeanLifecycle(new ServiceHandlerBeanLifecycle(event.getAnnotatedType().getJavaClass(), handlerClass, beanManager)); + beans.add(builder.create()); + } + catch (IllegalArgumentException e) + { + throw new RuntimeException(e); + } + } + } + + void afterBeanDiscovery(@Observes AfterBeanDiscovery event) + { + for (Bean bean : beans) + { + event.addBean(bean); + } + } +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java new file mode 100644 index 0000000..f245fb3 --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerManager.java @@ -0,0 +1,94 @@ +/* + * 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.extensions.servicehandler; + +import java.lang.reflect.Method; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.InjectionTarget; +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; + +import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder; +import org.jboss.weld.extensions.util.Reflections; + +/** + * Manages the handler class for the service handler extension. This class is + * responsible for managing the lifecycle of the handler class instances + * + * @author Stuart Douglas + * + */ +class ServiceHandlerManager +{ + private final Class handlerClass; + private final Method handlerMethod; + private final InjectionTarget injectionTarget; + + /** + * Creates a wrapper around an AutoProxy handler class + * + * @param handlerClass The handler class + * @throws IllegalArgumentException if the handler class is does not have a + * suitable @AroundInvoke method + */ + ServiceHandlerManager(Class handlerClass, BeanManager beanManager) throws IllegalArgumentException + { + this.handlerClass = handlerClass; + handlerMethod = getHandlerMethod(handlerClass); + //now create the InjectionTarget + AnnotatedTypeBuilder typeBuilder = new AnnotatedTypeBuilder().readFromType(handlerClass); + injectionTarget = beanManager.createInjectionTarget(typeBuilder.create()); + } + + T create(CreationalContext ctx) + { + T instance = injectionTarget.produce(ctx); + injectionTarget.inject(instance, ctx); + injectionTarget.postConstruct(instance); + return instance; + } + + Object invoke(Object instance, InvocationContext ctx) throws Exception + { + return handlerMethod.invoke(instance, ctx); + } + + Class getHandlerClass() + { + return handlerClass; + } + + private static Method getHandlerMethod(Class handlerClass) + { + //search for the handler method + for (Method m : Reflections.getAllMethods(handlerClass)) + { + if (m.isAnnotationPresent(AroundInvoke.class)) + { + if (m.getParameterTypes().length != 1 || m.getParameterTypes()[0] != InvocationContext.class) + { + throw new IllegalArgumentException("Could not find suitable AroundInvoke method on class " + handlerClass + " methods denoted @AroundInvoke must have a single InvokationContext parameter"); + } + return m; + } + } + throw new IllegalArgumentException("Could not find suitable AroundInvoke method on class " + handlerClass); + } + +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java new file mode 100644 index 0000000..373ceb7 --- /dev/null +++ b/impl/src/main/java/org/jboss/weld/extensions/servicehandler/ServiceHandlerMethodHandler.java @@ -0,0 +1,106 @@ +/* + * 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.extensions.servicehandler; + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.Map; + +import javassist.util.proxy.MethodHandler; + +import javax.interceptor.InvocationContext; + +/** + * MethodHandler that forwards calls to abstract methods to the service handler + * instance + * + * @author Stuart Douglas + * + */ +public class ServiceHandlerMethodHandler implements MethodHandler +{ + + private final ServiceHandlerManager handler; + private final H handlerInstance; + + public ServiceHandlerMethodHandler(ServiceHandlerManager handler, H handlerInstance) + { + this.handler = handler; + this.handlerInstance = handlerInstance; + } + + public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable + { + if (proceed == null) + { + return handler.invoke(handlerInstance, new InvocationContextImpl(self, thisMethod, args)); + } + return proceed.invoke(self, args); + } + + private final class InvocationContextImpl implements InvocationContext + { + + public InvocationContextImpl(Object target, Method method, Object[] params) + { + this.target = target; + this.method = method; + this.params = params; + } + + private final Object target; + private final Method method; + private final Object[] params; + + public Map getContextData() + { + return Collections.emptyMap(); + } + + public Method getMethod() + { + return method; + } + + public Object[] getParameters() + { + return params; + } + + public Object getTarget() + { + return target; + } + + public Object getTimer() + { + return null; + } + + public Object proceed() throws Exception + { + throw new UnsupportedOperationException("Cannot call proceed() in AutoProxy invocation handler"); + } + + public void setParameters(Object[] params) + { + throw new UnsupportedOperationException("Cannot call setParameters() in AutoProxy invocation handler"); + } + + } + +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java b/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java index 6f59288..0b0cc69 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java +++ b/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInspector.java @@ -1,9 +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.extensions.util; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import javax.enterprise.inject.Stereotype; +import javax.enterprise.inject.spi.Annotated; import javax.enterprise.inject.spi.BeanManager; public class AnnotationInspector @@ -64,4 +81,26 @@ public static A getAnnotation(AnnotatedElement element, f } } + /** + * Inspects an annotated element for the given meta annotation. This should + * only be used for user defined meta annotations, where the annotation must + * be physically present. + * + * @param element The element to inspect + * @param annotationType The meta annotation to search for + * @return The annotation instance found on this method or null if no + * matching annotation was found. + */ + public static A getMetaAnnotation(Annotated element, final Class annotationType) + { + for (Annotation annotation : element.getAnnotations()) + { + if (annotation.annotationType().isAnnotationPresent(annotationType)) + { + return annotation.annotationType().getAnnotation(annotationType); + } + } + return null; + } + } diff --git a/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension index ceb58ec..ec3a889 100644 --- a/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension +++ b/impl/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension @@ -5,3 +5,4 @@ org.jboss.weld.extensions.interceptor.InterceptorExtension org.jboss.weld.extensions.managedproducer.ManagedProducerExtension org.jboss.weld.extensions.defaultbean.DefaultBeanExtension org.jboss.weld.extensions.log.LoggerExtension +org.jboss.weld.extensions.servicehandler.ServiceHandlerExtension diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/field/Garply.java b/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/field/Garply.java index 91ed32b..f513b7a 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/field/Garply.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/field/Garply.java @@ -19,8 +19,8 @@ import javax.enterprise.inject.Produces; import javax.inject.Inject; -import org.jboss.weld.extensions.bean.generic.Generic; import org.jboss.weld.extensions.bean.generic.GenericProduct; +import org.jboss.weld.extensions.bean.generic.Generic; /** * A generic bean for the config annotation Message diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/method/Garply.java b/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/method/Garply.java index 88c54ca..b8106f0 100644 --- a/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/method/Garply.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/method/Garply.java @@ -19,8 +19,8 @@ import javax.enterprise.inject.Produces; import javax.inject.Inject; -import org.jboss.weld.extensions.bean.generic.Generic; import org.jboss.weld.extensions.bean.generic.GenericProduct; +import org.jboss.weld.extensions.bean.generic.Generic; /** * A generic bean for the config annotation Message diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoService.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoService.java new file mode 100644 index 0000000..b63abbb --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoService.java @@ -0,0 +1,33 @@ +/* + * 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.extensions.test.servicehandler; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.jboss.weld.extensions.servicehandler.ServiceHandler; + +@Retention(RUNTIME) +@Target(TYPE) +@ServiceHandler(DecoratedEchoServiceHandler.class) +public @interface DecoratedEchoService +{ + +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoServiceHandler.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoServiceHandler.java new file mode 100644 index 0000000..0c326a9 --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedEchoServiceHandler.java @@ -0,0 +1,34 @@ +/* + * 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.extensions.test.servicehandler; + +import javax.inject.Inject; +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; + +public class DecoratedEchoServiceHandler +{ + // test injection into the handler + @Inject + private EchoDecorator decorator; + + @AroundInvoke + public Object invoke(InvocationContext ctx) + { + return decorator.decorate(ctx.getMethod().getName().toString()); + } +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedHelloWorld.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedHelloWorld.java new file mode 100644 index 0000000..a670d58 --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/DecoratedHelloWorld.java @@ -0,0 +1,7 @@ +package org.jboss.weld.extensions.test.servicehandler; + +@DecoratedEchoService +public interface DecoratedHelloWorld +{ + public String decoratedHelloWorld(); +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoDecorator.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoDecorator.java new file mode 100644 index 0000000..f996b3f --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoDecorator.java @@ -0,0 +1,25 @@ +/* + * 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.extensions.test.servicehandler; + +public class EchoDecorator +{ + public String decorate(String message) + { + return "-" + message + "-"; + } +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/core/InjectLiteral.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoService.java similarity index 68% rename from impl/src/main/java/org/jboss/weld/extensions/core/InjectLiteral.java rename to impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoService.java index aed84f0..e264723 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/core/InjectLiteral.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoService.java @@ -14,18 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.core; +package org.jboss.weld.extensions.test.servicehandler; -import javax.enterprise.util.AnnotationLiteral; -import javax.inject.Inject; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; -class InjectLiteral extends AnnotationLiteral implements Inject +import org.jboss.weld.extensions.servicehandler.ServiceHandler; + +@Retention(RetentionPolicy.RUNTIME) +@ServiceHandler(EchoServiceHandler.class) +public @interface EchoService { - - private static final long serialVersionUID = 817936051028981316L; - - static final Inject INSTANCE = new InjectLiteral(); - - private InjectLiteral() {} -} \ No newline at end of file +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/core/NamedLiteral.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoServiceHandler.java similarity index 67% rename from impl/src/main/java/org/jboss/weld/extensions/core/NamedLiteral.java rename to impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoServiceHandler.java index f0ee303..5461ce7 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/core/NamedLiteral.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/EchoServiceHandler.java @@ -14,23 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.core; +package org.jboss.weld.extensions.test.servicehandler; -import javax.enterprise.util.AnnotationLiteral; -import javax.inject.Named; +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; -class NamedLiteral extends AnnotationLiteral implements Named +public class EchoServiceHandler { - private static final long serialVersionUID = 2239690880420187044L; - final String name; - - NamedLiteral(String name) - { - this.name = name; - } - - public String value() + @AroundInvoke + public Object invoke(InvocationContext ctx) { - return name; + return ctx.getMethod().getName().toString(); } -} \ No newline at end of file +} diff --git a/impl/src/main/java/org/jboss/weld/extensions/core/DefaultLiteral.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java similarity index 67% rename from impl/src/main/java/org/jboss/weld/extensions/core/DefaultLiteral.java rename to impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java index a5d2e4c..a972dd8 100644 --- a/impl/src/main/java/org/jboss/weld/extensions/core/DefaultLiteral.java +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/GoodbyeWorld.java @@ -14,18 +14,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jboss.weld.extensions.core; +package org.jboss.weld.extensions.test.servicehandler; -import javax.enterprise.inject.Default; -import javax.enterprise.util.AnnotationLiteral; - -class DefaultLiteral extends AnnotationLiteral implements Default +@EchoService +public abstract class GoodbyeWorld { + public String otherMethod() + { + return "not saying goodbye"; + } - private static final long serialVersionUID = 2768505716290514234L; - - static final Default INSTANCE = new DefaultLiteral(); - - private DefaultLiteral() {} + public abstract String goodbyeWorld(); -} \ No newline at end of file +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java new file mode 100644 index 0000000..2e3b60e --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/HelloWorld.java @@ -0,0 +1,23 @@ +/* + * 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.extensions.test.servicehandler; + +@EchoService +public interface HelloWorld +{ + public String helloWorld(); +} diff --git a/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java new file mode 100644 index 0000000..374e926 --- /dev/null +++ b/impl/src/test/java/org/jboss/weld/extensions/test/servicehandler/ServiceHandlerTest.java @@ -0,0 +1,73 @@ +/* + * 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.extensions.test.servicehandler; + +import javax.inject.Inject; + +import junit.framework.Assert; + +import org.jboss.arquillian.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.Archive; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author Stuart Douglas + * + */ +@RunWith(Arquillian.class) +public class ServiceHandlerTest +{ + + @Inject + private HelloWorld helloWorld; + + @Inject + private GoodbyeWorld goodbyteWorld; + + @Inject + private DecoratedHelloWorld decoratedHelloWorld; + + @Deployment + public static Archive deploy() + { + return ShrinkWrap.create("test.jar", JavaArchive.class).addPackage(ServiceHandlerTest.class.getPackage()); + } + + @Test + public void testProxiedInterface() + { + Assert.assertTrue(helloWorld.helloWorld().equals("helloWorld")); + } + + @Test + public void testProxiedAbstractClass() + { + Assert.assertTrue(goodbyteWorld.goodbyeWorld().equals("goodbyeWorld")); + Assert.assertFalse(goodbyteWorld.otherMethod().equals("otherMethod")); + } + + @Test + public void testInjectionIntoHandler() + { + Assert.assertTrue(decoratedHelloWorld.decoratedHelloWorld().equals("-decoratedHelloWorld-")); + } + +}