Author: vitold
Date: 2009-03-25 10:06:36 -0400 (Wed, 25 Mar 2009)
New Revision: 2201
Added:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/BeanChildrenChecker.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/JmsResourceChildrenChecker.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/ResourceChildrenChecker.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/SessionBeanChildrenChecker.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/SimpleBeanChildrenChecker.java
Modified:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
Log:
add method for validating Producer method and field declarations
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java 2009-03-25
10:38:49 UTC (rev 2200)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java 2009-03-25
14:06:36 UTC (rev 2201)
@@ -2,7 +2,6 @@
import java.io.IOException;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
@@ -19,11 +18,6 @@
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.jboss.webbeans.introspector.AnnotatedClass;
-import org.jboss.webbeans.introspector.AnnotatedField;
-import org.jboss.webbeans.introspector.AnnotatedMethod;
-import org.jboss.webbeans.introspector.MethodSignature;
-import org.jboss.webbeans.introspector.jlr.AnnotatedMethodImpl;
-import org.jboss.webbeans.introspector.jlr.MethodSignatureImpl;
import org.jboss.webbeans.resources.spi.ResourceLoadingException;
public class ParseXmlHelper
@@ -194,22 +188,6 @@
return elements;
}
-
- public static boolean isField(Element element, AnnotatedClass<?> beanClass,
AnnotatedClass<?> expectedType)
- {
- //TODO
- String fieldName = element.getParent().getName();
- AnnotatedField<?> beanField = beanClass.getDeclaredField(fieldName,
expectedType);
- if(beanField != null)
- return true;
- return false;
- }
-
- public static boolean isMethod(Element element, AnnotatedClass<?> beanClass,
AnnotatedClass<?> expectedType)
- {
- //TODO
- return false;
- }
private static Set<String> parseNamespaceFile(URL namespaceFile)
{
Modified: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java 2009-03-25 10:38:49
UTC (rev 2200)
+++ ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java 2009-03-25 14:06:36
UTC (rev 2201)
@@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
@@ -12,6 +13,9 @@
import java.util.Map;
import java.util.Set;
+import javax.annotation.Named;
+import javax.annotation.Stereotype;
+import javax.context.ScopeType;
import javax.inject.DefinitionException;
import javax.inject.DeploymentException;
import javax.inject.DeploymentType;
@@ -21,8 +25,14 @@
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.log.Log;
import org.jboss.webbeans.log.Logging;
+import org.jboss.webbeans.xml.check.BeanChildrenChecker;
+import org.jboss.webbeans.xml.check.JmsResourceChildrenChecker;
+import org.jboss.webbeans.xml.check.ResourceChildrenChecker;
+import org.jboss.webbeans.xml.check.SessionBeanChildrenChecker;
+import org.jboss.webbeans.xml.check.SimpleBeanChildrenChecker;
public class XmlParser
{
@@ -30,6 +40,8 @@
private final XmlEnvironment environment;
+ private List<BeanChildrenChecker> childrenCheckers = new
ArrayList<BeanChildrenChecker>();
+
private boolean haveAnyDeployElement = false;
private Map<String, Set<String>> packagesMap = new HashMap<String,
Set<String>>();
@@ -37,6 +49,10 @@
public XmlParser(XmlEnvironment environment)
{
this.environment = environment;
+ this.childrenCheckers.add(new JmsResourceChildrenChecker());
+ this.childrenCheckers.add(new ResourceChildrenChecker());
+ this.childrenCheckers.add(new SessionBeanChildrenChecker());
+ this.childrenCheckers.add(new SimpleBeanChildrenChecker());
}
public void parse()
@@ -61,6 +77,7 @@
{
AnnotatedClass<?> beanClass = ParseXmlHelper.loadElementClass(beanElement,
Object.class, environment, packagesMap);
checkProduces(beanElement, beanClass);
+ checkBeanChildren(beanElement, beanClass);
beanClasses.add(beanClass);
}
@@ -187,12 +204,26 @@
return deploymentClasses;
}
- public void checkProduces(Element beanElement, AnnotatedClass<?> beanClass)
- {//TODO
- Iterator<?> childIterator = beanElement.elementIterator();
- while(childIterator.hasNext())
+ private void checkBeanChildren(Element beanElement, AnnotatedClass<?>
beanClass)
+ {
+ for (BeanChildrenChecker childrenChecker : childrenCheckers)
{
- Element beanChild = (Element)childIterator.next();
+ if (childrenChecker.accept(beanElement, beanClass))
+ {
+ childrenChecker.checkChildren(beanElement);
+ return;
+ }
+ }
+
+ throw new DefinitionException("Definition of a bean " +
beanElement.getName() + " is incorrect");
+ }
+
+ private void checkProduces(Element beanElement, AnnotatedClass<?> beanClass)
+ {
+ Iterator<?> beanIterator = beanElement.elementIterator();
+ while(beanIterator.hasNext())
+ {
+ Element beanChild = (Element)beanIterator.next();
List<Element> producesElements =
ParseXmlHelper.findElementsInEeNamespace(beanChild, XmlConstants.PRODUCES);
if(producesElements.size() == 0)
@@ -201,18 +232,93 @@
if(producesElements.size() > 1)
throw new DefinitionException("There is more than one child
<Produces> element for <" + beanChild.getName() + ">
element");
+ List<AnnotatedClass<?>> producesChildTypes = new
ArrayList<AnnotatedClass<?>>();
+
Element producesElement = producesElements.get(0);
+ Iterator<?> producesIt = producesElement.elementIterator();
+ while(producesIt.hasNext())
+ {
+ Element producesChild = (Element)producesIt.next();
+ AnnotatedClass<?> producesChildClass =
ParseXmlHelper.loadElementClass(producesChild, Object.class, environment, packagesMap);
+ Class<?> producesChildType = producesChildClass.getRawType();
+ boolean isJavaClass = !producesChildType.isEnum() &&
!producesChildType.isPrimitive() && !producesChildType.isInterface();
+ boolean isInterface = producesChildType.isInterface() &&
!producesChildType.isAnnotation();
+ if(isJavaClass || isInterface)
+ {
+ producesChildTypes.add(producesChildClass);
+ continue;
+ }
+ if(producesChildType.isAnnotation())
+ {
+ if(producesChildClass.isAnnotationPresent(DeploymentType.class) ||
+ producesChildClass.isAnnotationPresent(ScopeType.class) ||
+ producesChildClass.isAnnotationPresent(Stereotype.class) ||
+ producesChildClass.isAnnotationPresent(Named.class))
+ continue;
+
+ throw new DefinitionException("<" + producesChild.getName() +
"> direct child of <Produces> element for <" + beanChild.getName()
+ + "> in bean" + beanElement.getName() + "must be
DeploymentType or ScopeType or Stereotype or Named");
+ }
+ throw new DefinitionException("Only Java class, interface type and Java
annotation type can be " +
+ "direct child of <Produces> element for <" +
beanChild.getName() + "> in bean" + beanElement.getName() +
+ ". Element <" + producesChild.getName() + "> is
incorrect");
+ }
- if(ParseXmlHelper.isField(producesElement, beanClass, beanClass))
+ if(producesChildTypes.size() != 1)
+ throw new DefinitionException("More than one or no one child element of
<Produces> element for <" + beanChild.getName() +
+ "> in bean" + beanElement.getName() + " represents a
Java class or interface type");
+
+ AnnotatedClass<?> expectedType = producesChildTypes.get(0);
+
+ Method beanMethod = null;
+ AnnotatedField<?> beanField =
beanClass.getDeclaredField(beanChild.getName(), expectedType);
+
+ try
{
+ List<Class<?>> paramClassesList = new
ArrayList<Class<?>>();
+ Iterator<?> beanChildIt = beanChild.elementIterator();
+ while(beanChildIt.hasNext())
+ {
+ Element methodChild = (Element)beanChildIt.next();
+ if(methodChild.getName().equalsIgnoreCase(XmlConstants.PRODUCES))
+ continue;
+ paramClassesList.add(ParseXmlHelper.loadElementClass(methodChild,
Object.class, environment, packagesMap).getRawType());
+ }
+ Class<?>[] paramClasses =
(Class<?>[])paramClassesList.toArray(new Class[0]);
+ beanMethod = beanClass.getRawType().getDeclaredMethod(beanChild.getName(),
paramClasses);
+ }
+ catch (SecurityException e)
+ {}
+ catch (NoSuchMethodException e)
+ {}
+
+ if(beanField != null && beanMethod != null)
+ throw new DefinitionException("Class '" + beanClass.getName() +
"' has produser field and method with the same name '" +
+ beanField.getName() + "'");
+
+ if(beanField != null)
+ {
if(beanChild.elements().size() > 1)
throw new DefinitionException("There is more than one direct child
element for producer field <" + beanChild.getName() + ">");
+ continue;
}
- if(ParseXmlHelper.isMethod(producesElement, beanClass, beanClass))
- {}
+ if(beanMethod != null)
+ {
+ Iterator<?> beanChildIt = producesElement.elementIterator();
+ while(beanChildIt.hasNext())
+ {
+ Element element = (Element)beanChildIt.next();
+ if(!element.getName().equalsIgnoreCase(XmlConstants.PRODUCES) &&
+ ParseXmlHelper.findElementsInEeNamespace(beanChild,
XmlConstants.INTERCEPTOR).size() == 0)
+ throw new DefinitionException("Only Produces and interceptor
binding types can be direct childs of a producer " +
+ "method '" + beanChild.getName() + "'
declaration in bean '" + beanElement.getName() + "'");
+ }
+ continue;
+ }
-// throw new DefinitionException("A producer doesn't declared in class
file as method or field");
+ throw new DefinitionException("A producer '" + beanChild.getName()
+ "' doesn't declared in '" + beanElement.getName() +
+ "' class file as method or field");
}
}
Added: ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/BeanChildrenChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/BeanChildrenChecker.java
(rev 0)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/BeanChildrenChecker.java 2009-03-25
14:06:36 UTC (rev 2201)
@@ -0,0 +1,11 @@
+package org.jboss.webbeans.xml.check;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+
+public interface BeanChildrenChecker
+{
+ boolean accept(Element element, AnnotatedClass<?> beanClass);
+
+ void checkChildren(Element element);
+}
Added:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/JmsResourceChildrenChecker.java
===================================================================
---
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/JmsResourceChildrenChecker.java
(rev 0)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/JmsResourceChildrenChecker.java 2009-03-25
14:06:36 UTC (rev 2201)
@@ -0,0 +1,25 @@
+package org.jboss.webbeans.xml.check;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.ParseXmlHelper;
+import org.jboss.webbeans.xml.XmlConstants;
+
+public class JmsResourceChildrenChecker implements BeanChildrenChecker
+{
+ public boolean accept(Element element, AnnotatedClass<?> beanClass)
+ {
+ if (ParseXmlHelper.isJavaEeNamespace(element) &&
+ (element.getName().equalsIgnoreCase(XmlConstants.TOPIC) ||
+ element.getName().equalsIgnoreCase(XmlConstants.QUEUE)))
+ return true;
+ return false;
+ }
+
+ public void checkChildren(Element element)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/ResourceChildrenChecker.java
===================================================================
--- ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/ResourceChildrenChecker.java
(rev 0)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/ResourceChildrenChecker.java 2009-03-25
14:06:36 UTC (rev 2201)
@@ -0,0 +1,41 @@
+package org.jboss.webbeans.xml.check;
+
+import java.util.Iterator;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.ParseXmlHelper;
+import org.jboss.webbeans.xml.XmlConstants;
+
+public class ResourceChildrenChecker implements BeanChildrenChecker
+{
+
+ public boolean accept(Element element, AnnotatedClass<?> beanClass)
+ {
+ if (ParseXmlHelper.isJavaEeNamespace(element) &&
+ (element.getName().equalsIgnoreCase(XmlConstants.TOPIC) ||
+ element.getName().equalsIgnoreCase(XmlConstants.QUEUE)))
+ return false;
+
+ Iterator<?> elIterator = element.elementIterator();
+ while (elIterator.hasNext())
+ {
+ Element child = (Element) elIterator.next();
+ if (ParseXmlHelper.isJavaEeNamespace(child) &&
+ (child.getName().equalsIgnoreCase(XmlConstants.RESOURCE) ||
+ child.getName().equalsIgnoreCase(XmlConstants.PERSISTENCE_CONTEXT)
||
+ child.getName().equalsIgnoreCase(XmlConstants.PERSISTENCE_UNIT) ||
+ child.getName().equalsIgnoreCase(XmlConstants.EJB) ||
+ child.getName().equalsIgnoreCase(XmlConstants.WEB_SERVICE_REF)))
+ return true;
+ }
+ return false;
+ }
+
+ public void checkChildren(Element element)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/SessionBeanChildrenChecker.java
===================================================================
---
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/SessionBeanChildrenChecker.java
(rev 0)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/SessionBeanChildrenChecker.java 2009-03-25
14:06:36 UTC (rev 2201)
@@ -0,0 +1,27 @@
+package org.jboss.webbeans.xml.check;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.CurrentManager;
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.XmlConstants;
+
+public class SessionBeanChildrenChecker implements BeanChildrenChecker
+{
+
+ public boolean accept(Element element, AnnotatedClass<?> beanClass)
+ {
+ ManagerImpl manager = CurrentManager.rootManager();
+ if (manager.getEjbDescriptorCache().containsKey(element.getName()) ||
+ element.attribute(XmlConstants.EJB_NAME) != null)
+ return true;
+ return false;
+ }
+
+ public void checkChildren(Element element)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added:
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/SimpleBeanChildrenChecker.java
===================================================================
---
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/SimpleBeanChildrenChecker.java
(rev 0)
+++
ri/trunk/impl/src/main/java/org/jboss/webbeans/xml/check/SimpleBeanChildrenChecker.java 2009-03-25
14:06:36 UTC (rev 2201)
@@ -0,0 +1,29 @@
+package org.jboss.webbeans.xml.check;
+
+import org.dom4j.Element;
+import org.jboss.webbeans.CurrentManager;
+import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.introspector.AnnotatedClass;
+import org.jboss.webbeans.xml.XmlConstants;
+
+public class SimpleBeanChildrenChecker implements BeanChildrenChecker
+{
+
+ public boolean accept(Element element, AnnotatedClass<?> beanClass)
+ {
+ ManagerImpl manager = CurrentManager.rootManager();
+ boolean isSessionBean =
manager.getEjbDescriptorCache().containsKey(element.getName()) ||
+ element.attribute(XmlConstants.EJB_NAME) !=
null;
+
+ if (!beanClass.isAbstract() && !isSessionBean &&
!beanClass.isParameterizedType())
+ return true;
+
+ return false;
+ }
+
+ public void checkChildren(Element element)
+ {
+ // TODO Auto-generated method stub
+
+ }
+}