mappingdefinition.java

来自「JiBX是一个为Java提供的XML数据绑定框架。它可以和现存的类一起运行」· Java 代码 · 共 811 行 · 第 1/3 页

JAVA
811
字号
        return m_referenceType;    }        public IComponent getImplComponent() {        return m_component;    }        public ClassFile getMarshaller() {        return m_marshaller;    }        public ClassFile getUnmarshaller() {        return m_unmarshaller;    }        public NameDefinition getName() {        return m_name;    }    public void addNamespace(NamespaceDefinition ns) throws JiBXException {        m_defContext.addNamespace(ns);    }    public boolean isAbstract() {        return m_isAbstract;    }    public boolean isBase() {        return m_extensions != null && m_extensions.size() > 0;    }    public void addExtension(MappingDefinition mdef) throws JiBXException {        if (m_extensions == null) {            m_extensions = new ArrayList();        }        if (!m_extensions.contains(mdef)) {            m_extensions.add(mdef);        }        ClassFile cf = mdef.getBoundClass().getClassFile();        if (!cf.isSuperclass(m_referenceType) &&            !cf.isImplements(m_referenceType)) {            m_referenceType = "java.lang.Object";        }    }        public IComponent buildRef(IContainer parent, IContextObj objc, String type,        PropertyDefinition prop) throws JiBXException {        if (prop.isThis()) {                        // directly incorporate base mapping definition            return new BaseMappingWrapper(m_thisBinding);                    } else if (m_isAbstract && m_extensions == null) {                        // create reference to use mapping definition directly            return new ComponentProperty(prop, m_component, false);                    } else {                        // create link to mapping definition            DirectObject dobj = new DirectObject(m_container, null,                m_class.getClassFile(), m_isAbstract || m_extensions != null,                m_marshaller, m_unmarshaller, getIndex(), null);            return new DirectProperty(prop, dobj);                    }    }        public void generateCode(boolean force) throws JiBXException {                // TODO: Split this sucker up!                // 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 name = m_class.deriveClassName(def.getPrefix(),            ADAPTERCLASS_SUFFIX);        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 unmarshaller access methods        boolean hasattr = m_component.hasAttribute();        boolean hascont = m_component.hasContent();        boolean hasname = !m_isAbstract && m_name != null;        if (def.isInput()) {                        // build the is present test method for item            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();                        // build the unmarshal method for item; this just generates code            //  to unmarshal attributes and content, first creating an            //  instance of the class if one was not passed in, then            //  returning the unmarshalled instance as the value of the call            String type = m_class.getClassName();            mb = new ContextMethodBuilder(UNMARSHAL_METHODNAME,                Type.OBJECT, UNMARSHAL_METHOD_ARGS, cf,                Constants.ACC_PUBLIC|Constants.ACC_FINAL, 1, type,                2, UNMARSHALCONTEXT_INTERFACE);            mb.addException(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS);                        // first part of generated code just checks if an object has            //  been supplied; if it has, this can just go direct to            //  unmarshalling            mb.loadObject();            BranchWrapper ifnnull = mb.appendIFNONNULL(this);                        // check for extension mapping handling required            if (m_extensions != null) {                                // generate name comparison unless an abstract mapping                BranchWrapper ifthis = null;                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);                    ifthis = mb.appendIFNE(this);                }                                // build code to check each extension mapping in turn,                //  keeping an instance of the unmarshaller for the matching                //  extension                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.appendDUP();                    mb.loadContext();                    mb.appendCallInterface(UNMARSHALLERPRESENT_METHOD,                        UNMARSHALLERPRESENT_SIGNATURE);                    iffounds[i] = mb.appendIFNE(this);                    mb.appendPOP();                }                                // generate code to throw exception if no matching extension                //  found                mb.appendCreateNew("java.lang.StringBuffer");                mb.appendDUP();                mb.appendLoadConstant("Element ");                mb.appendCallInit("java.lang.StringBuffer",                    "(Ljava/lang/String;)V");                mb.appendDUP();                mb.loadContext(UNMARSHALCONTEXT_CLASS);                mb.appendCallVirtual(CURRENTELEMENT_METHOD,                    CURRENTELEMENT_SIGNATURE);                mb.appendCallVirtual("java.lang.StringBuffer.append",                     "(Ljava/lang/String;)Ljava/lang/StringBuffer;");                mb.appendDUP();                mb.appendLoadConstant(" has no mapping that extends " +                    m_class.getClassName());                mb.appendCallVirtual("java.lang.StringBuffer.append",                     "(Ljava/lang/String;)Ljava/lang/StringBuffer;");                mb.appendCallVirtual("java.lang.StringBuffer.toString",                    "()Ljava/lang/String;");                mb.appendCreateNew(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS);                mb.appendDUP_X1();                mb.appendSWAP();                mb.appendCallInit(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS,                    MethodBuilder.EXCEPTION_CONSTRUCTOR_SIGNATURE1);                mb.appendThrow();                if (iffounds.length > 0) {                                    // finish by calling unmarshaller for extension mapping                    //  found and returning the result with no further                    //  processing                    mb.initStackState(iffounds[0]);                    BranchTarget found = mb.appendTargetACONST_NULL();                    for (int i = 0; i < iffounds.length; i++) {                        iffounds[i].setTarget(found, mb);                    }                    mb.loadContext();                    mb.appendCallInterface(UNMARSHALLERUNMARSHAL_METHOD,                        UNMARSHALLERUNMARSHAL_SIGNATURE);                    mb.appendReturn("java.lang.Object");                }                                // fall into instance creation if this mapping reference                if (ifthis != null) {                    mb.targetNext(ifthis);                }                            } else if (m_isAbstract) {                                // throw an exception when no instance supplied                mb.appendCreateNew(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS);                mb.appendDUP();                mb.appendLoadConstant("Abstract mapping requires instance to " +                    "be supplied for class " + m_class.getClassName());                mb.appendCallInit(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS,                    MethodBuilder.EXCEPTION_CONSTRUCTOR_SIGNATURE1);                mb.appendThrow();                            }            if (hasname) {

⌨️ 快捷键说明

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