You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by st...@apache.org on 2012/06/15 10:09:11 UTC

[2/2] git commit: DELTASPIKE-199 MessageBundle must only be used on Interfaces

DELTASPIKE-199 MessageBundle must only be used on Interfaces

also add/polish JavaDoc


Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/57c9f71a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/57c9f71a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/57c9f71a

Branch: refs/heads/master
Commit: 57c9f71a8d496695d9c6f78b74b5c4b3cad63199
Parents: 04f7d17
Author: Mark Struberg <st...@apache.org>
Authored: Fri Jun 15 09:22:10 2012 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Fri Jun 15 09:22:10 2012 +0200

----------------------------------------------------------------------
 .../core/api/message/annotation/MessageBundle.java |   15 ++--
 .../message/annotation/MessageContextConfig.java   |    3 +-
 .../api/message/annotation/MessageTemplate.java    |    5 +-
 .../core/impl/message/MessageBundleExtension.java  |   45 +++++++--
 .../message/broken/BrokenMessageBundleClass.java   |   43 ++++++++
 .../broken/BrokenMessageBundleOnClassTest.java     |   80 +++++++++++++++
 6 files changed, 173 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/57c9f71a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageBundle.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageBundle.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageBundle.java
index bf4c463..8f38f0e 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageBundle.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageBundle.java
@@ -22,19 +22,20 @@ import java.lang.annotation.Documented;
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
 
-import static java.lang.annotation.ElementType.FIELD;
-import static java.lang.annotation.ElementType.METHOD;
-import static java.lang.annotation.ElementType.PARAMETER;
 import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 /**
- * Marker annotation for a message-bundle interface which provides type-safe messages
- * (methods annotated with {@link MessageTemplate}).
+ * <p>Marker annotation for a message-bundle interface which provides type-safe messages
+ * (methods annotated with {@link MessageTemplate}).</p>
  *
- * Can be combined with {@link MessageContextConfig} to customize the message-resolution and processing
+ * <p>This annotation must only be used on interfaces.
+ * If this annotation gets used on a concrete class, a deployment error results!</p>
+ *
+ * <p>Can be combined with {@link MessageContextConfig} to customize the
+ * message-resolution and processing.</p>
  */
-@Target({ TYPE, METHOD, PARAMETER, FIELD })
+@Target({ TYPE })
 @Retention(RUNTIME)
 @Documented
 public @interface MessageBundle

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/57c9f71a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageContextConfig.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageContextConfig.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageContextConfig.java
index 2db4f76..84a7763 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageContextConfig.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageContextConfig.java
@@ -30,7 +30,8 @@ import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 /**
- * Allows to customize the message-resolution and processing in combination with {@link MessageBundle}
+ * Allows to customize the message-resolution and processing
+ * in combination with {@link MessageBundle}.
  */
 @Target({ TYPE })
 @Retention(RUNTIME)

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/57c9f71a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageTemplate.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageTemplate.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageTemplate.java
index a329bc3..dfdb02e 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageTemplate.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/annotation/MessageTemplate.java
@@ -26,7 +26,10 @@ import static java.lang.annotation.ElementType.METHOD;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
 /**
- * Provides the message(-template) for type-safe messages
+ * <p>Provides the message(-template) for type-safe messages</p>
+ *
+ * <p>This only works on interfaces which are annotated with
+ * {@link MessageBundle}.</p>
  */
 @Target(METHOD)
 @Retention(RUNTIME)

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/57c9f71a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java
index 8b18eb4..ffae84c 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/message/MessageBundleExtension.java
@@ -41,7 +41,12 @@ import org.apache.deltaspike.core.api.message.annotation.MessageTemplate;
 import org.apache.deltaspike.core.spi.activation.Deactivatable;
 import org.apache.deltaspike.core.util.ClassDeactivationUtils;
 
-//TODO move to extension package
+/**
+ * Extension for handling {@link MessageBundle}s.
+ *
+ * @see MessageBundle
+ * @see MessageTemplate
+ */
 public class MessageBundleExtension implements Extension, Deactivatable
 {
     private final Collection<AnnotatedType<?>> messageBundleTypes = new HashSet<AnnotatedType<?>>();
@@ -50,20 +55,20 @@ public class MessageBundleExtension implements Extension, Deactivatable
     private Boolean isActivated = null;
 
     @SuppressWarnings("UnusedDeclaration")
-    protected void init(@Observes BeforeBeanDiscovery afterBeanDiscovery)
+    protected void init(@Observes BeforeBeanDiscovery beforeBeanDiscovery)
     {
         initActivation();
     }
 
     @SuppressWarnings("UnusedDeclaration")
-    protected void detectInterfaces(@Observes ProcessAnnotatedType<?> event)
+    protected void detectInterfaces(@Observes ProcessAnnotatedType<?> processAnnotatedType)
     {
         if (!isActivated)
         {
             return;
         }
 
-        AnnotatedType<?> type = event.getAnnotatedType();
+        AnnotatedType<?> type = processAnnotatedType.getAnnotatedType();
 
         if (type.isAnnotationPresent(MessageBundle.class))
         {
@@ -75,6 +80,13 @@ public class MessageBundleExtension implements Extension, Deactivatable
 
     private void validateMessageBundle(Class<?> currentClass)
     {
+        // sanity check: annotated class must be an Interface
+        if (!currentClass.isInterface())
+        {
+            throw new IllegalStateException("@MessageBundle must only be used on Interfaces, but got used on class " +
+                    currentClass.getName());
+        }
+
         for (Method currentMethod : currentClass.getDeclaredMethods())
         {
             if (!currentMethod.isAnnotationPresent(MessageTemplate.class))
@@ -116,9 +128,16 @@ public class MessageBundleExtension implements Extension, Deactivatable
                 ". That is required for return-type " + Message.class.getName());
     }
 
-    // according to the Java EE 6 javadoc (the authority according to the powers
-    // that be),
-    // this is the correct order of type parameters
+    /**
+     * Part of a workaround for very old CDI containers. The spec originally had a
+     * mismatch in the generic parameters of ProcessProducerMethod between the JavaDoc
+     * and the spec PDF.
+     *
+     * According to the Java EE 6 javadoc (the authority according to the powers
+     * that be), this is the correct order of type parameters.
+     *
+     * @see #detectProducersInverted(javax.enterprise.inject.spi.ProcessProducerMethod)
+     */
     @SuppressWarnings("UnusedDeclaration")
     protected void detectProducers(@Observes ProcessProducerMethod<Object, TypedMessageBundleProducer> event)
     {
@@ -130,8 +149,16 @@ public class MessageBundleExtension implements Extension, Deactivatable
         captureProducers(event.getAnnotatedProducerMethod(), event.getBean());
     }
 
-    // according to JSR-299 spec, this is the correct order of type parameters
-    //X TODO re-visit it
+    /**
+     * Part of a workaround for very old CDI containers. The spec originally had a
+     * mismatch in the generic parameters of ProcessProducerMethod between the JavaDoc
+     * and the spec PDF.
+     *
+     * According to the old JSR-299 spec wording, this is the correct order of type parameters.
+     * This is now fixed in the spec as of today, but old containers might still fire it!
+     *
+     * @see #detectProducersInverted(javax.enterprise.inject.spi.ProcessProducerMethod)
+     */
     @Deprecated
     @SuppressWarnings("UnusedDeclaration")
     protected void detectProducersInverted(@Observes ProcessProducerMethod<TypedMessageBundleProducer, Object> event)

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/57c9f71a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleClass.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleClass.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleClass.java
new file mode 100644
index 0000000..6f0059f
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleClass.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.deltaspike.test.core.api.message.broken;
+
+import org.apache.deltaspike.core.api.message.Message;
+import org.apache.deltaspike.core.api.message.MessageContext;
+import org.apache.deltaspike.core.api.message.annotation.MessageBundle;
+import org.apache.deltaspike.core.api.message.annotation.MessageTemplate;
+
+/**
+ * This class is broken, as MessageBundle must only be used on Interfaces
+ */
+@MessageBundle
+public class BrokenMessageBundleClass
+{
+    @MessageTemplate("Welcome to DeltaSpike")
+    String welcomeToDeltaSpike()
+    {
+        return null;
+    }
+
+    @MessageTemplate("Welcome to %s")
+    Message welcomeTo(MessageContext messageContext, String name)
+    {
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/57c9f71a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleOnClassTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleOnClassTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleOnClassTest.java
new file mode 100644
index 0000000..435f21f
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/message/broken/BrokenMessageBundleOnClassTest.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.deltaspike.test.core.api.message.broken;
+
+import javax.enterprise.inject.spi.Extension;
+import javax.inject.Inject;
+
+import org.apache.deltaspike.core.impl.message.MessageBundleExtension;
+import org.apache.deltaspike.test.category.SeCategory;
+import org.apache.deltaspike.test.util.ArchiveUtils;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.container.test.api.ShouldThrowException;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.experimental.categories.Category;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests for broken MessageBundle definition
+ *
+ * This is currently disabled due to a few problems with DeploymentException validation:
+ * a.) some containers wrap the internally thrown IllegalStateException into another Exception
+ * b.) some arquillian container adapters throw up with a NPE
+ *
+ * TODO: fix the container and arquillian setup!
+ */
+//X @RunWith(Arquillian.class)
+@Category(SeCategory.class)
+public class BrokenMessageBundleOnClassTest
+{
+    @Inject
+    private BrokenMessageBundleClass messageBundle;
+
+    /**
+     * X TODO creating a WebArchive is only a workaround because JavaArchive
+     * cannot contain other archives.
+     */
+    @Deployment
+    @ShouldThrowException(IllegalStateException.class)
+    public static WebArchive deploy()
+    {
+        JavaArchive testJar = ShrinkWrap
+                .create(JavaArchive.class, "simpleMessageTest.jar")
+                .addPackage(BrokenMessageBundleClass.class.getPackage())
+                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+
+        return ShrinkWrap
+                .create(WebArchive.class, "simpleMessageTest.war")
+                .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive())
+                .addAsLibraries(testJar)
+                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
+                .addAsServiceProvider(Extension.class,
+                        MessageBundleExtension.class);
+    }
+
+    //X @Test
+    public void testSimpleMessage()
+    {
+        assertEquals("Welcome to DeltaSpike", messageBundle.welcomeToDeltaSpike());
+    }
+}