Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] - نسخه متنی

Larne Pekowsky

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







8.8. Dynamic Beans


BeanUtils can manage collections of arbitrary name/value pairs of data through the use of mapped properties. This approaches the idea of a dynamic bean with properties that may be defined on the fly. A Map- based bean falls short of this idea in a couple of ways. First, there is no check on the names. If a bean should have a property called "muse" and someone mistypes the name as "moose" in a set command, the result will be the creation of the "moose" property instead of an error. The second shortcoming of Map- based beans is that there is no check or conversion of types. A track could be given a live annotation by setting the value to Boolean.TRUE. If another line of code attempted to set the value to the string "true," there would be be neither an error nor an automatic conversion to a Boolean. This could cause problems down the line if code is expecting live annotations to be of a particular type.

While both these defects could be fixed by using a genuine bean, such an approach also has difficulties. Notably, a new class must be written and compiled, and if it is changed, the application may need to be restarted.

BeanUtils bridges the gap between Java beans and mapped beans with objects called

DynaBeans. DynaBeans are instances of a

DynaClass, which contain DynaProperties. A DynaProperty has both a name and a type, eliminating most of the defects of mapped beans.

A new DynaClass can be created by defining its name and set of properties. The following code could be used to define a set of annotations for tracks that can indicate whether a track is live, the name of a guest vocalist if present, and an array of other artists who have covered the song:

DynaProperty[] props = new DynaProperty[] {
new DynaProperty("live", Boolean.class),
new DynaProperty("guestVocalist", String.class),
new DynaProperty("coveredBy", Artist[].class),
};
DynaClass trackAnnotation =
new BasicDynaClass("TrackAnnotation", null, props);

The BasicDynaClass is constructed with a name and an array of properties. The second argument, which is null in the preceding example, is a class that will serve as the implementation. A null value means the default implementation will be used, which is BasicDynaBean.

Once the class has been defined, an instance can be constructed with


DynaBean annotation = trackAnnotation.newInstance();

Clearly this means that the annotation property in the track class should be defined as a DynaBean instead of a HashMap. However, the definition and construction of the DynaBean should not be done in TRack because this would defeat the whole point of making it dynamic. Instead, any program that uses track should set up the annotations as appropriate.

Although there are methods in DynaBean to get and set properties, the intended use is to allow PropertyUtils to do all the work. All the PropertyUtils methods act on DynaBeans in exactly the same way they act on regular JavaBeans. For example,

PropertyUtils.getProperty(track,
"annotation.coveredBy[0].name");

will return the name of the first artist to cover the song, and

PropertyUtils.setProperty(track,
"annotation.live",
"true");

will mark the track as live, correctly calling a converter to translate from the string "true" to Boolean.TRUE.


/ 207