primitivestringconversion.java
来自「对xml很好的java处理引擎,编译中绑定xml」· Java 代码 · 共 512 行 · 第 1/2 页
JAVA
512 行
* 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 { // choose between custom deserializer or standard built-in method if (m_isUnmarshalText) { // 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 generate call to deserializer genFromText(mb); } else { // generated instruction sequence just calls the appropriate // unmarshalling context method to get the value as a primitive mb.appendCall(attr ? m_unmarshalReqAttribute : m_unmarshalReqElement); } } /** * 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 { // set instructions based on value size if (m_valueType == LONG_TYPE || m_valueType == DOUBLE_TYPE) { mb.appendDUP2(); } else { mb.appendDUP(); } extra++; // first add code to check if the value is different from the default, // by duplicating the value, pushing the default, and executing the // appropriate branch comparison // TODO: this should not be done inline, but necessary for now Object value = m_default; if (m_isUnmarshalText) { try { String mname = m_deserializer.getName(); String cname = m_deserializer.getClassFile().getName(); Class clas = ClassFile.loadClass(cname); if (clas == null) { throw new JiBXException("Deserializer class " + cname + " not found for converting default value"); } else { // try first to find a declared method, then a public one Method meth; try { meth = clas.getDeclaredMethod(mname, SINGLE_STRING_ARGS); meth.setAccessible(true); } catch (NoSuchMethodException ex) { meth = clas.getMethod(mname, SINGLE_STRING_ARGS); } String text; if (value instanceof String || value == null) { text = (String)value; } else { text = value.toString(); } value = meth.invoke(null, new Object[] { text }); } } catch (IllegalAccessException ex) { throw new JiBXException("Conversion method not accessible", ex); } catch (InvocationTargetException ex) { throw new JiBXException("Internal error", ex); } catch (NoSuchMethodException ex) { throw new JiBXException("Internal error", ex); } } mb.appendLoadConstant(value); BranchWrapper ifne = null; switch (m_valueType) { case LONG_TYPE: mb.appendLCMP(); break; case FLOAT_TYPE: mb.appendFCMPG(); break; case DOUBLE_TYPE: mb.appendDCMPG(); break; default: ifne = mb.appendIF_ICMPNE(this); break; } if (ifne == null) { ifne = mb.appendIFNE(this); } // generate code for branch not taken case, popping the value from // stack along with extra parameters, and branching past using code genPopValues(extra, mb); BranchWrapper toend = mb.appendUnconditionalBranch(this); mb.targetNext(ifne); genToText(m_stackType, mb); return toend; } /** * Convert text representation into default value object. This override of * the base class method uses reflection to call the actual deserialization * method, returning the wrapped result value. If a custom deserializer is * defined this just returns the <code>String</code> value directly. * * @param text value representation to be converted * @return converted default value object * @throws JiBXException on conversion error */ protected Object convertDefault(String text) throws JiBXException { if (!m_isUnmarshalText) { try { String mname = m_deserializer.getName(); String cname = m_deserializer.getClassFile().getName(); Class clas = ClassFile.loadClass(cname); if (clas == null) { throw new JiBXException("Deserializer class " + cname + " not found for converting default value"); } else { // try first to find a declared method, then a public one Method meth; try { meth = clas.getDeclaredMethod(mname, SINGLE_STRING_ARGS); meth.setAccessible(true); } catch (NoSuchMethodException ex) { meth = clas.getMethod(mname, SINGLE_STRING_ARGS); } return meth.invoke(null, new Object[] { text }); } } catch (IllegalAccessException ex) { throw new JiBXException("Conversion method not accessible", ex); } catch (InvocationTargetException ex) { throw new JiBXException("Internal error", ex); } catch (NoSuchMethodException ex) { throw new JiBXException("Internal error", ex); } } else { return text; } } /** * Check if the type handled by this conversion is of a primitive type. * * @return <code>true</code> to indicate primitive type */ public boolean isPrimitive() { return true; } /** * Set serializer for conversion. This override of the base class method * sets a flag to indicate that values must be converted to text before * they are written to a document after executing the base class processing. * * @param ser fully qualified class and method name of serializer * @throws JiBXException if serializer not found or not usable */ protected void setSerializer(String ser) throws JiBXException { super.setSerializer(ser); m_isMarshalText = true; } /** * Set deserializer for conversion. This override of the base class method * sets a flag to indicate that values must be read from a document as text * and converted as a separate step after executing the base class * processing. * * @param deser fully qualified class and method name of deserializer * @throws JiBXException if deserializer not found or not usable */ protected void setDeserializer(String deser) throws JiBXException { super.setDeserializer(deser); m_isUnmarshalText = true; } /** * 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 PrimitiveStringConversion(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 + -
显示快捷键?