Package javax.xml.bind

Examples of javax.xml.bind.JAXBException


  {
    try {
      return _field.get(o);
    }
    catch (Exception e) {
      throw new JAXBException(e);
    }
  }
View Full Code Here


  {
    try {
      _field.set(o, value);
    }
    catch (Exception e) {
      throw new JAXBException(e);
    }
  }
View Full Code Here

        return null;

      return _get.invoke(o);
    }
    catch (Exception e) {
      throw new JAXBException(e);
    }
  }
View Full Code Here

        return;

      _set.invoke(o, value);
    }
    catch (Exception e) {
      throw new JAXBException(e);
    }
  }
View Full Code Here

    if (_datatypeFactory == null) {
      try {
        _datatypeFactory = DatatypeFactory.newInstance();
      }
      catch (DatatypeConfigurationException e) {
        throw new JAXBException(e);
      }
    }

    return _datatypeFactory;
  }
View Full Code Here

      if (ptype.getRawType().equals(Holder.class)) {
        Type[] arguments = ptype.getActualTypeArguments();

        if (arguments.length != 1)
          throw new JAXBException("Holder has incorrect number of arguments");

        return arguments[0];
      }
    }
View Full Code Here

        try {
          _constructor = _class.getDeclaredConstructor(NO_PARAMS);
          _constructor.setAccessible(true);
        }
        catch (Exception e2) {
          throw new JAXBException(L.l("{0}: Zero-arg constructor not found",
                                      _class.getName()), e2);
        }
      }

      _typeName = JAXBUtil.getXmlSchemaDatatype(_class, _context);

      // Special case: when name="", this is an "anonymous" type, bound
      // exclusively to a particular element name
      if (! "".equals(_typeName.getLocalPart()))
        _context.addXmlType(_typeName, this);

      // Check for the complete name of the element...
      String namespace = _context.getTargetNamespace();

      // look at package defaults first...
      XmlSchema schema = (XmlSchema) _package.getAnnotation(XmlSchema.class);

      if (schema != null && ! "".equals(schema.namespace()))
        namespace = schema.namespace();
     
      // then look at class specific overrides.
      XmlRootElement xre = _class.getAnnotation(XmlRootElement.class);

      if (xre != null) {
        String localName = null;
       
        if ("##default".equals(xre.name()))
          localName = JAXBUtil.identifierToXmlName(_class);
        else
          localName = xre.name();

        if (! "##default".equals(xre.namespace()))
          namespace = xre.namespace();

        if (namespace == null)
          _elementName = new QName(localName);
        else
          _elementName = new QName(namespace, localName);

        _context.addRootElement(this);
      }

      // order the elements, if specified
     
      XmlAccessOrder accessOrder = XmlAccessOrder.UNDEFINED;

      XmlAccessorOrder packageOrder =
        _package.getAnnotation(XmlAccessorOrder.class);

      XmlAccessorOrder classOrder =
        _class.getAnnotation(XmlAccessorOrder.class);

      if (packageOrder != null)
        accessOrder = packageOrder.value();

      if (classOrder != null)
        accessOrder = classOrder.value();

      // try property orders too
    
      XmlType xmlType = (XmlType) _class.getAnnotation(XmlType.class);
      HashMap<String,Integer> orderMap = null;

      if (xmlType != null &&
          (xmlType.propOrder().length != 1 ||
           ! "".equals(xmlType.propOrder()[0]))) {
        // non-default propOrder
        orderMap = new HashMap<String,Integer>();

        for (int i = 0; i < xmlType.propOrder().length; i++)
          orderMap.put(xmlType.propOrder()[i], i);
      }

      // Collect the fields/properties of the class
      if (orderMap != null) {
        for (int i = 0; i < orderMap.size(); i++)
          _elementMappings.add(null);
      }

      XmlAccessorType accessorType =
        _class.getAnnotation(XmlAccessorType.class);

      XmlAccessType accessType = (accessorType == null ?
                                  XmlAccessType.PUBLIC_MEMBER :
                                  accessorType.value());

      if (accessType != XmlAccessType.FIELD) {
        // getter/setter
        TreeSet<Method> methodSet = new TreeSet<Method>(methodComparator);

        Method[] declared = _class.getDeclaredMethods();

        for (Method m : declared)
          methodSet.add(m);

        Method[] methods = new Method[methodSet.size()];
        methodSet.toArray(methods);

        AccessibleObject.setAccessible(methods, true);

        while (methodSet.size() > 0) {
          Method m = methodSet.first();
          methodSet.remove(m);

          String name = null;
          Method get = null;
          Method set = null;

          if (m.getName().startsWith("get")) {
            get = m;

            if (Void.TYPE.equals(get.getReturnType()))
              continue;

            name = get.getName().substring(3); // 3 == "get".length());

            Class cl = get.getDeclaringClass();

            try {
              set = cl.getDeclaredMethod("set" + name, get.getReturnType());
            }
            catch (NoSuchMethodException e) {
              continue;
            }

            if (! methodSet.remove(set))
              continue;
          }
          else if (m.getName().startsWith("set")) {
            set = m;

            Class[] parameterTypes = set.getParameterTypes();

            if (parameterTypes.length != 1)
              continue;

            name = set.getName().substring(3); // 3 == "set".length());

            Class cl = set.getDeclaringClass();

            try {
              get = cl.getDeclaredMethod("get" + name);
            }
            catch (NoSuchMethodException e) {
              continue;
            }

            if (! parameterTypes[0].equals(get.getReturnType()))
              continue;

            if (! methodSet.remove(get))
              continue;
          }
          else
            continue;

          name = Character.toLowerCase(name.charAt(0)) + name.substring(1);

          // JAXB specifies that a "class" property must be specified as "clazz"
          // because of Object.getClass()
          if ("class".equals(name))
            continue;

          // XXX special cases for Throwable specified in JAX-WS
          // Should it be in the general JAXB?
          if (Throwable.class.isAssignableFrom(_class) &&
              ("stackTrace".equals(name) ||
               "cause".equals(name) ||
               "localizedMessage".equals(name)))
            continue;

          // XXX PUBLIC_MEMBER

          if (accessType == XmlAccessType.NONE &&
              ! JAXBUtil.isJAXBAnnotated(get) &&
              ! JAXBUtil.isJAXBAnnotated(set))
            continue;

          // jaxb/0456
          if (Modifier.isStatic(get.getModifiers()) &&
              JAXBUtil.isJAXBAnnotated(get))
            throw new JAXBException(L.l("JAXB annotations cannot be applied to static methods: {0}", get));

          // jaxb/0457
          if (Modifier.isStatic(set.getModifiers()) &&
              JAXBUtil.isJAXBAnnotated(set))
            throw new JAXBException(L.l("JAXB annotations cannot be applied to static methods: {0}", set));

          // jaxb/0374
          if (Modifier.isStatic(set.getModifiers()) ||
              Modifier.isStatic(get.getModifiers()))
            continue;

          if (get != null && get.isAnnotationPresent(XmlTransient.class))
            continue;
          if (set != null && set.isAnnotationPresent(XmlTransient.class))
            continue;

          get.setAccessible(true);
          set.setAccessible(true);

          Accessor a = new GetterSetterAccessor(name, get, set);

          if (orderMap != null) {
            Integer i = orderMap.remove(name);

            if (i != null)
              a.setOrder(i.intValue());
            // XXX else throw something?
          }

          processAccessor(a);
        }
      }

      if (accessType != XmlAccessType.PROPERTY) {
        // XXX Don't overwrite property accessors
        HashSet<Field> fieldSet = new HashSet<Field>();

        Field[] declared = _class.getDeclaredFields();

        for (Field f : declared)
          fieldSet.add(f);

        Field[] fields = new Field[fieldSet.size()];
        fieldSet.toArray(fields);

        AccessibleObject.setAccessible(fields, true);

        for (Field f : fields) {
          if (f.isAnnotationPresent(XmlLocation.class))
          {
            if (! f.getType().equals(Location.class))
              throw new JAXBException(L.l("Fields annotated by @Location must have type javax.xml.stream.Location"));

            _locationAccessor = new FieldAccessor(f);
          }

          // special case: jaxb/0250
          // fields which are static are skipped _unless_ they are also
          // both final and attributes
          if (Modifier.isStatic(f.getModifiers()) &&
              ! (Modifier.isFinal(f.getModifiers()) &&
                 f.isAnnotationPresent(XmlAttribute.class)))
            continue;

          if (f.isAnnotationPresent(XmlTransient.class))
            continue;
          // jaxb/0176: transient modifier ignored

          if (accessType == XmlAccessType.PUBLIC_MEMBER &&
              ! Modifier.isPublic(f.getModifiers()) &&
              ! JAXBUtil.isJAXBAnnotated(f))
            continue;

          if (accessType == XmlAccessType.NONE && ! JAXBUtil.isJAXBAnnotated(f))
            continue;

          Accessor a = new FieldAccessor(f);

          if (orderMap != null) {
            Integer i = orderMap.remove(f.getName());

            if (i != null)
              a.setOrder(i.intValue());
            // XXX else throw something?
          }

          processAccessor(a);
        }
      }

      // do ordering if necessary
      if (orderMap == null && accessOrder == XmlAccessOrder.ALPHABETICAL)
        Collections.sort(_elementMappings, XmlMapping.nameComparator);
    }
    catch (JAXBException e) {
      throw e;
    }
    catch (Exception e) {
      throw new JAXBException(L.l("{0}: Initialization error",
                                  _class.getName()), e);
    }

    if (! Object.class.equals(_class.getSuperclass()))
      _parent = _context.getSkeleton(_class.getSuperclass());
View Full Code Here

  {
    XmlMapping mapping = XmlMapping.newInstance(_context, accessor);

    if (mapping instanceof XmlValueMapping) {
      if (_value != null)
        throw new JAXBException(L.l("Cannot have two @XmlValue annotated fields or properties"));

      if (_elementMappings.size() > 0) {
        // in case of propOrder & XmlValue
        if (_elementMappings.size() != 1 || _elementMappings.get(0) != null)
          throw new JAXBException(L.l("Cannot have both @XmlValue and elements in a JAXB element (e.g. {0})", _elementMappings.get(0)));

        _elementMappings.clear();
      }

      _value = (XmlValueMapping) mapping;
    }
    else if (mapping instanceof AttributeMapping) {
      mapping.putQNames(_attributeQNameToMappingMap);
      _attributeMappings.add((AttributeMapping) mapping);
    }
    else if (mapping instanceof AnyAttributeMapping) {
      if (_anyAttributeMapping != null)
        throw new JAXBException(L.l("Cannot have two fields or properties with @XmlAnyAttribute annotation"));

      _anyAttributeMapping = (AnyAttributeMapping) mapping;
      _attributeMappings.add(mapping);
    }
    else if ((mapping instanceof ElementMapping) ||
             (mapping instanceof ElementRefMapping) ||
             (mapping instanceof ElementsMapping)) {
      if (_value != null)
        throw new JAXBException(L.l("{0}: Cannot have both @XmlValue and elements in a JAXB element", _class.getName()));

      if (mapping.getAccessor().getOrder() >= 0)
        _elementMappings.set(mapping.getAccessor().getOrder(), mapping);
      else
        _elementMappings.add(mapping);
    }
    else if (mapping instanceof AnyElementMapping) {
      if (_anyElementMapping != null)
        throw new JAXBException(L.l("{0}: Cannot have two @XmlAnyElement annotations in a single class", _class.getName()));

      _anyElementMapping = (AnyElementMapping) mapping;
    }
    else {
      throw new RuntimeException(L.l("Unknown mapping type {0}", mapping.getClass()));
View Full Code Here

          if (! "".equals(xmlType.factoryMethod())) {
            Method m =
              factoryClass.getMethod(xmlType.factoryMethod(), NO_PARAMS);

            if (! Modifier.isStatic(m.getModifiers()))
              throw new JAXBException(L.l("Factory method not static"));

            return (C) m.invoke(null);
          }
        }

        Constructor con = _class.getConstructor(NO_PARAMS);

        return (C)con.newInstance(NO_ARGS);
      }
    }
    catch (JAXBException e) {
      throw e;
    }
    catch (Exception e) {
      throw new JAXBException(e);
    }
  }
View Full Code Here

      if (m.getListener() != null)
        m.getListener().afterMarshal(obj);
    }
    catch (InvocationTargetException e) {
      throw new JAXBException(e);
    }
    catch (IllegalAccessException e) {
      throw new JAXBException(e);
    }
  }
View Full Code Here

TOP

Related Classes of javax.xml.bind.JAXBException

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.