?? propertydefinition.java
字號:
/*Copyright (c) 2003-2007, Dennis M. SosnoskiAll rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.binding.def;import java.util.ArrayList;import org.jibx.binding.classes.BranchWrapper;import org.jibx.binding.classes.ClassFile;import org.jibx.binding.classes.ClassItem;import org.jibx.binding.classes.ContextMethodBuilder;import org.jibx.binding.classes.MethodBuilder;import org.jibx.binding.model.ClassUtils;import org.jibx.binding.util.IntegerCache;import org.jibx.runtime.JiBXException;/** * Property definition from binding. This organizes shared information for * bindings linked to fields or get/set methods of an object, and provides * methods for related code generation. * * @author Dennis M. Sosnoski */public class PropertyDefinition{ // // Constants and such related to code generation. // recognized test-method signatures. private static final String[] TEST_METHOD_SIGNATURES = { "(Lorg/jibx/runtime/IMarshallingContext;)Z", "()Z" }; // recognized get-method signatures. private static final String[] GET_METHOD_SIGNATURES = { "(Lorg/jibx/runtime/IMarshallingContext;)", "()" }; // // Actual instance data /** Reference to "this" property of object flag. */ private boolean m_isThis; /** Reference to implicit value from collection. */ private boolean m_isImplicit; /** Optional item flag. */ private boolean m_isOptional; /** Containing object context. */ private final IContextObj m_objContext; /** Fully qualified name of actual type of value. */ private final String m_typeName; /** Fully qualified name of declared type of value loaded. */ private final String m_getValueType; /** Fully qualified name of declared type of value stored. */ private final String m_setValueType; /** Information for field (if given, may be <code>null</code>). */ private final ClassItem m_fieldItem; /** Information for test method (if given, may be <code>null</code>). */ private final ClassItem m_testMethod; /** Information for get method (if given, may be <code>null</code>). */ private final ClassItem m_getMethod; /** Information for set method (if given, may be <code>null</code>). */ private final ClassItem m_setMethod; /** * Constructor. * * @param parent containing binding definition structure * @param obj containing object context * @param type fully qualified name of type * @param isthis "this" object reference flag * @param opt optional property flag * @param fname containing object field name for property (may be * <code>null</code>) * @param test containing object method to test for property present (may be * <code>null</code>) * @param get containing object method to get property value (may be * <code>null</code>) * @param set containing object method to set property value (may be * <code>null</code>) * @throws JiBXException if configuration error */ public PropertyDefinition(IContainer parent, IContextObj obj, String type, boolean isthis, boolean opt, String fname, String test, String get, String set) throws JiBXException { m_objContext = obj; m_isThis = isthis; m_isOptional = opt; ClassFile cf = m_objContext.getBoundClass().getClassFile(); m_isImplicit = false; String dtype = null; String gtype = null; String stype = null; if (isthis) { if (type == null) { dtype = gtype = stype = cf.getName(); } else { dtype = gtype = stype = type; } } if (fname == null) { m_fieldItem = null; } else { m_fieldItem = cf.getField(fname); dtype = gtype = stype = m_fieldItem.getTypeName(); } if (test == null) { m_testMethod = null; } else { if (opt) { m_testMethod = cf.getMethod(test, TEST_METHOD_SIGNATURES); if (m_testMethod == null) { throw new JiBXException("test-method " + test + " not found in class " + cf.getName()); } } else { throw new JiBXException ("Test method only allowed for optional properties"); } } if (get == null) { m_getMethod = null; } else { m_getMethod = cf.getMethod(get, GET_METHOD_SIGNATURES); if (m_getMethod == null) { throw new JiBXException("get-method " + get + " not found in class " + cf.getName()); } else { gtype = m_getMethod.getTypeName(); if (dtype == null) { dtype = gtype; } } } if (set == null) { m_setMethod = null; } else { // need to handle overloads, so generate possible signatures ArrayList sigs = new ArrayList(); if (m_getMethod != null) { String psig = ClassUtils.getSignature(gtype); sigs.add("(" + psig + "Lorg/jibx/runtime/IUnmarshallingContext;" + ")V"); sigs.add("(" + psig + ")V"); } if (type != null) { String psig = ClassUtils.getSignature(type); sigs.add("(" + psig + "Lorg/jibx/runtime/IUnmarshallingContext;" + ")V"); sigs.add("(" + psig + ")V"); } if (m_fieldItem != null) { String psig = m_fieldItem.getSignature(); sigs.add("(" + psig + "Lorg/jibx/runtime/IUnmarshallingContext;" + ")V"); sigs.add("(" + psig + ")V"); } sigs.add ("(Ljava/lang/Object;Lorg/jibx/runtime/IUnmarshallingContext;)V"); sigs.add("(Ljava/lang/Object;)V"); // set method needs verification of argument and return type ClassItem setmeth = cf.getMethod(set, (String[])sigs.toArray(new String[0])); if (setmeth == null) { // nothing known about signature, try anything by name setmeth = cf.getMethod(set, ""); if (setmeth != null) { if (setmeth.getArgumentCount() != 1 || !setmeth.getTypeName().equals("void")) { setmeth = null; } } } // check if method found m_setMethod = setmeth; if (m_setMethod == null) { throw new JiBXException("set-method " + set + " not found in class " + cf.getName()); } else { stype = m_setMethod.getArgumentType(0); if (dtype == null) { dtype = stype; } } } if (gtype == null) { gtype = "java.lang.Object"; } m_getValueType = gtype; m_setValueType = stype; // check that enough information is supplied BindingDefinition root = parent.getBindingRoot(); if (!isthis && m_fieldItem == null) { if (root.isInput() && m_setMethod == null) { throw new JiBXException ("Missing way to set value for input binding"); } if (root.isOutput() && m_getMethod == null) { throw new JiBXException ("Missing way to get value for output binding"); } } // check that type information is consistent if (type == null) { m_typeName = dtype; } else { m_typeName = type; boolean valid = true; if (isthis) { valid = ClassItem.isAssignable(dtype, type); } else { if (root.isInput()) { valid = ClassItem.isAssignable(type, m_setValueType) || ClassItem.isAssignable(m_setValueType, type); } if (valid && root.isOutput()) { valid = ClassItem.isAssignable(type, m_getValueType) || ClassItem.isAssignable(m_getValueType, type); } } if (!valid) { throw new JiBXException ("Incompatible types for property definition"); } } } /** * Constructor for "this" object reference. * * @param obj containing object context * @param opt optional property flag */ public PropertyDefinition(IContextObj obj, boolean opt) { m_objContext = obj; m_isThis = true; m_isImplicit = false; m_isOptional = opt; ClassFile cf = m_objContext.getBoundClass().getClassFile(); m_fieldItem = m_testMethod = m_getMethod = m_setMethod = null; m_typeName = m_getValueType = m_setValueType = cf.getName(); } /** * Constructor for implicit object reference. * * @param type object type supplied * @param obj containing object context * @param opt optional property flag */ public PropertyDefinition(String type, IContextObj obj, boolean opt) { m_objContext = obj; m_isImplicit = true; m_isThis = false; m_isOptional = opt; m_fieldItem = m_testMethod = m_getMethod = m_setMethod = null; m_typeName = m_getValueType = m_setValueType = type; } /** * Check if property is "this" reference for object. * * @return <code>true</code> if reference to "this", <code>false</code> if * not */ public boolean isThis() { return m_isThis; } /** * Check if property is implicit value from collection. *
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -