📄 stringconversion.java
字号:
// String method String name = attr ? MARSHAL_ATTRIBUTE : MARSHAL_ELEMENT; mb.appendCallVirtual(name, MARSHAL_SIGNATURE); } /** * Generate code to pop values from stack. * * @param count number of values to be popped * @param mb method builder */ public void genPopValues(int count, ContextMethodBuilder mb) { while (--count >= 0) { if (mb.isStackTopLong()) { mb.appendPOP2(); } else { mb.appendPOP(); } } } /** * Generate code to check if an optional value is not equal to the default. * This abstract base class method must be implemented by every subclass. * 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 words 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 abstract BranchWrapper genToOptionalText(String type, ContextMethodBuilder mb, int extra) throws JiBXException; /** * Generate code to convert value to a <code>String</code>. 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. * * @param type fully qualified class name for value on stack * @param mb method builder * @throws JiBXException if error in configuration */ public void genToText(String type, ContextMethodBuilder mb) throws JiBXException { // check if a serializer is used for this type if (m_serializer != null) { // just generate call to the serializer (adding any checked // exceptions thrown by the serializer to the list needing // handling) if (!isPrimitive()) { mb.appendCreateCast(type, m_serializer.getArgumentType(0)); } mb.addMethodExceptions(m_serializer); if (m_serializer.getArgumentCount() > 1) { mb.loadContext(); } mb.appendCall(m_serializer); } else { // make sure this is a string mb.appendCreateCast(type, "java.lang.String"); } } /** * Generate code to convert and write optional value to generated document. * The generated code first tests if the value is the same as the supplied * default, and if so skips writing. The code assumes that the marshalling * context, the name information, and the actual value to be converted have * already been pushed on the stack. It consumes these, leaving only the * marshalling context on the stack. * * @param attr item is an attribute (vs element) flag * @param type fully qualified class name for value on stack * @param mb method builder * @throws JiBXException if error in configuration */ public void genWriteOptional(boolean attr, String type, ContextMethodBuilder mb) throws JiBXException { // start with code to convert value to String, if it's not equal to the // default value BranchWrapper toend = genToOptionalText(type, mb, MARSHAL_NAME_VALUES); // next use standard write code, followed by targeting branch genWriteText(attr, mb); if (toend != null) { mb.targetNext(toend); } } /** * Generate code to convert and write required value to generated document. * The code generated by this method assumes that the marshalling context, * the name information, and the actual value to be converted have already * been pushed on the stack. It consumes these, leaving the returned * marshalling context on the stack. * * @param attr item is an attribute (vs element) flag * @param type fully qualified class name for value on stack * @param mb method builder * @throws JiBXException if error in configuration */ public void genWriteRequired(boolean attr, String type, ContextMethodBuilder mb) throws JiBXException { // generate code to convert to text, followed by code to marshal text genToText(type, mb); genWriteText(attr, mb); } /** * Check if the type handled by this conversion is of a primitive type. * * @return <code>true</code> if a primitive type, <code>false</code> if an * object type */ public abstract boolean isPrimitive(); /** * Set serializer for conversion. This finds the named static method and * sets it as the serializer to be used for this conversion. The serializer * method is expected to take a single argument of either the handled * type or a superclass or interface of the handled type, and to return a * <code>String</code> result. * * @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 { // build all possible signature variations String[] tsigs = ClassItem.getSignatureVariants(m_typeName); String[] msigs = new String[tsigs.length*2]; for (int i = 0; i < tsigs.length; i++) { msigs[i*2] = "(" + tsigs[i] + ")Ljava/lang/String;"; msigs[i*2+1] = "(" + tsigs[i] + "Lorg/jibx/runtime/IMarshallingContext;)Ljava/lang/String;"; } // find a matching static method ClassItem method = ClassItem.findStaticMethod(ser, msigs); // report error if method not found if (method == null) { throw new JiBXException("Serializer " + ser + " not found"); } else { m_serializer = method; } } /** * Set deserializer for conversion. This finds the named static method and * sets it as the deserializer to be used for this conversion. The * deserializer method is expected to take a single argument of type * <code>String</code>, and to return a value of the handled type or a * subtype of that type. * * @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 { // find a matching static method ClassItem method = ClassItem.findStaticMethod(deser, DESERIALIZER_SIGNATURES); // report error if method not found or incompatible if (method == null) { throw new JiBXException("Deserializer " + deser + " not found"); } else if (ClassItem.isAssignable(method.getTypeName(), m_typeName)) { m_deserializer = method; } else { throw new JiBXException("Deserializer " + deser + " returns wrong type"); } } /** * Convert text representation into default value object. Each subclass * must implement this with the appropriate conversion handling. * * @param text value representation to be converted * @return converted default value object * @throws JiBXException on conversion error */ protected abstract Object convertDefault(String text) throws JiBXException; /** * Derive from existing formatting information. This abstract base class * method must be implemented by every subclass. It 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 abstract StringConversion derive(String type, String ser, String dser, String dflt) throws JiBXException;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -