Implementation of Apache Beanutils copy Properties

Source: Internet
Author: User

First say the pit:

    1. For boxed Boolean types, you cannot use the combination of ISABC and Setabc , only with the combination of Getabc and setabc (for any non-basic type, All the same.)
    2. Beanutils 's beanutils.copyproperties is not a member, but a get and set method, which is the property, for basic Boolean is to see is and set

This article environment:

Java Version "1.8.0"
Java (TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot (TM) 64-bit Server VM (build 25.0-b70, Mixed mode)

Commons-beanutils 1.9.2

Realize:

Get BeanInfo by class(BeanInfo = Introspector.getbeaninfo (Icontext.gettargetclass ());

specific code in org.apache.commons.beanutils.DefaultBeanIntrospector.introspect (Introspectioncontext icontext) Line ,

Many of the properties described in BeanInfo are as follows:

For the description of the method in methods, the description of the property is in propertiesdescripter .

The final copy is a copy of the call to Readmethodname and writemethodname through reflection, and a type check before copying.

Attention:

The acquisition of a property is a Private member variable that is not directly reflected through Java's introspector. Introspector This class Eclipse cannot track temporary variables at debug time, for unknown reasons ...

If use is and set for a boxed Boolean type , the following occurs:

Readmethodname is a null ...

see introspector Properties The source code is as follows (in introspector. Gettargetpropertyinfoline:507):

try {if (Argcount = = 0) {if (Name.startswith (Get_prefix)) {//Simple getter PD = new Pr        Opertydescriptor (This.beanclass, name.substring (3), method, NULL); } else if (Resulttype = = Boolean.class && name.startswith (is_prefix)) {//This above is lowercase boolean.class not boo Lean.class, refers to the base type of the Boolean//Boolean getter PD = new PropertyDescriptor (This.beanclass, name.subs        Tring (2), method, NULL);            }} else if (Argcount = = 1) {if (Int.class.equals (argtypes[0]) && Name.startswith (Get_prefix)) {        PD = new Indexedpropertydescriptor (This.beanclass, name.substring (3), NULL, NULL, method, NULL);            } else if (Void.class.equals (resulttype) && Name.startswith (Set_prefix)) {//Simple setter            PD = new PropertyDescriptor (This.beanclass, name.substring (3), null, method); if (Throwsexception (method, Propertyvetoexception.class)) {Pd.setconstRained (true); }}} else if (Argcount = = 2) {if (Void.class.equals (resulttype) && int.class.equals (Argtypes[0] ) && Name.startswith (Set_prefix)) {PD = new Indexedpropertydescriptor (This.beanclass, name.substring (3            ), NULL, NULL, NULL, method);            if (Throwsexception (method, Propertyvetoexception.class)) {pd.setconstrained (true); }}}} catch (Introspectionexception ex) {//This happens if a propertydescriptor or indexedpropertydescript  or//constructor fins that the method violates details of the DEISGN//pattern, e.g. by has an empty name, or a    Getter returning//void, or whatever. PD = NULL;}}

  

The principle is to cut the string and then splice the property according to set and get/is and set .

Java specifications for Java Beans can be downloaded here:

The property has the following description:

7 Properties

Properties are discrete, named attributes of a Java beans that can affect their appearance or its behaviour.

...

Properties show up in a number of ways:

1. Properties exposed in scripting environments as though they were

Objects. So in a Javascript environment I might does "B.label = foo" To set the value of a

Property.

2. Properties can be accessed programmatically by other components calling their getter

and setter methods (see section 7.1 below).

3. As part of the process of customizing a component (see section 9), it properties may

Be presented in a property sheet for a user to edit.

4. Typically a bean's properties would be persistent, so then their state would be stored away

As part of the persistent state of the bean.

Properties can have arbitrary types, including both built-in Java types such as "int" and class

or interfaces types such as "Java.awt.Color".

...

7.1 Accessor Methods

Properties is always accessed via method calls on their owning object. For readable properties

There'll is a getter method to read the property value. For writable properties there would be a

Setter method to allow the property value to be updated. Thus even when a script writer types

In something such as "B.label = foo" There was still a method call into the target object to set the

property, and the target object have full programmatic control.

So properties need not just is simple data fields, they can actually is computed values. Updates

May has various programmatic side effects. For example, changing a bean ' s background color

Property might also cause the bean is repainted with the new color.

For simple properties The accessor type signatures is:

void Setfoo (PropertyType value); Simple setter

PropertyType Getfoo (); Simple getter

Getfoo and Setfoo are simply example names. Accessor methods can have arbitrary names.

However for standard naming conventions for accessor methods see the design patterns described

In section 8.3.

...

8.3 Design Patterns for Properties

8.3.1 Simple Properties

By default, we use design patterns to locate properties by looking for methods of the form:

Public <PropertyType> get<propertyname> ();

public void set<propertyname> (<PropertyType> a);

If we discover a matching pair of "get<propertyname>" and "set<propertyname>" methods

That take and return the same type, then we regard these methods as defining a read-write property

Whose name would be "<propertyName>". We'll use the "get<propertyname>" method

To get the property value and the ' set<propertyname> ' method to set the property value. The

Pair of methods May is located either in the same class or one is in a base class and the

Other is in a derived class.

If we find only one of the these methods, then we regard it as defining either a read-only or a writeonly

Property called "<propertyName>"

By default we assume this properties is neither bound nor constrained (see section 7).

So a simple Read-write property "Foo" might is represented by a pair of methods:

Public Wombat Getfoo ();

public void Setfoo (Wombat W);

8.3.2 Boolean Properties

In addition, for Boolean properties, we allow a getter method to match the pattern:

public boolean is<propertyname> ();

This "is<propertyname>" method is provided instead of a "get<propertyname>" method,

Or it is provided in addition to a "get<propertyname>" method.

In either case, if the "is<propertyname>" method was present for a Boolean property then we'll

Use the ' is<propertyname> ' method to read the property value.

An example Boolean property might is:

public boolean ismarsupial ();

public void Setmarsupial (Boolean m);

Next : figure out what the hell this boolean.class is ... (You may want to look at the JVM specification)

Implementation of Apache Beanutils copy Properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.