📄 mappingdefinition.java
字号:
mb.loadContext(UNMARSHALCONTEXT_CLASS); mb.appendLoadConstant(map.getIndex()); NameDefinition mname = map.getName(); if (mname == null) { mb.appendACONST_NULL(); mb.appendACONST_NULL(); } else { map.getName().genPushUriPair(mb); } mb.appendLoadConstant(map.getUnmarshaller().getName()); mb.appendCallVirtual(ADDUNMARSHALLER_METHOD, ADDUNMARSHALLER_SIGNATURE); } } } // load object and cast to type mb.loadObject(); mb.appendCreateCast(type); // handle the actual unmarshalling if (hasattr) { m_component.genAttributeUnmarshal(mb); } if (hasattr || !hasname) { mb.loadContext(UNMARSHALCONTEXT_CLASS); mb.appendCallVirtual(PARSERNEXT_METHOD, PARSERNEXT_SIGNATURE); mb.appendPOP(); } if (hascont) { m_component.genContentUnmarshal(mb); } // undefine unmarshallings for child mappings of this mapping if (maps != null && maps.size() > 0) { for (int i = 0; i < maps.size(); i++) { IMapping map = (IMapping)maps.get(i); if (!map.isAbstract() || map.isBase()) { mb.loadContext(UNMARSHALCONTEXT_CLASS); mb.appendLoadConstant(map.getIndex()); mb.appendCallVirtual(REMOVEUNMARSHALLER_METHOD, REMOVEUNMARSHALLER_SIGNATURE); } } } // finish by returning unmarshalled object reference mb.appendReturn("java.lang.Object"); mb.codeComplete(false); mb.addMethod(); // add interface if mapped class is directly unmarshallable if (hasname && m_class.getClassFile() == m_class.getMungedFile()) { addIUnmarshallableMethod(); } } /** * Generate the {@link * org.jibx.runtime.IUnmarshaller#isPresent(org.jibx.runtime.IUnmarshallingContext)} * method implementation. * * @param cf class to receive method * @param hasname element name defined by this mapping flag * @throws JiBXException */ private void generateIsPresent(ClassFile cf, boolean hasname) throws JiBXException { // create the method builder ContextMethodBuilder mb = new ContextMethodBuilder (ISPRESENT_METHODNAME, Type.BOOLEAN, ISPRESENT_METHOD_ARGS, cf, Constants.ACC_PUBLIC|Constants.ACC_FINAL, -1, null, 1, UNMARSHALCONTEXT_INTERFACE); // generate name comparison unless an abstract mapping if (hasname) { // test if at defined element name mb.addException(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS); mb.loadContext(); m_name.genPushUriPair(mb); mb.appendCallInterface(UNMARSHAL_ISATMETHOD, UNMARSHAL_ISATSIGNATURE); } // check for extension mapping handling required if (m_extensions != null) { // return immediately if this mapping name check successful BranchWrapper ifthis = null; if (hasname) { ifthis = mb.appendIFNE(this); } // build code to check each extension mapping in turn; // return "true" if one matches, or "false" if none do mb.addException(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS); BranchWrapper[] iffounds = new BranchWrapper[m_extensions.size()]; for (int i = 0; i < iffounds.length; i++) { IMapping map = (IMapping)m_extensions.get(i); mb.loadContext(); mb.appendLoadConstant(map.getIndex()); mb.appendCallInterface(GETUNMARSHALLER_METHOD, GETUNMARSHALLER_SIGNATURE); mb.loadContext(); mb.appendCallInterface(UNMARSHALLERPRESENT_METHOD, UNMARSHALLERPRESENT_SIGNATURE); iffounds[i] = mb.appendIFNE(this); } mb.appendICONST_0(); mb.appendReturn("int"); mb.initStackState(iffounds[0]); BranchTarget found = mb.appendTargetLoadConstant(1); if (ifthis != null) { ifthis.setTarget(found, mb); } for (int i = 0; i < iffounds.length; i++) { iffounds[i].setTarget(found, mb); } } else if (!hasname) { // mapping with no separate element name, just return "true" mb.appendICONST_1(); } mb.appendReturn("int"); mb.codeComplete(false); mb.addMethod(); } /** * Generate the {@link org.jibx.runtime.IMarshaller#marshal(Object, * org.jibx.runtime.IMarshallingContext)} method implementation. * * @param cf class to receive method * @param hasattr attribute definition present flag * @param hascont content definition present flag * @throws JiBXException */ private void generateMarshalImplementation(ClassFile cf, boolean hasattr, boolean hascont) throws JiBXException { // build the marshal implementation method; this loads the // passed object and casts it to the target type, then handles // marshalling first attributes and followed by content for the // item ContextMethodBuilder mb = new ContextMethodBuilder (MARSHAL_METHODNAME, Type.VOID, MARSHAL_METHOD_ARGS, cf, Constants.ACC_PUBLIC|Constants.ACC_FINAL, 1, "java.lang.Object", 2, MARSHALCONTEXT_INTERFACE); mb.addException(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS); // TODO: optionally check for null value on object // define marshallings for child mappings of this mapping ArrayList maps = m_defContext.getMappings(); if (maps != null && maps.size() > 0) { for (int i = 0; i < maps.size(); i++) { IMapping map = (IMapping)maps.get(i); if (!map.isAbstract() || map.isBase()) { mb.loadContext(MARSHALCONTEXT_CLASS); mb.appendLoadConstant(map.getIndex()); mb.appendLoadConstant(map.getMarshaller().getName()); mb.appendCallVirtual(ADDMARSHALLER_METHOD, ADDMARSHALLER_SIGNATURE); } } } // handle the actual marshalling if (hasattr || hascont) { mb.loadObject(m_class.getClassName()); if (hasattr) { if (hascont) { mb.appendDUP(); } m_component.genAttributeMarshal(mb); } if (hasattr || m_isAbstract) { mb.loadContext(MARSHALCONTEXT_CLASS); mb.appendCallVirtual(CLOSESTART_METHOD, CLOSESTART_SIGNATURE); mb.appendPOP(); } if (hascont) { m_component.genContentMarshal(mb); } } // undefine marshallings for child mappings of this mapping if (maps != null && maps.size() > 0) { for (int i = 0; i < maps.size(); i++) { IMapping map = (IMapping)maps.get(i); if (!map.isAbstract() || map.isBase()) { mb.loadContext(MARSHALCONTEXT_CLASS); mb.appendLoadConstant(map.getIndex()); mb.appendCallVirtual(REMOVEMARSHALLER_METHOD, REMOVEMARSHALLER_SIGNATURE); } } } // finish with plain return mb.appendReturn(); mb.codeComplete(false); mb.addMethod(); } public void generateCode(boolean force) throws JiBXException { // first call code generation for child mappings m_defContext.generateCode(false, false); if (!force && m_isAbstract && m_extensions == null) { return; } // create the helper class BindingDefinition def = m_container.getBindingRoot(); String init = m_class.deriveClassName(def.getPrefix(), ADAPTERCLASS_SUFFIX); String name = init; int loop = 0; while (ClassCache.hasClassFile(name)) { name = init + ++loop; } ClassFile base = ClassCache.getClassFile("java.lang.Object"); String[] intfs = def.isInput() ? (def.isOutput() ? BOTH_INTERFACES : UNMARSHALLER_INTERFACES) : MARSHALLER_INTERFACES; ClassFile cf = new ClassFile(name, m_class.getMungedFile().getRoot(), base, Constants.ACC_PUBLIC, intfs); cf.addDefaultConstructor(); // add marshaller/unmarshaller implementation methods boolean hasattr = m_component.hasAttribute(); boolean hascont = m_component.hasContent(); boolean hasname = !m_isAbstract && m_name != null; if (def.isInput()) { generateIsPresent(cf, hasname); generateUnmarshalImplementation(cf, hasattr, hascont, hasname); } if (def.isOutput()) { // generate the basic method generateMarshalImplementation(cf, hasattr, hascont); generateIfExtendingCheck(cf, hasname); // add interface if mapped class is directly marshallable if (hasname && m_class.getClassFile() == m_class.getMungedFile()) { addIMarshallableMethod(); } // check for mapping with extensions to add extra method and // interface if (m_extensions != null) { generateAbstractMarshaller(cf); } } // add as generated class m_marshaller = m_unmarshaller = MungedClass.getUniqueSupportClass(cf); } public NameDefinition getWrapperName() { return m_name; } public void setLinkages() throws JiBXException { m_component.setLinkages(); m_defContext.setLinkages(); if (m_name != null) { m_name.fixNamespace(m_defContext); } } // DEBUG public void print(int depth) { BindingDefinition.indent(depth); System.out.print("mapping class " + m_class.getClassFile().getName()); if (m_name != null) { System.out.print(" to element " + m_name.toString()); } System.out.print(" (#" + getIndex() + ')'); if (m_baseMapping != null) { System.out.print(" extends " + m_baseMapping.getBoundType()); } if (m_isAbstract) { if (m_extensions != null) { System.out.print(" (abstract, " + m_extensions.size() + " extensions)"); } else { System.out.print(" (abstract)"); } } System.out.println(); m_defContext.print(depth+1); m_component.print(depth+1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -