Subject: [PATCH] WELD-407, checking if AnnotatedType has constructor and if javaMembers are in AnnotatedType hierarchy

From: mbriskar <[email-masked]>
Date: Tue, 30 Apr 2013 22:50:46 +0200

---
 .../weld/annotated/AnnotatedTypeValidator.java     |   32 ++++++++++++++++++++
 .../weld/logging/messages/MetadataMessage.java     |    2 ++
 .../org/jboss/weld/messages/metadata_en.properties |    2 ++
 3 files changed, 36 insertions(+)

diff --git a/impl/src/main/java/org/jboss/weld/annotated/AnnotatedTypeValidator.java b/impl/src/main/java/org/jboss/weld/annotated/AnnotatedTypeValidator.java
index 82e9a50..6ce0115 100644
--- a/impl/src/main/java/org/jboss/weld/annotated/AnnotatedTypeValidator.java
+++ b/impl/src/main/java/org/jboss/weld/annotated/AnnotatedTypeValidator.java
@@ -16,8 +16,18 @@
  */
 package org.jboss.weld.annotated;
 
+import static org.jboss.weld.logging.Category.BOOTSTRAP;
+import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
 import static org.jboss.weld.logging.messages.MetadataMessage.INVALID_PARAMETER_POSITION;
 import static org.jboss.weld.logging.messages.MetadataMessage.METADATA_SOURCE_RETURNED_NULL;
+import static org.jboss.weld.logging.messages.MetadataMessage.NO_CONSTRUCTOR;
+import static org.jboss.weld.logging.messages.MetadataMessage.NOT_IN_HIERARCHY;
+
+
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
 
 import javax.enterprise.inject.spi.Annotated;
 import javax.enterprise.inject.spi.AnnotatedMember;
@@ -25,6 +35,7 @@ import javax.enterprise.inject.spi.AnnotatedParameter;
 import javax.enterprise.inject.spi.AnnotatedType;
 
 import org.jboss.weld.exceptions.IllegalArgumentException;
+import org.slf4j.cal10n.LocLogger;
 
 /**
  * Validates that methods of an {@link Annotated} implementation return sane values.
@@ -32,6 +43,7 @@ import org.jboss.weld.exceptions.IllegalArgumentException;
  *
  */
 public class AnnotatedTypeValidator {
+    private static final LocLogger log = loggerFactory().getLogger(BOOTSTRAP);
 
     private AnnotatedTypeValidator() {
     }
@@ -58,6 +70,7 @@ public class AnnotatedTypeValidator {
 
     public static void validateAnnotatedType(AnnotatedType<?> type) {
         validateAnnotated(type);
+        checkSensibility(type);
         checkNotNull(type.getJavaClass(), "getJavaClass()", type);
         checkNotNull(type.getFields(), "getFields()", type);
         checkNotNull(type.getConstructors(), "getConstructors()", type);
@@ -69,4 +82,23 @@ public class AnnotatedTypeValidator {
             throw new IllegalArgumentException(METADATA_SOURCE_RETURNED_NULL, methodName, target);
         }
     }
+
+    private static void checkSensibility(AnnotatedType<?> type) {
+        //check if it has a constructor
+        if(type.getConstructors().isEmpty()) {
+            log.warn(NO_CONSTRUCTOR,type);
+        }
+        //check if its javaMembers belong to the class hierarchy of annotatedType
+        List<AnnotatedMember<?>> members = new ArrayList<AnnotatedMember<?>>();
+        members.addAll(type.getConstructors());
+        members.addAll(type.getFields());
+        members.addAll(type.getMethods());
+        Set<Type> typeClosures = type.getTypeClosure();
+        for(AnnotatedMember<?> member: members) {
+           if(!typeClosures.contains(member.getJavaMember().getDeclaringClass())) {
+                log.warn(NOT_IN_HIERARCHY,member.toString(),type.toString());
+           }
+        }
+    }
+
 }
diff --git a/impl/src/main/java/org/jboss/weld/logging/messages/MetadataMessage.java b/impl/src/main/java/org/jboss/weld/logging/messages/MetadataMessage.java
index edf4d99..7086221 100644
--- a/impl/src/main/java/org/jboss/weld/logging/messages/MetadataMessage.java
+++ b/impl/src/main/java/org/jboss/weld/logging/messages/MetadataMessage.java
@@ -51,4 +51,6 @@ public enum MetadataMessage {
     @MessageId("001113")NOT_A_SCOPE,
     @MessageId("001114")METADATA_SOURCE_RETURNED_NULL,
     @MessageId("001115")INVALID_PARAMETER_POSITION,
+    @MessageId("001116")NO_CONSTRUCTOR,
+    @MessageId("001117")NOT_IN_HIERARCHY;
 }
diff --git a/impl/src/main/resources/org/jboss/weld/messages/metadata_en.properties b/impl/src/main/resources/org/jboss/weld/messages/metadata_en.properties
index f727407..420e6f4 100644
--- a/impl/src/main/resources/org/jboss/weld/messages/metadata_en.properties
+++ b/impl/src/main/resources/org/jboss/weld/messages/metadata_en.properties
@@ -14,3 +14,5 @@ TYPES_EMPTY=BeanAttributes.getTypes() may not return an empty set {0}
 NOT_A_SCOPE={0} defined on {1} is not a scope annotation
 METADATA_SOURCE_RETURNED_NULL={0} returned null for {1}
 INVALID_PARAMETER_POSITION=Parameter position {0} of parameter {1} is not valid
+NO_CONSTRUCTOR=AnnotatedType ({0}) without a constructor
+NOT_IN_HIERARCHY=Member ({0}) does not belong to the actual class hierarchy of the annotatedType ({1})
-- 
1.7.9.5