Skip to content

Commit

Permalink
implement <Array> for constructor parameters
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@2665 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
Victor Yarmolovich committed May 7, 2009
1 parent 0f5e959 commit e4484d8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 32 deletions.
40 changes: 40 additions & 0 deletions impl/src/main/java/org/jboss/webbeans/xml/ParseXmlHelper.java
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -36,7 +37,12 @@
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.io.SAXReader;
import org.jboss.webbeans.bootstrap.api.ServiceRegistry;
import org.jboss.webbeans.introspector.AnnotatedClass;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.jlr.AnnotatedClassImpl;
import org.jboss.webbeans.resources.ClassTransformer;
import org.jboss.webbeans.resources.spi.ResourceLoader;
import org.jboss.webbeans.resources.spi.ResourceLoadingException;
import org.xml.sax.SAXException;

Expand Down Expand Up @@ -309,4 +315,38 @@ public static void checkForUniqueElements(List<Class<? extends Annotation>> list
throw new DefinitionException("A certain annotation type is declared more than once as a binding type, " +
"interceptor binding type or stereotype using XML");
}

public static AnnotatedClass<?> obtainArray(Element arrayElement, XmlEnvironment environment, Map<String, Set<String>> packagesMap)
{
AnnotatedClass<?> arrayType = obtainArrayType(arrayElement, environment, packagesMap);
Object array = Array.newInstance(arrayType.getRawType(), 0);
AnnotatedClass<?> result = AnnotatedClassImpl.of(array.getClass(), new ClassTransformer());
return result;
}

public static AnnotatedClass<?> obtainArrayType(Element arrayElement, XmlEnvironment environment, Map<String, Set<String>> packagesMap)
{
AnnotatedClass<?> arrayType = null;

boolean haveNotAnnotation = false;
Iterator<?> arrayIterator = arrayElement.elementIterator();
while (arrayIterator.hasNext())
{
Element arrayChild = (Element) arrayIterator.next();
AnnotatedClass<?> arrayChildType = ParseXmlHelper.loadElementClass(arrayChild, Object.class, environment, packagesMap);
boolean isAnnotation = arrayChildType.getRawType().isAnnotation();
if (!isAnnotation)
{
if (haveNotAnnotation)
throw new DefinitionException("<Array> element have second child which is not annotation, it is '" +
arrayChild.getName() + "'");
haveNotAnnotation = true;
arrayType = arrayChildType;
}
}
if (!haveNotAnnotation)
throw new DefinitionException("<Array> element must have one child elemen which is not annotation");

return arrayType;
}
}
20 changes: 1 addition & 19 deletions impl/src/main/java/org/jboss/webbeans/xml/XmlParser.java
Expand Up @@ -98,25 +98,7 @@ private void checkChildrenForArray(Element element)
Element child = (Element) childIterator.next();

if (XmlConstants.ARRAY.equalsIgnoreCase(child.getName()))
{
boolean haveNotAnnotation = false;
Iterator<?> arrayIterator = child.elementIterator();
while (arrayIterator.hasNext())
{
Element arrayChild = (Element) arrayIterator.next();
AnnotatedClass<?> arrayChildType = ParseXmlHelper.loadElementClass(arrayChild, Object.class, environment, packagesMap);
boolean isAnnotation = arrayChildType.getRawType().isAnnotation();
if (!isAnnotation)
{
if (haveNotAnnotation)
throw new DefinitionException("<Array> element have second child which is not annotation, it is '" +
arrayChild.getName() + "'");
haveNotAnnotation = true;
}
}
if (!haveNotAnnotation)
throw new DefinitionException("<Array> element must have one child elemen which is not annotation");
}
ParseXmlHelper.obtainArrayType(child, environment, packagesMap);
else
checkChildrenForArray(child);
}
Expand Down
Expand Up @@ -109,7 +109,12 @@ public void checkChildren(Element beanElement, AnnotatedClass<?> beanClass)
private void checkBeanChild(Element beanChildElement, AnnotatedClass<?> beanClass)
{
if (XmlConstants.ARRAY.equalsIgnoreCase(beanChildElement.getName()))
{
// bean child element declaring an array parameter of the bean constructor
AnnotatedClass<?> array = ParseXmlHelper.obtainArray(beanChildElement, environment, packagesMap);
constructorParameters.add(array);
return;
}

List<AnnotatedClass<?>> beanChildClassList = ParseXmlHelper.tryLoadElementClass(beanChildElement, Object.class, environment, packagesMap);

Expand All @@ -120,7 +125,7 @@ private void checkBeanChild(Element beanChildElement, AnnotatedClass<?> beanClas
Namespace beanChildNamespace = beanChildElement.getNamespace();
if (beanChildNamespace.equals(beanNamespace))
{
// bean child element declaring a method or field of the bean.
// bean child element declaring a method or field of the bean
checkFieldOrMethodChild(beanChildElement, beanClass);
return;
}
Expand Down
Expand Up @@ -82,18 +82,18 @@ protected void checkForConstructor(Element beanElement, AnnotatedClass<?> beanCl
if (parameters.size() != constructorParameters.size())
continue;

boolean isMacthable = true;
boolean isMatchable = true;

for (int i = 0; i < parameters.size(); i++)
{
if (!parameters.get(i).isAssignableFrom(constructorParameters.get(i)))
{
isMacthable = false;
isMatchable = false;
break;
}
}

if (isMacthable)
if (isMatchable)
matchableConstructors.add(constructor);
}

Expand Down
Expand Up @@ -14,9 +14,10 @@ public Order()
this.val = 0;
}

public Order(int val)
public Order(int val, String[] strArr)
{
this.val = val;
this.strArr = strArr;
}

public int getVal()
Expand Down
Expand Up @@ -3,11 +3,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:java:org.jboss.webbeans.test.unit.xml.parser.schema.valid http://mydomain.com/myapp/schema-1.2.xsd">
<myapp:Order>
<Array>
<String />
</Array>
<myapp:getVal />
<myapp:val />
<Integer />
<Array>
<String />
</Array>
</myapp:Order>
</Beans>
Expand Up @@ -29,12 +29,12 @@
<myapp:TestInterceptorBindingType />
<myapp:TestStereotype />
<myapp:TestDeploymentType />
<Array>
<String />
</Array>
<myapp:getVal />
<myapp:val />
<Integer />
<Array>
<String />
</Array>
</myapp:Order>
<!--
<myapp:PaymentService>
Expand All @@ -48,13 +48,13 @@
</Resource>
</myapp:PaymentService>
-->

<!--
<Topic>
<Resource>
<name>java:app/service/PaymentService</name>
</Resource>
</Topic>

-->
<Decorators>
<myapp:TestDecorator />
</Decorators>
Expand Down

0 comments on commit e4484d8

Please sign in to comment.