nestedcollection.java
来自「JiBX是一个为Java提供的XML数据绑定框架。它可以和现存的类一起运行」· Java 代码 · 共 845 行 · 第 1/3 页
JAVA
845 行
* index number. */ /*package*/ static class IndexedStore extends CollectionStore { /** Method used to set items by index in collection. */ private final ClassItem m_setMethod; /** Flag for method returns result. */ private final boolean m_isReturned; /** * Constructor. * * @param set method used to store items by index in collection * @param ret value returned by add flag */ /*package*/ IndexedStore(ClassItem set, boolean ret) { m_setMethod = set; m_isReturned = ret; } protected void genStoreInit(ContextMethodBuilder mb) throws JiBXException { // create index local with appended code to set initial value mb.appendLoadConstant(-1); mb.defineSlot(m_setMethod, Type.INT); } protected void genStoreItem(ContextMethodBuilder mb) throws JiBXException { // start by getting local variable slot for the index int islot = mb.getSlot(m_setMethod); // append code to first load object and swap with item, then // increment index and swap copy with item, and finally call // collection method to store item at new index position if (!m_setMethod.isStatic()) { mb.loadObject(); mb.appendSWAP(); } mb.appendIncrementLocal(1, islot); mb.appendLoadLocal(islot); mb.appendSWAP(); mb.appendCall(m_setMethod); if (m_isReturned) { mb.appendPOP(); } } protected void genStoreDone(ContextMethodBuilder mb) throws JiBXException { mb.freeSlot(m_setMethod); } } /** * Collection item load strategy for collection with items accessed by * iterator or enumeration. */ /*package*/ static class IteratorLoad extends CollectionLoad { /** Method used to get iterator for collection. */ private final ClassItem m_iterMethod; /** Fully qualified method name to test if more in iteration. */ private final String m_moreName; /** Fully qualified method name to get next item in iteration. */ private final String m_nextName; /** * Constructor. * * @param iter method to get iterator or enumerator from collection * @param more fully qualified method name to test if more in iteration * @param next fully qualified method name to get next item in iteration */ /*package*/ IteratorLoad(ClassItem iter, String more, String next) { m_iterMethod = iter; m_moreName = more; m_nextName = next; } protected void genLoadInit(ContextMethodBuilder mb) throws JiBXException { // create iterator local with appended code to set initial value if (!m_iterMethod.isStatic()) { mb.loadObject(); } mb.appendCall(m_iterMethod); mb.defineSlot(m_iterMethod, Type.getType("Ljava/util/Iterator;")); } protected BranchWrapper genLoadItem(ContextMethodBuilder mb) throws JiBXException { // start with code to load and test iterator int islot = mb.getSlot(m_iterMethod); mb.appendLoadLocal(islot); mb.appendCallInterface(m_moreName, "()Z"); BranchWrapper ifempty = mb.appendIFEQ(this); // append code to get next item from iterator mb.appendLoadLocal(islot); mb.appendCallInterface(m_nextName, "()Ljava/lang/Object;"); return ifempty; } protected void genLoadDone(ContextMethodBuilder mb) throws JiBXException { mb.freeSlot(m_iterMethod); } } /** * Collection item store strategy for collection with add method. */ /*package*/ static class AddStore extends CollectionStore { /** Method used to add item to collection. */ private final ClassItem m_addMethod; /** Flag for method returns result. */ private final boolean m_isReturned; /** * Constructor. * * @param add method used to add item to collection * @param ret value returned by add flag */ /*package*/ AddStore(ClassItem add, boolean ret) { m_addMethod = add; m_isReturned = ret; } protected void genStoreItem(ContextMethodBuilder mb) throws JiBXException { // append code to call collection method to add the item if (!m_addMethod.isStatic()) { mb.loadObject(); mb.appendSWAP(); } mb.appendCall(m_addMethod); if (m_isReturned) { mb.appendPOP(); } } } /** * Collection item load strategy for array. */ /*package*/ static class ArrayLoad extends CollectionLoad { /** Array item type. */ private final String m_itemType; /** Handle for referencing loop counter local variable. */ private Object m_slotHandle = new Object(); /** * Constructor. * * @param itype array item type */ /*package*/ ArrayLoad(String itype) { m_itemType = itype; } protected void genLoadInit(ContextMethodBuilder mb) throws JiBXException { // create index local with initial value -1 mb.appendLoadConstant(-1); mb.defineSlot(m_slotHandle, Type.INT); } protected BranchWrapper genLoadItem(ContextMethodBuilder mb) throws JiBXException { // start by getting local variable slots for the index int islot = mb.getSlot(m_slotHandle); // append code to first increment index, then check for end of // collection reached mb.appendIncrementLocal(1, islot); mb.appendLoadLocal(islot); mb.loadObject(); mb.appendARRAYLENGTH(); mb.appendISUB(); BranchWrapper ifempty = mb.appendIFGE(this); // finish by loading array item at current index position mb.loadObject(); mb.appendLoadLocal(islot); mb.appendALOAD(m_itemType); return ifempty; } protected void genLoadDone(ContextMethodBuilder mb) throws JiBXException { mb.freeSlot(m_slotHandle); } } /** * Collection item store strategy for array. */ /*package*/ static class ArrayStore extends CollectionStore { /** Array item type. */ private final String m_itemType; /** * Constructor. * * @param itype array item type */ /*package*/ ArrayStore(String itype) { m_itemType = itype; } protected void genStoreInit(ContextMethodBuilder mb) throws JiBXException { // create index local with initial value -1 mb.appendLoadConstant(-1); mb.defineSlot(m_itemType, Type.INT); } protected void genStoreItem(ContextMethodBuilder mb) throws JiBXException { // start by getting local variable slot for the index int islot = mb.getSlot(m_itemType); // append code to first increment index and check array size mb.appendIncrementLocal(1, islot); mb.appendLoadLocal(islot); mb.loadObject(); mb.appendARRAYLENGTH(); mb.appendISUB(); BranchWrapper ifnotfull = mb.appendIFLT(this); // grow the array size to make room for more values mb.loadObject(); mb.appendCallStatic(GROWARRAY_METHOD, GROWARRAY_SIGNATURE); mb.storeObject(); // swap the array reference with the item, swap index with item, and // finally store item at new index position mb.targetNext(ifnotfull); mb.loadObject(); mb.appendSWAP(); mb.appendLoadLocal(islot); mb.appendSWAP(); if (!ClassItem.isPrimitive(m_itemType)) { mb.appendCreateCast(m_itemType); } mb.appendASTORE(m_itemType); } protected void genStoreDone(ContextMethodBuilder mb) throws JiBXException { // resize the array to match actual item count int islot = mb.getSlot(m_itemType); mb.appendIncrementLocal(1, islot); mb.appendLoadLocal(islot); mb.loadObject(); mb.appendCallStatic(RESIZEARRAY_METHOD, RESIZEARRAY_SIGNATURE); mb.storeObject(); mb.freeSlot(m_itemType); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?