commons-beanutils-1.9.1-src/build.properties.sample 100644 0 0 3045 12262570613 17600 0 ustar 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Repository base path
repository=${user.home}/.m2/repository
# The pathname of the collections classes JAR file
commons-collections.home=${repository}/commons-collections
commons-collections.jar=${commons-collections.home}/commons-collections/3.2.1/commons-collections-3.2.1.jar
commons-collections-testframework.jar=${commons-collections.home}/commons-collections-testframework/3.2.1/commons-collections-testframework-3.2.1.jar
# The pathname of the Commons Logging JAR file
commons-logging.home=${repository}/commons-logging/commons-logging
commons-logging.jar=${commons-logging.home}/1.1.1/commons-logging-1.1.1.jar
# The pathname of the "junit.jar" JAR file
junit.home=${repository}/junit/junit
junit.jar=${junit.home}/3.8.1/junit-3.8.1.jar
commons-beanutils-1.9.1-src/build.xml 100644 0 0 30073 12262570613 14745 0 ustar 0 0
commons-beanutils-1.9.1-src/checkstyle.xml 100644 0 0 4307 12262570613 15765 0 ustar 0 0
commons-beanutils-1.9.1-src/license-header.txt 100644 0 0 1463 12262570606 16520 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
commons-beanutils-1.9.1-src/LICENSE.txt 100644 0 0 26450 12262570613 14753 0 ustar 0 0
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
commons-beanutils-1.9.1-src/NOTICE.txt 100644 0 0 267 12262570613 14610 0 ustar 0 0 Apache Commons BeanUtils
Copyright 2000-2014 The Apache Software Foundation
This product includes software developed at
The Apache Software Foundation (http://www.apache.org/).
commons-beanutils-1.9.1-src/pom.xml 100644 0 0 33360 12262570613 14443 0 ustar 0 0
org.apache.commonscommons-parent324.0.0commons-beanutilscommons-beanutils1.9.1Apache Commons BeanUtils2000Apache Commons BeanUtils provides an easy-to-use but flexible wrapper around reflection and introspection.http://commons.apache.org/proper/commons-beanutils/jirahttp://issues.apache.org/jira/browse/BEANUTILSscm:svn:http://svn.apache.org/repos/asf/commons/proper/beanutils/tags/BEANUTILS_1_9_1/scm:svn:https://svn.apache.org/repos/asf/commons/proper/beanutils/tags/BEANUTILS_1_9_1/http://svn.apache.org/viewvc/commons/proper/beanutils/tags/BEANUTILS_1_9_1/apache.websiteApache Commons Beanutils Sitescm:svn:https://svn.apache.org/repos/infra/websites/production/commons/content/proper/commons-beanutilsRobert Burrell Donkinrdonkinrdonkin@apache.orgThe Apache Software FoundationDion Gillarddiondion@apache.orgThe Apache Software FoundationCraig McClanahancraigmcccraigmcc@apache.orgThe Apache Software FoundationGeir Magnusson Jr.geirmgeirm@apache.orgThe Apache Software FoundationScott Sanderssanderssanders@apache.orgThe Apache Software FoundationJames Strachanjstrachanjstrachan@apache.orgThe Apache Software FoundationRodney Waldhoffrwaldhoffrwaldhoff@apache.orgThe Apache Software FoundationMartin van den Bemtmvdbmvdb@apache.orgThe Apache Software FoundationYoav Shapirayoavsyoavs@apache.orgThe Apache Software FoundationNiall Pembertonniallpniallp@apache.orgThe Apache Software FoundationSimon Kitchingskitchingskitching@apache.orgThe Apache Software FoundationJames Carmanjcarmanjcarman@apache.orgThe Apache Software FoundationBenedikt Ritterbritterbritter@apache.orgThe Apache Software FoundationTim O'Brientobrientobrien@apache.orgThe Apache Software FoundationDavid Eric Pughepughepugh@apache.orgThe Apache Software FoundationRodney Waldhoffrwaldhoffrwaldhoff@apache.orgThe Apache Software FoundationMorgan James Delagrangemorgandmorgand@apache.orgThe Apache Software FoundationJohn E. Conlonjconlonjconlon@apache.orgThe Apache Software FoundationStephen Colebournescolebournescolebourne@apache.orgThe Apache Software FoundationPaul JackStephen ColebourneBerin LoritschAlex CrownMarcus ZanderPaul HamamntRune JohannesenClebert SuconicNorm DeaneRalph SchaerChris AudleyRey FrançoisGregor RaýmanJan SorensenEric PabstPaulo GasparMichael SmithGeorge FranciscusErik MeadeTomas VibergYauheny MikulskiMichael SzlapaJuozas Baliukacommons-loggingcommons-logging1.1.1commons-collectionscommons-collections3.2.1commons-collectionscommons-collections-testframework3.2.1testjunitjunit3.8.1testorg.apache.maven.pluginsmaven-surefire-pluginpertest-Xmx25M**/*TestCase.java**/*MemoryTestCase.javatrueorg.apache.commons.logging.impl.LogFactoryImplorg.apache.commons.logging.impl.SimpleLog>
WARNmaven-assembly-pluginsrc/main/assembly/bin.xmlsrc/main/assembly/src.xmlgnuorg.apache.maven.pluginsmaven-scm-publish-pluginjavadocs**release-notes**1.51.5beanutils1.9.1BEANUTILS12310460org.apache.maven.pluginsmaven-checkstyle-plugin2.6${basedir}/checkstyle.xmlfalse${basedir}/license-header.txtorg.apache.maven.pluginsmaven-javadoc-plugintrue
http://download.oracle.com/javase/1.4.2/docs/api/
http://commons.apache.org/collections/api-release/
org.apache.maven.pluginsmaven-changes-plugin2.9%URL%/%ISSUE%changes-report
commons-beanutils-1.9.1-src/README.txt 100644 0 0 2005 12262570613 14574 0 ustar 0 0 README
======
Jars
----
BeanUtils now comes packaged (into jars) in two different ways:
an all-in-one jar (commons-beanutils.jar);
and as modular component jars:
commons-beanutils-core.jar (core classes)
commons-beanutils-bean-collections.jar (additional dependency on commons-collections 3.0)
Those who want it all should grab the all-in-one jar (and the optional dependencies)
whereas those who need minimal dependencies should use commons-beanutils-core.jar
plus any optional jars they plan to use.
All classes that were in the last release are in commons-beanutils-core except for BeanComparator.
Commons-Collections
-------------------
BeanUtils now ships with a small number of commons collections classes.
This is a temporary measure intended to allow BeanUtils core to be used with
either commons-collections 2 or commons-collections-3 without a dependency on either.
It is intended that soon BeanUtils core will have no dependency on any commons collection
packaged classes. commons-beanutils-1.9.1-src/RELEASE-NOTES.txt 100644 0 0 21260 12262570606 15633 0 ustar 0 0 $Id: RELEASE-NOTES.txt 1555941 2014-01-06 17:45:14Z oheger $
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Commons BeanUtils Package
Version 1.9.1
Release Notes
INTRODUCTION:
============
This document contains the release notes for this version of the Commons
BeanUtils package, and highlights changes since the previous version.
For more information on Commons BeanUtils, see
o http://commons.apache.org/beanutils/
Release 1.9.1 is a bug fix release which addresses a problem with the new
feature of custom introspection introduced with release 1.9.0. It is fully
binary compatible with the previous release. The minimum required Java version
is 1.5.
BUGFIXES in version 1.9.1
=========================
* [BEANUTILS-456]
For PropertyDescriptors obtained via custom introspection now additional
information is stored to prevent that write methods are lost during
garbage collection.
Release Notes for version 1.9.0
Release 1.9.0 contains some bug fixes and improvements that have accumulated
after the 1.8.3 release. The most obvious change is that the new version now
requires JDK 1.5 or higher, and that language features introduced with Java 5
(mainly generics) are used. A new feature has been introduced, too: the support
for customizing bean introspection.
Compatibility with 1.8.3
========================
Adding generics to the BeanUtils API has been done in a backwards compatible
way. This means that after type erasure the resulting classes look the same as
in the previous version. A drawback of this approach is that sometimes it is
not possible to use the logically correct type parameters because then
backwards compatibility would be broken. One example is the BeanMap class: The
class is now a Map
* This, for example, could be used in JSTL in the following way to access
* a DynaBean's fooProperty:
*
${myDynaBean.map.fooProperty}
*
* @return a Map representation of this DynaBean
* @since 1.8.0
*/
public Map getMap() {
// cache the Map
if (mapDecorator == null) {
mapDecorator = new DynaBeanPropertyMapDecorator(this);
}
return mapDecorator;
}
// ------------------------------------------------------ DynaBean Methods
/**
* Does the specified mapped property contain a value for the specified
* key value?
*
* @param name Name of the property to check
* @param key Name of the key to check
* @return true if the mapped property contains a value for
* the specified key, otherwise false
*
* @exception IllegalArgumentException if there is no property
* of the specified name
*/
public boolean contains(String name, String key) {
Object value = values.get(name);
if (value == null) {
throw new NullPointerException
("No mapped value for '" + name + "(" + key + ")'");
} else if (value instanceof Map) {
return (((Map, ?>) value).containsKey(key));
} else {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'");
}
}
/**
* Return the value of a simple property with the specified name.
*
* @param name Name of the property whose value is to be retrieved
* @return The property's value
*
* @exception IllegalArgumentException if there is no property
* of the specified name
*/
public Object get(String name) {
// Return any non-null value for the specified property
Object value = values.get(name);
if (value != null) {
return (value);
}
// Return a null value for a non-primitive property
Class> type = getDynaProperty(name).getType();
if (!type.isPrimitive()) {
return (value);
}
// Manufacture default values for primitive properties
if (type == Boolean.TYPE) {
return (Boolean.FALSE);
} else if (type == Byte.TYPE) {
return (new Byte((byte) 0));
} else if (type == Character.TYPE) {
return (new Character((char) 0));
} else if (type == Double.TYPE) {
return (new Double(0.0));
} else if (type == Float.TYPE) {
return (new Float((float) 0.0));
} else if (type == Integer.TYPE) {
return (new Integer(0));
} else if (type == Long.TYPE) {
return (new Long(0));
} else if (type == Short.TYPE) {
return (new Short((short) 0));
} else {
return (null);
}
}
/**
* Return the value of an indexed property with the specified name.
*
* @param name Name of the property whose value is to be retrieved
* @param index Index of the value to be retrieved
* @return The indexed property's value
*
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception IllegalArgumentException if the specified property
* exists, but is not indexed
* @exception IndexOutOfBoundsException if the specified index
* is outside the range of the underlying property
* @exception NullPointerException if no array or List has been
* initialized for this property
*/
public Object get(String name, int index) {
Object value = values.get(name);
if (value == null) {
throw new NullPointerException
("No indexed value for '" + name + "[" + index + "]'");
} else if (value.getClass().isArray()) {
return (Array.get(value, index));
} else if (value instanceof List) {
return ((List>) value).get(index);
} else {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]'");
}
}
/**
* Return the value of a mapped property with the specified name,
* or null if there is no value for the specified key.
*
* @param name Name of the property whose value is to be retrieved
* @param key Key of the value to be retrieved
* @return The mapped property's value
*
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception IllegalArgumentException if the specified property
* exists, but is not mapped
*/
public Object get(String name, String key) {
Object value = values.get(name);
if (value == null) {
throw new NullPointerException
("No mapped value for '" + name + "(" + key + ")'");
} else if (value instanceof Map) {
return (((Map, ?>) value).get(key));
} else {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'");
}
}
/**
* Return the DynaClass instance that describes the set of
* properties available for this DynaBean.
*
* @return The associated DynaClass
*/
public DynaClass getDynaClass() {
return (this.dynaClass);
}
/**
* Remove any existing value for the specified key on the
* specified mapped property.
*
* @param name Name of the property for which a value is to
* be removed
* @param key Key of the value to be removed
*
* @exception IllegalArgumentException if there is no property
* of the specified name
*/
public void remove(String name, String key) {
Object value = values.get(name);
if (value == null) {
throw new NullPointerException
("No mapped value for '" + name + "(" + key + ")'");
} else if (value instanceof Map) {
((Map, ?>) value).remove(key);
} else {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'");
}
}
/**
* Set the value of a simple property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param value Value to which this property is to be set
*
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception NullPointerException if an attempt is made to set a
* primitive property to null
*/
public void set(String name, Object value) {
DynaProperty descriptor = getDynaProperty(name);
if (value == null) {
if (descriptor.getType().isPrimitive()) {
throw new NullPointerException
("Primitive value for '" + name + "'");
}
} else if (!isAssignable(descriptor.getType(), value.getClass())) {
throw new ConversionException
("Cannot assign value of type '" +
value.getClass().getName() +
"' to property '" + name + "' of type '" +
descriptor.getType().getName() + "'");
}
values.put(name, value);
}
/**
* Set the value of an indexed property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param index Index of the property to be set
* @param value Value to which this property is to be set
*
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception IllegalArgumentException if the specified property
* exists, but is not indexed
* @exception IndexOutOfBoundsException if the specified index
* is outside the range of the underlying property
*/
public void set(String name, int index, Object value) {
Object prop = values.get(name);
if (prop == null) {
throw new NullPointerException
("No indexed value for '" + name + "[" + index + "]'");
} else if (prop.getClass().isArray()) {
Array.set(prop, index, value);
} else if (prop instanceof List) {
try {
@SuppressWarnings("unchecked")
// This is safe to cast because list properties are always
// of type Object
List list = (List) prop;
list.set(index, value);
} catch (ClassCastException e) {
throw new ConversionException(e.getMessage());
}
} else {
throw new IllegalArgumentException
("Non-indexed property for '" + name + "[" + index + "]'");
}
}
/**
* Set the value of a mapped property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param key Key of the property to be set
* @param value Value to which this property is to be set
*
* @exception ConversionException if the specified value cannot be
* converted to the type required for this property
* @exception IllegalArgumentException if there is no property
* of the specified name
* @exception IllegalArgumentException if the specified property
* exists, but is not mapped
*/
public void set(String name, String key, Object value) {
Object prop = values.get(name);
if (prop == null) {
throw new NullPointerException
("No mapped value for '" + name + "(" + key + ")'");
} else if (prop instanceof Map) {
@SuppressWarnings("unchecked")
// This is safe to cast because mapped properties are always
// maps of types String -> Object
Map map = (Map) prop;
map.put(key, value);
} else {
throw new IllegalArgumentException
("Non-mapped property for '" + name + "(" + key + ")'");
}
}
// ------------------------------------------------------ Protected Methods
/**
* Return the property descriptor for the specified property name.
*
* @param name Name of the property for which to retrieve the descriptor
* @return The property descriptor
*
* @exception IllegalArgumentException if this is not a valid property
* name for our DynaClass
*/
protected DynaProperty getDynaProperty(String name) {
DynaProperty descriptor = getDynaClass().getDynaProperty(name);
if (descriptor == null) {
throw new IllegalArgumentException
("Invalid property name '" + name + "'");
}
return (descriptor);
}
/**
* Is an object of the source class assignable to the destination class?
*
* @param dest Destination class
* @param source Source class
* @return true if the source class is assignable to the
* destination class, otherwise false
*/
protected boolean isAssignable(Class> dest, Class> source) {
if (dest.isAssignableFrom(source) ||
((dest == Boolean.TYPE) && (source == Boolean.class)) ||
((dest == Byte.TYPE) && (source == Byte.class)) ||
((dest == Character.TYPE) && (source == Character.class)) ||
((dest == Double.TYPE) && (source == Double.class)) ||
((dest == Float.TYPE) && (source == Float.class)) ||
((dest == Integer.TYPE) && (source == Integer.class)) ||
((dest == Long.TYPE) && (source == Long.class)) ||
((dest == Short.TYPE) && (source == Short.class))) {
return (true);
} else {
return (false);
}
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BasicDynaClass.java 100644 0 0 22775 12262570612 26730 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
/**
*
Minimal implementation of the DynaClass interface. Can be
* used as a convenience base class for more sophisticated implementations.
*
*
IMPLEMENTATION NOTE - The DynaBean
* implementation class supplied to our constructor MUST have a one-argument
* constructor of its own that accepts a DynaClass. This is
* used to associate the DynaBean instance with this DynaClass.
*
* @version $Id: BasicDynaClass.java 1540186 2013-11-08 21:08:30Z oheger $
*/
public class BasicDynaClass implements DynaClass, Serializable {
// ----------------------------------------------------------- Constructors
/**
* Construct a new BasicDynaClass with default parameters.
*/
public BasicDynaClass() {
this(null, null, null);
}
/**
* Construct a new BasicDynaClass with the specified parameters.
*
* @param name Name of this DynaBean class
* @param dynaBeanClass The implementation class for new instances
*/
public BasicDynaClass(String name, Class> dynaBeanClass) {
this(name, dynaBeanClass, null);
}
/**
* Construct a new BasicDynaClass with the specified parameters.
*
* @param name Name of this DynaBean class
* @param dynaBeanClass The implementation class for new intances
* @param properties Property descriptors for the supported properties
*/
public BasicDynaClass(String name, Class> dynaBeanClass,
DynaProperty[] properties) {
super();
if (name != null) {
this.name = name;
}
if (dynaBeanClass == null) {
dynaBeanClass = BasicDynaBean.class;
}
setDynaBeanClass(dynaBeanClass);
if (properties != null) {
setProperties(properties);
}
}
// ----------------------------------------------------- Instance Variables
/**
* The constructor of the dynaBeanClass that we will use
* for creating new instances.
*/
protected transient Constructor> constructor = null;
/**
* The method signature of the constructor we will use to create
* new DynaBean instances.
*/
protected static Class>[] constructorTypes = { DynaClass.class };
/**
* The argument values to be passed to the constructore we will use
* to create new DynaBean instances.
*/
protected Object[] constructorValues = { this };
/**
* The DynaBean implementation class we will use for
* creating new instances.
*/
protected Class> dynaBeanClass = BasicDynaBean.class;
/**
* The "name" of this DynaBean class.
*/
protected String name = this.getClass().getName();
/**
* The set of dynamic properties that are part of this DynaClass.
*/
protected DynaProperty[] properties = new DynaProperty[0];
/**
* The set of dynamic properties that are part of this DynaClass,
* keyed by the property name. Individual descriptor instances will
* be the same instances as those in the properties list.
*/
protected HashMap propertiesMap = new HashMap();
// ------------------------------------------------------ DynaClass Methods
/**
* Return the name of this DynaClass (analogous to the
* getName() method of java.lang.ClassDynaClass implementation class to support
* different dynamic classes, with different sets of properties.
*
* @return the name of the DynaClass
*/
public String getName() {
return (this.name);
}
/**
* Return a property descriptor for the specified property, if it exists;
* otherwise, return null.
*
* @param name Name of the dynamic property for which a descriptor
* is requested
* @return The descriptor for the specified property
*
* @exception IllegalArgumentException if no property name is specified
*/
public DynaProperty getDynaProperty(String name) {
if (name == null) {
throw new IllegalArgumentException
("No property name specified");
}
return propertiesMap.get(name);
}
/**
*
Return an array of ProperyDescriptors for the properties
* currently defined in this DynaClass. If no properties are defined, a
* zero-length array will be returned.
*
*
FIXME - Should we really be implementing
* getBeanInfo() instead, which returns property descriptors
* and a bunch of other stuff?
*
* @return the set of properties for this DynaClass
*/
public DynaProperty[] getDynaProperties() {
return (properties);
}
/**
* Instantiate and return a new DynaBean instance, associated
* with this DynaClass.
*
* @return A new DynaBean instance
* @exception IllegalAccessException if the Class or the appropriate
* constructor is not accessible
* @exception InstantiationException if this Class represents an abstract
* class, an array class, a primitive type, or void; or if instantiation
* fails for some other reason
*/
public DynaBean newInstance()
throws IllegalAccessException, InstantiationException {
try {
// Refind the constructor after a deserialization (if needed)
if (constructor == null) {
setDynaBeanClass(this.dynaBeanClass);
}
// Invoke the constructor to create a new bean instance
return ((DynaBean) constructor.newInstance(constructorValues));
} catch (InvocationTargetException e) {
throw new InstantiationException
(e.getTargetException().getMessage());
}
}
// --------------------------------------------------------- Public Methods
/**
* Return the Class object we will use to create new instances in the
* newInstance() method. This Class MUST
* implement the DynaBean interface.
*
* @return The class of the {@link DynaBean}
*/
public Class> getDynaBeanClass() {
return (this.dynaBeanClass);
}
// ------------------------------------------------------ Protected Methods
/**
* Set the Class object we will use to create new instances in the
* newInstance() method. This Class MUST
* implement the DynaBean interface.
*
* @param dynaBeanClass The new Class object
*
* @exception IllegalArgumentException if the specified Class does not
* implement the DynaBean interface
*/
protected void setDynaBeanClass(Class> dynaBeanClass) {
// Validate the argument type specified
if (dynaBeanClass.isInterface()) {
throw new IllegalArgumentException
("Class " + dynaBeanClass.getName() +
" is an interface, not a class");
}
if (!DynaBean.class.isAssignableFrom(dynaBeanClass)) {
throw new IllegalArgumentException
("Class " + dynaBeanClass.getName() +
" does not implement DynaBean");
}
// Identify the Constructor we will use in newInstance()
try {
this.constructor = dynaBeanClass.getConstructor(constructorTypes);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException
("Class " + dynaBeanClass.getName() +
" does not have an appropriate constructor");
}
this.dynaBeanClass = dynaBeanClass;
}
/**
* Set the list of dynamic properties supported by this DynaClass.
*
* @param properties List of dynamic properties to be supported
*/
protected void setProperties(DynaProperty[] properties) {
this.properties = properties;
propertiesMap.clear();
for (int i = 0; i < properties.length; i++) {
propertiesMap.put(properties[i].getName(), properties[i]);
}
}
}
././@LongLink 100644 0 0 150 12262571655 10262 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanAccessLanguageException.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanAccessLanguageException.j100644 0 0 3447 12262570611 30701 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
/**
* Thrown to indicate that the Bean Access Language cannot execute query
* against given bean. This is a runtime exception and access langauges are encouraged
* to subclass to create custom exceptions whenever appropriate.
*
* @since 1.7
* @version $Id: BeanAccessLanguageException.java 1454597 2013-03-08 21:58:12Z britter $
*/
public class BeanAccessLanguageException extends IllegalArgumentException {
// --------------------------------------------------------- Constuctors
/**
* Constructs a BeanAccessLanguageException without a detail message.
*/
public BeanAccessLanguageException() {
super();
}
/**
* Constructs a BeanAccessLanguageException without a detail message.
*
* @param message the detail message explaining this exception
*/
public BeanAccessLanguageException(String message) {
super(message);
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanComparator.java 100644 0 0 20676 12262570612 27000 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Comparator;
import org.apache.commons.collections.comparators.ComparableComparator;
/**
*
* This comparator compares two beans by the specified bean property.
* It is also possible to compare beans based on nested, indexed,
* combined, mapped bean properties. Please see the {@link PropertyUtilsBean}
* documentation for all property name possibilities.
*
*
* Note: The BeanComparator passes the values of the specified
* bean property to a ComparableComparator, if no comparator is
* specified in the constructor. If you are comparing two beans based
* on a property that could contain "null" values, a suitable Comparator
* or ComparatorChain should be supplied in the constructor.
* Note that the passed in {@code Comparator} must be able to handle the
* passed in objects. Because the type of the property to be compared is not
* known at compile time no type checks can be performed by the compiler.
* Thus {@code ClassCastException} exceptions can be thrown if unexpected
* property values occur.
*
*
* @param the type of beans to be compared by this {@code Comparator}
* @version $Id: BeanComparator.java 1540510 2013-11-10 18:39:37Z oheger $
*/
public class BeanComparator implements Comparator, Serializable {
private String property;
private final Comparator> comparator;
/**
*
Constructs a Bean Comparator without a property set.
*
* Note that this is intended to be used
* only in bean-centric environments.
*
* Until {@link #setProperty} is called with a non-null value.
* this comparator will compare the Objects only.
*
*/
public BeanComparator() {
this( null );
}
/**
*
Constructs a property-based comparator for beans.
* This compares two beans by the property
* specified in the property parameter. This constructor creates
* a BeanComparator that uses a ComparableComparator
* to compare the property values.
*
*
*
Passing "null" to this constructor will cause the BeanComparator
* to compare objects based on natural order, that is
* java.lang.Comparable.
*
*
* @param property String Name of a bean property, which may contain the
* name of a simple, nested, indexed, mapped, or combined
* property. See {@link PropertyUtilsBean} for property query language syntax.
* If the property passed in is null then the actual objects will be compared
*/
public BeanComparator( String property ) {
this( property, ComparableComparator.getInstance() );
}
/**
* Constructs a property-based comparator for beans.
* This constructor creates
* a BeanComparator that uses the supplied Comparator to compare
* the property values.
*
* @param property Name of a bean property, can contain the name
* of a simple, nested, indexed, mapped, or combined
* property. See {@link PropertyUtilsBean} for property query language
* syntax.
* @param comparator BeanComparator will pass the values of the
* specified bean property to this Comparator.
* If your bean property is not a comparable or
* contains null values, a suitable comparator
* may be supplied in this constructor.
*/
public BeanComparator( String property, Comparator> comparator ) {
setProperty( property );
if (comparator != null) {
this.comparator = comparator;
} else {
this.comparator = ComparableComparator.getInstance();
}
}
/**
* Sets the method to be called to compare two JavaBeans
*
* @param property String method name to call to compare
* If the property passed in is null then the actual objects will be compared
*/
public void setProperty( String property ) {
this.property = property;
}
/**
* Gets the property attribute of the BeanComparator
*
* @return String method name to call to compare.
* A null value indicates that the actual objects will be compared
*/
public String getProperty() {
return property;
}
/**
* Gets the Comparator being used to compare beans.
*
* @return the Comparator being used to compare beans
*/
public Comparator> getComparator() {
return comparator;
}
/**
* Compare two JavaBeans by their shared property.
* If {@link #getProperty} is null then the actual objects will be compared.
*
* @param o1 Object The first bean to get data from to compare against
* @param o2 Object The second bean to get data from to compare
* @return int negative or positive based on order
*/
public int compare( T o1, T o2 ) {
if ( property == null ) {
// compare the actual objects
return internalCompare( o1, o2 );
}
try {
Object value1 = PropertyUtils.getProperty( o1, property );
Object value2 = PropertyUtils.getProperty( o2, property );
return internalCompare( value1, value2 );
}
catch ( IllegalAccessException iae ) {
throw new RuntimeException( "IllegalAccessException: " + iae.toString() );
}
catch ( InvocationTargetException ite ) {
throw new RuntimeException( "InvocationTargetException: " + ite.toString() );
}
catch ( NoSuchMethodException nsme ) {
throw new RuntimeException( "NoSuchMethodException: " + nsme.toString() );
}
}
/**
* Two BeanComparator's are equals if and only if
* the wrapped comparators and the property names to be compared
* are equal.
* @param o Comparator to compare to
* @return whether the the comparators are equal or not
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof BeanComparator)) {
return false;
}
final BeanComparator> beanComparator = (BeanComparator>) o;
if (!comparator.equals(beanComparator.comparator)) {
return false;
}
if (property != null)
{
if (!property.equals(beanComparator.property)) {
return false;
}
}
else
{
return (beanComparator.property == null);
}
return true;
}
/**
* Hashcode compatible with equals.
* @return the hash code for this comparator
*/
@Override
public int hashCode() {
int result;
result = comparator.hashCode();
return result;
}
/**
* Compares the given values using the internal {@code Comparator}.
* Note: This comparison cannot be performed in a type-safe way; so
* {@code ClassCastException} exceptions may be thrown.
*
* @param val1 the first value to be compared
* @param val2 the second value to be compared
* @return the result of the comparison
*/
private int internalCompare(Object val1, Object val2) {
@SuppressWarnings("rawtypes")
// to make the compiler happy
Comparator c = comparator;
return c.compare(val1, val2);
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanIntrospectionData.java 100644 0 0 14127 12262570612 30315 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
*
* An internally used helper class for storing introspection information about a bean
* class.
*
*
* This class is used by {@link PropertyUtilsBean}. When accessing bean properties via
* reflection information about the properties available and their types and access
* methods must be present. {@code PropertyUtilsBean} stores this information in a cache
* so that it can be accessed quickly. The cache stores instances of this class.
*
*
* This class mainly stores information about the properties of a bean class. Per default,
* this is contained in {@code PropertyDescriptor} objects. Some additional information
* required by the {@code BeanUtils} library is also stored here.
*
*
* @version $Id: BeanIntrospectionData.java 1555233 2014-01-03 19:41:04Z oheger $
* @since 1.9.1
*/
class BeanIntrospectionData {
/** An array with property descriptors for the managed bean class. */
private final PropertyDescriptor[] descriptors;
/** A map for remembering the write method names for properties. */
private final Map writeMethodNames;
/**
* Creates a new instance of {@code BeanIntrospectionData} and initializes its
* completely.
*
* @param descs the array with the descriptors of the available properties
*/
public BeanIntrospectionData(PropertyDescriptor[] descs) {
this(descs, setUpWriteMethodNames(descs));
}
/**
* Creates a new instance of {@code BeanIntrospectionData} and allows setting the map
* with write method names. This constructor is mainly used for testing purposes.
*
* @param descs the array with the descriptors of the available properties
* @param writeMethNames the map with the names of write methods
*/
BeanIntrospectionData(PropertyDescriptor[] descs, Map writeMethNames) {
descriptors = descs;
writeMethodNames = writeMethNames;
}
/**
* Returns the array with property descriptors.
*
* @return the property descriptors for the associated bean class
*/
public PropertyDescriptor[] getDescriptors() {
return descriptors;
}
/**
* Returns the {@code PropertyDescriptor} for the property with the specified name. If
* this property is unknown, result is null.
*
* @param name the name of the property in question
* @return the {@code PropertyDescriptor} for this property or null
*/
public PropertyDescriptor getDescriptor(String name) {
for (PropertyDescriptor pd : getDescriptors()) {
if (name.equals(pd.getName())) {
return pd;
}
}
return null;
}
/**
* Returns the write method for the property determined by the given
* {@code PropertyDescriptor}. This information is normally available in the
* descriptor object itself. However, at least by the ORACLE implementation, the
* method is stored as a {@code SoftReference}. If this reference has been freed by
* the GC, it may be the case that the method cannot be obtained again. Then,
* additional information stored in this object is necessary to obtain the method
* again.
*
* @param beanCls the class of the affected bean
* @param desc the {@code PropertyDescriptor} of the desired property
* @return the write method for this property or null if there is none
*/
public Method getWriteMethod(Class> beanCls, PropertyDescriptor desc) {
Method method = desc.getWriteMethod();
if (method == null) {
String methodName = writeMethodNames.get(desc.getName());
if (methodName != null) {
method = MethodUtils.getAccessibleMethod(beanCls, methodName,
desc.getPropertyType());
if (method != null) {
try {
desc.setWriteMethod(method);
} catch (IntrospectionException e) {
// ignore, in this case the method is not cached
}
}
}
}
return method;
}
/**
* Initializes the map with the names of the write methods for the supported
* properties. The method names - if defined - need to be stored separately because
* they may get lost when the GC claims soft references used by the
* {@code PropertyDescriptor} objects.
*
* @param descs the array with the descriptors of the available properties
* @return the map with the names of write methods for properties
*/
private static Map setUpWriteMethodNames(PropertyDescriptor[] descs) {
Map methods = new HashMap();
for (PropertyDescriptor pd : descs) {
Method method = pd.getWriteMethod();
if (method != null) {
methods.put(pd.getName(), method.getName());
}
}
return methods;
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanIntrospector.java 100644 0 0 4530 12262570612 27333 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.beans.IntrospectionException;
/**
*
* Definition of an interface for components that can perform introspection on
* bean classes.
*
*
* Before {@link PropertyUtils} can be used for interaction with a specific Java
* class, the class's properties have to be determined. This is called
* introspection and is initiated automatically on demand.
* PropertyUtils does not perform introspection on its own, but
* delegates this task to one or more objects implementing this interface. This
* makes it possible to customize introspection which may be useful for certain
* code bases using non-standard conventions for accessing properties.
*
*
* @version $Id: BeanIntrospector.java 1540359 2013-11-09 18:10:52Z oheger $
* @since 1.9
*/
public interface BeanIntrospector {
/**
* Performs introspection on a Java class. The current class to be inspected
* can be queried from the passed in IntrospectionContext
* object. A typical implementation has to obtain this class, determine its
* properties according to the rules it implements, and add them to the
* passed in context object.
*
* @param icontext the context object for interaction with the initiator of
* the introspection request
* @throws IntrospectionException if an error occurs during introspection
*/
void introspect(IntrospectionContext icontext)
throws IntrospectionException;
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanMap.java 100644 0 0 100615 12262570612 25416 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.keyvalue.AbstractMapEntry;
/**
* An implementation of Map for JavaBeans which uses introspection to
* get and put properties in the bean.
*
* If an exception occurs during attempts to get or set a property then the
* property is considered non existent in the Map
*
* @version $Id: BeanMap.java 1540518 2013-11-10 19:04:04Z oheger $
*/
public class BeanMap extends AbstractMap implements Cloneable {
private transient Object bean;
private transient HashMap readMethods = new HashMap();
private transient HashMap writeMethods = new HashMap();
private transient HashMap> types = new HashMap>();
/**
* An empty array. Used to invoke accessors via reflection.
*/
public static final Object[] NULL_ARGUMENTS = {};
/**
* Maps primitive Class types to transformers. The transformer
* transform strings into the appropriate primitive wrapper.
*
* N.B. private & unmodifiable replacement for the (public & static) defaultTransformers instance.
*/
private static final Map, Transformer> typeTransformers =
Collections.unmodifiableMap(createTypeTransformers());
/**
* This HashMap has been made unmodifiable to prevent issues when
* loaded in a shared ClassLoader enviroment.
*
* @see "http://issues.apache.org/jira/browse/BEANUTILS-112"
* @deprecated Use {@link BeanMap#getTypeTransformer(Class)} method
*/
@Deprecated
public static HashMap defaultTransformers = new HashMap() {
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public boolean containsKey(Object key) {
return typeTransformers.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return typeTransformers.containsValue(value);
}
@Override
public Set entrySet() {
return typeTransformers.entrySet();
}
@Override
public Object get(Object key) {
return typeTransformers.get(key);
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public Set keySet() {
return typeTransformers.keySet();
}
@Override
public Object put(Object key, Object value) {
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map m) {
throw new UnsupportedOperationException();
}
@Override
public Object remove(Object key) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
return typeTransformers.size();
}
@Override
public Collection values() {
return typeTransformers.values();
}
};
private static Map, Transformer> createTypeTransformers() {
Map, Transformer> defaultTransformers =
new HashMap, Transformer>();
defaultTransformers.put(
Boolean.TYPE,
new Transformer() {
public Object transform( Object input ) {
return Boolean.valueOf( input.toString() );
}
}
);
defaultTransformers.put(
Character.TYPE,
new Transformer() {
public Object transform( Object input ) {
return new Character( input.toString().charAt( 0 ) );
}
}
);
defaultTransformers.put(
Byte.TYPE,
new Transformer() {
public Object transform( Object input ) {
return Byte.valueOf( input.toString() );
}
}
);
defaultTransformers.put(
Short.TYPE,
new Transformer() {
public Object transform( Object input ) {
return Short.valueOf( input.toString() );
}
}
);
defaultTransformers.put(
Integer.TYPE,
new Transformer() {
public Object transform( Object input ) {
return Integer.valueOf( input.toString() );
}
}
);
defaultTransformers.put(
Long.TYPE,
new Transformer() {
public Object transform( Object input ) {
return Long.valueOf( input.toString() );
}
}
);
defaultTransformers.put(
Float.TYPE,
new Transformer() {
public Object transform( Object input ) {
return Float.valueOf( input.toString() );
}
}
);
defaultTransformers.put(
Double.TYPE,
new Transformer() {
public Object transform( Object input ) {
return Double.valueOf( input.toString() );
}
}
);
return defaultTransformers;
}
// Constructors
//-------------------------------------------------------------------------
/**
* Constructs a new empty BeanMap.
*/
public BeanMap() {
}
/**
* Constructs a new BeanMap that operates on the
* specified bean. If the given bean is null, then
* this map will be empty.
*
* @param bean the bean for this map to operate on
*/
public BeanMap(Object bean) {
this.bean = bean;
initialise();
}
// Map interface
//-------------------------------------------------------------------------
/**
* Renders a string representation of this object.
* @return a String representation of this object
*/
@Override
public String toString() {
return "BeanMap<" + String.valueOf(bean) + ">";
}
/**
* Clone this bean map using the following process:
*
*
*
If there is no underlying bean, return a cloned BeanMap without a
* bean.
*
*
Since there is an underlying bean, try to instantiate a new bean of
* the same type using Class.newInstance().
*
*
If the instantiation fails, throw a CloneNotSupportedException
*
*
Clone the bean map and set the newly instantiated bean as the
* underlying bean for the bean map.
*
*
Copy each property that is both readable and writable from the
* existing object to a cloned bean map.
*
*
If anything fails along the way, throw a
* CloneNotSupportedException.
*
*
*
* @return a cloned instance of this bean map
* @throws CloneNotSupportedException if the underlying bean
* cannot be cloned
*/
@Override
public Object clone() throws CloneNotSupportedException {
BeanMap newMap = (BeanMap)super.clone();
if(bean == null) {
// no bean, just an empty bean map at the moment. return a newly
// cloned and empty bean map.
return newMap;
}
Object newBean = null;
Class extends Object> beanClass = bean.getClass(); // Cannot throw Exception
try {
newBean = beanClass.newInstance();
} catch (Exception e) {
// unable to instantiate
CloneNotSupportedException cnse = new CloneNotSupportedException
("Unable to instantiate the underlying bean \"" +
beanClass.getName() + "\": " + e);
BeanUtils.initCause(cnse, e);
throw cnse;
}
try {
newMap.setBean(newBean);
} catch (Exception exception) {
CloneNotSupportedException cnse = new CloneNotSupportedException
("Unable to set bean in the cloned bean map: " +
exception);
BeanUtils.initCause(cnse, exception);
throw cnse;
}
try {
// copy only properties that are readable and writable. If its
// not readable, we can't get the value from the old map. If
// its not writable, we can't write a value into the new map.
Iterator> readableKeys = readMethods.keySet().iterator();
while(readableKeys.hasNext()) {
Object key = readableKeys.next();
if(getWriteMethod(key) != null) {
newMap.put(key, get(key));
}
}
} catch (Exception exception) {
CloneNotSupportedException cnse = new CloneNotSupportedException
("Unable to copy bean values to cloned bean map: " +
exception);
BeanUtils.initCause(cnse, exception);
throw cnse;
}
return newMap;
}
/**
* Puts all of the writable properties from the given BeanMap into this
* BeanMap. Read-only and Write-only properties will be ignored.
*
* @param map the BeanMap whose properties to put
*/
public void putAllWriteable(BeanMap map) {
Iterator> readableKeys = map.readMethods.keySet().iterator();
while (readableKeys.hasNext()) {
Object key = readableKeys.next();
if (getWriteMethod(key) != null) {
this.put(key, map.get(key));
}
}
}
/**
* This method reinitializes the bean map to have default values for the
* bean's properties. This is accomplished by constructing a new instance
* of the bean which the map uses as its underlying data source. This
* behavior for clear() differs from the Map contract in that
* the mappings are not actually removed from the map (the mappings for a
* BeanMap are fixed).
*/
@Override
public void clear() {
if(bean == null) {
return;
}
Class extends Object> beanClass = null;
try {
beanClass = bean.getClass();
bean = beanClass.newInstance();
}
catch (Exception e) {
UnsupportedOperationException uoe =
new UnsupportedOperationException("Could not create new instance of class: " + beanClass);
BeanUtils.initCause(uoe, e);
throw uoe;
}
}
/**
* Returns true if the bean defines a property with the given name.
*
* The given name must be a String; if not, this method
* returns false. This method will also return false if the bean
* does not define a property with that name.
*
* Write-only properties will not be matched as the test operates against
* property read methods.
*
* @param name the name of the property to check
* @return false if the given name is null or is not a String;
* false if the bean does not define a property with that name; or
* true if the bean does define a property with that name
*/
@Override
public boolean containsKey(Object name) {
Method method = getReadMethod(name);
return method != null;
}
/**
* Returns true if the bean defines a property whose current value is
* the given object.
*
* @param value the value to check
* @return false true if the bean has at least one property whose
* current value is that object, false otherwise
*/
@Override
public boolean containsValue(Object value) {
// use default implementation
return super.containsValue(value);
}
/**
* Returns the value of the bean's property with the given name.
*
* The given name must be a {@link String} and must not be
* null; otherwise, this method returns null.
* If the bean defines a property with the given name, the value of
* that property is returned. Otherwise, null is
* returned.
*
* Write-only properties will not be matched as the test operates against
* property read methods.
*
* @param name the name of the property whose value to return
* @return the value of the property with that name
*/
@Override
public Object get(Object name) {
if ( bean != null ) {
Method method = getReadMethod( name );
if ( method != null ) {
try {
return method.invoke( bean, NULL_ARGUMENTS );
}
catch ( IllegalAccessException e ) {
logWarn( e );
}
catch ( IllegalArgumentException e ) {
logWarn( e );
}
catch ( InvocationTargetException e ) {
logWarn( e );
}
catch ( NullPointerException e ) {
logWarn( e );
}
}
}
return null;
}
/**
* Sets the bean property with the given name to the given value.
*
* @param name the name of the property to set
* @param value the value to set that property to
* @return the previous value of that property
* @throws IllegalArgumentException if the given name is null;
* if the given name is not a {@link String}; if the bean doesn't
* define a property with that name; or if the bean property with
* that name is read-only
* @throws ClassCastException if an error occurs creating the method args
*/
@Override
public Object put(Object name, Object value) throws IllegalArgumentException, ClassCastException {
if ( bean != null ) {
Object oldValue = get( name );
Method method = getWriteMethod( name );
if ( method == null ) {
throw new IllegalArgumentException( "The bean of type: "+
bean.getClass().getName() + " has no property called: " + name );
}
try {
Object[] arguments = createWriteMethodArguments( method, value );
method.invoke( bean, arguments );
Object newValue = get( name );
firePropertyChange( name, oldValue, newValue );
}
catch ( InvocationTargetException e ) {
IllegalArgumentException iae = new IllegalArgumentException(e.getMessage());
if (BeanUtils.initCause(iae, e) == false) {
logInfo(e);
}
throw iae;
}
catch ( IllegalAccessException e ) {
IllegalArgumentException iae = new IllegalArgumentException(e.getMessage());
if (BeanUtils.initCause(iae, e) == false) {
logInfo(e);
}
throw iae;
}
return oldValue;
}
return null;
}
/**
* Returns the number of properties defined by the bean.
*
* @return the number of properties defined by the bean
*/
@Override
public int size() {
return readMethods.size();
}
/**
* Get the keys for this BeanMap.
*
* Write-only properties are not included in the returned set of
* property names, although it is possible to set their value and to get
* their type.
*
* @return BeanMap keys. The Set returned by this method is not
* modifiable.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
// The set actually contains strings; however, because it cannot be
// modified there is no danger in selling it as Set
@Override
public Set keySet() {
return Collections.unmodifiableSet((Set) readMethods.keySet());
}
/**
* Gets a Set of MapEntry objects that are the mappings for this BeanMap.
*
* Each MapEntry can be set but not removed.
*
* @return the unmodifiable set of mappings
*/
@Override
public Set> entrySet() {
return Collections.unmodifiableSet(new AbstractSet>() {
@Override
public Iterator> iterator() {
return entryIterator();
}
@Override
public int size() {
return BeanMap.this.readMethods.size();
}
});
}
/**
* Returns the values for the BeanMap.
*
* @return values for the BeanMap. The returned collection is not
* modifiable.
*/
@Override
public Collection values() {
ArrayList answer = new ArrayList( readMethods.size() );
for ( Iterator iter = valueIterator(); iter.hasNext(); ) {
answer.add( iter.next() );
}
return Collections.unmodifiableList(answer);
}
// Helper methods
//-------------------------------------------------------------------------
/**
* Returns the type of the property with the given name.
*
* @param name the name of the property
* @return the type of the property, or null if no such
* property exists
*/
public Class> getType(String name) {
return types.get( name );
}
/**
* Convenience method for getting an iterator over the keys.
*
* Write-only properties will not be returned in the iterator.
*
* @return an iterator over the keys
*/
public Iterator keyIterator() {
return readMethods.keySet().iterator();
}
/**
* Convenience method for getting an iterator over the values.
*
* @return an iterator over the values
*/
public Iterator valueIterator() {
final Iterator> iter = keyIterator();
return new Iterator() {
public boolean hasNext() {
return iter.hasNext();
}
public Object next() {
Object key = iter.next();
return get(key);
}
public void remove() {
throw new UnsupportedOperationException( "remove() not supported for BeanMap" );
}
};
}
/**
* Convenience method for getting an iterator over the entries.
*
* @return an iterator over the entries
*/
public Iterator> entryIterator() {
final Iterator iter = keyIterator();
return new Iterator>() {
public boolean hasNext() {
return iter.hasNext();
}
public Map.Entry next() {
Object key = iter.next();
Object value = get(key);
@SuppressWarnings("unchecked")
// This should not cause any problems; the key is actually a
// string, but it does no harm to expose it as Object
Map.Entry tmpEntry = new Entry( BeanMap.this, key, value );
return tmpEntry;
}
public void remove() {
throw new UnsupportedOperationException( "remove() not supported for BeanMap" );
}
};
}
// Properties
//-------------------------------------------------------------------------
/**
* Returns the bean currently being operated on. The return value may
* be null if this map is empty.
*
* @return the bean being operated on by this map
*/
public Object getBean() {
return bean;
}
/**
* Sets the bean to be operated on by this map. The given value may
* be null, in which case this map will be empty.
*
* @param newBean the new bean to operate on
*/
public void setBean( Object newBean ) {
bean = newBean;
reinitialise();
}
/**
* Returns the accessor for the property with the given name.
*
* @param name the name of the property
* @return the accessor method for the property, or null
*/
public Method getReadMethod(String name) {
return readMethods.get(name);
}
/**
* Returns the mutator for the property with the given name.
*
* @param name the name of the property
* @return the mutator method for the property, or null
*/
public Method getWriteMethod(String name) {
return writeMethods.get(name);
}
// Implementation methods
//-------------------------------------------------------------------------
/**
* Returns the accessor for the property with the given name.
*
* @param name the name of the property
* @return null if the name is null; null if the name is not a
* {@link String}; null if no such property exists; or the accessor
* method for that property
*/
protected Method getReadMethod( Object name ) {
return readMethods.get( name );
}
/**
* Returns the mutator for the property with the given name.
*
* @param name the name of the
* @return null if the name is null; null if the name is not a
* {@link String}; null if no such property exists; null if the
* property is read-only; or the mutator method for that property
*/
protected Method getWriteMethod( Object name ) {
return writeMethods.get( name );
}
/**
* Reinitializes this bean. Called during {@link #setBean(Object)}.
* Does introspection to find properties.
*/
protected void reinitialise() {
readMethods.clear();
writeMethods.clear();
types.clear();
initialise();
}
private void initialise() {
if(getBean() == null) {
return;
}
Class extends Object> beanClass = getBean().getClass();
try {
//BeanInfo beanInfo = Introspector.getBeanInfo( bean, null );
BeanInfo beanInfo = Introspector.getBeanInfo( beanClass );
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if ( propertyDescriptors != null ) {
for ( int i = 0; i < propertyDescriptors.length; i++ ) {
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
if ( propertyDescriptor != null ) {
String name = propertyDescriptor.getName();
Method readMethod = propertyDescriptor.getReadMethod();
Method writeMethod = propertyDescriptor.getWriteMethod();
Class extends Object> aType = propertyDescriptor.getPropertyType();
if ( readMethod != null ) {
readMethods.put( name, readMethod );
}
if ( writeMethod != null ) {
writeMethods.put( name, writeMethod );
}
types.put( name, aType );
}
}
}
}
catch ( IntrospectionException e ) {
logWarn( e );
}
}
/**
* Called during a successful {@link #put(Object,Object)} operation.
* Default implementation does nothing. Override to be notified of
* property changes in the bean caused by this map.
*
* @param key the name of the property that changed
* @param oldValue the old value for that property
* @param newValue the new value for that property
*/
protected void firePropertyChange( Object key, Object oldValue, Object newValue ) {
}
// Implementation classes
//-------------------------------------------------------------------------
/**
* Map entry used by {@link BeanMap}.
*/
protected static class Entry extends AbstractMapEntry {
private final BeanMap owner;
/**
* Constructs a new Entry.
*
* @param owner the BeanMap this entry belongs to
* @param key the key for this entry
* @param value the value for this entry
*/
protected Entry( BeanMap owner, Object key, Object value ) {
super( key, value );
this.owner = owner;
}
/**
* Sets the value.
*
* @param value the new value for the entry
* @return the old value for the entry
*/
@Override
public Object setValue(Object value) {
Object key = getKey();
Object oldValue = owner.get( key );
owner.put( key, value );
Object newValue = owner.get( key );
super.setValue( newValue );
return oldValue;
}
}
/**
* Creates an array of parameters to pass to the given mutator method.
* If the given object is not the right type to pass to the method
* directly, it will be converted using {@link #convertType(Class,Object)}.
*
* @param method the mutator method
* @param value the value to pass to the mutator method
* @return an array containing one object that is either the given value
* or a transformed value
* @throws IllegalAccessException if {@link #convertType(Class,Object)}
* raises it
* @throws IllegalArgumentException if any other exception is raised
* by {@link #convertType(Class,Object)}
* @throws ClassCastException if an error occurs creating the method args
*/
protected Object[] createWriteMethodArguments( Method method, Object value )
throws IllegalAccessException, ClassCastException {
try {
if ( value != null ) {
Class extends Object>[] types = method.getParameterTypes();
if ( types != null && types.length > 0 ) {
Class extends Object> paramType = types[0];
if ( ! paramType.isAssignableFrom( value.getClass() ) ) {
value = convertType( paramType, value );
}
}
}
Object[] answer = { value };
return answer;
}
catch ( InvocationTargetException e ) {
IllegalArgumentException iae = new IllegalArgumentException(e.getMessage());
if (BeanUtils.initCause(iae, e) == false) {
logInfo(e);
}
throw iae;
}
catch ( InstantiationException e ) {
IllegalArgumentException iae = new IllegalArgumentException(e.getMessage());
if (BeanUtils.initCause(iae, e) == false) {
logInfo(e);
}
BeanUtils.initCause(iae, e);
throw iae;
}
}
/**
* Converts the given value to the given type. First, reflection is
* is used to find a public constructor declared by the given class
* that takes one argument, which must be the precise type of the
* given value. If such a constructor is found, a new object is
* created by passing the given value to that constructor, and the
* newly constructed object is returned.
*
* If no such constructor exists, and the given type is a primitive
* type, then the given value is converted to a string using its
* {@link Object#toString() toString()} method, and that string is
* parsed into the correct primitive type using, for instance,
* {@link Integer#valueOf(String)} to convert the string into an
* int.
*
* If no special constructor exists and the given type is not a
* primitive type, this method returns the original value.
*
* @param newType the type to convert the value to
* @param value the value to convert
* @return the converted value
* @throws NumberFormatException if newType is a primitive type, and
* the string representation of the given value cannot be converted
* to that type
* @throws InstantiationException if the constructor found with
* reflection raises it
* @throws InvocationTargetException if the constructor found with
* reflection raises it
* @throws IllegalAccessException never
* @throws IllegalArgumentException never
*/
protected Object convertType( Class> newType, Object value )
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// try call constructor
Class>[] types = { value.getClass() };
try {
Constructor> constructor = newType.getConstructor( types );
Object[] arguments = { value };
return constructor.newInstance( arguments );
}
catch ( NoSuchMethodException e ) {
// try using the transformers
Transformer transformer = getTypeTransformer( newType );
if ( transformer != null ) {
return transformer.transform( value );
}
return value;
}
}
/**
* Returns a transformer for the given primitive type.
*
* @param aType the primitive type whose transformer to return
* @return a transformer that will convert strings into that type,
* or null if the given type is not a primitive type
*/
protected Transformer getTypeTransformer( Class> aType ) {
return typeTransformers.get( aType );
}
/**
* Logs the given exception to System.out. Used to display
* warnings while accessing/mutating the bean.
*
* @param ex the exception to log
*/
protected void logInfo(Exception ex) {
// Deliberately do not use LOG4J or Commons Logging to avoid dependencies
System.out.println( "INFO: Exception: " + ex );
}
/**
* Logs the given exception to System.err. Used to display
* errors while accessing/mutating the bean.
*
* @param ex the exception to log
*/
protected void logWarn(Exception ex) {
// Deliberately do not use LOG4J or Commons Logging to avoid dependencies
System.out.println( "WARN: Exception: " + ex );
ex.printStackTrace();
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanPredicate.java 100644 0 0 11130 12262570612 26552 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.reflect.InvocationTargetException;
/**
*
Predicate implementation that applies the given Predicate
* to the result of calling the given property getter.
*
*
* @version $Id: BeanPredicate.java 1454597 2013-03-08 21:58:12Z britter $
*/
public class BeanPredicate implements Predicate {
private final Log log = LogFactory.getLog(this.getClass());
/** Name of the property whose value will be predicated */
private String propertyName;
/** Predicate to be applied to the property value */
private Predicate predicate;
/**
* Constructs a BeanPredicate that applies the given
* Predicate to the named property value.
* @param propertyName the name of the property whose value is to be predicated,
* not null
* @param predicate the Predicate to be applied,
* not null
*/
public BeanPredicate(String propertyName, Predicate predicate) {
this.propertyName = propertyName;
this.predicate = predicate;
}
/**
* Evaluates the given object by applying the {@link #getPredicate()}
* to a property value named by {@link #getPropertyName()}.
*
* @param object The object being evaluated
* @return the result of the predicate evaluation
* @throws IllegalArgumentException when the property cannot be evaluated
*/
public boolean evaluate(Object object) {
boolean evaluation = false;
try {
Object propValue = PropertyUtils.getProperty( object, propertyName );
evaluation = predicate.evaluate(propValue);
} catch (IllegalArgumentException e) {
final String errorMsg = "Problem during evaluation.";
log.error("ERROR: " + errorMsg, e);
throw e;
} catch (IllegalAccessException e) {
final String errorMsg = "Unable to access the property provided.";
log.error(errorMsg, e);
throw new IllegalArgumentException(errorMsg);
} catch (InvocationTargetException e) {
final String errorMsg = "Exception occurred in property's getter";
log.error(errorMsg, e);
throw new IllegalArgumentException(errorMsg);
} catch (NoSuchMethodException e) {
final String errorMsg = "Property not found.";
log.error(errorMsg, e);
throw new IllegalArgumentException(errorMsg);
}
return evaluation;
}
/**
* Gets the name of the property whose value is to be predicated.
* in the evaluation.
* @return the property name, not null
*/
public String getPropertyName() {
return propertyName;
}
/**
* Sets the name of the property whose value is to be predicated.
* @param propertyName the name of the property whose value is to be predicated,
* not null
*/
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
/**
* Gets the Predicate to be applied to the value of the named property
* during {@link #evaluate}.
* @return Predicate, not null
*/
public Predicate getPredicate() {
return predicate;
}
/**
* Sets the Predicate to be applied to the value of the named property
* during {@link #evaluate(Object)}.
* @param predicate Predicate, not null
*/
public void setPredicate(Predicate predicate) {
this.predicate = predicate;
}
}
././@LongLink 100644 0 0 153 12262571655 10265 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanPropertyValueChangeClosure.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanPropertyValueChangeClosur100644 0 0 25262 12262570612 31064 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import org.apache.commons.collections.Closure;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.reflect.InvocationTargetException;
/**
*
Closure that sets a property.
*
* An implementation of org.apache.commons.collections.Closure that updates
* a specified property on the object provided with a specified value.
* The BeanPropertyValueChangeClosure constructor takes two parameters which determine
* what property will be updated and with what value.
*
*
*
*
public BeanPropertyValueChangeClosure( String propertyName, Object propertyValue )
*
*
*
* Will create a Closure that will update an object by setting the property
* specified by propertyName to the value specified by propertyValue.
*
*
*
*
* Note: Property names can be a simple, nested, indexed, or mapped property as defined by
* org.apache.commons.beanutils.PropertyUtils. If any object in the property path
* specified by propertyName is null then the outcome is based on the
* value of the ignoreNull attribute.
*
*
* A typical usage might look like:
*
* // create the closure
* BeanPropertyValueChangeClosure closure =
* new BeanPropertyValueChangeClosure( "activeEmployee", Boolean.TRUE );
*
* // update the Collection
* CollectionUtils.forAllDo( peopleCollection, closure );
*
*
*
* This would take a Collection of person objects and update the
* activeEmployee property of each object in the Collection to
* true. Assuming...
*
*
* The top level object in the peopleCollection is an object which represents a
* person.
*
*
* The person object has a setActiveEmployee( boolean ) method which updates
* the value for the object's activeEmployee property.
*
*
*
* @version $Id: BeanPropertyValueChangeClosure.java 1454597 2013-03-08 21:58:12Z britter $
* @see org.apache.commons.beanutils.PropertyUtils
* @see org.apache.commons.collections.Closure
*/
public class BeanPropertyValueChangeClosure implements Closure {
/** For logging. */
private final Log log = LogFactory.getLog(this.getClass());
/**
* The name of the property which will be updated when this Closure executes.
*/
private String propertyName;
/**
* The value that the property specified by propertyName
* will be updated to when this Closure executes.
*/
private Object propertyValue;
/**
* Determines whether null objects in the property path will genenerate an
* IllegalArgumentException or not. If set to true then if any objects
* in the property path leading up to the target property evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged but
* not rethrown. If set to false then if any objects in the property path leading
* up to the target property evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged and
* rethrown.
*/
private boolean ignoreNull;
/**
* Constructor which takes the name of the property to be changed, the new value to set
* the property to, and assumes ignoreNull to be false.
*
* @param propertyName The name of the property that will be updated with the value specified by
* propertyValue.
* @param propertyValue The value that propertyName will be set to on the target
* object.
* @throws IllegalArgumentException If the propertyName provided is null or empty.
*/
public BeanPropertyValueChangeClosure(String propertyName, Object propertyValue) {
this(propertyName, propertyValue, false);
}
/**
* Constructor which takes the name of the property to be changed, the new value to set
* the property to and a boolean which determines whether null objects in the
* property path will genenerate an IllegalArgumentException or not.
*
* @param propertyName The name of the property that will be updated with the value specified by
* propertyValue.
* @param propertyValue The value that propertyName will be set to on the target
* object.
* @param ignoreNull Determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not.
* @throws IllegalArgumentException If the propertyName provided is null or empty.
*/
public BeanPropertyValueChangeClosure(String propertyName, Object propertyValue, boolean ignoreNull) {
super();
if ((propertyName != null) && (propertyName.length() > 0)) {
this.propertyName = propertyName;
this.propertyValue = propertyValue;
this.ignoreNull = ignoreNull;
} else {
throw new IllegalArgumentException("propertyName cannot be null or empty");
}
}
/**
* Updates the target object provided using the property update criteria provided when this
* BeanPropertyValueChangeClosure was constructed. If any object in the property
* path leading up to the target property is null then the outcome will be based on
* the value of the ignoreNull attribute. By default, ignoreNull is
* false and would result in an IllegalArgumentException if an object
* in the property path leading up to the target property is null.
*
* @param object The object to be updated.
* @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or
* NoSuchMethodException is thrown when trying to access the property specified on the object
* provided. Or if an object in the property path provided is null and
* ignoreNull is set to false.
*/
public void execute(Object object) {
try {
PropertyUtils.setProperty(object, propertyName, propertyValue);
} catch (IllegalArgumentException e) {
final String errorMsg = "Unable to execute Closure. Null value encountered in property path...";
if (ignoreNull) {
log.warn("WARNING: " + errorMsg + e);
} else {
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
}
} catch (IllegalAccessException e) {
final String errorMsg = "Unable to access the property provided.";
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
} catch (InvocationTargetException e) {
final String errorMsg = "Exception occurred in property's getter";
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
} catch (NoSuchMethodException e) {
final String errorMsg = "Property not found";
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
}
}
/**
* Returns the name of the property which will be updated when this Closure executes.
*
* @return The name of the property which will be updated when this Closure executes.
*/
public String getPropertyName() {
return propertyName;
}
/**
* Returns the value that the property specified by propertyName
* will be updated to when this Closure executes.
*
* @return The value that the property specified by propertyName
* will be updated to when this Closure executes.
*/
public Object getPropertyValue() {
return propertyValue;
}
/**
* Returns the flag that determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not. If set to true then
* if any objects in the property path leading up to the target property evaluate to
* null then the IllegalArgumentException throw by
* PropertyUtils will be logged but not rethrown. If set to false then
* if any objects in the property path leading up to the target property evaluate to
* null then the IllegalArgumentException throw by
* PropertyUtils will be logged and rethrown.
*
* @return The flag that determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not.
*/
public boolean isIgnoreNull() {
return ignoreNull;
}
}
././@LongLink 100644 0 0 155 12262571655 10267 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanPropertyValueEqualsPredicate.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanPropertyValueEqualsPredic100644 0 0 31061 12262570612 31062 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.collections.Predicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
Predicate that evaluates a property value against a specified value.
*
* An implementation of org.apache.commons.collections.Predicate that evaluates a
* property value on the object provided against a specified value and returns true
* if equal; false otherwise.
* The BeanPropertyValueEqualsPredicate constructor takes two parameters which
* determine what property will be evaluated on the target object and what its expected value should
* be.
*
*
*
*
public BeanPropertyValueEqualsPredicate( String propertyName, Object propertyValue )
*
*
*
* Will create a Predicate that will evaluate the target object and return
* true if the property specified by propertyName has a value which
* is equal to the the value specified by propertyValue. Or return
* false otherwise.
*
*
*
*
* Note: Property names can be a simple, nested, indexed, or mapped property as defined by
* org.apache.commons.beanutils.PropertyUtils. If any object in the property path
* specified by propertyName is null then the outcome is based on the
* value of the ignoreNull attribute.
*
*
* A typical usage might look like:
*
* // create the closure
* BeanPropertyValueEqualsPredicate predicate =
* new BeanPropertyValueEqualsPredicate( "activeEmployee", Boolean.FALSE );
*
* // filter the Collection
* CollectionUtils.filter( peopleCollection, predicate );
*
*
*
* This would take a Collection of person objects and filter out any people whose
* activeEmployee property is false. Assuming...
*
*
* The top level object in the peeopleCollection is an object which represents a
* person.
*
*
* The person object has a getActiveEmployee() method which returns
* the boolean value for the object's activeEmployee property.
*
*
*
*
* Another typical usage might look like:
*
* // create the closure
* BeanPropertyValueEqualsPredicate predicate =
* new BeanPropertyValueEqualsPredicate( "personId", "456-12-1234" );
*
* // search the Collection
* CollectionUtils.find( peopleCollection, predicate );
*
*
*
* This would search a Collection of person objects and return the first object whose
* personId property value equals 456-12-1234. Assuming...
*
*
* The top level object in the peeopleCollection is an object which represents a
* person.
*
*
* The person object has a getPersonId() method which returns
* the value for the object's personId property.
*
*
*
*
* @version $Id: BeanPropertyValueEqualsPredicate.java 1540509 2013-11-10 18:39:11Z oheger $
* @see org.apache.commons.beanutils.PropertyUtils
* @see org.apache.commons.collections.Predicate
*/
public class BeanPropertyValueEqualsPredicate implements Predicate {
/** For logging. */
private final Log log = LogFactory.getLog(this.getClass());
/**
* The name of the property which will be evaluated when this Predicate is executed.
*/
private String propertyName;
/**
* The value that the property specified by propertyName
* will be compared to when this Predicate executes.
*/
private Object propertyValue;
/**
*
Should null objects in the property path be ignored?
*
* Determines whether null objects in the property path will genenerate an
* IllegalArgumentException or not. If set to true then if any objects
* in the property path evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged but
* not rethrown and false will be returned. If set to false then if
* any objects in the property path evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged and
* rethrown.
*
*/
private boolean ignoreNull;
/**
* Constructor which takes the name of the property, its expected value to be used in evaluation,
* and assumes ignoreNull to be false.
*
* @param propertyName The name of the property that will be evaluated against the expected value.
* @param propertyValue The value to use in object evaluation.
* @throws IllegalArgumentException If the property name provided is null or empty.
*/
public BeanPropertyValueEqualsPredicate(String propertyName, Object propertyValue) {
this(propertyName, propertyValue, false);
}
/**
* Constructor which takes the name of the property, its expected value
* to be used in evaluation, and a boolean which determines whether null objects in
* the property path will genenerate an IllegalArgumentException or not.
*
* @param propertyName The name of the property that will be evaluated against the expected value.
* @param propertyValue The value to use in object evaluation.
* @param ignoreNull Determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not.
* @throws IllegalArgumentException If the property name provided is null or empty.
*/
public BeanPropertyValueEqualsPredicate(String propertyName, Object propertyValue, boolean ignoreNull) {
super();
if ((propertyName != null) && (propertyName.length() > 0)) {
this.propertyName = propertyName;
this.propertyValue = propertyValue;
this.ignoreNull = ignoreNull;
} else {
throw new IllegalArgumentException("propertyName cannot be null or empty");
}
}
/**
* Evaulates the object provided against the criteria specified when this
* BeanPropertyValueEqualsPredicate was constructed. Equality is based on
* either reference or logical equality as defined by the property object's equals method. If
* any object in the property path leading up to the target property is null then
* the outcome will be based on the value of the ignoreNull attribute. By default,
* ignoreNull is false and would result in an
* IllegalArgumentException if an object in the property path leading up to the
* target property is null.
*
* @param object The object to be evaluated.
* @return True if the object provided meets all the criteria for this Predicate;
* false otherwise.
* @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or
* NoSuchMethodException is thrown when trying to access the property specified on the object
* provided. Or if an object in the property path provided is null and
* ignoreNull is set to false.
*/
public boolean evaluate(Object object) {
boolean evaluation = false;
try {
evaluation = evaluateValue(propertyValue,
PropertyUtils.getProperty(object, propertyName));
} catch (IllegalArgumentException e) {
final String errorMsg = "Problem during evaluation. Null value encountered in property path...";
if (ignoreNull) {
log.warn("WARNING: " + errorMsg + e);
} else {
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
}
} catch (IllegalAccessException e) {
final String errorMsg = "Unable to access the property provided.";
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
} catch (InvocationTargetException e) {
final String errorMsg = "Exception occurred in property's getter";
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
} catch (NoSuchMethodException e) {
final String errorMsg = "Property not found.";
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
}
return evaluation;
}
/**
* Utility method which evaluates whether the actual property value equals the expected property
* value.
*
* @param expected The expected value.
* @param actual The actual value.
* @return True if they are equal; false otherwise.
*/
protected boolean evaluateValue(Object expected, Object actual) {
return (expected == actual) || ((expected != null) && expected.equals(actual));
}
/**
* Returns the name of the property which will be evaluated when this Predicate is
* executed.
*
* @return The name of the property which will be evaluated when this Predicate is
* executed.
*/
public String getPropertyName() {
return propertyName;
}
/**
* Returns the value that the property specified by propertyName will be compared to
* when this Predicate executes.
*
* @return The value that the property specified by propertyName will be compared to
* when this Predicate executes.
*/
public Object getPropertyValue() {
return propertyValue;
}
/**
* Returns the flag which determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not. If set to true then
* if any objects in the property path evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged but
* not rethrown and false will be returned. If set to false then if
* any objects in the property path evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged and
* rethrown.
*
* @return The flag which determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not.
*/
public boolean isIgnoreNull() {
return ignoreNull;
}
}
././@LongLink 100644 0 0 153 12262571655 10265 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanToPropertyValueTransformer.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanToPropertyValueTransforme100644 0 0 23447 12262570612 31135 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.reflect.InvocationTargetException;
/**
*
Transformer that outputs a property value.
*
*
An implementation of org.apache.commons.collections.Transformer that transforms
* the object provided by returning the value of a specified property of the object. The
* constructor for BeanToPropertyValueTransformer requires the name of the property
* that will be used in the transformation. The property can be a simple, nested, indexed, or
* mapped property as defined by org.apache.commons.beanutils.PropertyUtils. If any
* object in the property path specified by propertyName is null then the
* outcome is based on the value of the ignoreNull attribute.
*
*
*
* A typical usage might look like:
*
* // create the transformer
* BeanToPropertyValueTransformer transformer = new BeanToPropertyValueTransformer( "person.address.city" );
*
* // transform the Collection
* Collection peoplesCities = CollectionUtils.collect( peopleCollection, transformer );
*
*
*
*
* This would take a Collection of person objects and return a Collection
* of objects which represents the cities in which each person lived. Assuming...
*
*
* The top level object in the peeopleCollection is an object which represents a
* person.
*
*
* The person object has a getAddress() method which returns an object which
* represents a person's address.
*
*
* The address object has a getCity() method which returns an object which
* represents the city in which a person lives.
*
*
*
* @version $Id: BeanToPropertyValueTransformer.java 1454597 2013-03-08 21:58:12Z britter $
* @see org.apache.commons.beanutils.PropertyUtils
* @see org.apache.commons.collections.Transformer
*/
public class BeanToPropertyValueTransformer implements Transformer {
/** For logging. */
private final Log log = LogFactory.getLog(this.getClass());
/** The name of the property that will be used in the transformation of the object. */
private String propertyName;
/**
*
Should null objects on the property path throw an IllegalArgumentException?
*
* Determines whether null objects in the property path will genenerate an
* IllegalArgumentException or not. If set to true then if any objects
* in the property path evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged but
* not rethrown and null will be returned. If set to false then if any
* objects in the property path evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged and
* rethrown.
*
*/
private boolean ignoreNull;
/**
* Constructs a Transformer which does not ignore nulls.
* Constructor which takes the name of the property that will be used in the transformation and
* assumes ignoreNull to be false.
*
* @param propertyName The name of the property that will be used in the transformation.
* @throws IllegalArgumentException If the propertyName is null or
* empty.
*/
public BeanToPropertyValueTransformer(String propertyName) {
this(propertyName, false);
}
/**
* Constructs a Transformer and sets ignoreNull.
* Constructor which takes the name of the property that will be used in the transformation and
* a boolean which determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not.
*
* @param propertyName The name of the property that will be used in the transformation.
* @param ignoreNull Determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not.
* @throws IllegalArgumentException If the propertyName is null or
* empty.
*/
public BeanToPropertyValueTransformer(String propertyName, boolean ignoreNull) {
super();
if ((propertyName != null) && (propertyName.length() > 0)) {
this.propertyName = propertyName;
this.ignoreNull = ignoreNull;
} else {
throw new IllegalArgumentException(
"propertyName cannot be null or empty");
}
}
/**
* Returns the value of the property named in the transformer's constructor for
* the object provided. If any object in the property path leading up to the target property is
* null then the outcome will be based on the value of the ignoreNull
* attribute. By default, ignoreNull is false and would result in an
* IllegalArgumentException if an object in the property path leading up to the
* target property is null.
*
* @param object The object to be transformed.
* @return The value of the property named in the transformer's constructor for the object
* provided.
* @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or
* NoSuchMethodException is thrown when trying to access the property specified on the object
* provided. Or if an object in the property path provided is null and
* ignoreNull is set to false.
*/
public Object transform(Object object) {
Object propertyValue = null;
try {
propertyValue = PropertyUtils.getProperty(object, propertyName);
} catch (IllegalArgumentException e) {
final String errorMsg = "Problem during transformation. Null value encountered in property path...";
if (ignoreNull) {
log.warn("WARNING: " + errorMsg + e);
} else {
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
}
} catch (IllegalAccessException e) {
final String errorMsg = "Unable to access the property provided.";
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
} catch (InvocationTargetException e) {
final String errorMsg = "Exception occurred in property's getter";
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
} catch (NoSuchMethodException e) {
final String errorMsg = "No property found for name [" +
propertyName + "]";
IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
if (!BeanUtils.initCause(iae, e)) {
log.error(errorMsg, e);
}
throw iae;
}
return propertyValue;
}
/**
* Returns the name of the property that will be used in the transformation of the bean.
*
* @return The name of the property that will be used in the transformation of the bean.
*/
public String getPropertyName() {
return propertyName;
}
/**
* Returns the flag which determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not. If set to true then
* if any objects in the property path evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged but
* not rethrown and null will be returned. If set to false then if any
* objects in the property path evaluate to null then the
* IllegalArgumentException throw by PropertyUtils will be logged and
* rethrown.
*
* @return The flag which determines whether null objects in the property path will
* genenerate an IllegalArgumentException or not.
*/
public boolean isIgnoreNull() {
return ignoreNull;
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanUtils.java 100644 0 0 45666 12262570612 25777 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
/**
*
Utility methods for populating JavaBeans properties via reflection.
*
*
The implementations are provided by {@link BeanUtilsBean}.
* These static utility methods use the default instance.
* More sophisticated behaviour can be provided by using a BeanUtilsBean instance.
*
* @version $Id: BeanUtils.java 1540186 2013-11-08 21:08:30Z oheger $
* @see BeanUtilsBean
*/
public class BeanUtils {
// ------------------------------------------------------ Private Variables
/**
* The debugging detail level for this component.
*
* Note that this static variable will have unexpected side-effects if
* this class is deployed in a shared classloader within a container.
* However as it is actually completely ignored by this class due to its
* deprecated status, it doesn't do any actual harm.
*
* @deprecated BeanUtils now uses commons-logging for all log messages.
* Use your favorite logging tool to configure logging for
* this class.
*/
@Deprecated
private static int debug = 0;
/**
* The debug static property is no longer used
* @return debug property
* @deprecated BeanUtils now uses commons-logging for all log messages.
* Use your favorite logging tool to configure logging for
* this class.
*/
@Deprecated
public static int getDebug() {
return (debug);
}
/**
* The debug static property is no longer used
* @param newDebug debug property
* @deprecated BeanUtils now uses commons-logging for all log messages.
* Use your favorite logging tool to configure logging for
* this class.
*/
@Deprecated
public static void setDebug(int newDebug) {
debug = newDebug;
}
// --------------------------------------------------------- Class Methods
/**
*
Clone a bean based on the available property getters and setters,
* even if the bean class itself does not implement Cloneable.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean to be cloned
* @return the cloned bean
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InstantiationException if a new instance of the bean's
* class cannot be instantiated
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#cloneBean
*/
public static Object cloneBean(Object bean)
throws IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException {
return BeanUtilsBean.getInstance().cloneBean(bean);
}
/**
*
Copy property values from the origin bean to the destination bean
* for all cases where the property names are the same.
*
*
For more details see BeanUtilsBean.
*
* @param dest Destination bean whose properties are modified
* @param orig Origin bean whose properties are retrieved
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if the dest or
* orig argument is null or if the dest
* property type is different from the source type and the relevant
* converter has not been registered.
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @see BeanUtilsBean#copyProperties
*/
public static void copyProperties(Object dest, Object orig)
throws IllegalAccessException, InvocationTargetException {
BeanUtilsBean.getInstance().copyProperties(dest, orig);
}
/**
*
Copy the specified property value to the specified destination bean,
* performing any type conversion that is required.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean on which setting is to be performed
* @param name Property name (can be nested/indexed/mapped/combo)
* @param value Value to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @see BeanUtilsBean#copyProperty
*/
public static void copyProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException {
BeanUtilsBean.getInstance().copyProperty(bean, name, value);
}
/**
*
Return the entire set of properties for which the specified bean
* provides a read method.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean whose properties are to be extracted
* @return Map of property descriptors
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#describe
*/
public static Map describe(Object bean)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtilsBean.getInstance().describe(bean);
}
/**
*
Return the value of the specified array property of the specified
* bean, as a String array.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean whose property is to be extracted
* @param name Name of the property to be extracted
* @return The array property value
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getArrayProperty
*/
public static String[] getArrayProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtilsBean.getInstance().getArrayProperty(bean, name);
}
/**
*
Return the value of the specified indexed property of the specified
* bean, as a String.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean whose property is to be extracted
* @param name propertyname[index] of the property value
* to be extracted
* @return The indexed property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getIndexedProperty(Object, String)
*/
public static String getIndexedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtilsBean.getInstance().getIndexedProperty(bean, name);
}
/**
* Return the value of the specified indexed property of the specified
* bean, as a String. The index is specified as a method parameter and
* must *not* be included in the property name expression
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean whose property is to be extracted
* @param name Simple property name of the property value to be extracted
* @param index Index of the property value to be extracted
* @return The indexed property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getIndexedProperty(Object, String, int)
*/
public static String getIndexedProperty(Object bean,
String name, int index)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtilsBean.getInstance().getIndexedProperty(bean, name, index);
}
/**
*
Return the value of the specified indexed property of the specified
* bean, as a String.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean whose property is to be extracted
* @param name propertyname(index) of the property value
* to be extracted
* @return The mapped property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getMappedProperty(Object, String)
*/
public static String getMappedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtilsBean.getInstance().getMappedProperty(bean, name);
}
/**
* Return the value of the specified mapped property of the specified
* bean, as a String.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean whose property is to be extracted
* @param name Simple property name of the property value to be extracted
* @param key Lookup key of the property value to be extracted
* @return The mapped property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getMappedProperty(Object, String, String)
*/
public static String getMappedProperty(Object bean,
String name, String key)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtilsBean.getInstance().getMappedProperty(bean, name, key);
}
/**
*
Return the value of the (possibly nested) property of the specified
* name, for the specified bean, as a String.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean whose property is to be extracted
* @param name Possibly nested name of the property to be extracted
* @return The nested property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if a nested reference to a
* property returns null
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getNestedProperty
*/
public static String getNestedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtilsBean.getInstance().getNestedProperty(bean, name);
}
/**
*
Return the value of the specified property of the specified bean,
* no matter which property reference format is used, as a String.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean whose property is to be extracted
* @param name Possibly indexed and/or nested name of the property
* to be extracted
* @return The property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getProperty
*/
public static String getProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtilsBean.getInstance().getProperty(bean, name);
}
/**
*
Return the value of the specified simple property of the specified
* bean, converted to a String.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean whose property is to be extracted
* @param name Name of the property to be extracted
* @return The property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
* @see BeanUtilsBean#getSimpleProperty
*/
public static String getSimpleProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return BeanUtilsBean.getInstance().getSimpleProperty(bean, name);
}
/**
*
Populate the JavaBeans properties of the specified bean, based on
* the specified name/value pairs.
*
*
For more details see BeanUtilsBean.
*
* @param bean JavaBean whose properties are being populated
* @param properties Map keyed by property name, with the
* corresponding (String or String[]) value(s) to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @see BeanUtilsBean#populate
*/
public static void populate(Object bean, Map properties)
throws IllegalAccessException, InvocationTargetException {
BeanUtilsBean.getInstance().populate(bean, properties);
}
/**
*
Set the specified property value, performing type conversions as
* required to conform to the type of the destination property.
*
*
For more details see BeanUtilsBean.
*
* @param bean Bean on which setting is to be performed
* @param name Property name (can be nested/indexed/mapped/combo)
* @param value Value to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @see BeanUtilsBean#setProperty
*/
public static void setProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException {
BeanUtilsBean.getInstance().setProperty(bean, name, value);
}
/**
* If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
*
* @param throwable The throwable.
* @param cause The cause of the throwable.
* @return true if the cause was initialized, otherwise false.
* @since 1.8.0
*/
public static boolean initCause(Throwable throwable, Throwable cause) {
return BeanUtilsBean.getInstance().initCause(throwable, cause);
}
/**
* Create a cache.
* @param the key type of the cache
* @param the value type of the cache
* @return a new cache
* @since 1.8.0
*/
public static Map createCache() {
return new WeakFastHashMap();
}
/**
* Return whether a Map is fast
* @param map The map
* @return Whether it is fast or not.
* @since 1.8.0
*/
public static boolean getCacheFast(Map, ?> map) {
if (map instanceof WeakFastHashMap) {
return ((WeakFastHashMap, ?>) map).getFast();
} else {
return false;
}
}
/**
* Set whether fast on a Map
* @param map The map
* @param fast Whether it should be fast or not.
* @since 1.8.0
*/
public static void setCacheFast(Map, ?> map, boolean fast) {
if (map instanceof WeakFastHashMap) {
((WeakFastHashMap, ?>)map).setFast(fast);
}
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java 100644 0 0 135362 12262570612 26576 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.beans.IndexedPropertyDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.expression.Resolver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
JavaBean property population methods.
*
*
This class provides implementations for the utility methods in
* {@link BeanUtils}.
* Different instances can be used to isolate caches between classloaders
* and to vary the value converters registered.
*
* @version $Id: BeanUtilsBean.java 1546738 2013-11-30 16:24:19Z oheger $
* @see BeanUtils
* @since 1.7
*/
public class BeanUtilsBean {
// ------------------------------------------------------ Private Class Variables
/**
* Contains BeanUtilsBean instances indexed by context classloader.
*/
private static final ContextClassLoaderLocal
BEANS_BY_CLASSLOADER = new ContextClassLoaderLocal() {
// Creates the default instance used when the context classloader is unavailable
@Override
protected BeanUtilsBean initialValue() {
return new BeanUtilsBean();
}
};
/**
* Gets the instance which provides the functionality for {@link BeanUtils}.
* This is a pseudo-singleton - an single instance is provided per (thread) context classloader.
* This mechanism provides isolation for web apps deployed in the same container.
*
* @return The (pseudo-singleton) BeanUtils bean instance
*/
public static BeanUtilsBean getInstance() {
return BEANS_BY_CLASSLOADER.get();
}
/**
* Sets the instance which provides the functionality for {@link BeanUtils}.
* This is a pseudo-singleton - an single instance is provided per (thread) context classloader.
* This mechanism provides isolation for web apps deployed in the same container.
*
* @param newInstance The (pseudo-singleton) BeanUtils bean instance
*/
public static void setInstance(BeanUtilsBean newInstance) {
BEANS_BY_CLASSLOADER.set(newInstance);
}
// --------------------------------------------------------- Attributes
/**
* Logging for this instance
*/
private final Log log = LogFactory.getLog(BeanUtils.class);
/** Used to perform conversions between object types when setting properties */
private final ConvertUtilsBean convertUtilsBean;
/** Used to access properties*/
private final PropertyUtilsBean propertyUtilsBean;
/** A reference to Throwable's initCause method, or null if it's not there in this JVM */
private static final Method INIT_CAUSE_METHOD = getInitCauseMethod();
// --------------------------------------------------------- Constuctors
/**
*
Constructs an instance using new property
* and conversion instances.
*/
public BeanUtilsBean() {
this(new ConvertUtilsBean(), new PropertyUtilsBean());
}
/**
*
Constructs an instance using given conversion instances
* and new {@link PropertyUtilsBean} instance.
*
* @param convertUtilsBean use this ConvertUtilsBean
* to perform conversions from one object to another
*
* @since 1.8.0
*/
public BeanUtilsBean(ConvertUtilsBean convertUtilsBean) {
this(convertUtilsBean, new PropertyUtilsBean());
}
/**
*
Constructs an instance using given property and conversion instances.
*
* @param convertUtilsBean use this ConvertUtilsBean
* to perform conversions from one object to another
* @param propertyUtilsBean use this PropertyUtilsBean
* to access properties
*/
public BeanUtilsBean(
ConvertUtilsBean convertUtilsBean,
PropertyUtilsBean propertyUtilsBean) {
this.convertUtilsBean = convertUtilsBean;
this.propertyUtilsBean = propertyUtilsBean;
}
// --------------------------------------------------------- Public Methods
/**
*
Clone a bean based on the available property getters and setters,
* even if the bean class itself does not implement Cloneable.
*
*
* Note: this method creates a shallow clone.
* In other words, any objects referred to by the bean are shared with the clone
* rather than being cloned in turn.
*
*
* @param bean Bean to be cloned
* @return the cloned bean
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InstantiationException if a new instance of the bean's
* class cannot be instantiated
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public Object cloneBean(Object bean)
throws IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException {
if (log.isDebugEnabled()) {
log.debug("Cloning bean: " + bean.getClass().getName());
}
Object newBean = null;
if (bean instanceof DynaBean) {
newBean = ((DynaBean) bean).getDynaClass().newInstance();
} else {
newBean = bean.getClass().newInstance();
}
getPropertyUtils().copyProperties(newBean, bean);
return (newBean);
}
/**
*
Copy property values from the origin bean to the destination bean
* for all cases where the property names are the same. For each
* property, a conversion is attempted as necessary. All combinations of
* standard JavaBeans and DynaBeans as origin and destination are
* supported. Properties that exist in the origin bean, but do not exist
* in the destination bean (or are read-only in the destination bean) are
* silently ignored.
*
*
If the origin "bean" is actually a Map, it is assumed
* to contain String-valued simple property names as the keys, pointing at
* the corresponding property values that will be converted (if necessary)
* and set in the destination bean. Note that this method
* is intended to perform a "shallow copy" of the properties and so complex
* properties (for example, nested ones) will not be copied.
*
*
This method differs from populate(), which
* was primarily designed for populating JavaBeans from the map of request
* parameters retrieved on an HTTP request, is that no scalar->indexed
* or indexed->scalar manipulations are performed. If the origin property
* is indexed, the destination property must be also.
*
*
If you know that no type conversions are required, the
* copyProperties() method in {@link PropertyUtils} will
* execute faster than this method.
*
*
FIXME - Indexed and mapped properties that do not
* have getter and setter methods for the underlying array or Map are not
* copied by this method.
*
* @param dest Destination bean whose properties are modified
* @param orig Origin bean whose properties are retrieved
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if the dest or
* orig argument is null or if the dest
* property type is different from the source type and the relevant
* converter has not been registered.
* @exception InvocationTargetException if the property accessor method
* throws an exception
*/
public void copyProperties(Object dest, Object orig)
throws IllegalAccessException, InvocationTargetException {
// Validate existence of the specified beans
if (dest == null) {
throw new IllegalArgumentException
("No destination bean specified");
}
if (orig == null) {
throw new IllegalArgumentException("No origin bean specified");
}
if (log.isDebugEnabled()) {
log.debug("BeanUtils.copyProperties(" + dest + ", " +
orig + ")");
}
// Copy the properties, converting as necessary
if (orig instanceof DynaBean) {
DynaProperty[] origDescriptors =
((DynaBean) orig).getDynaClass().getDynaProperties();
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
// Need to check isReadable() for WrapDynaBean
// (see Jira issue# BEANUTILS-61)
if (getPropertyUtils().isReadable(orig, name) &&
getPropertyUtils().isWriteable(dest, name)) {
Object value = ((DynaBean) orig).get(name);
copyProperty(dest, name, value);
}
}
} else if (orig instanceof Map) {
@SuppressWarnings("unchecked")
// Map properties are always of type
Map propMap = (Map) orig;
for (Map.Entry entry : propMap.entrySet()) {
String name = entry.getKey();
if (getPropertyUtils().isWriteable(dest, name)) {
copyProperty(dest, name, entry.getValue());
}
}
} else /* if (orig is a standard JavaBean) */ {
PropertyDescriptor[] origDescriptors =
getPropertyUtils().getPropertyDescriptors(orig);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (getPropertyUtils().isReadable(orig, name) &&
getPropertyUtils().isWriteable(dest, name)) {
try {
Object value =
getPropertyUtils().getSimpleProperty(orig, name);
copyProperty(dest, name, value);
} catch (NoSuchMethodException e) {
// Should not happen
}
}
}
}
}
/**
*
Copy the specified property value to the specified destination bean,
* performing any type conversion that is required. If the specified
* bean does not have a property of the specified name, or the property
* is read only on the destination bean, return without
* doing anything. If you have custom destination property types, register
* {@link Converter}s for them by calling the register()
* method of {@link ConvertUtils}.
*
*
IMPLEMENTATION RESTRICTIONS:
*
*
Does not support destination properties that are indexed,
* but only an indexed setter (as opposed to an array setter)
* is available.
*
Does not support destination properties that are mapped,
* but only a keyed setter (as opposed to a Map setter)
* is available.
*
The desired property type of a mapped setter cannot be
* determined (since Maps support any data type), so no conversion
* will be performed.
*
*
* @param bean Bean on which setting is to be performed
* @param name Property name (can be nested/indexed/mapped/combo)
* @param value Value to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
*/
public void copyProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException {
// Trace logging (if enabled)
if (log.isTraceEnabled()) {
StringBuilder sb = new StringBuilder(" copyProperty(");
sb.append(bean);
sb.append(", ");
sb.append(name);
sb.append(", ");
if (value == null) {
sb.append("");
} else if (value instanceof String) {
sb.append((String) value);
} else if (value instanceof String[]) {
String[] values = (String[]) value;
sb.append('[');
for (int i = 0; i < values.length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append(values[i]);
}
sb.append(']');
} else {
sb.append(value.toString());
}
sb.append(')');
log.trace(sb.toString());
}
// Resolve any nested expression to get the actual target bean
Object target = bean;
Resolver resolver = getPropertyUtils().getResolver();
while (resolver.hasNested(name)) {
try {
target = getPropertyUtils().getProperty(target, resolver.next(name));
name = resolver.remove(name);
} catch (NoSuchMethodException e) {
return; // Skip this property setter
}
}
if (log.isTraceEnabled()) {
log.trace(" Target bean = " + target);
log.trace(" Target name = " + name);
}
// Declare local variables we will require
String propName = resolver.getProperty(name); // Simple name of target property
Class> type = null; // Java type of target property
int index = resolver.getIndex(name); // Indexed subscript value (if any)
String key = resolver.getKey(name); // Mapped key value (if any)
// Calculate the target property type
if (target instanceof DynaBean) {
DynaClass dynaClass = ((DynaBean) target).getDynaClass();
DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
if (dynaProperty == null) {
return; // Skip this property setter
}
type = dynaPropertyType(dynaProperty, value);
} else {
PropertyDescriptor descriptor = null;
try {
descriptor =
getPropertyUtils().getPropertyDescriptor(target, name);
if (descriptor == null) {
return; // Skip this property setter
}
} catch (NoSuchMethodException e) {
return; // Skip this property setter
}
type = descriptor.getPropertyType();
if (type == null) {
// Most likely an indexed setter on a POJB only
if (log.isTraceEnabled()) {
log.trace(" target type for property '" +
propName + "' is null, so skipping ths setter");
}
return;
}
}
if (log.isTraceEnabled()) {
log.trace(" target propName=" + propName + ", type=" +
type + ", index=" + index + ", key=" + key);
}
// Convert the specified value to the required type and store it
if (index >= 0) { // Destination must be indexed
value = convertForCopy(value, type.getComponentType());
try {
getPropertyUtils().setIndexedProperty(target, propName,
index, value);
} catch (NoSuchMethodException e) {
throw new InvocationTargetException
(e, "Cannot set " + propName);
}
} else if (key != null) { // Destination must be mapped
// Maps do not know what the preferred data type is,
// so perform no conversions at all
// FIXME - should we create or support a TypedMap?
try {
getPropertyUtils().setMappedProperty(target, propName,
key, value);
} catch (NoSuchMethodException e) {
throw new InvocationTargetException
(e, "Cannot set " + propName);
}
} else { // Destination must be simple
value = convertForCopy(value, type);
try {
getPropertyUtils().setSimpleProperty(target, propName, value);
} catch (NoSuchMethodException e) {
throw new InvocationTargetException
(e, "Cannot set " + propName);
}
}
}
/**
*
Return the entire set of properties for which the specified bean
* provides a read method. This map contains the to String
* converted property values for all properties for which a read method
* is provided (i.e. where the getReadMethod() returns non-null).
*
*
This map can be fed back to a call to
* BeanUtils.populate() to reconsitute the same set of
* properties, modulo differences for read-only and write-only
* properties, but only if there are no indexed properties.
*
*
Warning: if any of the bean property implementations
* contain (directly or indirectly) a call to this method then
* a stack overflow may result. For example:
*
* class MyBean
* {
* public Map getParameterMap()
* {
* BeanUtils.describe(this);
* }
* }
*
* will result in an infinite regression when getParametersMap
* is called. It is recommended that such methods are given alternative
* names (for example, parametersMap).
*
* @param bean Bean whose properties are to be extracted
* @return Map of property descriptors
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public Map describe(Object bean)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null) {
// return (Collections.EMPTY_MAP);
return (new java.util.HashMap());
}
if (log.isDebugEnabled()) {
log.debug("Describing bean: " + bean.getClass().getName());
}
Map description = new HashMap();
if (bean instanceof DynaBean) {
DynaProperty[] descriptors =
((DynaBean) bean).getDynaClass().getDynaProperties();
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
description.put(name, getProperty(bean, name));
}
} else {
PropertyDescriptor[] descriptors =
getPropertyUtils().getPropertyDescriptors(bean);
Class> clazz = bean.getClass();
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (getPropertyUtils().getReadMethod(clazz, descriptors[i]) != null) {
description.put(name, getProperty(bean, name));
}
}
}
return (description);
}
/**
* Return the value of the specified array property of the specified
* bean, as a String array.
*
* @param bean Bean whose property is to be extracted
* @param name Name of the property to be extracted
* @return The array property value
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public String[] getArrayProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getProperty(bean, name);
if (value == null) {
return (null);
} else if (value instanceof Collection) {
ArrayList values = new ArrayList();
for (Object item : (Collection>) value) {
if (item == null) {
values.add(null);
} else {
// convert to string using convert utils
values.add(getConvertUtils().convert(item));
}
}
return (values.toArray(new String[values.size()]));
} else if (value.getClass().isArray()) {
int n = Array.getLength(value);
String[] results = new String[n];
for (int i = 0; i < n; i++) {
Object item = Array.get(value, i);
if (item == null) {
results[i] = null;
} else {
// convert to string using convert utils
results[i] = getConvertUtils().convert(item);
}
}
return (results);
} else {
String[] results = new String[1];
results[0] = getConvertUtils().convert(value);
return (results);
}
}
/**
* Return the value of the specified indexed property of the specified
* bean, as a String. The zero-relative index of the
* required value must be included (in square brackets) as a suffix to
* the property name, or IllegalArgumentException will be
* thrown.
*
* @param bean Bean whose property is to be extracted
* @param name propertyname[index] of the property value
* to be extracted
* @return The indexed property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public String getIndexedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getIndexedProperty(bean, name);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the specified indexed property of the specified
* bean, as a String. The index is specified as a method parameter and
* must *not* be included in the property name expression
*
* @param bean Bean whose property is to be extracted
* @param name Simple property name of the property value to be extracted
* @param index Index of the property value to be extracted
* @return The indexed property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public String getIndexedProperty(Object bean,
String name, int index)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getIndexedProperty(bean, name, index);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the specified indexed property of the specified
* bean, as a String. The String-valued key of the required value
* must be included (in parentheses) as a suffix to
* the property name, or IllegalArgumentException will be
* thrown.
*
* @param bean Bean whose property is to be extracted
* @param name propertyname(index) of the property value
* to be extracted
* @return The mapped property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public String getMappedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getMappedProperty(bean, name);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the specified mapped property of the specified
* bean, as a String. The key is specified as a method parameter and
* must *not* be included in the property name expression
*
* @param bean Bean whose property is to be extracted
* @param name Simple property name of the property value to be extracted
* @param key Lookup key of the property value to be extracted
* @return The mapped property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public String getMappedProperty(Object bean,
String name, String key)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getMappedProperty(bean, name, key);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the (possibly nested) property of the specified
* name, for the specified bean, as a String.
*
* @param bean Bean whose property is to be extracted
* @param name Possibly nested name of the property to be extracted
* @return The nested property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException if a nested reference to a
* property returns null
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public String getNestedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getNestedProperty(bean, name);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the specified property of the specified bean,
* no matter which property reference format is used, as a String.
*
* @param bean Bean whose property is to be extracted
* @param name Possibly indexed and/or nested name of the property
* to be extracted
* @return The property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public String getProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return (getNestedProperty(bean, name));
}
/**
* Return the value of the specified simple property of the specified
* bean, converted to a String.
*
* @param bean Bean whose property is to be extracted
* @param name Name of the property to be extracted
* @return The property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* property cannot be found
*/
public String getSimpleProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getSimpleProperty(bean, name);
return (getConvertUtils().convert(value));
}
/**
*
Populate the JavaBeans properties of the specified bean, based on
* the specified name/value pairs. This method uses Java reflection APIs
* to identify corresponding "property setter" method names, and deals
* with setter arguments of type String, boolean,
* int, long, float, and
* double. In addition, array setters for these types (or the
* corresponding primitive types) can also be identified.
*
*
The particular setter method to be called for each property is
* determined using the usual JavaBeans introspection mechanisms. Thus,
* you may identify custom setter methods using a BeanInfo class that is
* associated with the class of the bean itself. If no such BeanInfo
* class is available, the standard method name conversion ("set" plus
* the capitalized name of the property in question) is used.
*
*
NOTE: It is contrary to the JavaBeans Specification
* to have more than one setter method (with different argument
* signatures) for the same property.
*
*
WARNING - The logic of this method is customized
* for extracting String-based request parameters from an HTTP request.
* It is probably not what you want for general property copying with
* type conversion. For that purpose, check out the
* copyProperties() method instead.
*
* @param bean JavaBean whose properties are being populated
* @param properties Map keyed by property name, with the
* corresponding (String or String[]) value(s) to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
*/
public void populate(Object bean, Map properties)
throws IllegalAccessException, InvocationTargetException {
// Do nothing unless both arguments have been specified
if ((bean == null) || (properties == null)) {
return;
}
if (log.isDebugEnabled()) {
log.debug("BeanUtils.populate(" + bean + ", " +
properties + ")");
}
// Loop through the property name/value pairs to be set
for(Map.Entry entry : properties.entrySet()) {
// Identify the property name and value(s) to be assigned
String name = entry.getKey();
if (name == null) {
continue;
}
// Perform the assignment for this property
setProperty(bean, name, entry.getValue());
}
}
/**
*
Set the specified property value, performing type conversions as
* required to conform to the type of the destination property.
*
*
If the property is read only then the method returns
* without throwing an exception.
*
*
If null is passed into a property expecting a primitive value,
* then this will be converted as if it were a null string.
*
*
WARNING - The logic of this method is customized
* to meet the needs of populate(), and is probably not what
* you want for general property copying with type conversion. For that
* purpose, check out the copyProperty() method instead.
*
*
WARNING - PLEASE do not modify the behavior of this
* method without consulting with the Struts developer community. There
* are some subtleties to its functionality that are not documented in the
* Javadoc description above, yet are vital to the way that Struts utilizes
* this method.
*
* @param bean Bean on which setting is to be performed
* @param name Property name (can be nested/indexed/mapped/combo)
* @param value Value to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
*/
public void setProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException {
// Trace logging (if enabled)
if (log.isTraceEnabled()) {
StringBuilder sb = new StringBuilder(" setProperty(");
sb.append(bean);
sb.append(", ");
sb.append(name);
sb.append(", ");
if (value == null) {
sb.append("");
} else if (value instanceof String) {
sb.append((String) value);
} else if (value instanceof String[]) {
String[] values = (String[]) value;
sb.append('[');
for (int i = 0; i < values.length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append(values[i]);
}
sb.append(']');
} else {
sb.append(value.toString());
}
sb.append(')');
log.trace(sb.toString());
}
// Resolve any nested expression to get the actual target bean
Object target = bean;
Resolver resolver = getPropertyUtils().getResolver();
while (resolver.hasNested(name)) {
try {
target = getPropertyUtils().getProperty(target, resolver.next(name));
if (target == null) { // the value of a nested property is null
return;
}
name = resolver.remove(name);
} catch (NoSuchMethodException e) {
return; // Skip this property setter
}
}
if (log.isTraceEnabled()) {
log.trace(" Target bean = " + target);
log.trace(" Target name = " + name);
}
// Declare local variables we will require
String propName = resolver.getProperty(name); // Simple name of target property
Class> type = null; // Java type of target property
int index = resolver.getIndex(name); // Indexed subscript value (if any)
String key = resolver.getKey(name); // Mapped key value (if any)
// Calculate the property type
if (target instanceof DynaBean) {
DynaClass dynaClass = ((DynaBean) target).getDynaClass();
DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
if (dynaProperty == null) {
return; // Skip this property setter
}
type = dynaPropertyType(dynaProperty, value);
} else if (target instanceof Map) {
type = Object.class;
} else if (target != null && target.getClass().isArray() && index >= 0) {
type = Array.get(target, index).getClass();
} else {
PropertyDescriptor descriptor = null;
try {
descriptor =
getPropertyUtils().getPropertyDescriptor(target, name);
if (descriptor == null) {
return; // Skip this property setter
}
} catch (NoSuchMethodException e) {
return; // Skip this property setter
}
if (descriptor instanceof MappedPropertyDescriptor) {
if (((MappedPropertyDescriptor) descriptor).getMappedWriteMethod() == null) {
if (log.isDebugEnabled()) {
log.debug("Skipping read-only property");
}
return; // Read-only, skip this property setter
}
type = ((MappedPropertyDescriptor) descriptor).
getMappedPropertyType();
} else if (index >= 0 && descriptor instanceof IndexedPropertyDescriptor) {
if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null) {
if (log.isDebugEnabled()) {
log.debug("Skipping read-only property");
}
return; // Read-only, skip this property setter
}
type = ((IndexedPropertyDescriptor) descriptor).
getIndexedPropertyType();
} else if (key != null) {
if (descriptor.getReadMethod() == null) {
if (log.isDebugEnabled()) {
log.debug("Skipping read-only property");
}
return; // Read-only, skip this property setter
}
type = (value == null) ? Object.class : value.getClass();
} else {
if (descriptor.getWriteMethod() == null) {
if (log.isDebugEnabled()) {
log.debug("Skipping read-only property");
}
return; // Read-only, skip this property setter
}
type = descriptor.getPropertyType();
}
}
// Convert the specified value to the required type
Object newValue = null;
if (type.isArray() && (index < 0)) { // Scalar value into array
if (value == null) {
String[] values = new String[1];
values[0] = null;
newValue = getConvertUtils().convert(values, type);
} else if (value instanceof String) {
newValue = getConvertUtils().convert(value, type);
} else if (value instanceof String[]) {
newValue = getConvertUtils().convert((String[]) value, type);
} else {
newValue = convert(value, type);
}
} else if (type.isArray()) { // Indexed value into array
if (value instanceof String || value == null) {
newValue = getConvertUtils().convert((String) value,
type.getComponentType());
} else if (value instanceof String[]) {
newValue = getConvertUtils().convert(((String[]) value)[0],
type.getComponentType());
} else {
newValue = convert(value, type.getComponentType());
}
} else { // Value into scalar
if (value instanceof String) {
newValue = getConvertUtils().convert((String) value, type);
} else if (value instanceof String[]) {
newValue = getConvertUtils().convert(((String[]) value)[0],
type);
} else {
newValue = convert(value, type);
}
}
// Invoke the setter method
try {
getPropertyUtils().setProperty(target, name, newValue);
} catch (NoSuchMethodException e) {
throw new InvocationTargetException
(e, "Cannot set " + propName);
}
}
/**
* Gets the ConvertUtilsBean instance used to perform the conversions.
*
* @return The ConvertUtils bean instance
*/
public ConvertUtilsBean getConvertUtils() {
return convertUtilsBean;
}
/**
* Gets the PropertyUtilsBean instance used to access properties.
*
* @return The ConvertUtils bean instance
*/
public PropertyUtilsBean getPropertyUtils() {
return propertyUtilsBean;
}
/**
* If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
*
* @param throwable The throwable.
* @param cause The cause of the throwable.
* @return true if the cause was initialized, otherwise false.
* @since 1.8.0
*/
public boolean initCause(Throwable throwable, Throwable cause) {
if (INIT_CAUSE_METHOD != null && cause != null) {
try {
INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause });
return true;
} catch (Throwable e) {
return false; // can't initialize cause
}
}
return false;
}
/**
*
Convert the value to an object of the specified class (if
* possible).
*
* @param value Value to be converted (may be null)
* @param type Class of the value to be converted to
* @return The converted value
*
* @exception ConversionException if thrown by an underlying Converter
* @since 1.8.0
*/
protected Object convert(Object value, Class> type) {
Converter converter = getConvertUtils().lookup(type);
if (converter != null) {
log.trace(" USING CONVERTER " + converter);
return converter.convert(type, value);
} else {
return value;
}
}
/**
* Performs a type conversion of a property value before it is copied to a target
* bean. This method delegates to {@link #convert(Object, Class)}, but null
* values are not converted. This causes null values to be copied verbatim.
*
* @param value the value to be converted and copied
* @param type the target type of the conversion
* @return the converted value
*/
private Object convertForCopy(Object value, Class> type) {
return (value != null) ? convert(value, type) : value;
}
/**
* Returns a Method allowing access to
* {@link Throwable#initCause(Throwable)} method of {@link Throwable},
* or null if the method
* does not exist.
*
* @return A Method for Throwable.initCause, or
* null if unavailable.
*/
private static Method getInitCauseMethod() {
try {
Class>[] paramsClasses = new Class>[] { Throwable.class };
return Throwable.class.getMethod("initCause", paramsClasses);
} catch (NoSuchMethodException e) {
Log log = LogFactory.getLog(BeanUtils.class);
if (log.isWarnEnabled()) {
log.warn("Throwable does not have initCause() method in JDK 1.3");
}
return null;
} catch (Throwable e) {
Log log = LogFactory.getLog(BeanUtils.class);
if (log.isWarnEnabled()) {
log.warn("Error getting the Throwable initCause() method", e);
}
return null;
}
}
/**
* Determines the type of a {@code DynaProperty}. Here a special treatment
* is needed for mapped properties.
*
* @param dynaProperty the property descriptor
* @param value the value object to be set for this property
* @return the type of this property
*/
private static Class> dynaPropertyType(DynaProperty dynaProperty,
Object value) {
if (!dynaProperty.isMapped()) {
return dynaProperty.getType();
}
return (value == null) ? String.class : value.getClass();
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/BeanUtilsBean2.java 100644 0 0 5670 12262570612 26616 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
/**
*
{@link BeanUtilsBean} implementation that creates a
* {@link ConvertUtilsBean2} and delegates conversion to
* {@link ConvertUtilsBean#convert(Object, Class)}.
*
*
*
* To configure this implementation for the current context ClassLoader invoke
* BeanUtilsBean.setInstance(new BeanUtilsBean2());
*
*
*
* BeanUtils 1.7.0 delegated all conversion to String to the converter
* registered for the String.class. One of the improvements in
* BeanUtils 1.8.0 was to upgrade the {@link Converter} implementations so
* that they could handle conversion to String for their type (e.g.
* IntegerConverter now handles conversion from an Integer to a String as
* well as String to Integer).
*
*
*
* In order to take advantage of these improvements BeanUtils needs to change
* how it gets the appropriate {@link Converter}. This functionality has been
* implemented in the new {@link ConvertUtilsBean#lookup(Class, Class)} and
* {@link ConvertUtilsBean#convert(Object, Class)} methods. However changing
* {@link BeanUtilsBean} to use these methods could create compatibility
* issues for existing users. In order to avoid that, this new
* {@link BeanUtilsBean} implementation has been created (and the associated
* {@link ConvertUtilsBean2}).
*
Constructs an instance using new property
* and conversion instances.
*/
public BeanUtilsBean2() {
super(new ConvertUtilsBean2());
}
/**
*
Convert the value to an object of the specified class (if
* possible).
*
* @param value Value to be converted (may be null)
* @param type Class of the value to be converted to
* @return The converted value
*/
@Override
protected Object convert(Object value, Class> type) {
return getConvertUtils().convert(value, type);
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/ConstructorUtils.java 100644 0 0 44525 12262570612 27450 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
/**
*
Utility reflection methods focussed on constructors, modelled after {@link MethodUtils}.
*
*
Known Limitations
*
Accessing Public Constructors In A Default Access Superclass
*
There is an issue when invoking public constructors contained in a default access superclass.
* Reflection locates these constructors fine and correctly assigns them as public.
* However, an IllegalAccessException is thrown if the constructors is invoked.
*
*
ConstructorUtils contains a workaround for this situation.
* It will attempt to call setAccessible on this constructor.
* If this call succeeds, then the method can be invoked as normal.
* This call will only succeed when the application has sufficient security privilages.
* If this call fails then a warning will be logged and the method may fail.
*
* @version $Id: ConstructorUtils.java 1540518 2013-11-10 19:04:04Z oheger $
*/
public class ConstructorUtils {
// --------------------------------------------------------- Private Members
/** An empty class array */
private static final Class>[] EMPTY_CLASS_PARAMETERS = new Class>[0];
/** An empty object array */
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
// --------------------------------------------------------- Public Methods
/**
*
Convenience method returning new instance of klazz using a single argument constructor.
* The formal parameter type is inferred from the actual values of arg.
* See {@link #invokeExactConstructor(Class, Object[], Class[])} for more details.
*
*
The signatures should be assignment compatible.
*
* @param the type of the object to be constructed
* @param klass the class to be constructed.
* @param arg the actual argument. May be null (this will result in calling the default constructor).
* @return new instance of klazz
*
* @throws NoSuchMethodException If the constructor cannot be found
* @throws IllegalAccessException If an error occurs accessing the constructor
* @throws InvocationTargetException If an error occurs invoking the constructor
* @throws InstantiationException If an error occurs instantiating the class
*
* @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
*/
public static T invokeConstructor(Class klass, Object arg)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
Object[] args = toArray(arg);
return invokeConstructor(klass, args);
}
/**
*
Returns new instance of klazz created using the actual arguments args.
* The formal parameter types are inferred from the actual values of args.
* See {@link #invokeExactConstructor(Class, Object[], Class[])} for more details.
*
*
The signatures should be assignment compatible.
*
* @param the type of the object to be constructed
* @param klass the class to be constructed.
* @param args actual argument array. May be null (this will result in calling the default constructor).
* @return new instance of klazz
*
* @throws NoSuchMethodException If the constructor cannot be found
* @throws IllegalAccessException If an error occurs accessing the constructor
* @throws InvocationTargetException If an error occurs invoking the constructor
* @throws InstantiationException If an error occurs instantiating the class
*
* @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
*/
public static T invokeConstructor(Class klass, Object[] args)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
if (null == args) {
args = EMPTY_OBJECT_ARRAY;
}
int arguments = args.length;
Class> parameterTypes[] = new Class>[arguments];
for (int i = 0; i < arguments; i++) {
parameterTypes[i] = args[i].getClass();
}
return invokeConstructor(klass, args, parameterTypes);
}
/**
*
Returns new instance of klazz created using constructor
* with signature parameterTypes and actual arguments args.
*
*
The signatures should be assignment compatible.
*
* @param the type of the object to be constructed
* @param klass the class to be constructed.
* @param args actual argument array. May be null (this will result in calling the default constructor).
* @param parameterTypes parameter types array
* @return new instance of klazz
*
* @throws NoSuchMethodException if matching constructor cannot be found
* @throws IllegalAccessException thrown on the constructor's invocation
* @throws InvocationTargetException thrown on the constructor's invocation
* @throws InstantiationException thrown on the constructor's invocation
* @see Constructor#newInstance
*/
public static T invokeConstructor(
Class klass,
Object[] args,
Class>[] parameterTypes)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
if (parameterTypes == null) {
parameterTypes = EMPTY_CLASS_PARAMETERS;
}
if (args == null) {
args = EMPTY_OBJECT_ARRAY;
}
Constructor ctor =
getMatchingAccessibleConstructor(klass, parameterTypes);
if (null == ctor) {
throw new NoSuchMethodException(
"No such accessible constructor on object: " + klass.getName());
}
return ctor.newInstance(args);
}
/**
*
Convenience method returning new instance of klazz using a single argument constructor.
* The formal parameter type is inferred from the actual values of arg.
* See {@link #invokeExactConstructor(Class, Object[], Class[])} for more details.
*
*
The signatures should match exactly.
*
* @param the type of the object to be constructed
* @param klass the class to be constructed.
* @param arg the actual argument. May be null (this will result in calling the default constructor).
* @return new instance of klazz
*
* @throws NoSuchMethodException If the constructor cannot be found
* @throws IllegalAccessException If an error occurs accessing the constructor
* @throws InvocationTargetException If an error occurs invoking the constructor
* @throws InstantiationException If an error occurs instantiating the class
*
* @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
*/
public static T invokeExactConstructor(Class klass, Object arg)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
Object[] args = toArray(arg);
return invokeExactConstructor(klass, args);
}
/**
*
Returns new instance of klazz created using the actual arguments args.
* The formal parameter types are inferred from the actual values of args.
* See {@link #invokeExactConstructor(Class, Object[], Class[])} for more details.
*
*
The signatures should match exactly.
*
* @param the type of the object to be constructed
* @param klass the class to be constructed.
* @param args actual argument array. May be null (this will result in calling the default constructor).
* @return new instance of klazz
*
* @throws NoSuchMethodException If the constructor cannot be found
* @throws IllegalAccessException If an error occurs accessing the constructor
* @throws InvocationTargetException If an error occurs invoking the constructor
* @throws InstantiationException If an error occurs instantiating the class
*
* @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
*/
public static T invokeExactConstructor(Class klass, Object[] args)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
if (null == args) {
args = EMPTY_OBJECT_ARRAY;
}
int arguments = args.length;
Class> parameterTypes[] = new Class[arguments];
for (int i = 0; i < arguments; i++) {
parameterTypes[i] = args[i].getClass();
}
return invokeExactConstructor(klass, args, parameterTypes);
}
/**
*
Returns new instance of klazz created using constructor
* with signature parameterTypes and actual arguments
* args.
*
*
The signatures should match exactly.
*
* @param the type of the object to be constructed
* @param klass the class to be constructed.
* @param args actual argument array. May be null (this will result in calling the default constructor).
* @param parameterTypes parameter types array
* @return new instance of klazz
*
* @throws NoSuchMethodException if matching constructor cannot be found
* @throws IllegalAccessException thrown on the constructor's invocation
* @throws InvocationTargetException thrown on the constructor's invocation
* @throws InstantiationException thrown on the constructor's invocation
* @see Constructor#newInstance
*/
public static T invokeExactConstructor(
Class klass,
Object[] args,
Class>[] parameterTypes)
throws
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
if (args == null) {
args = EMPTY_OBJECT_ARRAY;
}
if (parameterTypes == null) {
parameterTypes = EMPTY_CLASS_PARAMETERS;
}
Constructor ctor = getAccessibleConstructor(klass, parameterTypes);
if (null == ctor) {
throw new NoSuchMethodException(
"No such accessible constructor on object: " + klass.getName());
}
return ctor.newInstance(args);
}
/**
* Returns a constructor with single argument.
* @param the type of the constructor
* @param klass the class to be constructed
* @param parameterType The constructor parameter type
* @return null if matching accessible constructor can not be found.
* @see Class#getConstructor
* @see #getAccessibleConstructor(java.lang.reflect.Constructor)
*/
public static Constructor getAccessibleConstructor(
Class klass,
Class> parameterType) {
Class>[] parameterTypes = { parameterType };
return getAccessibleConstructor(klass, parameterTypes);
}
/**
* Returns a constructor given a class and signature.
* @param the type to be constructed
* @param klass the class to be constructed
* @param parameterTypes the parameter array
* @return null if matching accessible constructor can not be found
* @see Class#getConstructor
* @see #getAccessibleConstructor(java.lang.reflect.Constructor)
*/
public static Constructor getAccessibleConstructor(
Class klass,
Class>[] parameterTypes) {
try {
return getAccessibleConstructor(
klass.getConstructor(parameterTypes));
} catch (NoSuchMethodException e) {
return (null);
}
}
/**
* Returns accessible version of the given constructor.
* @param the type of the constructor
* @param ctor prototype constructor object.
* @return null if accessible constructor can not be found.
* @see java.lang.SecurityManager
*/
public static Constructor getAccessibleConstructor(Constructor ctor) {
// Make sure we have a method to check
if (ctor == null) {
return (null);
}
// If the requested method is not public we cannot call it
if (!Modifier.isPublic(ctor.getModifiers())) {
return (null);
}
// If the declaring class is public, we are done
Class clazz = ctor.getDeclaringClass();
if (Modifier.isPublic(clazz.getModifiers())) {
return (ctor);
}
// what else can we do?
return null;
}
private static Object[] toArray(Object arg) {
Object[] args = null;
if (arg != null) {
args = new Object[] { arg };
}
return args;
}
// -------------------------------------------------------- Private Methods
/**
*
Find an accessible constructor with compatible parameters.
* Compatible parameters mean that every method parameter is assignable from
* the given parameters. In other words, it finds constructor that will take
* the parameters given.
*
*
First it checks if there is constructor matching the exact signature.
* If no such, all the constructors of the class are tested if their signatures
* are assignment compatible with the parameter types.
* The first matching constructor is returned.
*
* @param the type of the class to be inspected
* @param clazz find constructor for this class
* @param parameterTypes find method with compatible parameters
* @return a valid Constructor object. If there's no matching constructor, returns null.
*/
private static Constructor getMatchingAccessibleConstructor(
Class clazz,
Class>[] parameterTypes) {
// see if we can find the method directly
// most of the time this works and it's much faster
try {
Constructor ctor = clazz.getConstructor(parameterTypes);
try {
//
// XXX Default access superclass workaround
//
// When a public class has a default access superclass
// with public methods, these methods are accessible.
// Calling them from compiled code works fine.
//
// Unfortunately, using reflection to invoke these methods
// seems to (wrongly) to prevent access even when the method
// modifer is public.
//
// The following workaround solves the problem but will only
// work from sufficiently privilages code.
//
// Better workarounds would be greatfully accepted.
//
ctor.setAccessible(true);
} catch (SecurityException se) {
/* SWALLOW, if workaround fails don't fret. */
}
return ctor;
} catch (NoSuchMethodException e) { /* SWALLOW */
}
// search through all methods
int paramSize = parameterTypes.length;
Constructor>[] ctors = clazz.getConstructors();
for (int i = 0, size = ctors.length; i < size; i++) {
// compare parameters
Class>[] ctorParams = ctors[i].getParameterTypes();
int ctorParamSize = ctorParams.length;
if (ctorParamSize == paramSize) {
boolean match = true;
for (int n = 0; n < ctorParamSize; n++) {
if (!MethodUtils
.isAssignmentCompatible(
ctorParams[n],
parameterTypes[n])) {
match = false;
break;
}
}
if (match) {
// get accessible version of method
Constructor> ctor = getAccessibleConstructor(ctors[i]);
if (ctor != null) {
try {
ctor.setAccessible(true);
} catch (SecurityException se) {
/* Swallow SecurityException
* TODO: Why?
*/
}
@SuppressWarnings("unchecked")
// Class.getConstructors() actually returns constructors
// of type T, so it is safe to cast.
Constructor typedCtor = (Constructor) ctor;
return typedCtor;
}
}
}
}
return null;
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/ContextClassLoaderLocal.java 100644 0 0 22555 12262570612 30615 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.util.Map;
import java.util.WeakHashMap;
/**
* An instance of this class represents a value that is provided per (thread)
* context classloader.
*
*
Occasionally it is necessary to store data in "global" variables
* (including uses of the Singleton pattern). In applications which have only
* a single classloader such data can simply be stored as "static" members on
* some class. When multiple classloaders are involved, however, this approach
* can fail; in particular, this doesn't work when the code may be run within a
* servlet container or a j2ee container, and the class on which the static
* member is defined is loaded via a "shared" classloader that is visible to all
* components running within the container. This class provides a mechanism for
* associating data with a ClassLoader instance, which ensures that when the
* code runs in such a container each component gets its own copy of the
* "global" variable rather than unexpectedly sharing a single copy of the
* variable with other components that happen to be running in the same
* container at the same time (eg servlets or EJBs.)
*
*
This class is strongly patterned after the java.lang.ThreadLocal
* class, which performs a similar task in allowing data to be associated
* with a particular thread.
*
*
When code that uses this class is run as a "normal" application, ie
* not within a container, the effect is identical to just using a static
* member variable to store the data, because Thread.getContextClassLoader
* always returns the same classloader (the system classloader).
*
*
Expected usage is as follows:
*
* public class SomeClass {
* private static final ContextClassLoaderLocal<String> global
* = new ContextClassLoaderLocal<String>() {
* protected String initialValue() {
* return new String("Initial value");
* };
*
* public void testGlobal() {
* String s = global.get();
* System.out.println("global value:" + s);
* buf.set("New Value");
* }
*
*
*
*
Note: This class takes some care to ensure that when
* a component which uses this class is "undeployed" by a container the
* component-specific classloader and all its associated classes (and their
* static variables) are garbage-collected. Unfortunately there is one
* scenario in which this does not work correctly and there
* is unfortunately no known workaround other than ensuring that the
* component (or its container) calls the "unset" method on this class for
* each instance of this class when the component is undeployed. The problem
* occurs if:
*
*
the class containing a static instance of this class was loaded via
* a shared classloader, and
*
the value stored in the instance is an object whose class was loaded
* via the component-specific classloader (or any of the objects it refers
* to were loaded via that classloader).
*
* The result is that the map managed by this object still contains a strong
* reference to the stored object, which contains a strong reference to the
* classloader that loaded it, meaning that although the container has
* "undeployed" the component the component-specific classloader and all the
* related classes and static variables cannot be garbage-collected. This is
* not expected to be an issue with the commons-beanutils library as the only
* classes which use this class are BeanUtilsBean and ConvertUtilsBean and
* there is no obvious reason for a user of the beanutils library to subclass
* either of those classes.
*
*
Note: A WeakHashMap bug in several 1.3 JVMs results in
* a memory leak for those JVMs.
*
*
Note: Of course all of this would be unnecessary if
* containers required each component to load the full set of classes it
* needs, ie avoided providing classes loaded via a "shared" classloader.
*
* @param the type of data stored in an instance
* @version $Id: ContextClassLoaderLocal.java 1540186 2013-11-08 21:08:30Z oheger $
* @see java.lang.Thread#getContextClassLoader
*/
public class ContextClassLoaderLocal {
private final Map valueByClassLoader = new WeakHashMap();
private boolean globalValueInitialized = false;
private T globalValue;
/**
* Construct a context classloader instance
*/
public ContextClassLoaderLocal() {
super();
}
/**
* Returns the initial value for this ContextClassLoaderLocal
* variable. This method will be called once per Context ClassLoader for
* each ContextClassLoaderLocal, the first time it is accessed
* with get or set. If the programmer desires ContextClassLoaderLocal variables
* to be initialized to some value other than null, ContextClassLoaderLocal must
* be subclassed, and this method overridden. Typically, an anonymous
* inner class will be used. Typical implementations of initialValue
* will call an appropriate constructor and return the newly constructed
* object.
*
* @return a new Object to be used as an initial value for this ContextClassLoaderLocal
*/
protected T initialValue() {
return null;
}
/**
* Gets the instance which provides the functionality for {@link BeanUtils}.
* This is a pseudo-singleton - an single instance is provided per (thread) context classloader.
* This mechanism provides isolation for web apps deployed in the same container.
* @return the object currently associated with the context-classloader of the current thread.
*/
public synchronized T get() {
// synchronizing the whole method is a bit slower
// but guarantees no subtle threading problems, and there's no
// need to synchronize valueByClassLoader
// make sure that the map is given a change to purge itself
valueByClassLoader.isEmpty();
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) {
T value = valueByClassLoader.get(contextClassLoader);
if ((value == null)
&& !valueByClassLoader.containsKey(contextClassLoader)) {
value = initialValue();
valueByClassLoader.put(contextClassLoader, value);
}
return value;
}
} catch (SecurityException e) { /* SWALLOW - should we log this? */ }
// if none or exception, return the globalValue
if (!globalValueInitialized) {
globalValue = initialValue();
globalValueInitialized = true;
}//else already set
return globalValue;
}
/**
* Sets the value - a value is provided per (thread) context classloader.
* This mechanism provides isolation for web apps deployed in the same container.
*
* @param value the object to be associated with the entrant thread's context classloader
*/
public synchronized void set(T value) {
// synchronizing the whole method is a bit slower
// but guarentees no subtle threading problems
// make sure that the map is given a change to purge itself
valueByClassLoader.isEmpty();
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) {
valueByClassLoader.put(contextClassLoader, value);
return;
}
} catch (SecurityException e) { /* SWALLOW - should we log this? */ }
// if in doubt, set the global value
globalValue = value;
globalValueInitialized = true;
}
/**
* Unsets the value associated with the current thread's context classloader
*/
public synchronized void unset() {
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
unset(contextClassLoader);
} catch (SecurityException e) { /* SWALLOW - should we log this? */ }
}
/**
* Unsets the value associated with the given classloader
* @param classLoader The classloader to unset for
*/
public synchronized void unset(ClassLoader classLoader) {
valueByClassLoader.remove(classLoader);
}
} commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/ConversionException.java 100644 0 0 5152 12262570613 30060 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
/**
*
A ConversionException indicates that a call to
* Converter.convert() has failed to complete successfully.
*
* @since 1.3
* @version $Id: ConversionException.java 1454606 2013-03-08 22:30:51Z britter $
*/
public class ConversionException extends RuntimeException {
// ----------------------------------------------------------- Constructors
/**
* Construct a new exception with the specified message.
*
* @param message The message describing this exception
*/
public ConversionException(String message) {
super(message);
}
/**
* Construct a new exception with the specified message and root cause.
*
* @param message The message describing this exception
* @param cause The root cause of this exception
*/
public ConversionException(String message, Throwable cause) {
super(message);
this.cause = cause;
}
/**
* Construct a new exception with the specified root cause.
*
* @param cause The root cause of this exception
*/
public ConversionException(Throwable cause) {
super(cause.getMessage());
this.cause = cause;
}
// ------------------------------------------------------------- Properties
/**
* The root cause of this ConversionException, compatible with
* JDK 1.4's extensions to java.lang.Throwable.
*/
protected Throwable cause = null;
/**
* Return the root cause of this conversion exception.
* @return the root cause of this conversion exception
*/
@Override
public Throwable getCause() {
return (this.cause);
}
}
commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/Converter.java 100644 0 0 4076 12262570610 26024 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
/**
*
General purpose data type converter that can be registered and used
* within the BeanUtils package to manage the conversion of objects from
* one type to another.
*
*
Converter subclasses bundled with the BeanUtils library are required
* to be thread-safe, as users of the library may call conversion methods
* from more than one thread simultaneously.
*
*
Custom converter subclasses created by users of the library can be
* non-thread-safe if the application using them is single-threaded. However
* it is recommended that they be written in a thread-safe manner anyway.
*
* @version $Id: Converter.java 1540186 2013-11-08 21:08:30Z oheger $
* @since 1.3
*/
public interface Converter {
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param the desired result type
* @param type Data type to which this value should be converted
* @param value The input value to be converted
* @return The converted value
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public T convert(Class type, Object value);
}
././@LongLink 100644 0 0 156 12262571655 10270 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/AbstractArrayConverter.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/AbstractArrayConve100644 0 0 16333 12262570611 31104 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils.converters;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
*
Convenience base class for converters that translate the String
* representation of an array into a corresponding array of primitives
* object. This class encapsulates the functionality required to parse
* the String into a list of String elements that can later be
* individually converted to the appropriate primitive type.
*
*
The input syntax accepted by the parseElements() method
* is designed to be compatible with the syntax used to initialize arrays
* in a Java source program, except that only String literal values are
* supported. For maximum flexibility, the surrounding '{' and '}'
* characters are optional, and individual elements may be separated by
* any combination of whitespace and comma characters.
*
* @version $Id: AbstractArrayConverter.java 1454606 2013-03-08 22:30:51Z britter $
* @since 1.4
* @deprecated Replaced by the new {@link ArrayConverter} implementation
*/
@Deprecated
public abstract class AbstractArrayConverter implements Converter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public AbstractArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
* @since 1.8.0
*/
public AbstractArrayConverter(Object defaultValue) {
if (defaultValue == NO_DEFAULT) {
this.useDefault = false;
} else {
this.defaultValue = defaultValue;
this.useDefault = true;
}
}
// ------------------------------------------------------- Static Variables
/**
* This is a special reference that can be passed as the "default object"
* to the constructor to indicate that no default is desired. Note that
* the value 'null' cannot be used for this purpose, as the caller may
* want a null to be returned as the default.
* @since 1.8.0
*/
public static final Object NO_DEFAULT = new Object();
// ----------------------------------------------------- Instance Variables
/**
*
Model object for string arrays.
*/
protected static String[] strings = new String[0];
/**
* The default value specified to our Constructor, if any.
*/
protected Object defaultValue = null;
/**
* Should we return the default value on conversion errors?
*/
protected boolean useDefault = true;
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type. This method must be implemented by a concrete
* subclass.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
* @return The converted value
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public abstract Object convert(Class type, Object value);
// ------------------------------------------------------ Protected Methods
/**
*
Parse an incoming String of the form similar to an array initializer
* in the Java language into a List individual Strings
* for each element, according to the following rules.
*
*
The string is expected to be a comma-separated list of values.
*
The string may optionally have matching '{' and '}' delimiters
* around the list.
*
Whitespace before and after each element is stripped.
*
Elements in the list may be delimited by single or double quotes.
* Within a quoted elements, the normal Java escape sequences are valid.
*
*
* @param svalue String value to be parsed
* @return The parsed list of String values
*
* @exception ConversionException if the syntax of svalue
* is not syntactically valid
* @exception NullPointerException if svalue
* is null
*/
protected List parseElements(String svalue) {
// Validate the passed argument
if (svalue == null) {
throw new NullPointerException();
}
// Trim any matching '{' and '}' delimiters
svalue = svalue.trim();
if (svalue.startsWith("{") && svalue.endsWith("}")) {
svalue = svalue.substring(1, svalue.length() - 1);
}
try {
// Set up a StreamTokenizer on the characters in this String
StreamTokenizer st =
new StreamTokenizer(new StringReader(svalue));
st.whitespaceChars(',',','); // Commas are delimiters
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
st.ordinaryChars('.', '.');
st.ordinaryChars('-', '-');
st.wordChars('0', '9'); // Needed to make part of tokens
st.wordChars('.', '.');
st.wordChars('-', '-');
// Split comma-delimited tokens into a List
ArrayList list = new ArrayList();
while (true) {
int ttype = st.nextToken();
if ((ttype == StreamTokenizer.TT_WORD) ||
(ttype > 0)) {
list.add(st.sval);
} else if (ttype == StreamTokenizer.TT_EOF) {
break;
} else {
throw new ConversionException
("Encountered token of type " + ttype);
}
}
// Return the completed list
return (list);
} catch (IOException e) {
throw new ConversionException(e);
}
}
}
././@LongLink 100644 0 0 151 12262571655 10263 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/AbstractConverter.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/AbstractConverter.100644 0 0 42201 12262570611 31051 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils.converters;
import java.lang.reflect.Array;
import java.util.Collection;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Base {@link Converter} implementation that provides the structure
* for handling conversion to and from a specified type.
*
* This implementation provides the basic structure for
* converting to/from a specified type optionally using a default
* value or throwing a {@link ConversionException} if a
* conversion error occurs.
*
* Implementations should provide conversion to the specified
* type and from the specified type to a String value
* by implementing the following methods:
*
*
convertToString(value) - convert to a String
* (default implementation uses the objects toString()
* method).
*
convertToType(Class, value) - convert
* to the specified type
*
*
* The default value has to be compliant to the default type of this
* converter - which is enforced by the generic type parameter. If a
* conversion is not possible and a default value is set, the converter
* tries to transform the default value to the requested target type.
* If this fails, a {@code ConversionException} if thrown.
*
* @version $Id: AbstractConverter.java 1540518 2013-11-10 19:04:04Z oheger $
* @since 1.8.0
*/
public abstract class AbstractConverter implements Converter {
/** Debug logging message to indicate default value configuration */
private static final String DEFAULT_CONFIG_MSG =
"(N.B. Converters can be configured to use default values to avoid throwing exceptions)";
/** Current package name */
// getPackage() below returns null on some platforms/jvm versions during the unit tests.
// private static final String PACKAGE = AbstractConverter.class.getPackage().getName() + ".";
private static final String PACKAGE = "org.apache.commons.beanutils.converters.";
/**
* Logging for this instance.
*/
private transient Log log;
/**
* Should we return the default value on conversion errors?
*/
private boolean useDefault = false;
/**
* The default value specified to our Constructor, if any.
*/
private Object defaultValue = null;
// ----------------------------------------------------------- Constructors
/**
* Construct a Converter that throws a
* ConversionException if an error occurs.
*/
public AbstractConverter() {
}
/**
* Construct a Converter that returns a default
* value if an error occurs.
*
* @param defaultValue The default value to be returned
* if the value to be converted is missing or an error
* occurs converting the value.
*/
public AbstractConverter(Object defaultValue) {
setDefaultValue(defaultValue);
}
// --------------------------------------------------------- Public Methods
/**
* Indicates whether a default value will be returned or exception
* thrown in the event of a conversion error.
*
* @return true if a default value will be returned for
* conversion errors or false if a {@link ConversionException}
* will be thrown.
*/
public boolean isUseDefault() {
return useDefault;
}
/**
* Convert the input object into an output object of the
* specified type.
*
* @param the target type of the conversion
* @param type Data type to which this value should be converted
* @param value The input value to be converted
* @return The converted value.
* @throws ConversionException if conversion cannot be performed
* successfully and no default is specified.
*/
public T convert(Class type, Object value) {
if (type == null) {
return convertToDefaultType(type, value);
}
Class> sourceType = value == null ? null : value.getClass();
Class targetType = ConvertUtils.primitiveToWrapper(type);
if (log().isDebugEnabled()) {
log().debug("Converting"
+ (value == null ? "" : " '" + toString(sourceType) + "'")
+ " value '" + value + "' to type '" + toString(targetType) + "'");
}
value = convertArray(value);
// Missing Value
if (value == null) {
return handleMissing(targetType);
}
sourceType = value.getClass();
try {
// Convert --> String
if (targetType.equals(String.class)) {
return targetType.cast(convertToString(value));
// No conversion necessary
} else if (targetType.equals(sourceType)) {
if (log().isDebugEnabled()) {
log().debug(" No conversion required, value is already a "
+ toString(targetType));
}
return targetType.cast(value);
// Convert --> Type
} else {
Object result = convertToType(targetType, value);
if (log().isDebugEnabled()) {
log().debug(" Converted to " + toString(targetType) +
" value '" + result + "'");
}
return targetType.cast(result);
}
} catch (Throwable t) {
return handleError(targetType, value, t);
}
}
/**
* Convert the input object into a String.
*
* N.B.This implementation simply uses the value's
* toString() method and should be overriden if a
* more sophisticated mechanism for conversion to a String
* is required.
*
* @param value The input value to be converted.
* @return the converted String value.
* @throws Throwable if an error occurs converting to a String
*/
protected String convertToString(Object value) throws Throwable {
return value.toString();
}
/**
* Convert the input object into an output object of the
* specified type.
*
* Typical implementations will provide a minimum of
* String --> type conversion.
*
* @param Target type of the conversion.
* @param type Data type to which this value should be converted.
* @param value The input value to be converted.
* @return The converted value.
* @throws Throwable if an error occurs converting to the specified type
*/
protected abstract T convertToType(Class type, Object value) throws Throwable;
/**
* Return the first element from an Array (or Collection)
* or the value unchanged if not an Array (or Collection).
*
* N.B. This needs to be overriden for array/Collection converters.
*
* @param value The value to convert
* @return The first element in an Array (or Collection)
* or the value unchanged if not an Array (or Collection)
*/
protected Object convertArray(Object value) {
if (value == null) {
return null;
}
if (value.getClass().isArray()) {
if (Array.getLength(value) > 0) {
return Array.get(value, 0);
} else {
return null;
}
}
if (value instanceof Collection) {
Collection> collection = (Collection>)value;
if (collection.size() > 0) {
return collection.iterator().next();
} else {
return null;
}
}
return value;
}
/**
* Handle Conversion Errors.
*
* If a default value has been specified then it is returned
* otherwise a ConversionException is thrown.
*
* @param Target type of the conversion.
* @param type Data type to which this value should be converted.
* @param value The input value to be converted
* @param cause The exception thrown by the convert method
* @return The default value.
* @throws ConversionException if no default value has been
* specified for this {@link Converter}.
*/
protected T handleError(Class type, Object value, Throwable cause) {
if (log().isDebugEnabled()) {
if (cause instanceof ConversionException) {
log().debug(" Conversion threw ConversionException: " + cause.getMessage());
} else {
log().debug(" Conversion threw " + cause);
}
}
if (useDefault) {
return handleMissing(type);
}
ConversionException cex = null;
if (cause instanceof ConversionException) {
cex = (ConversionException)cause;
if (log().isDebugEnabled()) {
log().debug(" Re-throwing ConversionException: " + cex.getMessage());
log().debug(" " + DEFAULT_CONFIG_MSG);
}
} else {
String msg = "Error converting from '" + toString(value.getClass()) +
"' to '" + toString(type) + "' " + cause.getMessage();
cex = new ConversionException(msg, cause);
if (log().isDebugEnabled()) {
log().debug(" Throwing ConversionException: " + msg);
log().debug(" " + DEFAULT_CONFIG_MSG);
}
BeanUtils.initCause(cex, cause);
}
throw cex;
}
/**
* Handle missing values.
*
* If a default value has been specified, then it is returned (after a cast
* to the desired target class); otherwise a ConversionException is thrown.
*
* @param the desired target type
* @param type Data type to which this value should be converted.
* @return The default value.
* @throws ConversionException if no default value has been
* specified for this {@link Converter}.
*/
protected T handleMissing(Class type) {
if (useDefault || type.equals(String.class)) {
Object value = getDefault(type);
if (useDefault && value != null && !(type.equals(value.getClass()))) {
try {
value = convertToType(type, defaultValue);
} catch (Throwable t) {
throw new ConversionException("Default conversion to " + toString(type)
+ " failed.", t);
}
}
if (log().isDebugEnabled()) {
log().debug(" Using default "
+ (value == null ? "" : toString(value.getClass()) + " ")
+ "value '" + defaultValue + "'");
}
// value is now either null or of the desired target type
return type.cast(value);
}
ConversionException cex = new ConversionException("No value specified for '" +
toString(type) + "'");
if (log().isDebugEnabled()) {
log().debug(" Throwing ConversionException: " + cex.getMessage());
log().debug(" " + DEFAULT_CONFIG_MSG);
}
throw cex;
}
/**
* Set the default value, converting as required.
*
* If the default value is different from the type the
* Converter handles, it will be converted
* to the handled type.
*
* @param defaultValue The default value to be returned
* if the value to be converted is missing or an error
* occurs converting the value.
* @throws ConversionException if an error occurs converting
* the default value
*/
protected void setDefaultValue(Object defaultValue) {
useDefault = false;
if (log().isDebugEnabled()) {
log().debug("Setting default value: " + defaultValue);
}
if (defaultValue == null) {
this.defaultValue = null;
} else {
this.defaultValue = convert(getDefaultType(), defaultValue);
}
useDefault = true;
}
/**
* Return the default type this Converter handles.
*
* @return The default type this Converter handles.
*/
protected abstract Class> getDefaultType();
/**
* Return the default value for conversions to the specified
* type.
* @param type Data type to which this value should be converted.
* @return The default value for the specified type.
*/
protected Object getDefault(Class> type) {
if (type.equals(String.class)) {
return null;
} else {
return defaultValue;
}
}
/**
* Provide a String representation of this converter.
*
* @return A String representation of this converter
*/
@Override
public String toString() {
return toString(getClass()) + "[UseDefault=" + useDefault + "]";
}
// ----------------------------------------------------------- Package Methods
/**
* Accessor method for Log instance.
*
* The Log instance variable is transient and
* accessing it through this method ensures it
* is re-initialized when this instance is
* de-serialized.
*
* @return The Log instance.
*/
Log log() {
if (log == null) {
log = LogFactory.getLog(getClass());
}
return log;
}
/**
* Provide a String representation of a java.lang.Class.
* @param type The java.lang.Class.
* @return The String representation.
*/
String toString(Class> type) {
String typeName = null;
if (type == null) {
typeName = "null";
} else if (type.isArray()) {
Class> elementType = type.getComponentType();
int count = 1;
while (elementType.isArray()) {
elementType = elementType .getComponentType();
count++;
}
typeName = elementType.getName();
for (int i = 0; i < count; i++) {
typeName += "[]";
}
} else {
typeName = type.getName();
}
if (typeName.startsWith("java.lang.") ||
typeName.startsWith("java.util.") ||
typeName.startsWith("java.math.")) {
typeName = typeName.substring("java.lang.".length());
} else if (typeName.startsWith(PACKAGE)) {
typeName = typeName.substring(PACKAGE.length());
}
return typeName;
}
/**
* Performs a conversion to the default type. This method is called if we do
* not have a target class. In this case, the T parameter is not set.
* Therefore, we can cast to it (which is required to fulfill the contract
* of the method signature).
*
* @param the type of the result object
* @param targetClass the target class of the conversion
* @param value the value to be converted
* @return the converted value
*/
private T convertToDefaultType(Class targetClass, Object value) {
@SuppressWarnings("unchecked")
T result = (T) convert(getDefaultType(), value);
return result;
}
/**
* Generates a standard conversion exception with a message indicating that
* the passed in value cannot be converted to the desired target type.
*
* @param type the target type
* @param value the value to be converted
* @return a {@code ConversionException} with a standard message
* @since 1.9
*/
protected ConversionException conversionException(Class> type, Object value) {
return new ConversionException("Can't convert value '" + value
+ "' to type " + type);
}
}
././@LongLink 100644 0 0 146 12262571655 10267 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/ArrayConverter.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/ArrayConverter.jav100644 0 0 46646 12262570611 31106 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils.converters;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* Generic {@link Converter} implementation that handles conversion
* to and from array objects.
*
* Can be configured to either return a default value or throw a
* ConversionException if a conversion error occurs.
*
* The main features of this implementation are:
*
*
Element Conversion - delegates to a {@link Converter},
* appropriate for the type, to convert individual elements
* of the array. This leverages the power of existing converters
* without having to replicate their functionality for converting
* to the element type and removes the need to create a specifc
* array type converters.
*
Arrays or Collections - can convert from either arrays or
* Collections to an array, limited only by the capability
* of the delegate {@link Converter}.
*
Delimited Lists - can Convert to and from a
* delimited list in String format.
*
Conversion to String - converts an array to a
* String in one of two ways: as a delimited list
* or by converting the first element in the array to a String - this
* is controlled by the {@link ArrayConverter#setOnlyFirstToString(boolean)}
* parameter.
*
Multi Dimensional Arrays - it is possible to convert a String
* to a multi-dimensional arrays, by embedding {@link ArrayConverter}
* within each other - see example below.
*
Default Value
*
*
No Default - use the
* {@link ArrayConverter#ArrayConverter(Class, Converter)}
* constructor to create a converter which throws a
* {@link ConversionException} if the value is missing or
* invalid.
*
Default values - use the
* {@link ArrayConverter#ArrayConverter(Class, Converter, int)}
* constructor to create a converter which returns a default
* value. The defaultSize parameter controls the
* default value in the following way:
*
*
defaultSize < 0 - default is null
*
defaultSize = 0 - default is an array of length zero
*
defaultSize > 0 - default is an array with a
* length specified by defaultSize (N.B. elements
* in the array will be null)
*
*
*
*
*
Parsing Delimited Lists
* This implementation can convert a delimited list in String format
* into an array of the appropriate type. By default, it uses a comma as the delimiter
* but the following methods can be used to configure parsing:
*
*
setDelimiter(char) - allows the character used as
* the delimiter to be configured [default is a comma].
*
setAllowedChars(char[]) - adds additional characters
* (to the default alphabetic/numeric) to those considered to be
* valid token characters.
*
*
*
Multi Dimensional Arrays
* It is possible to convert a String to mulit-dimensional arrays by using
* {@link ArrayConverter} as the element {@link Converter}
* within another {@link ArrayConverter}.
*
* For example, the following code demonstrates how to construct a {@link Converter}
* to convert a delimited String into a two dimensional integer array:
*
*
* // Construct an Integer Converter
* IntegerConverter integerConverter = new IntegerConverter();
*
* // Construct an array Converter for an integer array (i.e. int[]) using
* // an IntegerConverter as the element converter.
* // N.B. Uses the default comma (i.e. ",") as the delimiter between individual numbers
* ArrayConverter arrayConverter = new ArrayConverter(int[].class, integerConverter);
*
* // Construct a "Matrix" Converter which converts arrays of integer arrays using
* // the pre-ceeding ArrayConverter as the element Converter.
* // N.B. Uses a semi-colon (i.e. ";") as the delimiter to separate the different sets of numbers.
* // Also the delimiter used by the first ArrayConverter needs to be added to the
* // "allowed characters" for this one.
* ArrayConverter matrixConverter = new ArrayConverter(int[][].class, arrayConverter);
* matrixConverter.setDelimiter(';');
* matrixConverter.setAllowedChars(new char[] {','});
*
* // Do the Conversion
* String matrixString = "11,12,13 ; 21,22,23 ; 31,32,33 ; 41,42,43";
* int[][] result = (int[][])matrixConverter.convert(int[][].class, matrixString);
*
*
* @version $Id: ArrayConverter.java 1540518 2013-11-10 19:04:04Z oheger $
* @since 1.8.0
*/
public class ArrayConverter extends AbstractConverter {
private final Class> defaultType;
private final Converter elementConverter;
private int defaultSize;
private char delimiter = ',';
private char[] allowedChars = new char[] {'.', '-'};
private boolean onlyFirstToString = true;
// ----------------------------------------------------------- Constructors
/**
* Construct an arrayConverter with the specified
* componentConverter that throws a
* ConversionException if an error occurs.
*
* @param defaultType The default array type this
* Converter handles
* @param elementConverter Converter used to convert
* individual array elements.
*/
public ArrayConverter(Class> defaultType, Converter elementConverter) {
super();
if (defaultType == null) {
throw new IllegalArgumentException("Default type is missing");
}
if (!defaultType.isArray()) {
throw new IllegalArgumentException("Default type must be an array.");
}
if (elementConverter == null) {
throw new IllegalArgumentException("Component Converter is missing.");
}
this.defaultType = defaultType;
this.elementConverter = elementConverter;
}
/**
* Construct an arrayConverter with the specified
* componentConverter that returns a default
* array of the specified size (or null) if an error occurs.
*
* @param defaultType The default array type this
* Converter handles
* @param elementConverter Converter used to convert
* individual array elements.
* @param defaultSize Specifies the size of the default array value or if less
* than zero indicates that a null default value should be used.
*/
public ArrayConverter(Class> defaultType, Converter elementConverter, int defaultSize) {
this(defaultType, elementConverter);
this.defaultSize = defaultSize;
Object defaultValue = null;
if (defaultSize >= 0) {
defaultValue = Array.newInstance(defaultType.getComponentType(), defaultSize);
}
setDefaultValue(defaultValue);
}
/**
* Set the delimiter to be used for parsing a delimited String.
*
* @param delimiter The delimiter [default ',']
*/
public void setDelimiter(char delimiter) {
this.delimiter = delimiter;
}
/**
* Set the allowed characters to be used for parsing a delimited String.
*
* @param allowedChars Characters which are to be considered as part of
* the tokens when parsing a delimited String [default is '.' and '-']
*/
public void setAllowedChars(char[] allowedChars) {
this.allowedChars = allowedChars;
}
/**
* Indicates whether converting to a String should create
* a delimited list or just convert the first value.
*
* @param onlyFirstToString true converts only
* the first value in the array to a String, false
* converts all values in the array into a delimited list (default
* is true
*/
public void setOnlyFirstToString(boolean onlyFirstToString) {
this.onlyFirstToString = onlyFirstToString;
}
/**
* Return the default type this Converter handles.
*
* @return The default type this Converter handles.
*/
@Override
protected Class> getDefaultType() {
return defaultType;
}
/**
* Handles conversion to a String.
*
* @param value The value to be converted.
* @return the converted String value.
* @throws Throwable if an error occurs converting to a String
*/
@Override
protected String convertToString(Object value) throws Throwable {
int size = 0;
Iterator> iterator = null;
Class> type = value.getClass();
if (type.isArray()) {
size = Array.getLength(value);
} else {
Collection> collection = convertToCollection(type, value);
size = collection.size();
iterator = collection.iterator();
}
if (size == 0) {
return (String)getDefault(String.class);
}
if (onlyFirstToString) {
size = 1;
}
// Create a StringBuffer containing a delimited list of the values
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < size; i++) {
if (i > 0) {
buffer.append(delimiter);
}
Object element = iterator == null ? Array.get(value, i) : iterator.next();
element = elementConverter.convert(String.class, element);
if (element != null) {
buffer.append(element);
}
}
return buffer.toString();
}
/**
* Handles conversion to an array of the specified type.
*
* @param Target type of the conversion.
* @param type The type to which this value should be converted.
* @param value The input value to be converted.
* @return The converted value.
* @throws Throwable if an error occurs converting to the specified type
*/
@Override
protected T convertToType(Class type, Object value) throws Throwable {
if (!type.isArray()) {
throw new ConversionException(toString(getClass())
+ " cannot handle conversion to '"
+ toString(type) + "' (not an array).");
}
// Handle the source
int size = 0;
Iterator> iterator = null;
if (value.getClass().isArray()) {
size = Array.getLength(value);
} else {
Collection> collection = convertToCollection(type, value);
size = collection.size();
iterator = collection.iterator();
}
// Allocate a new Array
Class> componentType = type.getComponentType();
Object newArray = Array.newInstance(componentType, size);
// Convert and set each element in the new Array
for (int i = 0; i < size; i++) {
Object element = iterator == null ? Array.get(value, i) : iterator.next();
// TODO - probably should catch conversion errors and throw
// new exception providing better info back to the user
element = elementConverter.convert(componentType, element);
Array.set(newArray, i, element);
}
@SuppressWarnings("unchecked")
// This is safe because T is an array type and newArray is an array of
// T's component type
T result = (T) newArray;
return result;
}
/**
* Returns the value unchanged.
*
* @param value The value to convert
* @return The value unchanged
*/
@Override
protected Object convertArray(Object value) {
return value;
}
/**
* Converts non-array values to a Collection prior
* to being converted either to an array or a String.
*
*
*
{@link Collection} values are returned unchanged
*
{@link Number}, {@link Boolean} and {@link java.util.Date}
* values returned as a the only element in a List.
*
All other types are converted to a String and parsed
* as a delimited list.
*
*
* N.B. The method is called by both the
* {@link ArrayConverter#convertToType(Class, Object)} and
* {@link ArrayConverter#convertToString(Object)} methods for
* non-array types.
*
* @param type The type to convert the value to
* @param value value to be converted
* @return Collection elements.
*/
protected Collection> convertToCollection(Class> type, Object value) {
if (value instanceof Collection) {
return (Collection>)value;
}
if (value instanceof Number ||
value instanceof Boolean ||
value instanceof java.util.Date) {
List list = new ArrayList(1);
list.add(value);
return list;
}
return parseElements(type, value.toString());
}
/**
* Return the default value for conversions to the specified
* type.
* @param type Data type to which this value should be converted.
* @return The default value for the specified type.
*/
@Override
protected Object getDefault(Class> type) {
if (type.equals(String.class)) {
return null;
}
Object defaultValue = super.getDefault(type);
if (defaultValue == null) {
return null;
}
if (defaultValue.getClass().equals(type)) {
return defaultValue;
} else {
return Array.newInstance(type.getComponentType(), defaultSize);
}
}
/**
* Provide a String representation of this array converter.
*
* @return A String representation of this array converter
*/
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(toString(getClass()));
buffer.append("[UseDefault=");
buffer.append(isUseDefault());
buffer.append(", ");
buffer.append(elementConverter.toString());
buffer.append(']');
return buffer.toString();
}
/**
*
Parse an incoming String of the form similar to an array initializer
* in the Java language into a List individual Strings
* for each element, according to the following rules.
*
*
The string is expected to be a comma-separated list of values.
*
The string may optionally have matching '{' and '}' delimiters
* around the list.
*
Whitespace before and after each element is stripped.
*
Elements in the list may be delimited by single or double quotes.
* Within a quoted elements, the normal Java escape sequences are valid.
*
*
* @param type The type to convert the value to
* @param value String value to be parsed
* @return List of parsed elements.
*
* @throws ConversionException if the syntax of svalue
* is not syntactically valid
* @throws NullPointerException if svalue
* is null
*/
private List parseElements(Class> type, String value) {
if (log().isDebugEnabled()) {
log().debug("Parsing elements, delimiter=[" + delimiter + "], value=[" + value + "]");
}
// Trim any matching '{' and '}' delimiters
value = value.trim();
if (value.startsWith("{") && value.endsWith("}")) {
value = value.substring(1, value.length() - 1);
}
try {
// Set up a StreamTokenizer on the characters in this String
StreamTokenizer st = new StreamTokenizer(new StringReader(value));
st.whitespaceChars(delimiter , delimiter); // Set the delimiters
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
st.wordChars('0', '9'); // Needed to make part of tokens
for (int i = 0; i < allowedChars.length; i++) {
st.ordinaryChars(allowedChars[i], allowedChars[i]);
st.wordChars(allowedChars[i], allowedChars[i]);
}
// Split comma-delimited tokens into a List
List list = null;
while (true) {
int ttype = st.nextToken();
if ((ttype == StreamTokenizer.TT_WORD) || (ttype > 0)) {
if (st.sval != null) {
if (list == null) {
list = new ArrayList();
}
list.add(st.sval);
}
} else if (ttype == StreamTokenizer.TT_EOF) {
break;
} else {
throw new ConversionException("Encountered token of type "
+ ttype + " parsing elements to '" + toString(type) + ".");
}
}
if (list == null) {
list = Collections.emptyList();
}
if (log().isDebugEnabled()) {
log().debug(list.size() + " elements parsed");
}
// Return the completed list
return (list);
} catch (IOException e) {
throw new ConversionException("Error converting from String to '"
+ toString(type) + "': " + e.getMessage(), e);
}
}
}
././@LongLink 100644 0 0 153 12262571655 10265 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/BigDecimalConverter.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/BigDecimalConverte100644 0 0 4767 12262570611 31025 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils.converters;
import java.math.BigDecimal;
/**
* {@link NumberConverter} implementation that handles conversion to
* and from java.math.BigDecimal objects.
*
* This implementation can be configured to handle conversion either
* by using BigDecimal's default String conversion, or by using a Locale's pattern
* or by specifying a format pattern. See the {@link NumberConverter}
* documentation for further details.
*
* Can be configured to either return a default value or throw a
* ConversionException if a conversion error occurs.
*
* @version $Id: BigDecimalConverter.java 1540186 2013-11-08 21:08:30Z oheger $
* @since 1.3
*/
public final class BigDecimalConverter extends NumberConverter {
/**
* Construct a java.math.BigDecimalConverter that throws
* a ConversionException if an error occurs.
*/
public BigDecimalConverter() {
super(true);
}
/**
* Construct a java.math.BigDecimalConverter that returns
* a default value if an error occurs.
*
* @param defaultValue The default value to be returned
* if the value to be converted is missing or an error
* occurs converting the value.
*/
public BigDecimalConverter(Object defaultValue) {
super(true, defaultValue);
}
/**
* Return the default type this Converter handles.
*
* @return The default type this Converter handles.
* @since 1.8.0
*/
@Override
protected Class getDefaultType() {
return BigDecimal.class;
}
}
././@LongLink 100644 0 0 153 12262571655 10265 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/BigIntegerConverter.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/BigIntegerConverte100644 0 0 4771 12262570611 31057 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils.converters;
import java.math.BigInteger;
/**
* {@link NumberConverter} implementation that handles conversion to
* and from java.math.BigInteger objects.
*
* This implementation can be configured to handle conversion either
* by using BigInteger's default String conversion, or by using a Locale's pattern
* or by specifying a format pattern. See the {@link NumberConverter}
* documentation for further details.
*
* Can be configured to either return a default value or throw a
* ConversionException if a conversion error occurs.
*
* @version $Id: BigIntegerConverter.java 1540186 2013-11-08 21:08:30Z oheger $
* @since 1.3
*/
public final class BigIntegerConverter extends NumberConverter {
/**
* Construct a java.math.BigIntegerConverter that throws
* a ConversionException if an error occurs.
*/
public BigIntegerConverter() {
super(false);
}
/**
* Construct a java.math.BigIntegerConverter that returns
* a default value if an error occurs.
*
* @param defaultValue The default value to be returned
* if the value to be converted is missing or an error
* occurs converting the value.
*/
public BigIntegerConverter(Object defaultValue) {
super(false, defaultValue);
}
/**
* Return the default type this Converter handles.
*
* @return The default type this Converter handles.
* @since 1.8.0
*/
@Override
protected Class getDefaultType() {
return BigInteger.class;
}
}
././@LongLink 100644 0 0 155 12262571655 10267 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/BooleanArrayConverter.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/BooleanArrayConver100644 0 0 22777 12262570611 31113 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
/**
*
Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
* String into a primitive array of boolean. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.
*
*
By default, the values to be converted are expected to be those
* recognised by a default instance of BooleanConverter. A customised
* BooleanConverter can be provided in order to recognise alternative values
* as true/false.
*
* @version $Id: BooleanArrayConverter.java 1454606 2013-03-08 22:30:51Z britter $
* @since 1.4
* @deprecated Replaced by the new {@link ArrayConverter} implementation
*/
@Deprecated
public final class BooleanArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link org.apache.commons.beanutils.Converter} that will throw
* a {@link ConversionException} if a conversion error occurs.
*
*
Conversion of strings to boolean values will be done via a default
* instance of class BooleanConverter.
*/
public BooleanArrayConverter() {
super();
this.booleanConverter = DEFAULT_CONVERTER;
}
/**
* Create a {@link org.apache.commons.beanutils.Converter} that will return
* the specified default value if a conversion error occurs.
*
*
Conversion of strings to boolean values will be done via a default
* instance of class BooleanConverter.
*
* @param defaultValue The default value to be returned
*/
public BooleanArrayConverter(Object defaultValue) {
super(defaultValue);
this.booleanConverter = DEFAULT_CONVERTER;
}
/**
* Create a {@link org.apache.commons.beanutils.Converter} that will return
* the specified default value if a conversion error occurs.
*
*
Conversion of strings to boolean values will be done via the
* specified converter.
*
* @param converter is the converter object that will be used to
* convert each input string-value into a boolean.
*
* @param defaultValue is the default value to be returned by method
* convert if conversion fails; null is a valid default value. See the
* documentation for method "convert" for more information.
* The value BooleanArrayConverter.NO_DEFAULT may be passed here to
* specify that an exception should be thrown on conversion failure.
*
*/
public BooleanArrayConverter(BooleanConverter converter, Object defaultValue) {
super(defaultValue);
this.booleanConverter = converter;
}
// ------------------------------------------------------- Static Variables
/**
* Type which this class converts its input to. This value can be
* used as a parameter to the ConvertUtils.register method.
* @since 1.8.0
*/
public static final Class MODEL = new boolean[0].getClass();
/**
* The converter that all instances of this class will use to
* do individual string->boolean conversions, unless overridden
* in the constructor.
*/
private static final BooleanConverter DEFAULT_CONVERTER
= new BooleanConverter();
// ---------------------------------------------------- Instance Variables
/**
* This object is used to perform the conversion of individual strings
* into Boolean/boolean values.
*/
protected final BooleanConverter booleanConverter;
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of type
* array-of-boolean.
*
*
If the input value is null, then the default value specified in the
* constructor is returned. If no such value was provided, then a
* ConversionException is thrown instead.
*
*
If the input value is of type String[] then the returned array shall
* be of the same size as this array, with a true or false value in each
* array element depending on the result of applying method
* BooleanConverter.convert to each string.
*
*
For all other types of value, the object's toString method is
* expected to return a string containing a comma-separated list of
* values, eg "true, false, true". See the documentation for
* {@link AbstractArrayConverter#parseElements} for more information on
* the exact formats supported.
*
*
If the result of value.toString() cannot be split into separate
* words, then the default value is also returned (or an exception thrown).
*
*
*
If any of the elements in the value array (or the elements resulting
* from splitting up value.toString) are not recognised by the
* BooleanConverter associated with this object, then what happens depends
* on whether that BooleanConverter has a default value or not: if it does,
* then that unrecognised element is converted into the BooleanConverter's
* default value. If the BooleanConverter does not have a default
* value, then the default value for this object is returned as the
* complete conversion result (not just for the element), or an
* exception is thrown if this object has no default value defined.
*
* @param type is the type to which this value should be converted. In the
* case of this BooleanArrayConverter class, this value is ignored.
*
* @param value is the input value to be converted.
*
* @return an object of type boolean[], or the default value if there was
* any sort of error during conversion and the constructor
* was provided with a default value.
*
* @exception ConversionException if conversion cannot be performed
* successfully and the constructor was not provided with a default
* value to return on conversion failure.
*
* @exception NullPointerException if value is an array, and any of the
* array elements are null.
*/
@Override
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Deal with the no-conversion-needed case
if (MODEL == value.getClass()) {
return (value);
}
// Deal with input value as a String array
//
// TODO: use if (value.getClass().isArray() instead...
// this requires casting to Object[], then using values[i].toString()
if (strings.getClass() == value.getClass()) {
try {
String[] values = (String[]) value;
boolean[] results = new boolean[values.length];
for (int i = 0; i < values.length; i++) {
String stringValue = values[i];
Object result = booleanConverter.convert(Boolean.class, stringValue);
results[i] = ((Boolean) result).booleanValue();
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
// We only get here if the input value is not of type String[].
// In this case, we assume value.toString() returns a comma-separated
// sequence of values; see method AbstractArrayConverter.parseElements
// for more information.
try {
List list = parseElements(value.toString());
boolean[] results = new boolean[list.size()];
for (int i = 0; i < results.length; i++) {
String stringValue = (String) list.get(i);
Object result = booleanConverter.convert(Boolean.class, stringValue);
results[i] = ((Boolean) result).booleanValue();
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
././@LongLink 100644 0 0 150 12262571655 10262 L ustar 0 0 commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/BooleanConverter.java commons-beanutils-1.9.1-src/src/main/java/org/apache/commons/beanutils/converters/BooleanConverter.j100644 0 0 23750 12262570611 31047 0 ustar 0 0 /*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils.converters;
/**
* {@link org.apache.commons.beanutils.Converter} implementation that handles conversion
* to and from Boolean objects.
* {@link org.apache.commons.beanutils.Converter} implementation that
* handles conversion to and from java.lang.Boolean objects.
*
* Can be configured to either return a default value or throw a
* ConversionException if a conversion error occurs.
*
* By default any object whose string representation is one of the values
* {"yes", "y", "true", "on", "1"} is converted to Boolean.TRUE, and
* string representations {"no", "n", "false", "off", "0"} are converted
* to Boolean.FALSE. The recognised true/false strings can be changed by:
*
Case is ignored when converting values to true or false.
*
* @version $Id: BooleanConverter.java 1540518 2013-11-10 19:04:04Z oheger $
* @since 1.3
*/
public final class BooleanConverter extends AbstractConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link org.apache.commons.beanutils.Converter} that will throw a
* {@link org.apache.commons.beanutils.ConversionException}
* if a conversion error occurs, ie the string value being converted is
* not one of the known true strings, nor one of the known false strings.
*/
public BooleanConverter() {
super();
}
/**
* Create a {@link org.apache.commons.beanutils.Converter} that will return the specified default value
* if a conversion error occurs, ie the string value being converted is
* not one of the known true strings, nor one of the known false strings.
*
* @param defaultValue The default value to be returned if the value
* being converted is not recognised. This value may be null, in which
* case null will be returned on conversion failure. When non-null, it is
* expected that this value will be either Boolean.TRUE or Boolean.FALSE.
* The special value BooleanConverter.NO_DEFAULT can also be passed here,
* in which case this constructor acts like the no-argument one.
*/
public BooleanConverter(Object defaultValue) {
super();
if (defaultValue != NO_DEFAULT) {
setDefaultValue(defaultValue);
}
}
/**
* Create a {@link org.apache.commons.beanutils.Converter} that will throw a
* {@link org.apache.commons.beanutils.ConversionException}
* if a conversion error occurs, ie the string value being converted is
* not one of the known true strings, nor one of the known false strings.
*
* The provided string arrays are copied, so that changes to the elements
* of the array after this call is made do not affect this object.
*
* @param trueStrings is the set of strings which should convert to the
* value Boolean.TRUE. The value null must not be present. Case is
* ignored.
*
* @param falseStrings is the set of strings which should convert to the
* value Boolean.TRUE. The value null must not be present. Case is
* ignored.
* @since 1.8.0
*/
public BooleanConverter(String[] trueStrings, String[] falseStrings) {
super();
this.trueStrings = copyStrings(trueStrings);
this.falseStrings = copyStrings(falseStrings);
}
/**
* Create a {@link org.apache.commons.beanutils.Converter} that will return
* the specified default value if a conversion error occurs.
*
* The provided string arrays are copied, so that changes to the elements
* of the array after this call is made do not affect this object.
*
* @param trueStrings is the set of strings which should convert to the
* value Boolean.TRUE. The value null must not be present. Case is
* ignored.
*
* @param falseStrings is the set of strings which should convert to the
* value Boolean.TRUE. The value null must not be present. Case is
* ignored.
*
* @param defaultValue The default value to be returned if the value
* being converted is not recognised. This value may be null, in which
* case null will be returned on conversion failure. When non-null, it is
* expected that this value will be either Boolean.TRUE or Boolean.FALSE.
* The special value BooleanConverter.NO_DEFAULT can also be passed here,
* in which case an exception will be thrown on conversion failure.
* @since 1.8.0
*/
public BooleanConverter(String[] trueStrings, String[] falseStrings,
Object defaultValue) {
super();
this.trueStrings = copyStrings(trueStrings);
this.falseStrings = copyStrings(falseStrings);
if (defaultValue != NO_DEFAULT) {
setDefaultValue(defaultValue);
}
}
// ----------------------------------------------------- Static Variables
/**
* This is a special reference that can be passed as the "default object"
* to the constructor to indicate that no default is desired. Note that
* the value 'null' cannot be used for this purpose, as the caller may
* want a null to be returned as the default.
* @deprecated Use constructors without default value.
*/
@Deprecated
public static final Object NO_DEFAULT = new Object();
// ----------------------------------------------------- Instance Variables
/**
* The set of strings that are known to map to Boolean.TRUE.
*/
private String[] trueStrings = {"true", "yes", "y", "on", "1"};
/**
* The set of strings that are known to map to Boolean.FALSE.
*/
private String[] falseStrings = {"false", "no", "n", "off", "0"};
// --------------------------------------------------------- Protected Methods
/**
* Return the default type this Converter handles.
*
* @return The default type this Converter handles.
* @since 1.8.0
*/
@Override
protected Class getDefaultType() {
return Boolean.class;
}
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param Target type of the conversion.
* @param type is the type to which this value should be converted. In the
* case of this BooleanConverter class, this value is ignored.
*
* @param value is the input value to be converted. The toString method
* shall be invoked on this object, and the result compared (ignoring
* case) against the known "true" and "false" string values.
*
* @return Boolean.TRUE if the value was a recognised "true" value,
* Boolean.FALSE if the value was a recognised "false" value, or
* the default value if the value was not recognised and the constructor
* was provided with a default value.
*
* @throws Throwable if an error occurs converting to the specified type
* @since 1.8.0
*/
@Override
protected T convertToType(Class type, Object value) throws Throwable {
if (Boolean.class.equals(type) || Boolean.TYPE.equals(type)) {
// All the values in the trueStrings and falseStrings arrays are
// guaranteed to be lower-case. By converting the input value
// to lowercase too, we can use the efficient String.equals method
// instead of the less-efficient String.equalsIgnoreCase method.
String stringValue = value.toString().toLowerCase();
for (int i = 0; i < trueStrings.length; ++i) {
if (trueStrings[i].equals(stringValue)) {
return type.cast(Boolean.TRUE);
}
}
for (int i = 0; i < falseStrings.length; ++i) {
if (falseStrings[i].equals(stringValue)) {
return type.cast(Boolean.FALSE);
}
}
}
throw conversionException(type, value);
}
/**
* This method creates a copy of the provided array, and ensures that
* all the strings in the newly created array contain only lower-case
* letters.
*
* Using this method to copy string arrays means that changes to the
* src array do not modify the dst array.
*/
private static String[] copyStrings(String[] src) {
String[] dst = new String[src.length];
for(int i=0; i