objectstringconversion.java
来自「JiBX是一个为Java提供的XML数据绑定框架。它可以和现存的类一起运行」· Java 代码 · 共 394 行 · 第 1/2 页
JAVA
394 行
* Generate code to parse and convert optional attribute or element. The * code generated by this method assumes that the unmarshalling context * and name information for the attribute or element have already * been pushed on the stack. It consumes these and leaves the converted * value (or converted default value, if the item itself is missing) on * the stack. * * @param attr item is an attribute (vs element) flag * @param mb method builder * @throws JiBXException if error in configuration */ public void genParseOptional(boolean attr, ContextMethodBuilder mb) throws JiBXException { // first part of generated instruction sequence is to push the default // value, then call the appropriate unmarshalling context method to get // the value as a String mb.appendLoadConstant((String)m_default); String name = attr ? UNMARSHAL_OPT_ATTRIBUTE : UNMARSHAL_OPT_ELEMENT; mb.appendCallVirtual(name, UNMARSHAL_OPT_SIGNATURE); // second part is to actually convert to an instance of the type genFromText(mb); } /** * Generate code to parse and convert required attribute or element. The * code generated by this method assumes that the unmarshalling context and * name information for the attribute or element have already been pushed * on the stack. It consumes these and leaves the converted value on the * stack. * * @param attr item is an attribute (vs element) flag * @param mb method builder * @throws JiBXException if error in configuration */ public void genParseRequired(boolean attr, ContextMethodBuilder mb) throws JiBXException { // first part of generated instruction sequence is a call to the // appropriate unmarshalling context method to get the value as a // String String name = attr ? UNMARSHAL_REQ_ATTRIBUTE : UNMARSHAL_REQ_ELEMENT; mb.appendCallVirtual(name, UNMARSHAL_REQ_SIGNATURE); // second part is to actually convert to an instance of the type genFromText(mb); } /** * Shared code generation for converting instance of type to * <code>String</code>. This override of the base class method checks for * serialization using the <code>toString</code> method and implements that * case directly, while calling the base class method for normal handling. * The code generated by this method assumes that the reference to the * instance to be converted is on the stack. It consumes the reference, * replacing it with the corresponding <code>String</code> value. * * @param type fully qualified class name for value on stack * @param mb marshal method builder * @throws JiBXException if error in configuration */ public void genToText(String type, ContextMethodBuilder mb) throws JiBXException { if (m_serializer == null && m_needSerialize) { // report error if no handling available if (m_instToString == null) { throw new JiBXException("No serializer for " + m_typeName + "; define serializer or toString() method"); } else { // generate code to call toString() method of instance (adding // any checked exceptions thrown by the method to the list // needing handling) mb.addMethodExceptions(m_instToString); mb.appendCall(m_instToString); } } else { super.genToText(type, mb); } } /** * Generate code to check if an optional value is not equal to the default. * The code generated by this method assumes that the actual value to be * converted has already been pushed on the stack. It consumes this, * leaving the converted text reference on the stack if it's not equal to * the default value. * * @param type fully qualified class name for value on stack * @param mb method builder * @param extra count of extra values to be popped from stack if missing * @return handle for branch taken when value is equal to the default * (target must be set by caller) * @throws JiBXException if error in configuration */ protected BranchWrapper genToOptionalText(String type, ContextMethodBuilder mb, int extra) throws JiBXException { // check if the default value is just null if (m_default == null) { // generate code to call the serializer and get String value on // stack genToText(type, mb); return null; } else { // start with code to call the serializer and get the String value // on stack genToText(type, mb); // add code to check if the serialized text is different from the // default, by duplicating the returned reference, pushing the // default, and calling the object comparison method. This is // followed by a branch if the comparison says they're not equal // (nonzero result, since it's a boolean value). mb.appendDUP(); mb.appendLoadConstant((String)m_default); mb.appendCallStatic(COMPARE_OBJECTS_METHOD, COMPARE_OBJECTS_SIGNATURE); BranchWrapper iffalse = mb.appendIFEQ(this); // finish by discarding copy of object reference on stack when // equal to the default genPopValues(extra+1, mb); BranchWrapper toend = mb.appendUnconditionalBranch(this); mb.targetNext(iffalse); return toend; } } /** * Check if the type handled by this conversion is of a primitive type. * * @return <code>false</code> to indicate object type */ public boolean isPrimitive() { return false; } /** * Convert text representation into default value object. For object types * this just returns the text value. * * @param text value representation to be converted * @return converted default value object * @throws JiBXException on conversion error */ protected Object convertDefault(String text) throws JiBXException { return text; } /** * Derive from existing formatting information. This allows constructing * a new instance from an existing format of the same or an ancestor * type, with the properties of the existing format copied to the new * instance except where overridden by the supplied values. * * @param type fully qualified name of class handled by conversion * @param ser fully qualified name of serialization method * (<code>null</code> if inherited) * @param dser fully qualified name of deserialization method * (<code>null</code> if inherited) * @param dflt default value text (<code>null</code> if inherited) * @return new instance initialized from existing one * @throws JiBXException if error in configuration information */ public StringConversion derive(String type, String ser, String dser, String dflt) throws JiBXException { if (type == null) { type = m_typeName; } StringConversion inst = new ObjectStringConversion(type, this); if (ser != null) { inst.setSerializer(ser); } if (dser != null) { inst.setDeserializer(dser); } if (dflt != null) { inst.m_default = inst.convertDefault(dflt); } return inst; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?