Package org.jboss.xb.binding.metadata

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


               log.trace(msg);
            }
            term.setPropertyMetaData(propertyMetaData);
         }

         MapEntryMetaData mapEntryMetaData = appInfo.getMapEntryMetaData();
         if(mapEntryMetaData != null)
         {
            if(propertyMetaData != null)
            {
               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());
               throw new JBossXBRuntimeException(msg);
            }

            if (trace)
            {
               String msg = term.isWildcard() || term.isModelGroup() ? term.toString() : "element name=" +
                  ((ElementBinding)term).getQName();

               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();
               log.trace(msg);
            }

            if(classMetaData != null)
            {
View Full Code Here


                        }
                     }
                     binding.setCharactersMetaData(charactersMetaData);
                  }

                  MapEntryMetaData mapEntryMetaData = appInfo.getMapEntryMetaData();
                  if(mapEntryMetaData != null)
                  {
                     if (trace)
                     {
                        log.trace("complex type " +
                           type.getName() +
                           " 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()
                        );
                     }

                     if(classMetaData != null)
                     {
View Full Code Here

               log.trace(msg);
            }
            term.setPropertyMetaData(propertyMetaData);
         }

         MapEntryMetaData mapEntryMetaData = appInfo.getMapEntryMetaData();
         if(mapEntryMetaData != null)
         {
            if(propertyMetaData != null)
            {
               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());
               throw new JBossXBRuntimeException(msg);
            }

            if (trace)
            {
               String msg = term.isWildcard() || term.isModelGroup() ? term.toString() : "element name=" +
                  ((ElementBinding)term).getQName();

               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();
               log.trace(msg);
            }

            if(classMetaData != null)
            {
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

      // first, class metadata and map entry metadata
      ClassMetaData clsMetaData = element.getClassMetaData();
      clsName = clsMetaData == null ? null : clsMetaData.getImpl();
      if(clsName == null)
      {
         MapEntryMetaData mapEntryMetaData = element.getMapEntryMetaData();
         if(mapEntryMetaData != null)
         {
            clsName = mapEntryMetaData.getImpl();
            if(clsName == null)
            {
               clsName = MapEntry.class.getName();
            }
         }
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

      return setKeyMethod;
   }

   private static MapEntryMetaData getMapEntryMetaData(TermBinding term, QName qName)
   {
      MapEntryMetaData mapEntryMetaData = term.getMapEntryMetaData();
      if(mapEntryMetaData == null)
      {
         String msg;
         if(term.isModelGroup())
         {
View Full Code Here

   private void invokePut(QName qName, TermBinding term, Object owner, Object o)
   {
      PutMethodMetaData putMethodMetaData = term.getPutMethodMetaData();

      MapEntryMetaData mapEntryMetaData = term.getMapEntryMetaData();
      if(mapEntryMetaData == null)
      {
         throw new JBossXBRuntimeException((putMethodMetaData == null ?
            "Parent object is an instance of java.util.Map" :
            "putMethod is specified for element " + qName
            ) +
            " but mapEntry is specified for neither element " +
            qName +
            " nor it's type."
         );
      }

      Class oClass = o.getClass();
      String getKeyMethodName = mapEntryMetaData.getGetKeyMethod();
      if(getKeyMethodName == null)
      {
         getKeyMethodName = "getKey";
      }

      Method keyMethod;
      try
      {
         keyMethod = oClass.getMethod(getKeyMethodName, null);
      }
      catch(NoSuchMethodException e)
      {
         throw new JBossXBRuntimeException("setParent failed for " +
            qName +
            "=" +
            o +
            ": getKeyMethod=" +
            getKeyMethodName +
            " not found in " + oClass
         );
      }

      Object key;
      try
      {
         key = keyMethod.invoke(o, null);
      }
      catch(Exception e)
      {
         throw new JBossXBRuntimeException("setParent failed for " +
            qName +
            "=" +
            o +
            ": getKeyMethod=" +
            getKeyMethodName +
            " threw an exception: " + e.getMessage(), e
         );
      }

      Class keyType = Object.class;
      Class valueType = Object.class;
      String putMethodName = "put";
      Class ownerClass = owner.getClass();

      if(putMethodMetaData != null)
      {
         if(putMethodMetaData.getKeyType() != null)
         {
            try
            {
               keyType = Thread.currentThread().getContextClassLoader().loadClass(putMethodMetaData.getKeyType());
            }
            catch(ClassNotFoundException e)
            {
               throw new JBossXBRuntimeException("setParent failed for " + qName + ": " + e.getMessage(), e);
            }
         }

         if(putMethodMetaData.getValueType() != null)
         {
            try
            {
               valueType = Thread.currentThread().getContextClassLoader().loadClass(putMethodMetaData.getValueType());
            }
            catch(ClassNotFoundException e)
            {
               throw new JBossXBRuntimeException("setParent failed for " + qName + ": " + e.getMessage(), e);
            }
         }

         String name = putMethodMetaData.getName();
         if(name != null)
         {
            putMethodName = name;
         }
      }

      Method putMethod;
      try
      {
         putMethod = ownerClass.getMethod(putMethodName, new Class[]{keyType, valueType});
      }
      catch(NoSuchMethodException e)
      {
         throw new JBossXBRuntimeException("setParent failed for " +
            qName +
            "=" +
            o +
            ": putMethod=" +
            putMethodName +
            "(" + keyType.getName() + " key, " + valueType.getName() + " value) not found in " + ownerClass
         );
      }

      Object value = o;
      String valueMethodName = mapEntryMetaData.getGetValueMethod();
      if(valueMethodName != null)
      {
         Method valueMethod;
         try
         {
            valueMethod = oClass.getMethod(valueMethodName, null);
         }
         catch(NoSuchMethodException e)
         {
            throw new JBossXBRuntimeException("setParent failed for " +
               qName +
               "=" +
               o +
               ": getValueMethod=" +
               mapEntryMetaData.getGetValueMethod() +
               " not found in " + oClass
            );
         }

         try
         {
            value = valueMethod.invoke(o, null);
         }
         catch(Exception e)
         {
            throw new JBossXBRuntimeException("setParent failed for " +
               qName +
               "=" +
               o +
               ": getValueMethod=" +
               mapEntryMetaData.getGetValueMethod() +
               " threw an exception: " + e.getMessage(), e
            );
         }
      }
      else if(o instanceof MapEntry)
View Full Code Here

/*      */             }
/*      */
/*  773 */             binding.setCharactersMetaData(charactersMetaData);
/*      */           }
/*      */
/*  776 */           MapEntryMetaData mapEntryMetaData = appInfo.getMapEntryMetaData();
/*  777 */           if (mapEntryMetaData != null)
/*      */           {
/*  779 */             if (this.trace)
/*      */             {
/*  781 */               log.trace("complex type " + type.getName() + " 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());
/*      */             }
/*      */
/*  799 */             if (classMetaData != null)
/*      */             {
/*  801 */               throw new JBossXBRuntimeException("Illegal binding: both jbxb:class and jbxb:mapEntry are specified for complex type " + type.getName());
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.