Package org.jibx.binding.classes

Examples of org.jibx.binding.classes.ContextMethodBuilder


     * @param hasname element name defined by this mapping flag
     */
    private void generateIsPresent(ClassFile cf, boolean hasname) {
       
        // 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.getMappingName());
                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();
    }
View Full Code Here


       
        // 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);
       
        // 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.getMappingName());
                    mb.appendLoadConstant(map.getMarshaller().getName());
                    mb.appendCallVirtual(ADDMARSHALLER_METHOD,
                        ADDMARSHALLER_SIGNATURE);
                }
            }
        }
        if (hasattr || hascont) {
           
            // start by pushing namespace translation for precompiled binding
            mb.loadContext();
            mb.appendLoadConstant(m_container.getBindingRoot().getFactoryName());
            mb.appendCallInterface(PUSH_NAMESPACES_METHOD,
                PUSH_NAMESPACES_SIG);
           
            // handle the actual marshalling
            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);
            }
           
            // pop namespace translation for precompiled binding
            mb.loadContext();
            mb.appendCallInterface(POP_NAMESPACES_METHOD,
                POP_NAMESPACES_SIG);
        }
       
        // 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.getMappingName());
                    mb.appendCallVirtual(REMOVEMARSHALLER_METHOD,
                        REMOVEMARSHALLER_SIGNATURE);
                }
            }
        }
       
        // finish with plain return
        mb.appendReturn();
        mb.codeComplete(false);
        mb.addMethod();
    }
View Full Code Here

     * @param cf handler class
     */
    private void generateAbstractMarshaller(ClassFile cf) {
       
        // build the implementation method
        ContextMethodBuilder mb = new ContextMethodBuilder(BASEMARSHAL_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
        mb.loadContext();
        mb.loadObject();
        mb.appendCallVirtual("java.lang.Object.getClass",
            "()Ljava/lang/Class;");
        mb.appendCallVirtual("java.lang.Class.getName", "()Ljava/lang/String;");
        mb.appendCallInterface(GETMARSHALLER_METHOD, GETMARSHALLER_SIGNATURE);
        mb.appendDUP();
        mb.appendLoadConstant(getMappingName());
        mb.appendCallInterface(CHECKEXTENDS_FULLNAME, CHECKEXTENDS_SIGNATURE);
        BranchWrapper ifvalid = mb.appendIFNE(this);
        
        // generate and throw exception describing the problem
        mb.appendCreateNew("java.lang.StringBuffer");
        mb.appendDUP();
        mb.appendLoadConstant("Mapping for type ");
        mb.appendCallInit("java.lang.StringBuffer", "(Ljava/lang/String;)V");
        mb.appendDUP();
        mb.loadObject();
        mb.appendCallVirtual("java.lang.Object.getClass",
            "()Ljava/lang/Class;");
        mb.appendCallVirtual("java.lang.Class.getName", "()Ljava/lang/String;");
        mb.appendCallVirtual("java.lang.StringBuffer.append",
            "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
        mb.appendDUP();
        mb.appendLoadConstant(" must extend abstract mapping for " +
            "type " + 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();
        
        // for valid extension mapping, just call the marshaller
        mb.targetNext(ifvalid);
        mb.loadObject();
        mb.loadContext();
        mb.appendCallInterface(MARSHALLERMARSHAL_METHOD,
            MARSHALLERMARSHAL_SIGNATURE);
        mb.appendReturn();
        mb.codeComplete(false);
        mb.addMethod();
       
        // add extended interface to constructed class
        cf.addInterface(ABSTRACTMARSHALLER_INTERFACE);
    }
View Full Code Here

        // 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();
        ContextMethodBuilder 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.getMappingName());
                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) {
           
            // just create an instance of the (non-abstract) mapped class
            mb.appendACONST_NULL();
            genNewInstance(mb);
            mb.storeObject();
           
        }
       
        // define unmarshallings for child mappings of this mapping
        mb.targetNext(ifnnull);
        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(UNMARSHALCONTEXT_CLASS);
                    mb.appendLoadConstant(map.getMappingName());
                    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.getMappingName());
                    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();
        }
View Full Code Here

  protected void addIMarshallableMethod() throws JiBXException {
     
      // set up for constructing actual marshal method
        BoundClass clas = getBoundClass();
      ClassFile cf = clas.getMungedFile();
      ContextMethodBuilder mb = new ContextMethodBuilder
          (MARSHALLABLE_METHODNAME, MARSHALLABLE_SIGNATURE, cf,
          Constants.ACC_PUBLIC, 0, clas.getClassFile().getName(),
          1, MARSHALLER_INTERFACE);
     
      // create call to marshalling context method with class name
      mb.loadContext();
      mb.appendLoadConstant(cf.getName());
      mb.appendCallInterface(GETMARSHALLER_METHOD, GETMARSHALLER_SIGNATURE);
     
      // call the returned marshaller with this object and the marshalling
      //  context as parameters
      mb.loadObject();
      mb.loadContext();
      mb.appendCallInterface(MARSHALLERMARSHAL_METHOD,
          MARSHALLERMARSHAL_SIGNATURE);
      mb.appendReturn();
     
      // add method to class
      clas.getUniqueNamed(mb);
     
      // generate and add get mapping name method
View Full Code Here

  protected void addIUnmarshallableMethod() throws JiBXException {
     
      // set up for constructing new method
        BoundClass clas = getBoundClass();
      ClassFile cf = clas.getMungedFile();
      ContextMethodBuilder mb = new ContextMethodBuilder
          (UNMARSHALLABLE_METHODNAME, UNMARSHALLABLE_SIGNATURE, cf,
          Constants.ACC_PUBLIC, 0, clas.getClassFile().getName(),
          1, UNMARSHALLER_INTERFACE);
     
      // create call to unmarshalling context method with class name
      mb.loadContext();
        mb.appendLoadConstant(cf.getName());
      mb.appendCallInterface(GETUNMARSHALLER_METHOD,
          GETUNMARSHALLER_SIGNATURE);
     
      // call the returned unmarshaller with this object and the unmarshalling
      //  context as parameters
      mb.loadObject();
      mb.loadContext();
      mb.appendCallInterface(UNMARSHALLERUNMARSHAL_METHOD,
          UNMARSHALLERUNMARSHAL_SIGNATURE);
      mb.appendReturn();
     
      // add the method to class
      clas.getUniqueNamed(mb);
       
        // generate and add get mapping name method
View Full Code Here

            // set up for constructing new method
            String name = m_container.getBindingRoot().getPrefix() +
                NEWINSTANCE_SUFFIX;
            ClassFile cf = m_class.getMungedFile();
            Type type = m_class.getClassFile().getType();
            ContextMethodBuilder mb = new ContextMethodBuilder(name, type,
                new Type[] {type, UNMARSHALCONTEXT_TYPE}, cf,
                Constants.ACC_PUBLIC|Constants.ACC_STATIC, 0,
                m_class.getClassName(), 1, UNMARSHALLING_CONTEXT);
           
            // check if actually need to create a new instance
            mb.loadObject();
            BranchWrapper haveinst = mb.appendIFNONNULL(this);
           
            // check for factory supplied to create instance
            boolean haveobj = true;
            if (m_factoryMethod == null) {
                if (m_createClass.isArray()) {
                   
                    // construct array instance directly with basic size
                    mb.appendLoadConstant(Utility.MINIMUM_GROWN_ARRAY_SIZE);
                    String tname = m_createClass.getName();
                    mb.appendCreateArray(tname.substring(0, tname.length()-2));
                   
                } else if (m_createClass.isInterface() ||
                    m_createClass.isAbstract()) {
                   
                    // cannot create instance, throw exception
                    mb.appendCreateNew(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS);
                    mb.appendDUP();
                    mb.appendLoadConstant("Cannot create instance of interface or abstract class " +
                        m_createClass.getName());
                    mb.appendCallInit(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS,
                        MethodBuilder.EXCEPTION_CONSTRUCTOR_SIGNATURE1);
                    mb.appendThrow();
                    haveobj = false;
                   
                } else {
                   
                    // check if we have a no argument constructor
                    ClassItem cons = m_createClass.getInitializerMethod("()V");
                    if (cons == null) {
                        if (m_container.getBindingRoot().isAddConstructors()) {
                            m_createClass.addDefaultConstructor();
                        } else {
                           
                            // cannot create instance, throw exception
                            mb.appendCreateNew(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS);
                            mb.appendDUP();
                            mb.appendLoadConstant("Cannot create instance of class " +
                                m_createClass.getName() + " (no default constructor)");
                            mb.appendCallInit(MethodBuilder.FRAMEWORK_EXCEPTION_CLASS,
                                MethodBuilder.EXCEPTION_CONSTRUCTOR_SIGNATURE1);
                            mb.appendThrow();
                            haveobj = false;
                           
                        }
                    } else {
                        cons.makeAccessible(m_class.getMungedFile());
                    }
                    if (haveobj) {
                       
                        // no factory, so create an instance, duplicate the
                        //  reference, and then call the null constructor
                        mb.appendCreateNew(m_createClass.getName());
                        mb.appendDUP();
                        mb.appendCallInit(m_createClass.getName(),"()V");
                       
                    }
                }
               
            } else {
               
                // generate call to factory method
                genUserMethodCall(true, m_factoryMethod, mb);
                mb.appendCreateCast(m_factoryMethod.getTypeName(),
                    m_class.getClassName());
               
            }
           
            // save created instance to be returned
            if (haveobj) {
                mb.storeObject();
            }
           
            // if preset method supplied add code to call it
            mb.targetNext(haveinst);
//            ** disable for now; can't take out of unmarshal methods, since
//            create not always called **
//            if (m_preSetMethod != null) {
//                genUserMethodCall(true, m_preSetMethod, mb);
//            }
           
            // finish method code with return of new instance
            mb.loadObject();
            mb.appendReturn(m_class.getClassName());
            m_newInstanceMethod = m_class.getUniqueMethod(mb).getItem();
        }
    }
View Full Code Here

        if (m_attributePresenceMethod == null) {
           
            // set up for constructing new method
            String name = m_container.getBindingRoot().getPrefix() +
                ATTR_PRESENCE_SUFFIX;
            ContextMethodBuilder meth = new ContextMethodBuilder(name,
                Type.BOOLEAN, UNMARSHALCONTEXT_ARGS, m_class.getMungedFile(),
                Constants.ACC_PUBLIC | Constants.ACC_STATIC, -1, null,
                0, UNMARSHALLING_CONTEXT);
            m_attributePresenceName = meth.getFullName();
           
            // generate the presence test code
            m_component.genAttrPresentTest(meth);
            meth.appendReturn(Type.BOOLEAN);
           
            // add method to class
            if (m_lockAttributePresence) {
                m_attributePresenceMethod =
                    m_class.getUniqueNamed(meth).getItem();
View Full Code Here

        if (m_contentPresenceMethod == null) {
           
            // set up for constructing new method
            String name = m_container.getBindingRoot().getPrefix() +
                PRESENCE_SUFFIX;
            ContextMethodBuilder meth = new ContextMethodBuilder(name,
                Type.BOOLEAN, UNMARSHALCONTEXT_ARGS, m_class.getMungedFile(),
                Constants.ACC_PUBLIC | Constants.ACC_STATIC, -1, null,
                0, UNMARSHALLING_CONTEXT);
            m_contentPresenceName = meth.getFullName();
           
            // generate the presence test code
            if (m_fakeContent) {
                meth.appendLoadConstant(1);
            } else {
                m_component.genContentPresentTest(meth);
            }
            meth.appendReturn(Type.BOOLEAN);
           
            // add method to class
            if (m_lockContentPresence) {
                m_contentPresenceMethod =
                    m_class.getUniqueNamed(meth).getItem();
View Full Code Here

TOP

Related Classes of org.jibx.binding.classes.ContextMethodBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.