[seam-commits] Seam SVN: r13662 - in modules/xml/trunk/impl/src: main/java/org/jboss/seam/xml/model and 3 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Fri Aug 27 22:48:04 EDT 2010
Author: swd847
Date: 2010-08-27 22:48:04 -0400 (Fri, 27 Aug 2010)
New Revision: 13662
Added:
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/util/Reflections.java
Removed:
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/AbstractFieldXmlItem.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java
Modified:
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/bootstrap/XmlExtension.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java
modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java
modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/NamespaceResolverTest.java
Log:
refactor property xml items to use PropertyQuery
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/bootstrap/XmlExtension.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/bootstrap/XmlExtension.java 2010-08-28 01:45:24 UTC (rev 13661)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/bootstrap/XmlExtension.java 2010-08-28 02:48:04 UTC (rev 13662)
@@ -129,7 +129,7 @@
{
for (String i : r.getProblems())
{
- errors.add(new RuntimeException(i));
+ errors.add(new Exception(i));
}
}
for (BeanResult<?> b : r.getFlattenedBeans())
Deleted: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/AbstractFieldXmlItem.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/AbstractFieldXmlItem.java 2010-08-28 01:45:24 UTC (rev 13661)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/AbstractFieldXmlItem.java 2010-08-28 02:48:04 UTC (rev 13662)
@@ -1,179 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc., and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.seam.xml.model;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.enterprise.inject.spi.BeanManager;
-
-import org.jboss.seam.xml.core.BeanResult;
-import org.jboss.seam.xml.fieldset.ArrayFieldSet;
-import org.jboss.seam.xml.fieldset.CollectionFieldSet;
-import org.jboss.seam.xml.fieldset.FieldValueObject;
-import org.jboss.seam.xml.fieldset.MapFieldSet;
-import org.jboss.seam.xml.fieldset.SimpleFieldValue;
-import org.jboss.seam.xml.util.TypeOccuranceInformation;
-import org.jboss.seam.xml.util.XmlConfigurationException;
-import org.jboss.weld.extensions.util.properties.Property;
-
-/**
- * represents either a field or a property of a bean
- *
- * @author stuart
- *
- */
-public abstract class AbstractFieldXmlItem extends AbstractXmlItem
-{
-
- protected Property<?> property;
- protected FieldValueObject fieldValue;
- protected HashSet<TypeOccuranceInformation> allowed = new HashSet<TypeOccuranceInformation>();
- private final Class<?> overridenFieldType;
- List<BeanResult<?>> inlineBeans = new ArrayList<BeanResult<?>>();
-
- public AbstractFieldXmlItem(XmlItemType type, XmlItem parent, Class<?> javaClass, String innerText, Map<String, String> attributes, Class<?> overridenFieldType, String document, int lineno)
- {
- super(type, parent, javaClass, innerText, attributes, document, lineno);
- this.overridenFieldType = overridenFieldType;
- }
-
- public FieldValueObject getFieldValue()
- {
- return fieldValue;
- }
-
- @Override
- public boolean resolveChildren(BeanManager manager)
- {
- List<EntryXmlItem> mapEntries = new ArrayList<EntryXmlItem>();
- List<ValueXmlItem> valueEntries = new ArrayList<ValueXmlItem>();
- if (fieldValue == null)
- {
- for (XmlItem i : children)
- {
- if (i.getType() == XmlItemType.VALUE)
- {
- valueEntries.add((ValueXmlItem) i);
- }
- else if (i.getType() == XmlItemType.ENTRY)
- {
- mapEntries.add((EntryXmlItem) i);
- }
-
- }
- }
- if (!mapEntries.isEmpty() || !valueEntries.isEmpty())
- {
- if (Map.class.isAssignableFrom(getFieldType()))
- {
- if (!valueEntries.isEmpty())
- {
- throw new XmlConfigurationException("Map fields cannot have <value> elements as children,only <entry> elements Field:" + getDeclaringClass().getName() + '.' + getFieldName(), getDocument(), getLineno());
- }
- if (!mapEntries.isEmpty())
- {
- for (EntryXmlItem entry : mapEntries)
- {
- // resolve inline beans if nessesary
- Set<BeanResult<?>> beans = entry.getBeanResults(manager);
- inlineBeans.addAll(beans);
-
- }
- fieldValue = new MapFieldSet(property, mapEntries);
- }
- }
- else if (Collection.class.isAssignableFrom(getFieldType()) || getFieldType().isArray())
- {
- if (!mapEntries.isEmpty())
- {
- throw new XmlConfigurationException("Collection fields must be set using <value> not <entry> Field:" + getDeclaringClass().getName() + '.' + getFieldName(), getDocument(), getLineno());
- }
- if (!valueEntries.isEmpty())
- {
- for (ValueXmlItem value : valueEntries)
- {
- // resolve inline beans if nessesary
- BeanResult<?> result = value.getBeanResult(manager);
- if (result != null)
- {
- inlineBeans.add(result);
- }
- }
- if (getFieldType().isArray())
- {
- fieldValue = new ArrayFieldSet(property, valueEntries);
- }
- else
- {
- fieldValue = new CollectionFieldSet(property, valueEntries);
- }
- }
- }
- else
- {
- if (!mapEntries.isEmpty())
- {
- throw new XmlConfigurationException("Only Map fields can be set using <entry> Field:" + getDeclaringClass().getName() + '.' + getFieldName(), getDocument(), getLineno());
- }
- if (valueEntries.size() != 1)
- {
- throw new XmlConfigurationException("Non collection fields can only have a single <value> element Field:" + getDeclaringClass().getName() + '.' + getFieldName(), getDocument(), getLineno());
- }
- ValueXmlItem value = valueEntries.get(0);
- BeanResult<?> result = value.getBeanResult(manager);
- fieldValue = new SimpleFieldValue(parent.getJavaClass(), property, value.getValue(), overridenFieldType);
- if (result != null)
- {
- inlineBeans.add(result);
- }
- }
- }
- return true;
- }
-
- public Set<TypeOccuranceInformation> getAllowedItem()
- {
- return allowed;
- }
-
- public abstract Class<?> getFieldType();
-
- public abstract String getFieldName();
-
- public abstract Class<?> getDeclaringClass();
-
- public Collection<? extends BeanResult> getInlineBeans()
- {
- return inlineBeans;
- }
-
- public Class<?> getOverridenFieldType()
- {
- return overridenFieldType;
- }
-
-}
\ No newline at end of file
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java 2010-08-28 01:45:24 UTC (rev 13661)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/ClassXmlItem.java 2010-08-28 02:48:04 UTC (rev 13662)
@@ -24,7 +24,6 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
-import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -45,6 +44,11 @@
import org.jboss.seam.xml.util.XmlConfigurationException;
import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder;
import org.jboss.weld.extensions.util.Reflections;
+import org.jboss.weld.extensions.util.properties.Properties;
+import org.jboss.weld.extensions.util.properties.Property;
+import org.jboss.weld.extensions.util.properties.query.NamedPropertyCriteria;
+import org.jboss.weld.extensions.util.properties.query.PropertyQueries;
+import org.jboss.weld.extensions.util.properties.query.PropertyQuery;
public class ClassXmlItem extends AbstractXmlItem
{
@@ -69,38 +73,22 @@
return allowed;
}
- public Set<AbstractFieldXmlItem> getShorthandFieldValues()
+ public Set<PropertyXmlItem> getShorthandFieldValues()
{
- Set<AbstractFieldXmlItem> values = new HashSet<AbstractFieldXmlItem>();
+ Set<PropertyXmlItem> values = new HashSet<PropertyXmlItem>();
for (Entry<String, String> e : attributes.entrySet())
{
-
- Field field = Reflections.findDeclaredField(getJavaClass(), e.getKey());
- if (field != null)
+ PropertyQuery<Object> query = PropertyQueries.createQuery(getJavaClass());
+ query.addCriteria(new NamedPropertyCriteria(e.getKey()));
+ Property<?> property = query.getFirstResult();
+ if (property != null)
{
- values.add(new FieldXmlItem(this, field, e.getValue(), null, document, lineno));
+ values.add(new PropertyXmlItem(this, property, e.getValue(), null, document, lineno));
}
else
{
- String methodName = "set" + Character.toUpperCase(e.getKey().charAt(0)) + e.getKey().substring(1);
- if (Reflections.methodExists(getJavaClass(), methodName))
- {
- Set<Method> methods = Reflections.getAllDeclaredMethods(getJavaClass());
- for (Method m : methods)
- {
- if (m.getName().equals(methodName) && m.getParameterTypes().length == 1)
- {
- values.add(new PropertyXmlItem(this, e.getKey(), m, e.getValue(), document, lineno));
- break;
- }
- }
- }
- else
- {
- throw new XmlConfigurationException("Could not resolve field: " + e.getKey(), document, lineno);
- }
+ throw new XmlConfigurationException("Could not resolve field: " + e.getKey(), document, lineno);
}
-
}
return values;
}
@@ -126,7 +114,7 @@
// get all the field values from the bean
Set<String> configuredFields = new HashSet<String>();
List<FieldValueObject> fields = new ArrayList<FieldValueObject>();
- for (AbstractFieldXmlItem xi : getChildrenOfType(AbstractFieldXmlItem.class))
+ for (PropertyXmlItem xi : getChildrenOfType(PropertyXmlItem.class))
{
inlineBeans.addAll(xi.getInlineBeans());
FieldValueObject f = xi.getFieldValue();
@@ -137,7 +125,7 @@
}
}
- for (AbstractFieldXmlItem f : getShorthandFieldValues())
+ for (PropertyXmlItem f : getShorthandFieldValues())
{
if (configuredFields.contains(f.getFieldName()))
{
@@ -170,13 +158,20 @@
constList.add(item);
}
}
- for (FieldXmlItem item : getChildrenOfType(FieldXmlItem.class))
+ for (PropertyXmlItem item : getChildrenOfType(PropertyXmlItem.class))
{
- for (AnnotationXmlItem fi : item.getChildrenOfType(AnnotationXmlItem.class))
+ if (item.getField() != null)
{
- Annotation a = AnnotationUtils.createAnnotation(fi);
- type.addToField(item.getField(), a);
+ for (AnnotationXmlItem fi : item.getChildrenOfType(AnnotationXmlItem.class))
+ {
+ Annotation a = AnnotationUtils.createAnnotation(fi);
+ type.addToField(item.getField(), a);
+ }
}
+ else if (!item.getChildrenOfType(AnnotationXmlItem.class).isEmpty())
+ {
+ throw new XmlConfigurationException("Property's that do not have an underlying field may not have annotations added to them", item.getDocument(), item.getLineno());
+ }
}
for (MethodXmlItem item : getChildrenOfType(MethodXmlItem.class))
{
@@ -232,23 +227,13 @@
{
boolean override = !getChildrenOfType(ReplacesXmlItem.class).isEmpty();
boolean extend = !getChildrenOfType(ModifiesXmlItem.class).isEmpty();
- Field member;
- try
- {
- member = VirtualProducerField.class.getField("field");
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
-
- BeanResultType beanType = BeanResultType.ADD;
if (override || extend)
{
throw new XmlConfigurationException("A virtual producer field may not containe <override> or <extend> tags", getDocument(), getLineno());
}
+ Field member = org.jboss.seam.xml.util.Reflections.getField(VirtualProducerField.class, "field");
ClassXmlItem vclass = new ClassXmlItem(null, VirtualProducerField.class, Collections.EMPTY_MAP, document, lineno);
- FieldXmlItem field = new FieldXmlItem(vclass, member, null, getJavaClass(), document, lineno);
+ PropertyXmlItem field = new PropertyXmlItem(vclass, Properties.createProperty(member), null, getJavaClass(), document, lineno);
vclass.addChild(field);
for (XmlItem i : this.getChildren())
{
Deleted: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java 2010-08-28 01:45:24 UTC (rev 13661)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/FieldXmlItem.java 2010-08-28 02:48:04 UTC (rev 13662)
@@ -1,110 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc., and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.seam.xml.model;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-
-import org.jboss.seam.xml.fieldset.ConstantFieldValue;
-import org.jboss.seam.xml.fieldset.ELFieldValue;
-import org.jboss.seam.xml.fieldset.FieldValue;
-import org.jboss.seam.xml.fieldset.SimpleFieldValue;
-import org.jboss.seam.xml.util.TypeOccuranceInformation;
-import org.jboss.weld.extensions.util.properties.Properties;
-import org.jboss.weld.extensions.util.properties.Property;
-
-public class FieldXmlItem extends AbstractFieldXmlItem
-{
-
- private final Field field;
-
- public FieldXmlItem(XmlItem parent, Field c, String innerText, Class<?> overridenFieldType, String document, int lineno)
- {
- super(XmlItemType.FIELD, parent, parent.getJavaClass(), innerText, null, overridenFieldType, document, lineno);
- this.field = c;
- this.property = getFieldValueSetter(c);
-
- if (innerText != null && innerText.length() > 0)
- {
- FieldValue fv;
- if (innerText.matches("^#\\{.*\\}$"))
- {
- fv = new ELFieldValue(innerText);
- }
- else
- {
- fv = new ConstantFieldValue(innerText);
- }
- fieldValue = new SimpleFieldValue(parent.getJavaClass(), property, fv, overridenFieldType);
- }
- allowed.add(TypeOccuranceInformation.of(XmlItemType.ANNOTATION, null, null));
- allowed.add(TypeOccuranceInformation.of(XmlItemType.VALUE, null, null));
- allowed.add(TypeOccuranceInformation.of(XmlItemType.ENTRY, null, null));
- }
-
- public Field getField()
- {
- return field;
- }
-
- private Property getFieldValueSetter(Field field)
- {
- String fieldName = field.getName();
- String methodName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
- Method setter = null;
- try
- {
- setter = field.getDeclaringClass().getMethod(methodName, field.getType());
- }
- catch (SecurityException e)
- {
-
- }
- catch (NoSuchMethodException e)
- {
-
- }
- if (setter != null)
- {
- return Properties.createProperty(setter);
- }
- return Properties.createProperty(field);
- }
-
- @Override
- public Class<?> getDeclaringClass()
- {
- return field.getDeclaringClass();
- }
-
- @Override
- public String getFieldName()
- {
- return field.getName();
- }
-
- @Override
- public Class<?> getFieldType()
- {
- return field.getType();
- }
-}
Modified: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java 2010-08-28 01:45:24 UTC (rev 13661)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/model/PropertyXmlItem.java 2010-08-28 02:48:04 UTC (rev 13662)
@@ -21,29 +21,55 @@
*/
package org.jboss.seam.xml.model;
-import java.lang.reflect.Method;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import javax.enterprise.inject.spi.BeanManager;
+
+import org.jboss.seam.xml.core.BeanResult;
+import org.jboss.seam.xml.fieldset.ArrayFieldSet;
+import org.jboss.seam.xml.fieldset.CollectionFieldSet;
import org.jboss.seam.xml.fieldset.ConstantFieldValue;
import org.jboss.seam.xml.fieldset.ELFieldValue;
import org.jboss.seam.xml.fieldset.FieldValue;
import org.jboss.seam.xml.fieldset.FieldValueObject;
+import org.jboss.seam.xml.fieldset.MapFieldSet;
import org.jboss.seam.xml.fieldset.SimpleFieldValue;
-import org.jboss.weld.extensions.util.properties.Properties;
+import org.jboss.seam.xml.util.TypeOccuranceInformation;
+import org.jboss.seam.xml.util.XmlConfigurationException;
+import org.jboss.weld.extensions.util.properties.Property;
-public class PropertyXmlItem extends AbstractFieldXmlItem
+public class PropertyXmlItem extends AbstractXmlItem
{
+ private final Property<?> property;
+ private final HashSet<TypeOccuranceInformation> allowed = new HashSet<TypeOccuranceInformation>();
+ private final Class<?> fieldType;
+ private final List<BeanResult<?>> inlineBeans = new ArrayList<BeanResult<?>>();
- private final String name;
- private final Class<?> type;
- private final Class<?> declaringClass;
+ private FieldValueObject fieldValue;
- public PropertyXmlItem(XmlItem parent, String name, Method setter, String innerText, String document, int lineno)
+ public PropertyXmlItem(XmlItem parent, Property<?> property, String innerText, String document, int lineno)
{
- super(XmlItemType.FIELD, parent, parent.getJavaClass(), innerText, null, null, document, lineno);
- this.name = name;
- this.type = setter.getParameterTypes()[0];
- this.declaringClass = setter.getDeclaringClass();
- this.property = Properties.createProperty(setter);
+ this(parent, property, innerText, null, document, lineno);
+ }
+
+ public PropertyXmlItem(XmlItem parent, Property<?> property, String innerText, Class<?> overridenFieldType, String document, int lineno)
+ {
+ super(XmlItemType.FIELD, parent, parent.getJavaClass(), innerText, null, document, lineno);
+ this.property = property;
+ if (overridenFieldType == null)
+ {
+ this.fieldType = property.getJavaClass();
+ }
+ else
+ {
+ this.fieldType = overridenFieldType;
+ }
if (innerText != null && innerText.length() > 0)
{
FieldValue fv;
@@ -55,8 +81,11 @@
{
fv = new ConstantFieldValue(innerText);
}
- fieldValue = new SimpleFieldValue(parent.getJavaClass(), property, fv, null);
+ fieldValue = new SimpleFieldValue(parent.getJavaClass(), property, fv, fieldType);
}
+ allowed.add(new TypeOccuranceInformation(XmlItemType.VALUE, null, null));
+ allowed.add(new TypeOccuranceInformation(XmlItemType.ANNOTATION, null, null));
+ allowed.add(new TypeOccuranceInformation(XmlItemType.ENTRY, null, null));
}
public FieldValueObject getFieldValue()
@@ -65,21 +94,137 @@
}
@Override
+ public boolean resolveChildren(BeanManager manager)
+ {
+ List<EntryXmlItem> mapEntries = new ArrayList<EntryXmlItem>();
+ List<ValueXmlItem> valueEntries = new ArrayList<ValueXmlItem>();
+ if (fieldValue == null)
+ {
+ for (XmlItem i : children)
+ {
+ if (i.getType() == XmlItemType.VALUE)
+ {
+ valueEntries.add((ValueXmlItem) i);
+ }
+ else if (i.getType() == XmlItemType.ENTRY)
+ {
+ mapEntries.add((EntryXmlItem) i);
+ }
+
+ }
+ }
+ if (!mapEntries.isEmpty() || !valueEntries.isEmpty())
+ {
+ if (Map.class.isAssignableFrom(getFieldType()))
+ {
+ if (!valueEntries.isEmpty())
+ {
+ throw new XmlConfigurationException("Map fields cannot have <value> elements as children,only <entry> elements Field:" + getDeclaringClass().getName() + '.' + getFieldName(), getDocument(), getLineno());
+ }
+ if (!mapEntries.isEmpty())
+ {
+ for (EntryXmlItem entry : mapEntries)
+ {
+ // resolve inline beans if nessesary
+ Set<BeanResult<?>> beans = entry.getBeanResults(manager);
+ inlineBeans.addAll(beans);
+
+ }
+ fieldValue = new MapFieldSet(property, mapEntries);
+ }
+ }
+ else if (Collection.class.isAssignableFrom(getFieldType()) || getFieldType().isArray())
+ {
+ if (!mapEntries.isEmpty())
+ {
+ throw new XmlConfigurationException("Collection fields must be set using <value> not <entry> Field:" + getDeclaringClass().getName() + '.' + getFieldName(), getDocument(), getLineno());
+ }
+ if (!valueEntries.isEmpty())
+ {
+ for (ValueXmlItem value : valueEntries)
+ {
+ // resolve inline beans if nessesary
+ BeanResult<?> result = value.getBeanResult(manager);
+ if (result != null)
+ {
+ inlineBeans.add(result);
+ }
+ }
+ if (getFieldType().isArray())
+ {
+ fieldValue = new ArrayFieldSet(property, valueEntries);
+ }
+ else
+ {
+ fieldValue = new CollectionFieldSet(property, valueEntries);
+ }
+ }
+ }
+ else
+ {
+ if (!mapEntries.isEmpty())
+ {
+ throw new XmlConfigurationException("Only Map fields can be set using <entry> Field:" + getDeclaringClass().getName() + '.' + getFieldName(), getDocument(), getLineno());
+ }
+ if (valueEntries.size() != 1)
+ {
+ throw new XmlConfigurationException("Non collection fields can only have a single <value> element Field:" + getDeclaringClass().getName() + '.' + getFieldName(), getDocument(), getLineno());
+ }
+ ValueXmlItem value = valueEntries.get(0);
+ BeanResult<?> result = value.getBeanResult(manager);
+ fieldValue = new SimpleFieldValue(parent.getJavaClass(), property, value.getValue(), fieldType);
+ if (result != null)
+ {
+ inlineBeans.add(result);
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Returns the field that corresponds to the property, or null if it does not
+ * exist
+ *
+ * @return
+ */
+ public Field getField()
+ {
+ if (property.getMember() instanceof Field)
+ {
+ return (Field) property.getMember();
+ }
+ return org.jboss.seam.xml.util.Reflections.getField(parent.getJavaClass(), property.getName());
+ }
+
+ public Set<TypeOccuranceInformation> getAllowedItem()
+ {
+ return allowed;
+ }
+
+ public Collection<? extends BeanResult> getInlineBeans()
+ {
+ return inlineBeans;
+ }
+
public Class<?> getDeclaringClass()
{
- return declaringClass;
+ return property.getDeclaringClass();
}
- @Override
public String getFieldName()
{
- return name;
+ return property.getName();
}
- @Override
public Class<?> getFieldType()
{
- return type;
+ return fieldType;
}
+ public Property<?> getProperty()
+ {
+ return property;
+ }
+
}
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-08-28 01:45:24 UTC (rev 13661)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/parser/namespace/PackageNamespaceElementResolver.java 2010-08-28 02:48:04 UTC (rev 13662)
@@ -21,8 +21,6 @@
*/
package org.jboss.seam.xml.parser.namespace;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -30,7 +28,6 @@
import org.jboss.seam.xml.model.AnnotationXmlItem;
import org.jboss.seam.xml.model.ClassXmlItem;
-import org.jboss.seam.xml.model.FieldXmlItem;
import org.jboss.seam.xml.model.MethodXmlItem;
import org.jboss.seam.xml.model.ParameterXmlItem;
import org.jboss.seam.xml.model.PropertyXmlItem;
@@ -40,6 +37,10 @@
import org.jboss.seam.xml.util.TypeOccuranceInformation;
import org.jboss.seam.xml.util.XmlConfigurationException;
import org.jboss.weld.extensions.util.Reflections;
+import org.jboss.weld.extensions.util.properties.Property;
+import org.jboss.weld.extensions.util.properties.query.NamedPropertyCriteria;
+import org.jboss.weld.extensions.util.properties.query.PropertyQueries;
+import org.jboss.weld.extensions.util.properties.query.PropertyQuery;
public class PackageNamespaceElementResolver implements NamespaceElementResolver
{
@@ -121,35 +122,23 @@
public static XmlItem resolveMethodOrField(String name, XmlItem parent, String innerText, String document, int lineno)
{
Class<?> p = parent.getJavaClass();
- Field f = null;
boolean methodFound = Reflections.methodExists(p, name);
- f = Reflections.findDeclaredField(p, name);
+ PropertyQuery<Object> query = PropertyQueries.createQuery(parent.getJavaClass());
+ query.addCriteria(new NamedPropertyCriteria(name));
+ Property<?> property = query.getFirstResult();
- if (methodFound && f != null)
+ if (methodFound && property != null)
{
- throw new XmlConfigurationException(parent.getJavaClass().getName() + " has both a method and a field named " + name + " and so cannot be configured via XML", document, lineno);
+ 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 (methodFound)
{
return new MethodXmlItem(parent, name, document, lineno);
}
- else if (f != null)
+ else if (property != null)
{
- return new FieldXmlItem(parent, f, innerText, null, document, lineno);
+ return new PropertyXmlItem(parent, property, innerText, null, document, lineno);
}
-
- String methodName = "set" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
- if (Reflections.methodExists(p, methodName))
- {
- Set<Method> methods = Reflections.getAllDeclaredMethods(p);
- for (Method m : methods)
- {
- if (m.getName().equals(methodName) && m.getParameterTypes().length == 1)
- {
- return new PropertyXmlItem(parent, name, m, innerText, document, lineno);
- }
- }
- }
return null;
}
}
Added: modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/util/Reflections.java
===================================================================
--- modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/util/Reflections.java (rev 0)
+++ modules/xml/trunk/impl/src/main/java/org/jboss/seam/xml/util/Reflections.java 2010-08-28 02:48:04 UTC (rev 13662)
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.seam.xml.util;
+
+import java.lang.reflect.Field;
+
+public class Reflections
+{
+ /**
+ * returns the field or null if not found
+ */
+ public static Field getField(Class<?> c, String name)
+ {
+ Class<?> i = c;
+ while (i != Object.class && i != null)
+ {
+ try
+ {
+ return i.getDeclaredField(name);
+ }
+ catch (Exception e)
+ {
+
+ }
+ i = i.getSuperclass();
+ }
+ return null;
+ }
+}
Modified: modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/NamespaceResolverTest.java
===================================================================
--- modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/NamespaceResolverTest.java 2010-08-28 01:45:24 UTC (rev 13661)
+++ modules/xml/trunk/impl/src/test/java/org/jboss/seam/xml/test/NamespaceResolverTest.java 2010-08-28 02:48:04 UTC (rev 13662)
@@ -26,9 +26,8 @@
import junit.framework.Assert;
-import org.jboss.seam.xml.model.AbstractFieldXmlItem;
-import org.jboss.seam.xml.model.FieldXmlItem;
import org.jboss.seam.xml.model.MethodXmlItem;
+import org.jboss.seam.xml.model.PropertyXmlItem;
import org.jboss.seam.xml.model.XmlItem;
import org.jboss.seam.xml.model.XmlItemType;
import org.jboss.seam.xml.parser.SaxNode;
@@ -74,8 +73,8 @@
Assert.assertTrue("Wrong method was resolved", ((MethodXmlItem) method).getMethod().getParameterTypes().length == 0);
XmlItem field = resolver.getItemForNamespace(new SaxNode("field1", null, null, null, null, 0), item);
- Assert.assertTrue("Element of wrong type returned", ((AbstractFieldXmlItem) field).getType() == XmlItemType.FIELD);
- Assert.assertTrue("field was not set", ((FieldXmlItem) field).getField() != null);
+ Assert.assertTrue("Element of wrong type returned", ((PropertyXmlItem) field).getType() == XmlItemType.FIELD);
+ Assert.assertTrue("field was not set", ((PropertyXmlItem) field).getField() != null);
}
More information about the seam-commits
mailing list