Author: swd847
Date: 2010-10-14 05:39:25 -0400 (Thu, 14 Oct 2010)
New Revision: 13851
Modified:
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ParametersXmlItem.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/XmlItemType.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java
modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/simple/Bean2.java
modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/simple/SimpleBeanTest.java
modules/xml/trunk/impl/src/test/resources/org/jboss/seam/xml/test/simple/simple-beans.xml
Log:
Allow the user to resolve fields and methods with the same name
Modified:
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ParametersXmlItem.java
===================================================================
---
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ParametersXmlItem.java 2010-10-14
07:56:30 UTC (rev 13850)
+++
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ParametersXmlItem.java 2010-10-14
09:39:25 UTC (rev 13851)
@@ -37,7 +37,7 @@
public Set<TypeOccuranceInformation> getAllowedItem()
{
- return Collections.singleton(TypeOccuranceInformation.of(XmlItemType.PARAMETER, 1,
null));
+ return Collections.singleton(TypeOccuranceInformation.of(XmlItemType.PARAMETER,
null, null));
}
}
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/XmlItemType.java
===================================================================
---
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/XmlItemType.java 2010-10-14
07:56:30 UTC (rev 13850)
+++
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/XmlItemType.java 2010-10-14
09:39:25 UTC (rev 13851)
@@ -23,5 +23,37 @@
public enum XmlItemType
{
- CLASS, METHOD, FIELD, ANNOTATION, VALUE, ENTRY, KEY, DEPENDENCY, PARAMETERS,
PARAMETER, ARRAY, REPLACE, MODIFIES;
+ CLASS, METHOD, FIELD, ANNOTATION, VALUE("value", "v"),
ENTRY("entry", "e"), KEY("key", "k"), DEPENDENCY,
PARAMETERS("parameters"), PARAMETER, ARRAY("array"),
REPLACE("replaces"), MODIFIES("modifies");
+
+ private final String elementName;
+ private final String alias;
+
+ private XmlItemType(String elementName, String alias)
+ {
+ this.elementName = elementName;
+ this.alias = alias;
+ }
+
+ private XmlItemType(String elementName)
+ {
+ this.elementName = elementName;
+ this.alias = null;
+ }
+
+ private XmlItemType()
+ {
+ this.elementName = "";
+ this.alias = "";
+ }
+
+ public String getElementName()
+ {
+ return elementName;
+ }
+
+ public String getAlias()
+ {
+ return alias;
+ }
+
}
Modified:
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java
===================================================================
---
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java 2010-10-14
07:56:30 UTC (rev 13850)
+++
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java 2010-10-14
09:39:25 UTC (rev 13851)
@@ -36,7 +36,6 @@
import org.jboss.seam.xml.parser.SaxNode;
import org.jboss.seam.xml.util.PropertyUtils;
import org.jboss.seam.xml.util.TypeOccuranceInformation;
-import org.jboss.seam.xml.util.XmlConfigurationException;
import org.jboss.weld.extensions.properties.Property;
import org.jboss.weld.extensions.properties.query.NamedPropertyCriteria;
import org.jboss.weld.extensions.properties.query.PropertyQueries;
@@ -106,7 +105,7 @@
// if the item can be a method of a FIELD
if (TypeOccuranceInformation.isTypeInSet(parent.getAllowedItem(),
XmlItemType.METHOD) || TypeOccuranceInformation.isTypeInSet(parent.getAllowedItem(),
XmlItemType.FIELD))
{
- return resolveMethodOrField(name, parent, node.getInnerText(),
node.getDocument(), node.getLineNo());
+ return resolveMethodOrField(name, parent, node);
}
else
{
@@ -120,7 +119,7 @@
return null;
}
- public static XmlItem resolveMethodOrField(String name, XmlItem parent, String
innerText, String document, int lineno)
+ public static XmlItem resolveMethodOrField(String name, XmlItem parent, SaxNode node)
{
Class<?> p = parent.getJavaClass();
boolean methodFound = Reflections.methodExists(p, name);
@@ -130,17 +129,29 @@
if (methodFound && property != null)
{
- throw new XmlConfigurationException(parent.getJavaClass().getName() + " has
both a method and a property named " + name + " and so cannot be configured via
XML", document, lineno);
+ // if there is both a method and a field of the same name
+ // we look for the <parameters> element
+ for (SaxNode child : node.getChildren())
+ {
+ if (child.getName().equals(XmlItemType.PARAMETERS.getElementName()))
+ {
+ property = null;
+ }
+ }
+ if (property != null)
+ {
+ methodFound = false;
+ }
}
if (methodFound)
{
- return new MethodXmlItem(parent, name, document, lineno);
+ return new MethodXmlItem(parent, name, node.getDocument(), node.getLineNo());
}
else if (property != null)
{
// ensure the property is accessible
PropertyUtils.setAccessible(property);
- return new PropertyXmlItem(parent, property, innerText, null, document,
lineno);
+ return new PropertyXmlItem(parent, property, node.getInnerText(), null,
node.getDocument(), node.getLineNo());
}
return null;
}
Modified:
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java
===================================================================
---
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java 2010-10-14
07:56:30 UTC (rev 13850)
+++
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/RootNamespaceElementResolver.java 2010-10-14
09:39:25 UTC (rev 13851)
@@ -57,31 +57,31 @@
XmlItem getRootItem(SaxNode node, XmlItem parent)
{
String item = node.getName();
- if (item.equals("value") || item.equals("v"))
+ if (item.equals(XmlItemType.VALUE.getElementName()) ||
item.equals(XmlItemType.VALUE.getAlias()))
{
return new ValueXmlItem(parent, node.getInnerText(), node.getDocument(),
node.getLineNo());
}
- else if (item.equals("key") || item.equals("k"))
+ else if (item.equals(XmlItemType.KEY.getElementName()) ||
item.equals(XmlItemType.KEY.getAlias()))
{
return new KeyXmlItem(parent, node.getInnerText(), node.getDocument(),
node.getLineNo());
}
- else if (item.equals("entry") || item.equals("e"))
+ else if (item.equals(XmlItemType.ENTRY.getElementName()) ||
item.equals(XmlItemType.ENTRY.getAlias()))
{
return new EntryXmlItem(parent, node.getDocument(), node.getLineNo());
}
- else if (item.equals("array"))
+ else if (item.equals(XmlItemType.ARRAY.getElementName()))
{
return new ArrayXmlItem(parent, node.getAttributes(), node.getDocument(),
node.getLineNo());
}
- else if (item.equals("replaces"))
+ else if (item.equals(XmlItemType.REPLACE.getElementName()))
{
return new ReplacesXmlItem(parent, node.getDocument(), node.getLineNo());
}
- else if (item.equals("modifies"))
+ else if (item.equals(XmlItemType.MODIFIES.getElementName()))
{
return new ModifiesXmlItem(parent, node.getDocument(), node.getLineNo());
}
- else if (item.equals("parameters"))
+ else if (item.equals(XmlItemType.PARAMETERS.getElementName()))
{
return new ParametersXmlItem(parent, node.getDocument(), node.getLineNo());
}
Modified: modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/simple/Bean2.java
===================================================================
---
modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/simple/Bean2.java 2010-10-14
07:56:30 UTC (rev 13850)
+++
modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/simple/Bean2.java 2010-10-14
09:39:25 UTC (rev 13851)
@@ -41,4 +41,6 @@
{
return new Bean3();
}
+
+ public String produceBean3;
}
Modified:
modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/simple/SimpleBeanTest.java
===================================================================
---
modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/simple/SimpleBeanTest.java 2010-10-14
07:56:30 UTC (rev 13850)
+++
modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/simple/SimpleBeanTest.java 2010-10-14
09:39:25 UTC (rev 13851)
@@ -51,6 +51,9 @@
Assert.assertTrue(x != null);
Assert.assertTrue(x.bean2 != null);
+ Bean2 bean2 = getReference(Bean2.class);
+ Assert.assertEquals("test value", bean2.produceBean3);
+
Bean3 y = getReference(Bean3.class);
Assert.assertTrue(y != null);
Assert.assertTrue("Post construct method not called", x.value == 1);
Modified:
modules/xml/trunk/impl/src/test/resources/org/jboss/seam/xml/test/simple/simple-beans.xml
===================================================================
---
modules/xml/trunk/impl/src/test/resources/org/jboss/seam/xml/test/simple/simple-beans.xml 2010-10-14
07:56:30 UTC (rev 13850)
+++
modules/xml/trunk/impl/src/test/resources/org/jboss/seam/xml/test/simple/simple-beans.xml 2010-10-14
09:39:25 UTC (rev 13851)
@@ -20,7 +20,9 @@
<replaces/>
<test:produceBean3>
<Produces/>
+ <parameters/>
</test:produceBean3>
+ <test:produceBean3>test value</test:produceBean3>
</test:Bean2>
<test:OverriddenBean>