Package org.jboss.xb.binding.metadata

Examples of org.jboss.xb.binding.metadata.MapEntryMetaData


/* 1287 */           log.trace(msg);
/*      */         }
/* 1289 */         term.setPropertyMetaData(propertyMetaData);
/*      */       }
/*      */
/* 1292 */       MapEntryMetaData mapEntryMetaData = appInfo.getMapEntryMetaData();
/* 1293 */       if (mapEntryMetaData != null)
/*      */       {
/* 1295 */         if (propertyMetaData != null)
/*      */         {
/* 1297 */           String msg = "A term can be bound either as a property or as a map entry but not both: " + (term.isModelGroup() ? term.toString() : ((ElementBinding)term).getQName().toString());
/*      */
/* 1300 */           throw new JBossXBRuntimeException(msg);
/*      */         }
/*      */
/* 1303 */         if (trace)
/*      */         {
/* 1305 */           String msg = "element name=" + ((ElementBinding)term).getQName();
/*      */
/* 1308 */           msg = msg + " is bound to a map entry: impl=" + mapEntryMetaData.getImpl() + ", getKeyMethod=" + mapEntryMetaData.getGetKeyMethod() + ", setKeyMethod=" + mapEntryMetaData.getSetKeyMethod() + ", getValueMethod=" + mapEntryMetaData.getGetValueMethod() + ", setValueMethod=" + mapEntryMetaData.getSetValueMethod() + ", valueType=" + mapEntryMetaData.getValueType() + ", nonNullValue=" + mapEntryMetaData.isNonNullValue();
/*      */
/* 1321 */           log.trace(msg);
/*      */         }
/*      */
/* 1324 */         if (classMetaData != null)
View Full Code Here


/* 109 */     return result;
/*     */   }
/*     */
/*     */   public MapEntryMetaData getMapEntryMetaData()
/*     */   {
/* 114 */     MapEntryMetaData result = this.mapEntryMetaData;
/* 115 */     if ((result == null) && (this.classMetaData == null))
/*     */     {
/* 117 */       result = this.typeBinding.getMapEntryMetaData();
/*     */     }
/* 119 */     return result;
View Full Code Here

/*  131 */         MapEntry mapEntry = (MapEntry)parent;
/*  132 */         mapEntry.setKey(o);
/*      */       }
/*  134 */       else if (parentTerm != null)
/*      */       {
/*  136 */         MapEntryMetaData mapEntryMetaData = getMapEntryMetaData(parentTerm, qName);
/*      */
/*  138 */         String getKeyMethodName = mapEntryMetaData.getGetKeyMethod();
/*  139 */         if (getKeyMethodName == null)
/*      */         {
/*  141 */           getKeyMethodName = "getKey";
/*      */         }
/*      */
/*  144 */         String setKeyMethodName = mapEntryMetaData.getSetKeyMethod();
/*  145 */         if (setKeyMethodName == null)
/*      */         {
/*  147 */           setKeyMethodName = "setKey";
/*      */         }
/*      */
/*  150 */         Class parentCls = parent.getClass();
/*  151 */         Method setKeyMethod = getSetMethod(parentCls, getKeyMethodName, setKeyMethodName);
/*  152 */         invokeSetter(setKeyMethod, parent, o, setKeyMethodName);
/*      */       }
/*      */       else
/*      */       {
/*  156 */         throw new JBossXBRuntimeException("Element " + qName + " bound as map entry key but parent element is not recognized as map entry and its metadata is not available.");
/*      */       }
/*      */
/*      */     }
/*  163 */     else if (term.isMapEntryValue())
/*      */     {
/*  165 */       if (trace)
/*      */       {
/*  167 */         log.trace("setParent " + qName + " mapValue");
/*      */       }
/*      */
/*  170 */       if ((parent instanceof MapEntry))
/*      */       {
/*  172 */         MapEntry mapEntry = (MapEntry)parent;
/*  173 */         mapEntry.setValue(o);
/*      */       }
/*  175 */       else if (parentTerm != null)
/*      */       {
/*  177 */         MapEntryMetaData mapEntryMetaData = getMapEntryMetaData(parentTerm, qName);
/*  178 */         setMapEntryValue(mapEntryMetaData, parent, o);
/*      */       }
/*      */       else
/*      */       {
/*  182 */         throw new JBossXBRuntimeException("Element " + qName + " bound as map entry key but parent element is not recognized as map entry and its metadata is not available.");
/*      */       }
/*      */
/*      */     }
/*      */     else
/*      */     {
/*  191 */       Object owner = parent;
/*  192 */       if ((parent instanceof MapEntry))
/*      */       {
/*  194 */         if (trace)
/*      */         {
/*  196 */           log.trace("setParent " + qName + " mapEntry");
/*      */         }
/*      */
/*  199 */         MapEntry mapEntry = (MapEntry)parent;
/*  200 */         owner = mapEntry.getValue();
/*  201 */         if (owner == null)
/*      */         {
/*  203 */           if (parentTerm == null)
/*      */           {
/*  205 */             throw new JBossXBRuntimeException("Binding metadata needed for lazy map entry value instantiation is not available for parent element of element " + qName);
/*      */           }
/*      */
/*  211 */           MapEntryMetaData mapEntryMetaData = getMapEntryMetaData(parentTerm, qName);
/*  212 */           String valueType = mapEntryMetaData.getValueType();
/*  213 */           if (valueType == null)
/*      */           {
/*  215 */             throw new JBossXBRuntimeException("Element " + qName + " is supposed to be bound as map entry value with lazy value instantiation " + "but value type is not specified in its map entry metadata.");
/*      */           }
/*      */
View Full Code Here

/*      */     {
/*  381 */       log.trace("startElement " + elementName + " parent=" + parent + " term=" + term);
/*      */     }
/*      */
/*  384 */     ClassMetaData classMetaData = term.getClassMetaData();
/*  385 */     MapEntryMetaData mapEntryMetaData = term.getMapEntryMetaData();
/*      */
/*  387 */     if (!term.isModelGroup())
/*      */     {
/*  389 */       TypeBinding type = ((ElementBinding)term).getType();
/*  390 */       if ((type.isSimple()) || ((classMetaData == null) && (mapEntryMetaData == null) && ((!type.isStartElementCreatesObject()) || (Constants.QNAME_ANYTYPE.equals(type.getQName())))))
/*      */       {
/*  395 */         if (trace)
/*      */         {
/*  397 */           log.trace("startElement " + elementName + " does not create an object");
/*      */         }
/*  399 */         return null;
/*      */       }
/*      */
/*      */     }
/*      */
/*  405 */     ElementBinding arrayItem = null;
/*  406 */     if (!term.isModelGroup())
/*      */     {
/*  408 */       TypeBinding type = ((ElementBinding)term).getType();
/*  409 */       if (type.getAttributes().isEmpty())
/*      */       {
/*  411 */         ParticleBinding typeParticle = type.getParticle();
/*  412 */         ModelGroupBinding modelGroup = (ModelGroupBinding)(typeParticle == null ? null : typeParticle.getTerm());
/*  413 */         arrayItem = modelGroup == null ? null : modelGroup.getArrayItem();
/*      */
/*  416 */         if ((arrayItem != null) && ((arrayItem.isSkip()) || (arrayItem.getMapEntryMetaData() != null) || (arrayItem.getPutMethodMetaData() != null) || (arrayItem.getAddMethodMetaData() != null)))
/*      */         {
/*  423 */           arrayItem = null;
/*      */         }
/*      */       }
/*      */     }
/*      */
/*  428 */     if (arrayItem != null)
/*      */     {
/*  430 */       Class wrapperType = null;
/*  431 */       if (classMetaData != null)
/*      */       {
/*  433 */         wrapperType = loadClassForTerm(classMetaData.getImpl(), term.getSchema().isIgnoreUnresolvedFieldOrClass(), elementName);
/*      */
/*  438 */         if ((GenericValueContainer.class.isAssignableFrom(wrapperType)) || (Collection.class.isAssignableFrom(wrapperType)) || (Map.class.isAssignableFrom(wrapperType)))
/*      */         {
/*  442 */           return newInstance(wrapperType, elementName, term.getSchema().isUseNoArgCtorIfFound());
/*      */         }
/*      */       }
/*      */
/*  446 */       if ((wrapperType == null) && (parent == null))
/*      */       {
/*  448 */         Class itemType = classForElement(arrayItem, null);
/*  449 */         if (itemType != null)
/*      */         {
/*  451 */           if (trace)
/*      */           {
/*  453 */             log.trace("startElement " + elementName + " new array " + itemType.getName());
/*      */           }
/*  455 */           return GenericValueContainer.FACTORY.array(itemType);
/*      */         }
/*      */       }
/*      */       else
/*      */       {
/*  460 */         PropertyMetaData propertyMetaData = wrapperType == null ? term.getPropertyMetaData() : arrayItem.getPropertyMetaData();
/*      */         String propName;
/*      */         String propName;
/*  464 */         if (propertyMetaData == null)
/*      */         {
/*  466 */           propName = Util.xmlNameToFieldName(wrapperType == null ? elementName.getLocalPart() : arrayItem.getQName().getLocalPart(), term.getSchema().isIgnoreLowLine());
/*      */         }
/*      */         else
/*      */         {
/*  473 */           propName = propertyMetaData.getName();
/*      */         }
/*      */
/*  476 */         if (trace)
/*      */         {
/*  478 */           log.trace("startElement " + elementName + " property=" + propName + " wrapper=" + wrapperType);
/*      */         }
/*      */
/*  481 */         Class parentClass = wrapperType;
/*  482 */         if (wrapperType == null)
/*      */         {
/*  484 */           if ((parent instanceof GenericValueContainer))
/*      */           {
/*  486 */             parentClass = ((GenericValueContainer)parent).getTargetClass();
/*      */           }
/*  488 */           else if ((parent instanceof ValueList))
/*      */           {
/*  490 */             parentClass = ((ValueList)parent).getTargetClass();
/*      */           }
/*      */           else
/*      */           {
/*  494 */             parentClass = parent.getClass();
/*      */           }
/*      */         }
/*      */
/*  498 */         Class fieldType = null;
/*  499 */         if (parentClass.isArray())
/*      */         {
/*  501 */           fieldType = parentClass.getComponentType();
/*      */         }
/*      */         else
/*      */         {
/*  509 */           FieldInfo fieldInfo = FieldInfo.getFieldInfo(parentClass, propName, false);
/*  510 */           if (fieldInfo != null)
/*      */           {
/*  512 */             fieldType = fieldInfo.getType();
/*  513 */             if ((particle.isRepeatable()) && (fieldType.isArray()))
/*      */             {
/*  515 */               fieldType = fieldType.getComponentType();
/*      */             }
/*      */           }
/*  518 */           else if ((((ElementBinding)term).getType().getInterceptors(arrayItem.getQName()).isEmpty()) && (arrayItem.getInterceptors().isEmpty()))
/*      */           {
/*  521 */             QName typeName = ((ElementBinding)term).getType().getQName();
/*  522 */             throw new JBossXBRuntimeException("Couldn't apply 'array wrapper' pattern for element " + elementName + " of type " + (typeName == null ? "anonymous" : typeName.toString()) + ": failed to resolve property " + propName + " and no interceptors applied to override handler.setParent(...)");
/*      */           }
/*      */
/*      */         }
/*      */
/*  531 */         if (fieldType != null)
/*      */         {
/*  534 */           if (fieldType.isArray())
/*      */           {
/*  536 */             return GenericValueContainer.FACTORY.array(wrapperType, propName, fieldType.getComponentType());
/*      */           }
/*  538 */           if (Collection.class.isAssignableFrom(fieldType))
/*      */           {
/*  540 */             if (wrapperType == null)
/*      */             {
/*  542 */               return new ValueListInitializer().newValueList(ValueListHandler.FACTORY.child(), Collection.class);
/*      */             }
/*      */
/*      */           }
/*      */           else
/*      */           {
/*  548 */             return GenericValueContainer.FACTORY.array(wrapperType, propName, fieldType);
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/*      */
/*  554 */     Object o = null;
/*  555 */     if (mapEntryMetaData != null)
/*      */     {
/*  557 */       if (mapEntryMetaData.getImpl() != null)
/*      */       {
/*  559 */         Class cls = loadClassForTerm(mapEntryMetaData.getImpl(), term.getSchema().isIgnoreUnresolvedFieldOrClass(), elementName);
/*      */
/*  561 */         if (trace)
/*      */         {
/*  563 */           log.trace("startElement " + elementName + " new map entry " + cls.getName());
/*      */         }
/*      */
/*  566 */         o = newInstance(cls, elementName, term.getSchema().isUseNoArgCtorIfFound());
/*      */       }
/*      */       else
/*      */       {
/*  570 */         o = new MapEntry();
/*  571 */         if (trace)
/*      */         {
/*  573 */           log.trace("startElement " + elementName + " new map entry");
/*      */         }
/*      */       }
/*      */
/*  577 */       if ((mapEntryMetaData.isNonNullValue()) && (mapEntryMetaData.getValueType() != null))
/*      */       {
/*      */         Class mapValueType;
/*      */         try {
/*  582 */           mapValueType = Thread.currentThread().getContextClassLoader().loadClass(mapEntryMetaData.getValueType());
/*      */         }
/*      */         catch (ClassNotFoundException e)
/*      */         {
/*  586 */           throw new JBossXBRuntimeException("startElement failed for " + elementName + ": failed to load class " + mapEntryMetaData.getValueType() + " for map entry value.");
/*      */         }
/*      */
/*      */         Object value;
/*      */         try
/*      */         {
/*  593 */           if (trace)
/*      */           {
/*  595 */             log.trace("startElement " + elementName + " map value type " + mapEntryMetaData.getValueType());
/*      */           }
/*  597 */           value = mapValueType.newInstance();
/*      */         }
/*      */         catch (Exception e)
/*      */         {
/*  601 */           throw new JBossXBRuntimeException("startElement failed for " + elementName + ": failed to create an instance of " + mapValueType + " for map entry value.");
/*      */         }
/*      */
/*  605 */         if ((o instanceof MapEntry))
/*      */         {
/*  607 */           ((MapEntry)o).setValue(value);
/*      */         }
/*      */         else
/*      */         {
/*  611 */           String getValueMethodName = mapEntryMetaData.getGetValueMethod();
/*  612 */           if (getValueMethodName == null)
/*      */           {
/*  614 */             getValueMethodName = "getValue";
/*      */           }
/*      */
/*  617 */           String setValueMethodName = mapEntryMetaData.getSetValueMethod();
/*  618 */           if (setValueMethodName == null)
/*      */           {
/*  620 */             setValueMethodName = "setValue";
/*      */           }
/*      */           Method getValueMethod;
View Full Code Here

/*  917 */     return setKeyMethod;
/*      */   }
/*      */
/*      */   private static MapEntryMetaData getMapEntryMetaData(TermBinding term, QName qName)
/*      */   {
/*  922 */     MapEntryMetaData mapEntryMetaData = term.getMapEntryMetaData();
/*  923 */     if (mapEntryMetaData == null)
/*      */     {
/*      */       String msg;
/*      */       String msg;
/*  926 */       if (term.isModelGroup())
View Full Code Here

/*      */
/*      */   private void invokePut(QName qName, TermBinding term, Object owner, Object o)
/*      */   {
/* 1127 */     PutMethodMetaData putMethodMetaData = term.getPutMethodMetaData();
/*      */
/* 1129 */     MapEntryMetaData mapEntryMetaData = term.getMapEntryMetaData();
/* 1130 */     if (mapEntryMetaData == null)
/*      */     {
/* 1132 */       throw new JBossXBRuntimeException((putMethodMetaData == null ? "Parent object is an instance of java.util.Map" : new StringBuilder().append("putMethod is specified for element ").append(qName).toString()) + " but mapEntry is specified for neither element " + qName + " nor it's type.");
/*      */     }
/*      */
/* 1142 */     Class oClass = o.getClass();
/* 1143 */     String getKeyMethodName = mapEntryMetaData.getGetKeyMethod();
/* 1144 */     if (getKeyMethodName == null)
/*      */     {
/* 1146 */       getKeyMethodName = "getKey";
/*      */     }
/*      */     Method keyMethod;
/*      */     try
/*      */     {
/* 1152 */       keyMethod = oClass.getMethod(getKeyMethodName, null);
/*      */     }
/*      */     catch (NoSuchMethodException e)
/*      */     {
/* 1156 */       throw new JBossXBRuntimeException("setParent failed for " + qName + "=" + o + ": getKeyMethod=" + getKeyMethodName + " not found in " + oClass);
/*      */     }
/*      */
/*      */     Object key;
/*      */     try
/*      */     {
/* 1169 */       key = keyMethod.invoke(o, null);
/*      */     }
/*      */     catch (Exception e)
/*      */     {
/* 1173 */       throw new JBossXBRuntimeException("setParent failed for " + qName + "=" + o + ": getKeyMethod=" + getKeyMethodName + " threw an exception: " + e.getMessage(), e);
/*      */     }
/*      */
/* 1183 */     Class keyType = Object.class;
/* 1184 */     Class valueType = Object.class;
/* 1185 */     String putMethodName = "put";
/* 1186 */     Class ownerClass = owner.getClass();
/*      */
/* 1188 */     if (putMethodMetaData != null)
/*      */     {
/* 1190 */       if (putMethodMetaData.getKeyType() != null)
/*      */       {
/*      */         try
/*      */         {
/* 1194 */           keyType = Thread.currentThread().getContextClassLoader().loadClass(putMethodMetaData.getKeyType());
/*      */         }
/*      */         catch (ClassNotFoundException e)
/*      */         {
/* 1198 */           throw new JBossXBRuntimeException("setParent failed for " + qName + ": " + e.getMessage(), e);
/*      */         }
/*      */       }
/*      */
/* 1202 */       if (putMethodMetaData.getValueType() != null)
/*      */       {
/*      */         try
/*      */         {
/* 1206 */           valueType = Thread.currentThread().getContextClassLoader().loadClass(putMethodMetaData.getValueType());
/*      */         }
/*      */         catch (ClassNotFoundException e)
/*      */         {
/* 1210 */           throw new JBossXBRuntimeException("setParent failed for " + qName + ": " + e.getMessage(), e);
/*      */         }
/*      */       }
/*      */
/* 1214 */       String name = putMethodMetaData.getName();
/* 1215 */       if (name != null)
/*      */       {
/* 1217 */         putMethodName = name;
/*      */       }
/*      */     }
/*      */     Method putMethod;
/*      */     try
/*      */     {
/* 1224 */       putMethod = ownerClass.getMethod(putMethodName, new Class[] { keyType, valueType });
/*      */     }
/*      */     catch (NoSuchMethodException e)
/*      */     {
/* 1228 */       throw new JBossXBRuntimeException("setParent failed for " + qName + "=" + o + ": putMethod=" + putMethodName + "(" + keyType.getName() + " key, " + valueType.getName() + " value) not found in " + ownerClass);
/*      */     }
/*      */
/* 1238 */     Object value = o;
/* 1239 */     String valueMethodName = mapEntryMetaData.getGetValueMethod();
/* 1240 */     if (valueMethodName != null)
/*      */     {
/*      */       Method valueMethod;
/*      */       try {
/* 1245 */         valueMethod = oClass.getMethod(valueMethodName, null);
/*      */       }
/*      */       catch (NoSuchMethodException e)
/*      */       {
/* 1249 */         throw new JBossXBRuntimeException("setParent failed for " + qName + "=" + o + ": getValueMethod=" + mapEntryMetaData.getGetValueMethod() + " not found in " + oClass);
/*      */       }
/*      */
/*      */       try
/*      */       {
/* 1261 */         value = valueMethod.invoke(o, null);
/*      */       }
/*      */       catch (Exception e)
/*      */       {
/* 1265 */         throw new JBossXBRuntimeException("setParent failed for " + qName + "=" + o + ": getValueMethod=" + mapEntryMetaData.getGetValueMethod() + " threw an exception: " + e.getMessage(), e);
/*      */       }
/*      */
/*      */     }
/* 1275 */     else if ((o instanceof MapEntry))
/*      */     {
View Full Code Here

/*      */   {
/* 1310 */     ClassMetaData clsMetaData = element.getClassMetaData();
/* 1311 */     String clsName = clsMetaData == null ? null : clsMetaData.getImpl();
/* 1312 */     if (clsName == null)
/*      */     {
/* 1314 */       MapEntryMetaData mapEntryMetaData = element.getMapEntryMetaData();
/* 1315 */       if (mapEntryMetaData != null)
/*      */       {
/* 1317 */         clsName = mapEntryMetaData.getImpl();
/* 1318 */         if (clsName == null)
/*      */         {
/* 1320 */           clsName = MapEntry.class.getName();
/*      */         }
/*      */       }
View Full Code Here

      return result;
   }

   public MapEntryMetaData getMapEntryMetaData()
   {
      MapEntryMetaData result = mapEntryMetaData;
      if(result == null && classMetaData == null)
      {
         result = typeBinding.getMapEntryMetaData();
      }
      return result;
View Full Code Here

            MapEntry mapEntry = (MapEntry)parent;
            mapEntry.setKey(o);
         }
         else if(parentTerm != null)
         {
            MapEntryMetaData mapEntryMetaData = getMapEntryMetaData(parentTerm, qName);

            String getKeyMethodName = mapEntryMetaData.getGetKeyMethod();
            if(getKeyMethodName == null)
            {
               getKeyMethodName = "getKey";
            }

            String setKeyMethodName = mapEntryMetaData.getSetKeyMethod();
            if(setKeyMethodName == null)
            {
               setKeyMethodName = "setKey";
            }

            Class<?> parentCls = parent.getClass();
            Method setKeyMethod = getSetMethod(parentCls, getKeyMethodName, setKeyMethodName);
            invokeSetter(setKeyMethod, parent, o, setKeyMethodName);
         }
         else
         {
            throw new JBossXBRuntimeException(
               "Element " +
               qName +
               " bound as map entry key but parent element is not recognized as map entry and its metadata is not available."
            );
         }
      }
      else if(term.isMapEntryValue())
      {
         if(trace)
         {
            log.trace("setParent " + qName + " mapValue");
         }

         if(parent instanceof MapEntry)
         {
            MapEntry mapEntry = (MapEntry)parent;
            mapEntry.setValue(o);
         }
         else if(parentTerm != null)
         {
            MapEntryMetaData mapEntryMetaData = getMapEntryMetaData(parentTerm, qName);
            setMapEntryValue(mapEntryMetaData, parent, o);
         }
         else
         {
            throw new JBossXBRuntimeException(
               "Element " +
               qName +
               " bound as map entry key but parent element is not recognized as map entry and its metadata is not available."
            );
         }
      }
      else
      {
         Object owner = parent;
         if(parent instanceof MapEntry)
         {
            if(trace)
            {
               log.trace("setParent " + qName + " mapEntry");
            }

            MapEntry mapEntry = (MapEntry)parent;
            owner = mapEntry.getValue();
            if(owner == null)
            {
               if(parentTerm == null)
               {
                  throw new JBossXBRuntimeException("Binding metadata needed for lazy map entry value instantiation is not available " +
                     "for parent element of element " +
                     qName
                  );
               }

               MapEntryMetaData mapEntryMetaData = getMapEntryMetaData(parentTerm, qName);
               String valueType = mapEntryMetaData.getValueType();
               if(valueType == null)
               {
                  throw new JBossXBRuntimeException("Element " +
                     qName +
                     " is supposed to be bound as map entry value with lazy value instantiation " +
View Full Code Here

      {
         log.trace("startElement " + elementName + " parent=" + parent + " term=" + term);
      }

      ClassMetaData classMetaData = term.getClassMetaData();
      MapEntryMetaData mapEntryMetaData = term.getMapEntryMetaData();

      if(!term.isModelGroup())
      {
         TypeBinding type = ((ElementBinding)term).getType();
         if(type.isSimple() ||
               classMetaData == null && mapEntryMetaData == null &&
               (!type.isStartElementCreatesObject() ||
                     Constants.QNAME_ANYTYPE.equals(type.getQName())))
         {
            if(trace)
            {
               log.trace("startElement " + elementName + " does not create an object");
            }
            return null;
         }
      }

      // if addMethod is specified, it's probably some collection field
      // but should not be set as a property. Instead, items are added to it using the addMethod
      ElementBinding arrayItem = null;
      if(!term.isModelGroup())
      {
         TypeBinding type = ((ElementBinding)term).getType();
         if(type.getAttributes().isEmpty())
         {
            ParticleBinding typeParticle = type.getParticle();
            ModelGroupBinding modelGroup = (ModelGroupBinding)(typeParticle == null ? null : typeParticle.getTerm());
            arrayItem = modelGroup == null ? null : modelGroup.getArrayItem();

            // todo refactor later (move it to modelGroup.getArrayItem()?)
            if(arrayItem != null &&
               (arrayItem.isSkip() ||
               arrayItem.getMapEntryMetaData() != null ||
               arrayItem.getPutMethodMetaData() != null ||
               arrayItem.getAddMethodMetaData() != null
               ))
            {
               arrayItem = null;
            }
         }
      }

      if(arrayItem != null)
      {
         Class<?> wrapperType = null;
         if(classMetaData != null)
         {
            wrapperType = loadClassForTerm(classMetaData.getImpl(),
               term.getSchema().isIgnoreUnresolvedFieldOrClass(),
               elementName
            );

            if(GenericValueContainer.class.isAssignableFrom(wrapperType) ||
               Collection.class.isAssignableFrom(wrapperType) ||
               Map.class.isAssignableFrom(wrapperType))
            {
               return newInstance(wrapperType, elementName, term.getSchema().isUseNoArgCtorIfFound());
            }
         }

         if(wrapperType == null && parent == null)
         {
            Class<?> itemType = classForElement(arrayItem, null);
            if(itemType != null)
            {
               if(trace)
               {
                  log.trace("startElement " + elementName + " new array " + itemType.getName());
               }
               return GenericValueContainer.FACTORY.array(itemType);
            }
         }
         else
         {
            PropertyMetaData propertyMetaData = wrapperType == null ?
               term.getPropertyMetaData() : arrayItem.getPropertyMetaData();

            String propName;
            if(propertyMetaData == null)
            {
               propName = Util.xmlNameToFieldName(
                  wrapperType == null ? elementName.getLocalPart() : arrayItem.getQName().getLocalPart(),
                  term.getSchema().isIgnoreLowLine()
               );
            }
            else
            {
               propName = propertyMetaData.getName();
            }

            if(trace)
            {
               log.trace("startElement " + elementName + " property=" + propName + " wrapper=" + wrapperType);
            }

            Class<?> parentClass = wrapperType;
            if(wrapperType == null)
            {
               if(parent instanceof GenericValueContainer)
               {
                  parentClass = ((GenericValueContainer)parent).getTargetClass();
               }
               else if(parent instanceof ValueList)
               {
                  parentClass = ((ValueList)parent).getTargetClass();
               }
               else
               {
                  parentClass = parent.getClass();
               }
            }

            Class<?> fieldType = null;
            if(parentClass.isArray())
            {
               fieldType = parentClass.getComponentType();
            }
            else
            {
               //fieldType = FieldInfo.getFieldInfo(parentClass, propName, true).getType();
               // this was changed to false because allow overriding of handler.setParent()
               // with an interceptor.add(). See CollectionOverridePropertyUnitTestCase
               // In other words, don't treat it as an array wrapper.
               FieldInfo fieldInfo = FieldInfo.getFieldInfo(parentClass, propName, false);
               if(fieldInfo != null)
               {
                  fieldType = fieldInfo.getType();
                  if (particle.isRepeatable() && fieldType.isArray())
                  {
                     fieldType = fieldType.getComponentType();
                  }
               }
               else if(((ElementBinding)term).getType().getInterceptors(arrayItem.getQName()).isEmpty() &&
                     arrayItem.getInterceptors().isEmpty())
               {
                  QName typeName = ((ElementBinding)term).getType().getQName();
                  throw new JBossXBRuntimeException(
                        "Couldn't apply 'array wrapper' pattern for element " +
                        elementName + " of type " +
                        (typeName == null ? "anonymous" : typeName.toString()) +
                        ": failed to resolve property " + propName +
                        " and no interceptors applied to override handler.setParent(...)");
               }
            }

            if(fieldType != null)
            {
               // TODO: review the logic for cases when wrapperType == null
               if (fieldType.isArray())
               {
                  return GenericValueContainer.FACTORY.array(wrapperType, propName, fieldType.getComponentType());
               }
               else if (Collection.class.isAssignableFrom(fieldType))
               {
                  if (wrapperType == null)
                  {
                     return new ValueListInitializer().newValueList(ValueListHandler.FACTORY.child(), Collection.class);
                     //o = new ArrayList();
                  }
               }
               else
               {
                  return GenericValueContainer.FACTORY.array(wrapperType, propName, fieldType);
               }
            }
         }
      }

      Object o = null;
      if (mapEntryMetaData != null)
      {
         if (mapEntryMetaData.getImpl() != null)
         {
            Class<?> cls = loadClassForTerm(mapEntryMetaData.getImpl(), term.getSchema().isIgnoreUnresolvedFieldOrClass(), elementName);

            if (trace)
            {
               log.trace("startElement " + elementName + " new map entry " + cls.getName());
            }

            o = newInstance(cls, elementName, term.getSchema().isUseNoArgCtorIfFound());
         }
         else
         {
            o = new MapEntry();
            if (trace)
            {
               log.trace("startElement " + elementName + " new map entry");
            }
         }

         if (mapEntryMetaData.isNonNullValue() && mapEntryMetaData.getValueType() != null)
         {
            Class<?> mapValueType;
            try
            {
               mapValueType = Thread.currentThread().getContextClassLoader().loadClass(mapEntryMetaData.getValueType());
            }
            catch (ClassNotFoundException e)
            {
               throw new JBossXBRuntimeException("startElement failed for " + elementName + ": failed to load class "
                     + mapEntryMetaData.getValueType() + " for map entry value.");
            }

            Object value;
            try
            {
               if (trace)
               {
                  log.trace("startElement " + elementName + " map value type " + mapEntryMetaData.getValueType());
               }
               value = mapValueType.newInstance();
            }
            catch (Exception e)
            {
               throw new JBossXBRuntimeException("startElement failed for " + elementName
                     + ": failed to create an instance of " + mapValueType + " for map entry value.");
            }

            if (o instanceof MapEntry)
            {
               ((MapEntry) o).setValue(value);
            }
            else
            {
               String getValueMethodName = mapEntryMetaData.getGetValueMethod();
               if (getValueMethodName == null)
               {
                  getValueMethodName = "getValue";
               }

               String setValueMethodName = mapEntryMetaData.getSetValueMethod();
               if (setValueMethodName == null)
               {
                  setValueMethodName = "setValue";
               }
View Full Code Here

TOP

Related Classes of org.jboss.xb.binding.metadata.MapEntryMetaData

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.