Nuiton-utils-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2005 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2004 -----
- December
- November
- October
- September
- August
- 3157 discussions
r2045 - in trunk/nuiton-validator/src/main: java/org/nuiton/validator java/org/nuiton/validator/bean resources
by tchemit@users.nuiton.org 25 Jan '11
by tchemit@users.nuiton.org 25 Jan '11
25 Jan '11
Author: tchemit
Date: 2011-01-25 08:04:54 +0100 (Tue, 25 Jan 2011)
New Revision: 2045
Url: http://nuiton.org/repositories/revision/nuiton-utils/2045
Log:
fire all events in BeanValidator only after complete result merge + remove validators.xml file from api
Removed:
trunk/nuiton-validator/src/main/resources/validators.xml
Modified:
trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java 2011-01-24 20:11:37 UTC (rev 2044)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java 2011-01-25 07:04:54 UTC (rev 2045)
@@ -65,7 +65,8 @@
return result;
}
- public boolean hasMessagesForScope(String field, NuitonValidatorScope scope) {
+ public boolean hasMessagesForScope(String field,
+ NuitonValidatorScope scope) {
boolean result = false;
if (messages != null) {
result = messages.containsKey(scope);
@@ -206,7 +207,9 @@
List<String> result = null;
if (messages != null) {
FieldMap<List<String>> fieldMap = messages.get(scope);
- result = new ArrayList<String>(fieldMap.keySet());
+ if (fieldMap != null) {
+ result = new ArrayList<String>(fieldMap.keySet());
+ }
}
if (result == null) {
result = Collections.emptyList();
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-24 20:11:37 UTC (rev 2044)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-25 07:04:54 UTC (rev 2045)
@@ -2,6 +2,7 @@
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
+import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.util.beans.BeanUtil;
@@ -17,6 +18,7 @@
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
@@ -317,7 +319,7 @@
validate();
}
setChanged(false);
- setValid(!messages.hasFatalMessages() && !messages.hasErrorMessagess());
+ setValid(messages == null || messages.isValid());
firePropertyChange(BEAN_PROPERTY, oldBean, bean);
}
@@ -516,7 +518,7 @@
public void doValidate() {
validate();
- setValid(!messages.hasFatalMessages() && !messages.hasErrorMessagess());
+ setValid(messages == null || messages.isValid());
setChanged(true);
}
@@ -621,19 +623,37 @@
pcs.firePropertyChange(propertyName, oldValue, newValue);
}
+ protected BeanValidatorEvent createEvent(String field,
+ NuitonValidatorScope scope,
+ String[] toAdd,
+ String[] toDelete) {
+ BeanValidatorEvent evt = new BeanValidatorEvent(
+ this,
+ field,
+ scope,
+ toAdd,
+ toDelete
+ );
+ return evt;
+ }
+
protected void fireFieldChanged(String field,
NuitonValidatorScope scope,
String[] toAdd,
String[] toDelete) {
- BeanValidatorEvent evt = new BeanValidatorEvent(
- this,
+ BeanValidatorEvent evt = createEvent(
field,
scope,
toAdd,
toDelete
);
+ fireFieldChanged(evt);
+ }
+
+ protected void fireFieldChanged(BeanValidatorEvent evt) {
+
for (BeanValidatorListener listener :
listenerList.getListeners(BeanValidatorListener.class)) {
listener.onFieldChanged(evt);
@@ -651,10 +671,13 @@
Set<NuitonValidatorScope> scopes = getDelegate().getEffectiveScopes();
+ // list of events to send after the merge of messages
+ List<BeanValidatorEvent> events = new ArrayList<BeanValidatorEvent>();
+
for (NuitonValidatorScope scope : scopes) {
// do the merge at scope level
- mergeMessages(scope, newMessages);
+ mergeMessages(scope, newMessages, events);
}
@@ -665,10 +688,19 @@
// finally keep the new messages as the current messages
messages = newMessages;
}
+
+ if (CollectionUtils.isNotEmpty(events)) {
+
+ // send all messages
+ for (BeanValidatorEvent event : events) {
+ fireFieldChanged(event);
+ }
+ }
}
protected void mergeMessages(NuitonValidatorScope scope,
- NuitonValidatorResult newMessages) {
+ NuitonValidatorResult newMessages,
+ List<BeanValidatorEvent> events) {
if (newMessages == null) {
@@ -678,7 +710,7 @@
for (String field : fieldsForScope) {
List<String> messagesForScope = messages.getMessagesForScope(field, scope);
- fireFieldChanged(field, scope, null, messagesForScope.toArray(new String[messagesForScope.size()]));
+ events.add(createEvent(field, scope, null, messagesForScope.toArray(new String[messagesForScope.size()])));
}
// suppress all messages for this scope
@@ -695,7 +727,7 @@
for (String field : newFields) {
List<String> messagesForScope = newMessages.getMessagesForScope(field, scope);
- fireFieldChanged(field, scope, messagesForScope.toArray(new String[messagesForScope.size()]), null);
+ events.add(createEvent(field, scope, messagesForScope.toArray(new String[messagesForScope.size()]), null));
}
// nothing else to do
@@ -715,7 +747,7 @@
// this fields has now messages but not before : new messages
List<String> messagesForScope = newMessages.getMessagesForScope(newField, scope);
- fireFieldChanged(newField, scope, messagesForScope.toArray(new String[messagesForScope.size()]), null);
+ events.add(createEvent(newField, scope, messagesForScope.toArray(new String[messagesForScope.size()]), null));
// treated field
itr.remove();
@@ -731,7 +763,7 @@
// this fields has no more messages
List<String> messagesForScope = messages.getMessagesForScope(oldField, scope);
- fireFieldChanged(oldField, scope, null, messagesForScope.toArray(new String[messagesForScope.size()]));
+ events.add(createEvent(oldField, scope, null, messagesForScope.toArray(new String[messagesForScope.size()])));
// treated field
itr.remove();
@@ -751,12 +783,12 @@
Set<String> toAdd = new HashSet<String>(newMessagesForScope);
toAdd.removeAll(oldMessagesForScope);
- fireFieldChanged(
+ events.add(createEvent(
field,
scope,
toAdd.isEmpty() ? null : toAdd.toArray(new String[toAdd.size()]),
toDelete.isEmpty() ? null : toDelete.toArray(new String[toDelete.size()])
- );
+ ));
}
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java 2011-01-24 20:11:37 UTC (rev 2044)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java 2011-01-25 07:04:54 UTC (rev 2045)
@@ -1,23 +1,23 @@
/*
* #%L
* Nuiton Utils :: Nuiton Validator
- *
+ *
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2011 CodeLutin, Tony Chemit
* %%
* This program 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 3 of the
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
- *
+ *
* This program 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 General Lesser Public License for more details.
- *
- * You should have received a copy of the GNU General Lesser Public
+ *
+ * You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
@@ -41,7 +41,7 @@
* @author tchemit <chemit(a)codelutin.com>
* @since 2.0
*/
-public class BeanValidatorMessage<E extends BeanValidatorMessage<?>> implements Comparable<E> , Serializable{
+public class BeanValidatorMessage<E extends BeanValidatorMessage<?>> implements Comparable<E>, Serializable {
private static final long serialVersionUID = 1L;
@@ -103,22 +103,22 @@
if (this == o) {
return true;
}
- if (o == null || getClass() != o.getClass()) {
+ if (!(o instanceof BeanValidatorMessage<?>)) {
return false;
}
BeanValidatorMessage<?> that = (BeanValidatorMessage<?>) o;
return field.equals(that.field) &&
- message.equals(that.message) &&
+ (message != null ? !message.equals(that.message) : that.message == null) &&
scope == that.scope;
}
@Override
public int hashCode() {
int result = field.hashCode();
- result = 31 * result + message.hashCode();
- result = 31 * result + scope.hashCode();
+ result = 31 * result + (message != null ? message.hashCode() : 0);
+ result = 31 * result + (scope != null ? scope.hashCode() : 0);
return result;
}
Deleted: trunk/nuiton-validator/src/main/resources/validators.xml
===================================================================
--- trunk/nuiton-validator/src/main/resources/validators.xml 2011-01-24 20:11:37 UTC (rev 2044)
+++ trunk/nuiton-validator/src/main/resources/validators.xml 2011-01-25 07:04:54 UTC (rev 2045)
@@ -1,48 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- #%L
- Nuiton Utils :: Nuiton Validator
-
- $Id$
- $HeadURL$
- %%
- Copyright (C) 2011 CodeLutin, Tony Chemit
- %%
- This program 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 3 of the
- License, or (at your option) any later version.
-
- This program 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 General Lesser Public License for more details.
-
- You should have received a copy of the GNU General Lesser Public
- License along with this program. If not, see
- <http://www.gnu.org/licenses/lgpl-3.0.html>.
- #L%
- -->
-
-<!DOCTYPE validators PUBLIC
- "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
-
-<!-- START SNIPPET: validators -->
-<validators>
- <validator name="requiredFile"
- class="org.nuiton.validator.field.RequiredFileFieldValidator"/>
- <validator name="existingFile"
- class="org.nuiton.validator.field.ExistingFileFieldValidator"/>
- <validator name="notExistingFile"
- class="org.nuiton.validator.field.NotExistingFileFieldValidator"/>
- <validator name="existingDirectory"
- class="org.nuiton.validator.field.ExistingDirectoryFieldValidator"/>
- <validator name="notExistingDirectory"
- class="org.nuiton.validator.field.NotExistingDirectoryFieldValidator"/>
- <validator name="collectionFieldExpression"
- class="org.nuiton.validator.field.CollectionFieldExpressionValidator"/>
- <validator name="collectionUniqueKey"
- class="org.nuiton.validator.field.CollectionUniqueKeyValidator"/>
-</validators>
-<!-- END SNIPPET: validators -->
1
0
Author: tchemit
Date: 2011-01-24 21:11:37 +0100 (Mon, 24 Jan 2011)
New Revision: 2044
Url: http://nuiton.org/repositories/revision/nuiton-utils/2044
Log:
Evolution #1224: Updates to nuiton-i18n 2.1
Modified:
trunk/pom.xml
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2011-01-24 19:22:46 UTC (rev 2043)
+++ trunk/pom.xml 2011-01-24 20:11:37 UTC (rev 2044)
@@ -198,7 +198,7 @@
<projectId>nuiton-utils</projectId>
- <nuitonI18nVersion>2.1-SNAPSHOT</nuitonI18nVersion>
+ <nuitonI18nVersion>2.1</nuitonI18nVersion>
<aspectwerkzVersion>2.0</aspectwerkzVersion>
<xworkVersion>2.1.3</xworkVersion>
1
0
Author: tchemit
Date: 2011-01-24 20:22:46 +0100 (Mon, 24 Jan 2011)
New Revision: 2043
Url: http://nuiton.org/repositories/revision/nuiton-utils/2043
Log:
Evolution #1231: Updates mavenpom to 2.4.2
Modified:
trunk/pom.xml
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2011-01-24 16:38:52 UTC (rev 2042)
+++ trunk/pom.xml 2011-01-24 19:22:46 UTC (rev 2043)
@@ -35,7 +35,7 @@
<parent>
<groupId>org.nuiton</groupId>
<artifactId>mavenpom4redmineAndCentral</artifactId>
- <version>2.4.2-SNAPSHOT</version>
+ <version>2.4.2</version>
</parent>
<artifactId>nuiton-utils-parent</artifactId>
1
0
r2042 - trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean
by tchemit@users.nuiton.org 24 Jan '11
by tchemit@users.nuiton.org 24 Jan '11
24 Jan '11
Author: tchemit
Date: 2011-01-24 17:38:52 +0100 (Mon, 24 Jan 2011)
New Revision: 2042
Url: http://nuiton.org/repositories/revision/nuiton-utils/2042
Log:
open api (for JAxx purpose to replace the old BeanValidator api)
Modified:
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-24 16:19:19 UTC (rev 2041)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-24 16:38:52 UTC (rev 2042)
@@ -368,6 +368,10 @@
);
}
+ public Class<O> getType() {
+ return delegate.getModel().getType();
+ }
+
protected void rebuildDelegateValidator(Class<O> beanType,
String context,
NuitonValidatorScope... scopes) {
@@ -447,8 +451,8 @@
return result;
}
- public NuitonValidatorScope getHighestScope(String field) {
- if (messages==null) {
+ public NuitonValidatorScope getHighestScope(String field) {
+ if (messages == null) {
// no messages
return null;
@@ -457,7 +461,7 @@
NuitonValidatorScope scope = messages.getFieldHighestScope(field);
return scope;
}
-
+
/**
* Convert a value.
* <p/>
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java 2011-01-24 16:19:19 UTC (rev 2041)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorMessage.java 2011-01-24 16:38:52 UTC (rev 2042)
@@ -26,6 +26,7 @@
import org.nuiton.validator.NuitonValidatorScope;
+import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
@@ -40,8 +41,10 @@
* @author tchemit <chemit(a)codelutin.com>
* @since 2.0
*/
-public class BeanValidatorMessage<E extends BeanValidatorMessage<?>> implements Comparable<E> {
+public class BeanValidatorMessage<E extends BeanValidatorMessage<?>> implements Comparable<E> , Serializable{
+ private static final long serialVersionUID = 1L;
+
/** the validator that produce the message */
protected BeanValidator<?> validator;
1
0
r2041 - in trunk/nuiton-validator/src/main/java/org/nuiton/validator: . bean
by tchemit@users.nuiton.org 24 Jan '11
by tchemit@users.nuiton.org 24 Jan '11
24 Jan '11
Author: tchemit
Date: 2011-01-24 17:19:19 +0100 (Mon, 24 Jan 2011)
New Revision: 2041
Url: http://nuiton.org/repositories/revision/nuiton-utils/2041
Log:
open api (for JAxx purpose to replace the old BeanValidator api)
Modified:
trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorFactory.java
trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorFactory.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorFactory.java 2011-01-24 13:08:51 UTC (rev 2040)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorFactory.java 2011-01-24 16:19:19 UTC (rev 2041)
@@ -131,6 +131,12 @@
return provider;
}
+ public static NuitonValidatorProvider getDefaultProvider() {
+ String providerName = getDefaultProviderName();
+ NuitonValidatorProvider provider = getProvider(providerName);
+ return provider;
+ }
+
public static String getDefaultProviderName() throws IllegalStateException {
if (defaultProviderName == null) {
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java 2011-01-24 13:08:51 UTC (rev 2040)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/NuitonValidatorResult.java 2011-01-24 16:19:19 UTC (rev 2041)
@@ -29,8 +29,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.TreeMap;
/**
@@ -96,7 +98,8 @@
return result;
}
- public void addMessagesForScope(NuitonValidatorScope scope, Map<String, List<String>> newMessages) {
+ public void addMessagesForScope(NuitonValidatorScope scope,
+ Map<String, List<String>> newMessages) {
if (messages == null) {
messages = new EnumMap<NuitonValidatorScope, FieldMap<List<String>>>(NuitonValidatorScope.class);
}
@@ -121,21 +124,20 @@
}
}
-
public void setMessagesForScope(NuitonValidatorScope scope,
String field,
List<String> messages) {
- if (this.messages==null) {
+ if (this.messages == null) {
this.messages = new EnumMap<NuitonValidatorScope, FieldMap<List<String>>>(NuitonValidatorScope.class);
}
FieldMap<List<String>> fieldMap = this.messages.get(scope);
- if (fieldMap==null) {
+ if (fieldMap == null) {
fieldMap = new FieldMap<List<String>>();
- this.messages.put(scope,fieldMap);
+ this.messages.put(scope, fieldMap);
}
- fieldMap.put(field,messages);
+ fieldMap.put(field, messages);
}
public List<String> getMessagesForScope(NuitonValidatorScope scope) {
@@ -238,11 +240,58 @@
}
}
- public EnumMap<NuitonValidatorScope, FieldMap<List<String>>> getMessages() {
+ public NuitonValidatorScope getFieldHighestScope(String field) {
+ if (messages == null) {
+ return null;
+ }
+ if (containsField(field, NuitonValidatorScope.FATAL)) {
+ return NuitonValidatorScope.FATAL;
+ }
+ if (containsField(field, NuitonValidatorScope.ERROR)) {
+ return NuitonValidatorScope.ERROR;
+ }
+ if (containsField(field, NuitonValidatorScope.WARNING)) {
+ return NuitonValidatorScope.WARNING;
+ }
+ if (containsField(field, NuitonValidatorScope.INFO)) {
+ return NuitonValidatorScope.INFO;
+ }
+
+ // no scope for the field
+ return null;
+ }
+
+ public NuitonValidatorScope[] getFieldScopes(String field) {
+ Set<NuitonValidatorScope> result = new HashSet<NuitonValidatorScope>();
+ if (messages != null) {
+
+ if (containsField(field, NuitonValidatorScope.FATAL)) {
+ result.add(NuitonValidatorScope.FATAL);
+ }
+ if (containsField(field, NuitonValidatorScope.ERROR)) {
+ result.add(NuitonValidatorScope.ERROR);
+ }
+ if (containsField(field, NuitonValidatorScope.WARNING)) {
+ result.add(NuitonValidatorScope.WARNING);
+ }
+ if (containsField(field, NuitonValidatorScope.INFO)) {
+ result.add(NuitonValidatorScope.INFO);
+ }
+ }
+
+ return result.toArray(new NuitonValidatorScope[result.size()]);
+ }
+
+ protected boolean containsField(String field, NuitonValidatorScope scope) {
+ FieldMap<List<String>> fieldMap = messages.get(scope);
+ return fieldMap != null && fieldMap.containsKey(field);
+ }
+
+ protected EnumMap<NuitonValidatorScope, FieldMap<List<String>>> getMessages() {
return messages;
}
- public Map<String, FieldMap<Object>> getTagValues() {
+ protected Map<String, FieldMap<Object>> getTagValues() {
return tagValues;
}
}
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-24 13:08:51 UTC (rev 2040)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-24 16:19:19 UTC (rev 2041)
@@ -447,6 +447,17 @@
return result;
}
+ public NuitonValidatorScope getHighestScope(String field) {
+ if (messages==null) {
+
+ // no messages
+ return null;
+ }
+
+ NuitonValidatorScope scope = messages.getFieldHighestScope(field);
+ return scope;
+ }
+
/**
* Convert a value.
* <p/>
1
0
r2040 - in trunk/nuiton-validator/src: main/java/org/nuiton/validator main/java/org/nuiton/validator/bean main/java/org/nuiton/validator/xwork2 test/java/org/nuiton/validator test/java/org/nuiton/validator/bean test/java/org/nuiton/validator/model test/resources/org/nuiton/validator/bean
by tchemit@users.nuiton.org 24 Jan '11
by tchemit@users.nuiton.org 24 Jan '11
24 Jan '11
Author: tchemit
Date: 2011-01-24 14:08:51 +0100 (Mon, 24 Jan 2011)
New Revision: 2040
Url: http://nuiton.org/repositories/revision/nuiton-utils/2040
Log:
strict check on fields when loading model of validator + finish reimplementation of BeanValidator (and add methods to obtain them in NuitonValidatorFactory) + remove legacy api
Added:
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorFactory.java
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorUtil.java
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/package-info.java
trunk/nuiton-validator/src/test/resources/org/nuiton/validator/bean/SimpleBean-marchepo-error-validation.xml
Removed:
trunk/nuiton-validator/src/main/java/org/nuiton/validator/legacy/
trunk/nuiton-validator/src/test/java/org/nuiton/validator/legacy/
Modified:
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java
trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorListener.java
trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java
trunk/nuiton-validator/src/test/java/org/nuiton/validator/AbstractValidatorDetectorTest.java
trunk/nuiton-validator/src/test/java/org/nuiton/validator/bean/BeanValidatorTest.java
trunk/nuiton-validator/src/test/java/org/nuiton/validator/model/ModelValidatorDetectorTestImpl.java
trunk/nuiton-validator/src/test/resources/org/nuiton/validator/bean/SimpleBean-error-validation.xml
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-24 12:54:50 UTC (rev 2039)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidator.java 2011-01-24 13:08:51 UTC (rev 2040)
@@ -31,11 +31,15 @@
* A such validator is designed to validate to keep the validation of a bean,
* means the bean is attached to the validator (field {@link #bean}.
* <p/>
- * <b>Note:</b> The {@code BeanValidator} should never be used for
+ * A such validator is also a JavaBean and you can listen his states
+ * modifications via the classic java bean api.
+ * <p/>
+ * <strong>Note:</strong> The {@code BeanValidator} should never be used for
* validation in a service approch since it needs to keep a reference to the
* bean to validate.
*
* @author tchemit <chemit(a)codelutin.com>
+ * @see BeanValidatorListener
* @since 2.0
*/
public class BeanValidator<O> {
@@ -52,12 +56,18 @@
/**
* Name of the bounded property {@code context}.
*
- * @see #bean
* @see #getContext()
* @see #setContext(String)
*/
public static final String CONTEXT_PROPERTY = "context";
+ /**
+ * Name of the bounded property {@code scopes}.
+ *
+ * @see #getScopes()
+ * @see #setScopes(NuitonValidatorScope...)
+ */
+ public static final String SCOPES_PROPERTY = "scopes";
/**
* Name of the bounded property {@link #valid}.
@@ -83,10 +93,16 @@
/** The bean to validate. */
protected O bean;
+ /** To chain to another validator (acting as parent of this one). */
+ protected BeanValidator<?> parentValidator;
+
/** The delegate validator used to validate the bean. */
protected NuitonValidator<O> delegate;
- /** State of validation */
+ /**
+ * State of validation (keep all messages of validation for the filled
+ * bean).
+ */
protected NuitonValidatorResult messages;
/**
@@ -95,7 +111,7 @@
*/
protected boolean changed;
- /** state of the validator (is true if no errors of error scope is found) */
+ /** State of the validator (is true if no errors of error scope is found). */
protected boolean valid = true;
/**
@@ -108,7 +124,7 @@
/** map of conversion errors detected by this validator */
protected Map<String, String> conversionErrors;
- /** listener that listens on bean modification */
+ /** Listener that listens on bean modification. */
protected PropertyChangeListener l;
/** delegate property change support */
@@ -117,15 +133,19 @@
/** A list of event listeners for this validators */
protected EventListenerList listenerList = new EventListenerList();
+ /**
+ * The provider of delegate validators.
+ * <p/>
+ * It will also produce validator model.
+ *
+ * @see NuitonValidatorProvider
+ */
protected final NuitonValidatorProvider validatorProvider;
- protected final NuitonValidatorModel<O> initialValidatorModel;
-
- protected NuitonValidatorModel<O> validatorModel;
-
public BeanValidator(NuitonValidatorProvider validatorProvider,
Class<O> beanClass,
String context) {
+
this(validatorProvider, beanClass,
context,
NuitonValidatorScope.values()
@@ -135,50 +155,69 @@
public BeanValidator(NuitonValidatorProvider validatorProvider,
Class<O> beanClass,
String context,
- NuitonValidatorScope... filterScopes) {
+ NuitonValidatorScope... scopes) {
+
+ // check if given bean class is Javabean compiliant
+ boolean javaBeanCompiliant = BeanUtil.isJavaBeanCompiliant(beanClass);
+
+ if (!javaBeanCompiliant) {
+
+ throw new IllegalArgumentException(
+ beanClass.getName() + " is not JavaBean compiliant (" +
+ BeanUtil.ADD_PROPERTY_CHANGE_LISTENER + ", or " +
+ BeanUtil.REMOVE_PROPERTY_CHANGE_LISTENER +
+ " method not found).");
+ }
+
this.validatorProvider = validatorProvider;
pcs = new PropertyChangeSupport(this);
conversionErrors = new TreeMap<String, String>();
- // initial model of validation (keep it when need to change
- // the validator model : we have all we need inside this one (type,
- // scopes...))
- initialValidatorModel = validatorProvider.getModel(beanClass,
- context,
- filterScopes
+ // build delegate validator
+ rebuildDelegateValidator(
+ beanClass,
+ context,
+ scopes
);
- // at the begin, validator model is exactly the initial validator model
- validatorModel = initialValidatorModel;
+ // context has changed
+ firePropertyChange(CONTEXT_PROPERTY,
+ null,
+ context
+ );
- pcs.firePropertyChange(CONTEXT_PROPERTY,
- null,
- context
+ // scopes has changed
+ firePropertyChange(SCOPES_PROPERTY,
+ null,
+ scopes
);
l = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
- // chaque modification lance la validation
+
+ // the bean has changed, replay validation
doValidate();
}
};
}
/**
- * Retourne vrai si l'objet bean a ete modifie depuis le dernier {@link
- * #setBean}
+ * Obtain the {@link #changed} property value.
+ * <p/>
+ * Returns {@code true} if bean was modified since last
+ * time a bean was attached.
*
- * @return <code>true</code> if bean was modify since last {@link
- * #setBean(Object)} invocation
+ * @return {@code true} if bean was modified since last attachement of
+ * a bean.
*/
public boolean isChanged() {
return changed;
}
/**
- * Permet de force la remise a false de l'etat de changement du bean
+ * To force the value of the property {@link #changed}.
*
* @param changed flag to force reset of property {@link #changed}
*/
@@ -186,10 +225,9 @@
this.changed = changed;
// force the property to be fired (never pass the older value)
- pcs.firePropertyChange(CHANGED_PROPERTY, null, changed);
+ firePropertyChange(CHANGED_PROPERTY, null, changed);
}
-
public boolean isCanValidate() {
return canValidate;
}
@@ -198,21 +236,45 @@
this.canValidate = canValidate;
}
+ /**
+ * Obtain the {@link #valid} property value.
+ *
+ * @return {@code true} if attached bean is valid (no error or fatal messages)
+ */
public boolean isValid() {
return valid;
}
+ /**
+ * Change the value of the {@link #valid} property.
+ *
+ * @param valid the new value of the property
+ */
public void setValid(boolean valid) {
this.valid = valid;
// force the property to be fired (never pass the older value)
- pcs.firePropertyChange(VALID_PROPERTY, null, valid);
+ firePropertyChange(VALID_PROPERTY, null, valid);
}
+ /**
+ * Obtain the actual bean attached to the validator.
+ *
+ * @return the bean attached to the validor or {@code null} if no bean
+ * is attached
+ */
public O getBean() {
return bean;
}
+ /**
+ * Change the attached bean.
+ * <p/>
+ * As a side effect, the internal {@link #messages} will be reset.
+ *
+ * @param bean the bean to attach (can be {@code null} to reset the
+ * validator).
+ */
public void setBean(O bean) {
O oldBean = this.bean;
if (log.isDebugEnabled()) {
@@ -256,38 +318,136 @@
}
setChanged(false);
setValid(!messages.hasFatalMessages() && !messages.hasErrorMessagess());
- pcs.firePropertyChange(BEAN_PROPERTY, oldBean, bean);
+ firePropertyChange(BEAN_PROPERTY, oldBean, bean);
}
public String getContext() {
- return validatorModel == null ? null : validatorModel.getContext();
+ return delegate.getModel().getContext();
}
public void setContext(String context) {
String oldContext = getContext();
+ NuitonValidatorModel<O> validatorModel = delegate.getModel();
+
// compute the new validator model
- NuitonValidatorScope[] scopes = new NuitonValidatorScope[initialValidatorModel.getScopes().size()];
- validatorModel = validatorProvider.getModel(initialValidatorModel.getType(), context, scopes);
+ NuitonValidatorScope[] scopes = new NuitonValidatorScope[validatorModel.getScopes().size()];
+ rebuildDelegateValidator(
+ validatorModel.getType(),
+ context,
+ scopes
+ );
+
+ firePropertyChange(CONTEXT_PROPERTY,
+ oldContext,
+ context
+ );
+ }
+
+ public Set<NuitonValidatorScope> getScopes() {
+ return delegate.getModel().getScopes();
+ }
+
+ public void setScopes(NuitonValidatorScope... scopes) {
+
+ Set<NuitonValidatorScope> oldScopes = getScopes();
+
+ NuitonValidatorModel<O> validatorModel = delegate.getModel();
+
+ rebuildDelegateValidator(
+ validatorModel.getType(),
+ validatorModel.getContext(),
+ scopes
+ );
+
+ firePropertyChange(SCOPES_PROPERTY,
+ oldScopes,
+ scopes
+ );
+ }
+
+ protected void rebuildDelegateValidator(Class<O> beanType,
+ String context,
+ NuitonValidatorScope... scopes) {
+ if (scopes.length == 0) {
+ scopes = NuitonValidatorScope.values();
+ }
+
+ // compute the new validator model
+ NuitonValidatorModel<O> validatorModel = validatorProvider.getModel(beanType,
+ context,
+ scopes
+ );
+
// remove old delegate validator
- delegate = null;
+ delegate = validatorProvider.newValidator(validatorModel);
// changing context could change fields definition
// so dettach bean, must rebuild the fields
if (bean != null) {
setBean(null);
}
+ }
- pcs.firePropertyChange(CONTEXT_PROPERTY,
- oldContext,
- context
- );
+ public BeanValidator<?> getParentValidator() {
+ return parentValidator;
+ }
+ public void setParentValidator(BeanValidator<?> parentValidator) {
+ this.parentValidator = parentValidator;
}
+ public boolean hasFatalErrors() {
+ boolean result = messages != null && messages.hasFatalMessages();
+ return result;
+ }
+
+ public boolean hasErrors() {
+ boolean result = messages != null && messages.hasErrorMessagess();
+ return result;
+ }
+
+ public boolean hasWarnings() {
+ boolean result = messages != null && messages.hasWarningMessages();
+ return result;
+ }
+
+ public boolean hasInfos() {
+ boolean result = messages != null && messages.hasInfoMessages();
+ return result;
+ }
+
/**
+ * Test a the validator contains the field given his name
+ *
+ * @param fieldName the name of the searched field
+ * @return <code>true</code> if validator contaisn this field,
+ * <code>false</code> otherwise
+ */
+ public boolean containsField(String fieldName) {
+ Set<String> effectiveFields = getDelegate().getEffectiveFields();
+ boolean result = effectiveFields.contains(fieldName);
+ return result;
+ }
+
+ public boolean isValid(String fieldName) {
+ if (messages == null) {
+
+ // no message, so this is valid
+ return true;
+ }
+
+ // field is valid if no fatal messages nor error messages
+ boolean result = !(
+ messages.hasMessagesForScope(fieldName, NuitonValidatorScope.FATAL) ||
+ messages.hasMessagesForScope(fieldName, NuitonValidatorScope.ERROR));
+
+ return result;
+ }
+
+ /**
* Convert a value.
* <p/>
* If an error occurs, then add an error in validator.
@@ -392,21 +552,21 @@
mergeMessages(result);
-// if (parentValidator != null) {
-// // chained validation
-// // the parent validator should not be changed from this validation
-// boolean wasModified = parentValidator.isChanged();
-// parentValidator.doValidate();
-// if (!wasModified) {
-// // push back old state
-// parentValidator.setChanged(false);
-// }
-// }
+ if (parentValidator != null) {
+ // chained validation
+ // the parent validator should not be changed from this validation
+ boolean wasModified = parentValidator.isChanged();
+ parentValidator.doValidate();
+ if (!wasModified) {
+ // push back old state
+ parentValidator.setChanged(false);
+ }
+ }
}
@Override
public String toString() {
- return super.toString() + "<beanClass:" + initialValidatorModel.getType() +
+ return super.toString() + "<beanClass:" + delegate.getModel().getType() +
", context:" + getContext() + ">";
}
@@ -440,6 +600,12 @@
pcs.removePropertyChangeListener(propertyName, listener);
}
+ public void firePropertyChange(String propertyName,
+ Object oldValue,
+ Object newValue) {
+ pcs.firePropertyChange(propertyName, oldValue, newValue);
+ }
+
protected void fireFieldChanged(String field,
NuitonValidatorScope scope,
String[] toAdd,
@@ -472,6 +638,7 @@
for (NuitonValidatorScope scope : scopes) {
+ // do the merge at scope level
mergeMessages(scope, newMessages);
}
@@ -522,8 +689,6 @@
List<String> oldFields = messages.getFieldsForScope(scope);
- Set<String> mergedFields = new HashSet<String>();
-
Iterator<String> itr;
// detects field with only new messages
@@ -555,10 +720,6 @@
// treated field
itr.remove();
- } else {
-
- // merged field
- mergedFields.add(oldField);
}
}
@@ -589,9 +750,6 @@
}
protected NuitonValidator<O> getDelegate() {
- if (delegate == null) {
- delegate = validatorProvider.newValidator(validatorModel);
- }
return delegate;
}
Added: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorFactory.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorFactory.java (rev 0)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorFactory.java 2011-01-24 13:08:51 UTC (rev 2040)
@@ -0,0 +1,90 @@
+package org.nuiton.validator.bean;
+
+import org.nuiton.validator.NuitonValidator;
+import org.nuiton.validator.NuitonValidatorFactory;
+import org.nuiton.validator.NuitonValidatorProvider;
+import org.nuiton.validator.NuitonValidatorScope;
+
+/**
+ * Factory of {@link BeanValidator}.
+ *
+ * To obtain a new {@link BeanValidator}, use one of the method
+ *
+ * <pre>
+ * BeanValidatorFactory.newBeanValidator(XXX)
+ * </pre>
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 2.0
+ * @see BeanValidator
+ */
+public class BeanValidatorFactory {
+
+ /**
+ * Obtain a new {@link BeanValidator} for the given parameters.
+ * <p/>
+ * <b>Note:</b> It will use the default provider of {@link NuitonValidator}
+ *
+ * @param type type of bean to validate
+ * @param context context of validation
+ * @param scopes authorized scopes (if {@code null}, will use all scopes)
+ * @param <O> type of bean to validate
+ * @return the new instanciated {@link BeanValidator}.
+ * @throws NullPointerException if type is {@code null}
+ * @see NuitonValidatorFactory#getDefaultProviderName()
+ */
+ public static <O> BeanValidator<O> newBeanValidator(Class<O> type,
+ String context,
+ NuitonValidatorScope... scopes) throws NullPointerException {
+
+
+ // get the provider default name
+ String providerName = NuitonValidatorFactory.getDefaultProviderName();
+
+ // get the bean validator with this provider
+ BeanValidator<O> beanValidator = newBeanValidator(providerName,
+ type,
+ context,
+ scopes
+ );
+ return beanValidator;
+
+ }
+
+
+ /**
+ * Obtain a new {@link BeanValidator} for the given parameters.
+ * <p/>
+ * <b>Note:</b> It will use the provider of {@link NuitonValidator}
+ * defined by the {@code providerName}.
+ *
+ * @param providerName name of {@link NuitonValidator} to use
+ * @param type type of bean to validate
+ * @param context context of validation
+ * @param scopes authorized scopes (if {@code null}, will use all scopes)
+ * @param <O> type of bean to validate
+ * @return the new instanciated {@link BeanValidator}.
+ * @throws NullPointerException if type is {@code null}
+ * @see NuitonValidatorFactory#getProvider(String)
+ */
+ public static <O> BeanValidator<O> newBeanValidator(String providerName,
+ Class<O> type,
+ String context,
+ NuitonValidatorScope... scopes) throws NullPointerException {
+
+ if (type == null) {
+ throw new NullPointerException(
+ "type parameter can not be null.");
+ }
+
+ NuitonValidatorProvider provider = NuitonValidatorFactory.getProvider(providerName);
+
+ BeanValidator<O> beanValidator = new BeanValidator<O>(provider,
+ type,
+ context,
+ scopes
+ );
+ return beanValidator;
+
+ }
+}
Property changes on: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorFactory.java
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision HeadURL
Added: svn:eol-style
+ native
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorListener.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorListener.java 2011-01-24 12:54:50 UTC (rev 2039)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorListener.java 2011-01-24 13:08:51 UTC (rev 2040)
@@ -28,9 +28,11 @@
import java.util.EventListener;
/**
- * The listener contract to be used on {@link BeanValidator}.
+ * The listener contract to be used on {@link BeanValidator} to fire that
+ * some messages has changed for a given field and scope.
*
* @author tchemit <chemit(a)codelutin.com>
+ * @see BeanValidatorEvent
* @since 2.0
*/
public interface BeanValidatorListener extends EventListener {
Copied: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorUtil.java (from rev 2035, trunk/nuiton-validator/src/main/java/org/nuiton/validator/legacy/BeanValidatorUtil.java)
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorUtil.java (rev 0)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/BeanValidatorUtil.java 2011-01-24 13:08:51 UTC (rev 2040)
@@ -0,0 +1,131 @@
+/*
+ * #%L
+ * Nuiton Utils :: Nuiton Validator
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2011 CodeLutin, Tony Chemit
+ * %%
+ * This program 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 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
+package org.nuiton.validator.bean;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.nuiton.validator.NuitonValidatorScope;
+
+import java.beans.BeanInfo;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.util.EnumMap;
+import java.util.EnumSet;
+import java.util.List;
+
+/**
+ * The helper class for validation module.
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 2.0
+ */
+public class BeanValidatorUtil {
+
+ /** Logger */
+ static private final Log log = LogFactory.getLog(BeanValidatorUtil.class);
+
+ protected BeanValidatorUtil() {
+ // no instance
+ }
+
+ /**
+ * Convert a value to a given type and then if was succesffull try to set it
+ * in the bean manage by the validator.
+ *
+ * @param validator validator to be involved
+ * @param fieldName the name of the bean property
+ * @param value the actual value to convert
+ * @param valueClass the type of the conversion
+ */
+ public static void convert(BeanValidator<?> validator,
+ String fieldName,
+ String value,
+ Class<?> valueClass) {
+
+ Object result = validator.convert(fieldName, value, valueClass);
+ if (result != null) {
+ try {
+ BeanInfo info =
+ Introspector.getBeanInfo(validator.getBean().getClass());
+
+ for (PropertyDescriptor descriptor :
+ info.getPropertyDescriptors()) {
+ if (fieldName.equals(descriptor.getName()) &&
+ descriptor.getWriteMethod() != null) {
+
+ descriptor.getWriteMethod().invoke(
+ validator.getBean(),
+ result
+ );
+ break;
+ }
+ }
+ } catch (Exception e) {
+ if (log.isErrorEnabled()) {
+ log.error("could not obtain beanInfo for " +
+ valueClass.getClass() + ", reason : " +
+ e.getMessage(), e);
+ }
+ }
+ } else {
+ //fixme : conversion failed, we should be able to notify ui
+ // that values has changed ?
+ // otherwise, bean value has not changed,...
+ }
+ }
+
+ public static EnumSet<NuitonValidatorScope> getScopes(
+ List<BeanValidatorMessage<?>> messages) {
+ EnumSet<NuitonValidatorScope> result =
+ EnumSet.noneOf(NuitonValidatorScope.class);
+ for (BeanValidatorMessage<?> m : messages) {
+ result.add(m.getScope());
+ }
+ return result;
+ }
+
+ public static EnumMap<NuitonValidatorScope, Integer> getScopesCount(
+ List<BeanValidatorMessage<?>> messages) {
+ EnumMap<NuitonValidatorScope, Integer> result =
+ new EnumMap<NuitonValidatorScope, Integer>(NuitonValidatorScope.class);
+ for (NuitonValidatorScope s : NuitonValidatorScope.values()) {
+ result.put(s, 0);
+ }
+ for (BeanValidatorMessage<?> m : messages) {
+
+ NuitonValidatorScope scope = m.getScope();
+
+ result.put(scope, result.get(scope) + 1);
+ }
+
+ for (NuitonValidatorScope s : NuitonValidatorScope.values()) {
+ if (result.get(s) == 0) {
+ result.remove(s);
+ }
+ }
+ return result;
+ }
+
+}
Added: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/package-info.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/package-info.java (rev 0)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/package-info.java 2011-01-24 13:08:51 UTC (rev 2040)
@@ -0,0 +1,61 @@
+/*
+ * #%L
+ * Nuiton Utils :: Nuiton Validator
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2011 CodeLutin
+ * %%
+ * This program 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 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
+package org.nuiton.validator.bean;
+/**
+ * Package of Nuiton- beanValidator api.
+ *
+ * <h1>The <b>BeanValidator</b> api</h1>
+ * <p>
+ * The {@link BeanValidator} purpose is to validate a bean, with a listener api
+ * to interact with outside world.
+ * </p>
+ * It is mainly used in GUI parts of an application (Jaxx-validator use it).
+ * <br/>
+ * The idea is to attach the bean to validate insed the validator, then the
+ * validator listen any modification of the bean to revalidate it and fires
+ * events when messages has changed on a field.
+ *
+ * <pre>
+ * BeanValidatorListener listener = new BeanValidatorListener() {XXX};
+ * BeanValidator<O> validator = XXX;
+ * validator.addBeanValidatorListener(listener);
+ * validator.setBean(o);
+ * </pre>
+ *
+ * <h2>Obtain a validator</h2>
+ * To obtain a bean validator use the factory of validators
+ * ({@link BeanValidatorFactory}.
+ * <br/>
+ * <pre>
+ * BeanValidator<O> validator = BeanValidatorFactory.newBeanValidator(O.class);
+ * </pre>
+ *
+ * <strong>To be continued...</strong>
+ *
+ * @since 2.0
+ */
+
+import org.nuiton.validator.bean.BeanValidatorFactory;
+import org.nuiton.validator.bean.BeanValidator;
Property changes on: trunk/nuiton-validator/src/main/java/org/nuiton/validator/bean/package-info.java
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision HeadURL
Added: svn:eol-style
+ native
Modified: trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java
===================================================================
--- trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java 2011-01-24 12:54:50 UTC (rev 2039)
+++ trunk/nuiton-validator/src/main/java/org/nuiton/validator/xwork2/XWork2ValidatorUtil.java 2011-01-24 13:08:51 UTC (rev 2040)
@@ -35,6 +35,7 @@
import com.opensymphony.xwork2.validator.Validator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.nuiton.util.beans.BeanUtil;
import org.nuiton.validator.NuitonValidatorScope;
import java.util.HashSet;
@@ -139,7 +140,7 @@
ActionValidatorManager validatorManager =
container.getInstance(ActionValidatorManager.class,
"no-annotations");
-
+
return validatorManager;
}
@@ -148,24 +149,34 @@
NuitonValidatorScope[] scopeUniverse) {
+ Set<String> availableFields = BeanUtil.getReadableProperties(type);
+
ActionValidatorManager validatorManager = newValidationManager(null);
Map<NuitonValidatorScope, String[]> fields = new TreeMap<NuitonValidatorScope, String[]>();
for (NuitonValidatorScope scope : scopeUniverse) {
- Set<String> fieldNames = detectFieldsForScope(validatorManager, type, scope, context, false);
+ Set<String> fieldNames = detectFieldsForScope(validatorManager,
+ type,
+ scope,
+ context,
+ availableFields,
+ false
+ );
if (log.isDebugEnabled()) {
- log.debug("detected validator fields for scope " + scope + ":" + context +
- " : " + fieldNames);
+ log.debug("detected validator fields for scope " + scope +
+ ":" + context + " : " + fieldNames);
}
if (!fieldNames.isEmpty()) {
// fields detected in this validator, keep it
- fields.put(scope, fieldNames.toArray(new String[fieldNames.size()]));
+ fields.put(scope,
+ fieldNames.toArray(new String[fieldNames.size()])
+ );
}
}
@@ -177,9 +188,9 @@
Class<?> type,
NuitonValidatorScope scope,
String context,
+ Set<String> availableFields,
boolean includeDefaultContext) {
-
String scopeContext = getContextForScope(context, scope);
@@ -197,6 +208,7 @@
}
for (Validator<?> v : validator.getValidators(type, scopeContext)) {
+
// we only work on FieldValidator at the moment
if (v instanceof FieldValidator) {
if (skip > 0) {
@@ -209,7 +221,19 @@
fieldValidator.getFieldName());
}
String fName = fieldValidator.getFieldName();
- fields.add(fName);
+ if (availableFields.contains(fName)) {
+
+ // safe field
+ fields.add(fName);
+ } else {
+
+ // not a readable property, can not add it
+ String message = "Field " + fName + " in scope [" + scopeContext + "] is not a readable property of " + type.getName();
+ if (log.isErrorEnabled()) {
+ log.error(message);
+ }
+ throw new IllegalStateException(message);
+ }
}
}
Modified: trunk/nuiton-validator/src/test/java/org/nuiton/validator/AbstractValidatorDetectorTest.java
===================================================================
--- trunk/nuiton-validator/src/test/java/org/nuiton/validator/AbstractValidatorDetectorTest.java 2011-01-24 12:54:50 UTC (rev 2039)
+++ trunk/nuiton-validator/src/test/java/org/nuiton/validator/AbstractValidatorDetectorTest.java 2011-01-24 13:08:51 UTC (rev 2040)
@@ -1,3 +1,27 @@
+/*
+ * #%L
+ * Nuiton Utils :: Nuiton Validator
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2011 CodeLutin
+ * %%
+ * This program 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 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
package org.nuiton.validator;
import org.junit.After;
Modified: trunk/nuiton-validator/src/test/java/org/nuiton/validator/bean/BeanValidatorTest.java
===================================================================
--- trunk/nuiton-validator/src/test/java/org/nuiton/validator/bean/BeanValidatorTest.java 2011-01-24 12:54:50 UTC (rev 2039)
+++ trunk/nuiton-validator/src/test/java/org/nuiton/validator/bean/BeanValidatorTest.java 2011-01-24 13:08:51 UTC (rev 2040)
@@ -30,8 +30,6 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
-import org.nuiton.validator.NuitonValidatorFactory;
-import org.nuiton.validator.NuitonValidatorProvider;
import org.nuiton.validator.NuitonValidatorScope;
import org.nuiton.validator.xwork2.XWork2NuitonValidatorProvider;
@@ -59,9 +57,14 @@
@Before
public void setUp() {
- NuitonValidatorProvider provider = NuitonValidatorFactory.getProvider(XWork2NuitonValidatorProvider.PROVIDER_NAME);
+
bean = new SimpleBean();
- validator = new BeanValidator<SimpleBean>(provider, SimpleBean.class, null);
+ }
+
+ protected void prepareValidator(String context) {
+
+ validator = BeanValidatorFactory.newBeanValidator(XWork2NuitonValidatorProvider.PROVIDER_NAME, SimpleBean.class, context);
+
validator.addBeanValidatorListener(fatalListener = new BeanValidatorListenerImpl(NuitonValidatorScope.FATAL));
validator.addBeanValidatorListener(errorListener = new BeanValidatorListenerImpl(NuitonValidatorScope.ERROR));
validator.addBeanValidatorListener(warningListener = new BeanValidatorListenerImpl(NuitonValidatorScope.WARNING));
@@ -89,9 +92,19 @@
private static final String INT_VALUE_INFO = "intValue.info";
+ @Test(expected = IllegalStateException.class)
+ public void testValidateWithBad() {
+
+ // with marchepo context, there is a unknown field in scope error
+
+ prepareValidator("marchepo");
+ }
+
@Test
public void testValidate() {
+ prepareValidator(null);
+
assertMessages(fatalListener);
assertMessages(errorListener);
assertMessages(warningListener);
@@ -176,6 +189,8 @@
public void testConvert() {
+ prepareValidator(null);
+
assertMessages(errorListener);
assertMessages(warningListener);
assertMessages(infoListener);
Modified: trunk/nuiton-validator/src/test/java/org/nuiton/validator/model/ModelValidatorDetectorTestImpl.java
===================================================================
--- trunk/nuiton-validator/src/test/java/org/nuiton/validator/model/ModelValidatorDetectorTestImpl.java 2011-01-24 12:54:50 UTC (rev 2039)
+++ trunk/nuiton-validator/src/test/java/org/nuiton/validator/model/ModelValidatorDetectorTestImpl.java 2011-01-24 13:08:51 UTC (rev 2040)
@@ -1,3 +1,27 @@
+/*
+ * #%L
+ * Nuiton Utils :: Nuiton Validator
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2011 CodeLutin
+ * %%
+ * This program 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 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
package org.nuiton.validator.model;
import org.junit.Assert;
@@ -39,7 +63,7 @@
NuitonValidator<?> validator;
Iterator<NuitonValidator<?>> iterator;
- // test with all context and all scopes : two validators (Person + Pet + Pet (context))
+ // test with all context and all scopes : 3 validators (Person + Pet + Pet (context))
result = detectValidators(Person.class, Pet.class);
@@ -52,14 +76,18 @@
ValidatorTestHelper.assertValidatorModel(validator, null, Person.class, NuitonValidatorScope.values());
ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.ERROR, NuitonValidatorScope.WARNING);
+ ValidatorTestHelper.assertValidatorEffectiveFields(validator, NuitonValidatorScope.ERROR, Person.PROPERTY_NAME, Person.PROPERTY_FIRSTNAME);
+ ValidatorTestHelper.assertValidatorEffectiveFields(validator, NuitonValidatorScope.WARNING, Person.PROPERTY_PET);
validator = iterator.next();
ValidatorTestHelper.assertValidatorModel(validator, null, Pet.class, NuitonValidatorScope.values());
ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.ERROR);
+ ValidatorTestHelper.assertValidatorEffectiveFields(validator, NuitonValidatorScope.ERROR, Pet.PROPERTY_NAME);
validator = iterator.next();
ValidatorTestHelper.assertValidatorModel(validator, CONTEXT, Pet.class, NuitonValidatorScope.values());
ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.INFO);
+ ValidatorTestHelper.assertValidatorEffectiveFields(validator, NuitonValidatorScope.INFO, Pet.PROPERTY_NAME);
}
@@ -82,6 +110,7 @@
validator = iterator.next();
ValidatorTestHelper.assertValidatorModel(validator, null, Person.class, NuitonValidatorScope.WARNING);
ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.WARNING);
+ ValidatorTestHelper.assertValidatorEffectiveFields(validator, NuitonValidatorScope.WARNING, Person.PROPERTY_PET);
// test with no context and only fatal scope : no validator
@@ -113,6 +142,7 @@
validator = iterator.next();
ValidatorTestHelper.assertValidatorModel(validator, CONTEXT, Pet.class, NuitonValidatorScope.values());
ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.INFO);
+ ValidatorTestHelper.assertValidatorEffectiveFields(validator, NuitonValidatorScope.INFO, Pet.PROPERTY_NAME);
// test with specific context fake and all scopes : no validator
@@ -147,6 +177,7 @@
validator = iterator.next();
ValidatorTestHelper.assertValidatorModel(validator, CONTEXT, Pet.class, NuitonValidatorScope.FATAL, NuitonValidatorScope.INFO);
ValidatorTestHelper.assertValidatorEffectiveScopes(validator, NuitonValidatorScope.INFO);
+ ValidatorTestHelper.assertValidatorEffectiveFields(validator, NuitonValidatorScope.INFO, Pet.PROPERTY_NAME);
// test with specific context fake and fatal scope : no validator
Modified: trunk/nuiton-validator/src/test/resources/org/nuiton/validator/bean/SimpleBean-error-validation.xml
===================================================================
--- trunk/nuiton-validator/src/test/resources/org/nuiton/validator/bean/SimpleBean-error-validation.xml 2011-01-24 12:54:50 UTC (rev 2039)
+++ trunk/nuiton-validator/src/test/resources/org/nuiton/validator/bean/SimpleBean-error-validation.xml 2011-01-24 13:08:51 UTC (rev 2040)
@@ -23,21 +23,21 @@
#L%
-->
<!DOCTYPE validators PUBLIC
- "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
+ "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
+ "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
- <field name="stringValue">
- <field-validator type="requiredstring">
- <message>stringValue.error</message>
- </field-validator>
- </field>
+ <field name="stringValue">
+ <field-validator type="requiredstring">
+ <message>stringValue.error</message>
+ </field-validator>
+ </field>
- <field name="intValue">
- <field-validator type="int">
- <param name="min">1</param>
- <message>intValue.error</message>
- </field-validator>
- </field>
+ <field name="intValue">
+ <field-validator type="int">
+ <param name="min">1</param>
+ <message>intValue.error</message>
+ </field-validator>
+ </field>
</validators>
\ No newline at end of file
Copied: trunk/nuiton-validator/src/test/resources/org/nuiton/validator/bean/SimpleBean-marchepo-error-validation.xml (from rev 2034, trunk/nuiton-validator/src/test/resources/org/nuiton/validator/bean/SimpleBean-error-validation.xml)
===================================================================
--- trunk/nuiton-validator/src/test/resources/org/nuiton/validator/bean/SimpleBean-marchepo-error-validation.xml (rev 0)
+++ trunk/nuiton-validator/src/test/resources/org/nuiton/validator/bean/SimpleBean-marchepo-error-validation.xml 2011-01-24 13:08:51 UTC (rev 2040)
@@ -0,0 +1,38 @@
+<!--
+ #%L
+ Nuiton Utils :: Nuiton Validator
+
+ $Id$
+ $HeadURL$
+ %%
+ Copyright (C) 2011 CodeLutin, Tony Chemit
+ %%
+ This program 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 3 of the
+ License, or (at your option) any later version.
+
+ This program 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 General Lesser Public License for more details.
+
+ You should have received a copy of the GNU General Lesser Public
+ License along with this program. If not, see
+ <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ #L%
+ -->
+<!DOCTYPE validators PUBLIC
+ "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
+ "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
+<validators>
+
+ <!-- add a unknown field -->
+
+ <field name="stringValueUnknown">
+ <field-validator type="requiredstring">
+ <message>stringValue.error.can.not.happen!</message>
+ </field-validator>
+ </field>
+
+</validators>
\ No newline at end of file
1
0
r2039 - trunk/nuiton-utils/src/main/java/org/nuiton/util/beans
by tchemit@users.nuiton.org 24 Jan '11
by tchemit@users.nuiton.org 24 Jan '11
24 Jan '11
Author: tchemit
Date: 2011-01-24 13:54:50 +0100 (Mon, 24 Jan 2011)
New Revision: 2039
Url: http://nuiton.org/repositories/revision/nuiton-utils/2039
Log:
reformat
Modified:
trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java
Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java
===================================================================
--- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 12:53:53 UTC (rev 2038)
+++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 12:54:50 UTC (rev 2039)
@@ -67,14 +67,16 @@
public static boolean isJavaBeanCompiliant(Class<?> type) {
try {
- type.getMethod(ADD_PROPERTY_CHANGE_LISTENER, PropertyChangeListener.class);
+ type.getMethod(ADD_PROPERTY_CHANGE_LISTENER,
+ PropertyChangeListener.class);
} catch (NoSuchMethodException e) {
// no add method
return false;
}
try {
- type.getMethod(REMOVE_PROPERTY_CHANGE_LISTENER, PropertyChangeListener.class);
+ type.getMethod(REMOVE_PROPERTY_CHANGE_LISTENER,
+ PropertyChangeListener.class);
} catch (NoSuchMethodException e) {
// no add method
return false;
@@ -145,7 +147,7 @@
// get properties for the class
getReadableProperties(beanType, result, exploredTypes);
- // the special getClass will never be a Javabean property...
+ // the special getClass will never be a JavaBean property...
result.remove("class");
return result;
1
0
r2038 - in trunk/nuiton-utils/src: main/java/org/nuiton/util/beans main/java/org/nuiton/util/rmi test/java/org/nuiton/util test/java/org/nuiton/util/beans test/java/org/nuiton/util/rmi
by tchemit@users.nuiton.org 24 Jan '11
by tchemit@users.nuiton.org 24 Jan '11
24 Jan '11
Author: tchemit
Date: 2011-01-24 13:53:53 +0100 (Mon, 24 Jan 2011)
New Revision: 2038
Url: http://nuiton.org/repositories/revision/nuiton-utils/2038
Log:
add svn keywords + license headers
Modified:
trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java
trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/RemoteProxyFactory.java
trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/ServiceExporter.java
trunk/nuiton-utils/src/test/java/org/nuiton/util/TestHelper.java
trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java
trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/AnyException.java
trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/RmiExporterAndProxyTest.java
trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeBean.java
trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeService.java
trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeServiceImpl.java
Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java
===================================================================
--- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -40,9 +40,12 @@
* @since 1.4.1
*/
public class BeanUtil {
- public static final String ADD_PROPERTY_CHANGE_LISTENER = "addPropertyChangeListener";
+
+ public static final String ADD_PROPERTY_CHANGE_LISTENER =
+ "addPropertyChangeListener";
- public static final String REMOVE_PROPERTY_CHANGE_LISTENER = "removePropertyChangeListener";
+ public static final String REMOVE_PROPERTY_CHANGE_LISTENER =
+ "removePropertyChangeListener";
protected BeanUtil() {
// no instance
Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/RemoteProxyFactory.java
===================================================================
--- trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/RemoteProxyFactory.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/RemoteProxyFactory.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -1,23 +1,23 @@
/*
* #%L
* Nuiton Utils
- *
+ * *
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2010 CodeLutin
* %%
* This program 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 3 of the
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
- *
+ *
* This program 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 General Lesser Public License for more details.
- *
- * You should have received a copy of the GNU General Lesser Public
+ *
+ * You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/ServiceExporter.java
===================================================================
--- trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/ServiceExporter.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/main/java/org/nuiton/util/rmi/ServiceExporter.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -1,23 +1,23 @@
/*
* #%L
* Nuiton Utils
- *
+ * *
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2010 CodeLutin
* %%
* This program 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 3 of the
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
- *
+ *
* This program 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 General Lesser Public License for more details.
- *
- * You should have received a copy of the GNU General Lesser Public
+ *
+ * You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/TestHelper.java
===================================================================
--- trunk/nuiton-utils/src/test/java/org/nuiton/util/TestHelper.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/test/java/org/nuiton/util/TestHelper.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -1,3 +1,27 @@
+/*
+ * #%L
+ * Nuiton Utils :: Nuiton Utils
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2004 - 2011 CodeLutin
+ * %%
+ * This program 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 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
package org.nuiton.util;
import org.apache.commons.logging.*;
Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java
===================================================================
--- trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -1,3 +1,27 @@
+/*
+ * #%L
+ * Nuiton Utils :: Nuiton Utils
+ *
+ * $Id$
+ * $HeadURL$
+ * %%
+ * Copyright (C) 2004 - 2011 CodeLutin
+ * %%
+ * This program 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 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 General Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU General Lesser Public
+ * License along with this program. If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
package org.nuiton.util.beans;
import org.junit.Assert;
Property changes on: trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java
___________________________________________________________________
Modified: svn:keywords
- Author Date Id Revision
+ Author Date Id Revision HeadURL
Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/AnyException.java
===================================================================
--- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/AnyException.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/AnyException.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -1,23 +1,23 @@
/*
* #%L
* Nuiton Utils
- *
+ * *
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2010 CodeLutin
* %%
* This program 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 3 of the
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
- *
+ *
* This program 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 General Lesser Public License for more details.
- *
- * You should have received a copy of the GNU General Lesser Public
+ *
+ * You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/RmiExporterAndProxyTest.java
===================================================================
--- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/RmiExporterAndProxyTest.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/RmiExporterAndProxyTest.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -1,23 +1,23 @@
/*
* #%L
* Nuiton Utils
- *
+ * *
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2010 CodeLutin
* %%
* This program 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 3 of the
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
- *
+ *
* This program 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 General Lesser Public License for more details.
- *
- * You should have received a copy of the GNU General Lesser Public
+ *
+ * You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeBean.java
===================================================================
--- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeBean.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeBean.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -1,23 +1,23 @@
/*
* #%L
* Nuiton Utils
- *
+ * *
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2010 CodeLutin
* %%
* This program 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 3 of the
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
- *
+ *
* This program 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 General Lesser Public License for more details.
- *
- * You should have received a copy of the GNU General Lesser Public
+ *
+ * You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeService.java
===================================================================
--- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeService.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeService.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -1,23 +1,23 @@
/*
* #%L
* Nuiton Utils
- *
+ * *
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2010 CodeLutin
* %%
* This program 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 3 of the
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
- *
+ *
* This program 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 General Lesser Public License for more details.
- *
- * You should have received a copy of the GNU General Lesser Public
+ *
+ * You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeServiceImpl.java
===================================================================
--- trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeServiceImpl.java 2011-01-24 12:51:41 UTC (rev 2037)
+++ trunk/nuiton-utils/src/test/java/org/nuiton/util/rmi/SomeServiceImpl.java 2011-01-24 12:53:53 UTC (rev 2038)
@@ -1,23 +1,23 @@
/*
* #%L
* Nuiton Utils
- *
+ * *
* $Id$
* $HeadURL$
* %%
* Copyright (C) 2004 - 2010 CodeLutin
* %%
* This program 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 3 of the
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
- *
+ *
* This program 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 General Lesser Public License for more details.
- *
- * You should have received a copy of the GNU General Lesser Public
+ *
+ * You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
1
0
r2037 - in trunk/nuiton-utils/src: main/java/org/nuiton/util/beans test/java/org/nuiton/util/beans
by tchemit@users.nuiton.org 24 Jan '11
by tchemit@users.nuiton.org 24 Jan '11
24 Jan '11
Author: tchemit
Date: 2011-01-24 13:51:41 +0100 (Mon, 24 Jan 2011)
New Revision: 2037
Url: http://nuiton.org/repositories/revision/nuiton-utils/2037
Log:
Evolution #1228: Add more usefull methods in BeanUtil + test the BeanUtil class
Added:
trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java
Modified:
trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java
trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanA.java
Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java
===================================================================
--- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 11:39:23 UTC (rev 2036)
+++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 12:51:41 UTC (rev 2037)
@@ -49,6 +49,38 @@
}
/**
+ * Test if the given type is JavaBean compiliant, says that it has two
+ * public methods :
+ * <ul>
+ * <li>{@code addPropertyChangeListener}</li>
+ * <li>{@code removePropertyChangeListener}</li>
+ * </ul>
+ *
+ * @param type type to test
+ * @return {@code true} if type is Javabean compiliant, {@code false}
+ * otherwise
+ * @since 2.0
+ */
+ public static boolean isJavaBeanCompiliant(Class<?> type) {
+
+ try {
+ type.getMethod(ADD_PROPERTY_CHANGE_LISTENER, PropertyChangeListener.class);
+ } catch (NoSuchMethodException e) {
+ // no add method
+ return false;
+ }
+
+ try {
+ type.getMethod(REMOVE_PROPERTY_CHANGE_LISTENER, PropertyChangeListener.class);
+ } catch (NoSuchMethodException e) {
+ // no add method
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
* Add the given {@code listener} to the given {@code bean} using the
* normalized method named {@code addPropertyChangeListener}.
*
@@ -110,6 +142,9 @@
// get properties for the class
getReadableProperties(beanType, result, exploredTypes);
+ // the special getClass will never be a Javabean property...
+ result.remove("class");
+
return result;
}
Modified: trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanA.java
===================================================================
--- trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanA.java 2011-01-24 11:39:23 UTC (rev 2036)
+++ trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanA.java 2011-01-24 12:51:41 UTC (rev 2037)
@@ -31,6 +31,7 @@
public class BeanA {
public static final String PROPERTY_AA = "aa";
+
public static final String PROPERTY_A = "a";
public static final String PROPERTY_B = "b";
@@ -135,4 +136,8 @@
Object newValue) {
pcs.firePropertyChange(propertyName, oldValue, newValue);
}
+
+ protected PropertyChangeListener[] getPropertyChangeListeners() {
+ return pcs.getPropertyChangeListeners();
+ }
}
Added: trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java
===================================================================
--- trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java (rev 0)
+++ trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java 2011-01-24 12:51:41 UTC (rev 2037)
@@ -0,0 +1,155 @@
+package org.nuiton.util.beans;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.beans.PropertyChangeListener;
+import java.beans.beancontext.BeanContextSupport;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Set;
+
+/**
+ * To test the {@link BeanUtil} class.
+ *
+ * @author tchemit <chemit(a)codelutin.com>
+ * @since 2.0
+ */
+public class BeanUtilTest {
+
+ @Test
+ public void isJavaBeanCompiliant() {
+
+ boolean javaBeanCompiliant;
+
+ javaBeanCompiliant = BeanUtil.isJavaBeanCompiliant(BeanA.class);
+ Assert.assertTrue(javaBeanCompiliant);
+
+ javaBeanCompiliant = BeanUtil.isJavaBeanCompiliant(BeanB.class);
+ Assert.assertTrue(javaBeanCompiliant);
+
+ javaBeanCompiliant = BeanUtil.isJavaBeanCompiliant(getClass());
+ Assert.assertFalse(javaBeanCompiliant);
+ }
+
+ @Test
+ public void getReadableProperties() {
+
+ assertFoundReadableProperties(BeanA.class,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F);
+
+ assertFoundReadableProperties(BeanB.class,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F,
+ BeanB.PROPERTY_BB,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+
+ assertFoundReadableProperties(getClass());
+ }
+
+ @Test
+ public void getWriteableProperties() {
+
+ assertFoundWriteableProperties(BeanA.class,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F);
+
+ assertFoundWriteableProperties(BeanB.class,
+ BeanA.PROPERTY_A,
+ BeanA.PROPERTY_B,
+ BeanA.PROPERTY_C,
+ BeanA.PROPERTY_D,
+ BeanA.PROPERTY_E,
+ BeanA.PROPERTY_F,
+ BeanB.PROPERTY_BB,
+ BeanB.PROPERTY_A2,
+ BeanB.PROPERTY_B2,
+ BeanB.PROPERTY_C2,
+ BeanB.PROPERTY_D2,
+ BeanB.PROPERTY_E2,
+ BeanB.PROPERTY_F2
+ );
+
+ assertFoundWriteableProperties(getClass());
+ }
+
+ @Test
+ public void addPropertyChangeListener() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
+
+ PropertyChangeListener l = new BeanContextSupport();
+
+ BeanA beanA = new BeanA();
+
+ BeanUtil.addPropertyChangeListener(l, beanA);
+
+ PropertyChangeListener[] listeners;
+ listeners = beanA.getPropertyChangeListeners();
+
+ Assert.assertEquals(1, listeners.length);
+ Assert.assertEquals(l, listeners[0]);
+
+
+ BeanUtil.addPropertyChangeListener(l, beanA);
+
+ listeners = beanA.getPropertyChangeListeners();
+
+ Assert.assertEquals(2, listeners.length);
+ Assert.assertEquals(l, listeners[0]);
+ Assert.assertEquals(l, listeners[1]);
+ }
+
+ @Test
+ public void removePropertyChangeListener() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
+
+ PropertyChangeListener[] listeners;
+
+ PropertyChangeListener l = new BeanContextSupport();
+
+ BeanA beanA = new BeanA();
+ beanA.addPropertyChangeListener(l);
+
+ listeners = beanA.getPropertyChangeListeners();
+ Assert.assertEquals(1, listeners.length);
+ Assert.assertEquals(l, listeners[0]);
+
+ BeanUtil.removePropertyChangeListener(l, beanA);
+
+ listeners = beanA.getPropertyChangeListeners();
+
+ Assert.assertEquals(0, listeners.length);
+ }
+
+ protected void assertFoundReadableProperties(Class<?> type, String... expectedproperties) {
+ Set<String> readableProperties = BeanUtil.getReadableProperties(type);
+ Assert.assertEquals(expectedproperties.length, readableProperties.size());
+ for (String expectedproperty : expectedproperties) {
+ Assert.assertTrue("Did not found property " + expectedproperty, readableProperties.contains(expectedproperty));
+ }
+ }
+
+ protected void assertFoundWriteableProperties(Class<?> type, String... expectedproperties) {
+ Set<String> readableProperties = BeanUtil.getReadableProperties(type);
+ Assert.assertEquals(expectedproperties.length, readableProperties.size());
+ for (String expectedproperty : expectedproperties) {
+ Assert.assertTrue(readableProperties.contains(expectedproperty));
+ }
+ }
+}
Property changes on: trunk/nuiton-utils/src/test/java/org/nuiton/util/beans/BeanUtilTest.java
___________________________________________________________________
Added: svn:keywords
+ Author Date Id Revision
Added: svn:eol-style
+ native
1
0
r2036 - trunk/nuiton-utils/src/main/java/org/nuiton/util/beans
by tchemit@users.nuiton.org 24 Jan '11
by tchemit@users.nuiton.org 24 Jan '11
24 Jan '11
Author: tchemit
Date: 2011-01-24 12:39:23 +0100 (Mon, 24 Jan 2011)
New Revision: 2036
Url: http://nuiton.org/repositories/revision/nuiton-utils/2036
Log:
Evolution #1228: Add more usefull methods in BeanUtil
Modified:
trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java
Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java
===================================================================
--- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 10:55:04 UTC (rev 2035)
+++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/BeanUtil.java 2011-01-24 11:39:23 UTC (rev 2036)
@@ -25,9 +25,13 @@
package org.nuiton.util.beans;
import org.apache.commons.beanutils.MethodUtils;
+import org.apache.commons.beanutils.PropertyUtils;
import java.beans.PropertyChangeListener;
+import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
+import java.util.HashSet;
+import java.util.Set;
/**
* Usefull methods around the {@link PropertyChangeListener}.
@@ -91,4 +95,118 @@
new Class[]{PropertyChangeListener.class}
);
}
+
+ /**
+ * Obtains all readable properties from a given type.
+ *
+ * @param beanType the type to seek
+ * @return the set of all readable properties for the given type
+ * @since 2.0
+ */
+ public static Set<String> getReadableProperties(Class<?> beanType) {
+ Set<Class<?>> exploredTypes = new HashSet<Class<?>>();
+ Set<String> result = new HashSet<String>();
+
+ // get properties for the class
+ getReadableProperties(beanType, result, exploredTypes);
+
+ return result;
+ }
+
+ /**
+ * Obtains all writeable properties from a given type.
+ *
+ * @param beanType the type to seek
+ * @return the set of all writeable properties for the given type
+ * @since 2.0
+ */
+ public static Set<String> getWriteableProperties(Class<?> beanType) {
+ Set<Class<?>> exploredTypes = new HashSet<Class<?>>();
+ Set<String> result = new HashSet<String>();
+
+ // get properties for the class
+ getWriteableProperties(beanType, result, exploredTypes);
+
+ return result;
+ }
+
+ protected static void getReadableProperties(Class<?> beanType,
+ Set<String> result,
+ Set<Class<?>> exploredTypes) {
+
+ if (exploredTypes.contains(beanType)) {
+
+ // already explored
+ return;
+ }
+ exploredTypes.add(beanType);
+
+ // get properties for the class
+ getReadableProperties(beanType, result);
+
+ if (beanType.getSuperclass() != null) {
+
+ // get properties fro super-class
+ getReadableProperties(beanType.getSuperclass(), result, exploredTypes);
+ }
+ Class<?>[] interfaces = beanType.getInterfaces();
+ for (Class<?> anInterface : interfaces) {
+
+ // get properties fro super-class
+ getReadableProperties(anInterface, result, exploredTypes);
+ }
+ }
+
+ protected static void getReadableProperties(Class<?> beanType,
+ Set<String> result) {
+
+ PropertyDescriptor[] descriptors =
+ PropertyUtils.getPropertyDescriptors(beanType);
+ for (PropertyDescriptor descriptor : descriptors) {
+ String name = descriptor.getName();
+ if (descriptor.getReadMethod() != null) {
+ result.add(name);
+ }
+ }
+ }
+
+ protected static void getWriteableProperties(Class<?> beanType,
+ Set<String> result,
+ Set<Class<?>> exploredTypes) {
+
+ if (exploredTypes.contains(beanType)) {
+
+ // already explored
+ return;
+ }
+ exploredTypes.add(beanType);
+
+ // get properties for the class
+ getWriteableProperties(beanType, result);
+
+ if (beanType.getSuperclass() != null) {
+
+ // get properties fro super-class
+ getWriteableProperties(beanType.getSuperclass(), result, exploredTypes);
+ }
+ Class<?>[] interfaces = beanType.getInterfaces();
+ for (Class<?> anInterface : interfaces) {
+
+ // get properties fro super-class
+ getWriteableProperties(anInterface, result, exploredTypes);
+ }
+ }
+
+ protected static void getWriteableProperties(Class<?> beanType,
+ Set<String> result) {
+
+ PropertyDescriptor[] descriptors =
+ PropertyUtils.getPropertyDescriptors(beanType);
+ for (PropertyDescriptor descriptor : descriptors) {
+ String name = descriptor.getName();
+ if (descriptor.getReadMethod() != null) {
+ result.add(name);
+ }
+ }
+ }
}
1
0