Path: blob/master/src/java.desktop/share/classes/com/sun/beans/decoder/FieldElementHandler.java
41171 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.beans.decoder;2526import com.sun.beans.finder.FieldFinder;2728import java.lang.reflect.Field;2930/**31* This class is intended to handle <field> element.32* This element simplifies access to the fields.33* If the {@code class} attribute is specified34* this element accesses static field of specified class.35* This element defines getter if it contains no argument.36* It returns the value of the field in this case.37* For example:<pre>38* <field name="TYPE" class="java.lang.Long"/></pre>39* is equivalent to {@code Long.TYPE} in Java code.40* This element defines setter if it contains one argument.41* It does not return the value of the field in this case.42* For example:<pre>43* <field name="id"><int>0</int></field></pre>44* is equivalent to {@code id = 0} in Java code.45* <p>The following attributes are supported:46* <dl>47* <dt>name48* <dd>the field name49* <dt>class50* <dd>the type is used for static fields only51* <dt>id52* <dd>the identifier of the variable that is intended to store the result53* </dl>54*55* @since 1.756*57* @author Sergey A. Malenkov58*/59final class FieldElementHandler extends AccessorElementHandler {60private Class<?> type;6162/**63* Parses attributes of the element.64* The following attributes are supported:65* <dl>66* <dt>name67* <dd>the field name68* <dt>class69* <dd>the type is used for static fields only70* <dt>id71* <dd>the identifier of the variable that is intended to store the result72* </dl>73*74* @param name the attribute name75* @param value the attribute value76*/77@Override78public void addAttribute(String name, String value) {79if (name.equals("class")) { // NON-NLS: the attribute name80this.type = getOwner().findClass(value);81} else {82super.addAttribute(name, value);83}84}8586/**87* Tests whether the value of this element can be used88* as an argument of the element that contained in this one.89*90* @return {@code true} if the value of this element should be used91* as an argument of the element that contained in this one,92* {@code false} otherwise93*/94@Override95protected boolean isArgument() {96return super.isArgument() && (this.type != null); // only static accessor can be used an argument97}9899/**100* Returns the context of the field.101* The context of the static field is the class object.102* The context of the non-static field is the value of the parent element.103*104* @return the context of the field105*/106@Override107protected Object getContextBean() {108return (this.type != null)109? this.type110: super.getContextBean();111}112113/**114* Returns the value of the field with specified {@code name}.115*116* @param name the name of the field117* @return the value of the specified field118*/119@Override120protected Object getValue(String name) {121try {122return getFieldValue(getContextBean(), name);123}124catch (Exception exception) {125getOwner().handleException(exception);126}127return null;128}129130/**131* Sets the new value for the field with specified {@code name}.132*133* @param name the name of the field134* @param value the new value for the specified field135*/136@Override137protected void setValue(String name, Object value) {138try {139setFieldValue(getContextBean(), name, value);140}141catch (Exception exception) {142getOwner().handleException(exception);143}144}145146/**147* Performs the search of the field with specified {@code name}148* in specified context and returns its value.149*150* @param bean the context bean that contains field151* @param name the name of the field152* @return the value of the field153* @throws IllegalAccessException if the field is not accesible154* @throws NoSuchFieldException if the field is not found155*/156static Object getFieldValue(Object bean, String name) throws IllegalAccessException, NoSuchFieldException {157return findField(bean, name).get(bean);158}159160/**161* Performs the search of the field with specified {@code name}162* in specified context and updates its value.163*164* @param bean the context bean that contains field165* @param name the name of the field166* @param value the new value for the field167* @throws IllegalAccessException if the field is not accesible168* @throws NoSuchFieldException if the field is not found169*/170private static void setFieldValue(Object bean, String name, Object value) throws IllegalAccessException, NoSuchFieldException {171findField(bean, name).set(bean, value);172}173174/**175* Performs the search of the field176* with specified {@code name} in specified context.177*178* @param bean the context bean that contains field179* @param name the name of the field180* @return field object that represents found field181* @throws NoSuchFieldException if the field is not found182*/183private static Field findField(Object bean, String name) throws NoSuchFieldException {184return (bean instanceof Class<?>)185? FieldFinder.findStaticField((Class<?>) bean, name)186: FieldFinder.findField(bean.getClass(), name);187}188}189190191