⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectstringconversion.java

📁 对xml很好的java处理引擎,编译中绑定xml
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*Copyright (c) 2003-2004, 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 org.jibx.binding.classes.*;import org.jibx.runtime.JiBXException;/** * Object string conversion handling. Defines serialization handling for * converting objects to and from a <code>String</code> value. The default is * to just use the object <code>toString()</code> method for serialization and * a constructor from a <code>String</code> value for deserialization. * <code>java.lang.String</code> itself is a special case, with no added code * used by default for either serializing or deserializing. * <code>java.lang.Object</code> is also a special case, with no added code * used by default for deserializing (the <code>String</code> value is used * directly). Other classes must either implement <code>toString()</code> and * a constructor from <code>String</code>, or use custom serializers and/or * deserializers. * * @author Dennis M. Sosnoski * @version 1.0 */public class ObjectStringConversion extends StringConversion{    //    // Constants for code generation.        private static final String TOSTRING_METHOD = "toString";    private static final String TOSTRING_SIGNATURE =        "()Ljava/lang/String;";    private static final String FROMSTRING_SIGNATURE =        "(Ljava/lang/String;)V";    //    // Actual instance data        /** Flag for conversion from <code>String</code> needed (type is anything     other than <code>String</code> or <code>Object</code>) */    private boolean m_needDeserialize;        /** Initializer used for creating instance from <code>String</code>     (only used if no conversion needed and no deserializer supplied;     may be <code>null</code>) */    private ClassItem m_initFromString;        /** Flag for conversion to <code>String</code> needed (type is anything     other than <code>String</code>) */    private boolean m_needSerialize;        /** <code>toString()</code> method for converting instance to     <code>String</code> (only used if conversion needed and no serializer     supplied; may be <code>null</code>) */    private ClassItem m_instToString;        /**     * Constructor. Initializes conversion handling based on the supplied     * inherited handling.     *     * @param type fully qualified name of class handled by conversion     * @param inherit conversion information inherited by this conversion     * @throws JiBXException if error in configuration     */    /*package*/ ObjectStringConversion(String type,        ObjectStringConversion inherit)        throws JiBXException {        super(type, inherit);        if (type.equals(inherit.m_typeName)) {            m_needDeserialize = inherit.m_needDeserialize;            m_initFromString = inherit.m_initFromString;            m_needSerialize = inherit.m_needSerialize;            m_instToString = inherit.m_instToString;        } else {            initMethods();        }    }    /**     * Constructor. Initializes conversion handling based on argument values.     * This form is only used for constructing the default set of conversions.     * Because of this, it throws an unchecked exception on error.     *     * @param dflt default value object (wrapped value for primitive types,     * otherwise <code>String</code>)     * @param ser fully qualified name of serialization method     * @param deser fully qualified name of deserialization method     * @param type fully qualified name of class handled by conversion     */    /*package*/ ObjectStringConversion(Object dflt, String ser, String deser,         String type) {        super(dflt, ser, deser, type);        try {            initMethods();        } catch (JiBXException ex) {            throw new IllegalArgumentException(ex.getMessage());        }    }    /**     * Initialize methods used for conversion of types without serializer or     * deserializer. Sets flags for types needed, with errors thrown at time     * of attempted use rather than at definition time. That offers the     * advantages of simpler handling (we don't need to know which directions     * are supported in a binding) and more flexibility (can support nested     * partial definitions cleanly).     */    private void initMethods() throws JiBXException {        if (!"java.lang.String".equals(m_typeName)) {            m_needSerialize = true;            m_needDeserialize = !"java.lang.Object".equals(m_typeName);            ClassFile cf = ClassCache.getClassFile(m_typeName);            m_initFromString = cf.getInitializerMethod(FROMSTRING_SIGNATURE);            m_instToString = cf.getMethod(TOSTRING_METHOD, TOSTRING_SIGNATURE);        }    }    /**     * Generate code to convert <code>String</code> representation. The     * code generated by this method assumes that the <code>String</code>     * value has already been pushed on the stack. It consumes this and     * leaves the converted value on the stack.     *     * @param mb method builder     */    public void genFromText(ContextMethodBuilder mb) throws JiBXException {                // first generate code to duplicate value and check for null, with        //  duplicate replaced by explicit null if already null (confusing        //  in the bytecode, but will be optimized out by any native code        //  generation)        mb.appendDUP();        BranchWrapper ifnnull = mb.appendIFNONNULL(this);        mb.appendPOP();        mb.appendACONST_NULL();        BranchWrapper toend = mb.appendUnconditionalBranch(this);        mb.targetNext(ifnnull);                // check if a deserializer is used for this type        if (m_deserializer != null) {                        // just generate call to the deserializer (adding any checked            //  exceptions thrown by the deserializer to the list needing            //  handling)            mb.addMethodExceptions(m_deserializer);            if (m_deserializer.getArgumentCount() > 1) {                mb.loadContext();            }            mb.appendCall(m_deserializer);                    } else if (m_initFromString != null) {                        // generate code to create an instance of object and pass text value            //  to constructor            mb.appendCreateNew(m_typeName);            mb.appendDUP_X1();            mb.appendSWAP();            mb.appendCallInit(m_typeName, FROMSTRING_SIGNATURE);                    } else if (m_needDeserialize) {            throw new JiBXException("No deserializer for " + m_typeName +                 "; define deserializer or constructor from java.lang.String");        }                // finish by setting target for null case branch        mb.targetNext(toend);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -