javax.xml.bind.annotation.XmlType Java Examples

The following examples show how to use javax.xml.bind.annotation.XmlType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Jaxb2CollectionHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Jaxb2CollectionHttpMessageConverter can read a generic
 * {@link Collection} where the generic type is a JAXB type annotated with
 * {@link XmlRootElement} or {@link XmlType}.
 */
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
	if (!(type instanceof ParameterizedType)) {
		return false;
	}
	ParameterizedType parameterizedType = (ParameterizedType) type;
	if (!(parameterizedType.getRawType() instanceof Class)) {
		return false;
	}
	Class<?> rawType = (Class<?>) parameterizedType.getRawType();
	if (!(Collection.class.isAssignableFrom(rawType))) {
		return false;
	}
	if (parameterizedType.getActualTypeArguments().length != 1) {
		return false;
	}
	Type typeArgument = parameterizedType.getActualTypeArguments()[0];
	if (!(typeArgument instanceof Class)) {
		return false;
	}
	Class<?> typeArgumentClass = (Class<?>) typeArgument;
	return (typeArgumentClass.isAnnotationPresent(XmlRootElement.class) ||
			typeArgumentClass.isAnnotationPresent(XmlType.class)) && canRead(mediaType);
}
 
Example #2
Source File: XmlAsserter.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static String convert(Object obj) {
    try {
        Class clazz = obj.getClass();
        if (!jaxbContextMap.containsKey(clazz)) {
            jaxbContextMap.put(clazz, JAXBContext.newInstance(clazz));
        }
        JAXBContext context = jaxbContextMap.get(clazz);
        Marshaller marshaller = context.createMarshaller();
        StringWriter writer = new StringWriter();
        if (obj.getClass().isAnnotationPresent(XmlRootElement.class)) {
            marshaller.marshal(obj, writer);
        } else if (obj.getClass().isAnnotationPresent(XmlType.class)) {
            JAXBElement jaxbElement = new JAXBElement(new QName("", obj.getClass().getSimpleName()), clazz, obj);
            marshaller.marshal(jaxbElement, writer);
        }
        return writer.toString();
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        Assert.fail(e.getMessage());
    }
    return null;
}
 
Example #3
Source File: XmlAsserter.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
private static String convert(Object obj) {
   try {
      Class clazz = obj.getClass();
      if (!jaxbContextMap.containsKey(clazz)) {
         jaxbContextMap.put(clazz, JAXBContext.newInstance(clazz));
      }

      JAXBContext context = (JAXBContext)jaxbContextMap.get(clazz);
      Marshaller marshaller = context.createMarshaller();
      StringWriter writer = new StringWriter();
      if (obj.getClass().isAnnotationPresent(XmlRootElement.class)) {
         marshaller.marshal(obj, (Writer)writer);
      } else if (obj.getClass().isAnnotationPresent(XmlType.class)) {
         JAXBElement jaxbElement = new JAXBElement(new QName("", obj.getClass().getSimpleName()), clazz, obj);
         marshaller.marshal(jaxbElement, (Writer)writer);
      }

      return writer.toString();
   } catch (Exception var6) {
      LOG.error(var6.getMessage(), var6);
      Assert.fail(var6.getMessage());
      return null;
   }
}
 
Example #4
Source File: Variable2Stub.java    From document-management-software with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Stub convertToStub(JavaLanguageVariable variable, StubTypeTreeRepository typeTreeRepository) {
	Stub stub = new Stub();
	stub.setStubName(variable.getVariableName());
	stub.setRequired(variable.isRequired());
	stub.setMultiOccurs(variable.isMultiOccurs());
	stub.setDescription(variable.getDescription());
	stub.setType(variable.getType());

	Class<?> type = variable.getType();
	if (variable.getType().isArray())
		type = variable.getType().getComponentType();

	if (type.isAnnotationPresent(XmlType.class) && !type.isEnum()) {
		convertFieldsToChildStubs(stub, type, typeTreeRepository);
	}

	return stub;
}
 
Example #5
Source File: Jaxb2CollectionHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Jaxb2CollectionHttpMessageConverter can read a generic
 * {@link Collection} where the generic type is a JAXB type annotated with
 * {@link XmlRootElement} or {@link XmlType}.
 */
@Override
public boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType) {
	if (!(type instanceof ParameterizedType)) {
		return false;
	}
	ParameterizedType parameterizedType = (ParameterizedType) type;
	if (!(parameterizedType.getRawType() instanceof Class)) {
		return false;
	}
	Class<?> rawType = (Class<?>) parameterizedType.getRawType();
	if (!(Collection.class.isAssignableFrom(rawType))) {
		return false;
	}
	if (parameterizedType.getActualTypeArguments().length != 1) {
		return false;
	}
	Type typeArgument = parameterizedType.getActualTypeArguments()[0];
	if (!(typeArgument instanceof Class)) {
		return false;
	}
	Class<?> typeArgumentClass = (Class<?>) typeArgument;
	return (typeArgumentClass.isAnnotationPresent(XmlRootElement.class) ||
			typeArgumentClass.isAnnotationPresent(XmlType.class)) && canRead(mediaType);
}
 
Example #6
Source File: XmlAsserter.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static String convert(Object obj) {
    try {
        Class clazz = obj.getClass();
        if (!jaxbContextMap.containsKey(clazz)) {
            jaxbContextMap.put(clazz, JAXBContext.newInstance(clazz));
        }
        JAXBContext context = jaxbContextMap.get(clazz);
        Marshaller marshaller = context.createMarshaller();
        StringWriter writer = new StringWriter();
        if (obj.getClass().isAnnotationPresent(XmlRootElement.class)) {
            marshaller.marshal(obj, writer);
        } else if (obj.getClass().isAnnotationPresent(XmlType.class)) {
            JAXBElement jaxbElement = new JAXBElement(new QName("", obj.getClass().getSimpleName()), clazz, obj);
            marshaller.marshal(jaxbElement, writer);
        }
        return writer.toString();
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        Assert.fail(e.getMessage());
    }
    return null;
}
 
Example #7
Source File: SchemaCollectionContextProxy.java    From cxf with Apache License 2.0 6 votes vote down vote up
private QName getTypeQName(Class<?> cls, String namespace) {
    QName qn = TYPE_MAP.get(cls);
    if (qn != null) {
        return qn;
    }
    XmlType xtype = cls.getAnnotation(XmlType.class);
    String tn = xtype == null ? "##default" : xtype.name();
    String tns = xtype == null ? "##default" : xtype.namespace();
    if ("##default".equals(tn)) {
        tn = java.beans.Introspector.decapitalize(cls.getSimpleName());
    }
    if ("##default".equals(tns) || StringUtils.isEmpty(tns)) {
        tns = JAXBUtils.getPackageNamespace(cls);
    }
    if ("##default".equals(tns) || StringUtils.isEmpty(tns)) {
        tns = namespace;
    }
    return new QName(tns, tn);
}
 
Example #8
Source File: PropertyComparator.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Uses the {@link XmlType} annotation for defining the property order.
 *
 * @param implementation  the implementation class where to search for {@code XmlType} annotation.
 * @param order           the {@link #order} map where to store the properties order.
 */
private static void defineOrder(Class<?> implementation, final Map<Object,Integer> order) {
    do {
        final XmlType xml = implementation.getAnnotation(XmlType.class);
        if (xml != null) {
            final String[] propOrder = xml.propOrder();
            for (int i=propOrder.length; --i>=0;) {
                /*
                 * Add the entries in reverse order because we are iterating from the child class to
                 * the parent class, and we want the properties in the parent class to be sorted first.
                 * If duplicated properties are found, keep the first occurence (i.e. sort the property
                 * with the most specialized child that declared it).
                 *
                 * We make an exception for ResponsibleParty.role, which should be replaced by Party.role
                 * but this replacement is not yet effective in GeoAPI 3.0.
                 */
                final String prop = propOrder[i];
                if (!"role".equals(prop) || !ResponsibleParty.class.isAssignableFrom(implementation)) {
                    order.putIfAbsent(prop, order.size());
                }
            }
        }
        implementation = implementation.getSuperclass();
    } while (implementation != null);
}
 
Example #9
Source File: WadlGenerator.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
public QName resolve(Class<?> type, Annotation[] annotations, Map<Class<?>, QName> clsMap) {
    QName qname = WadlGenerator.this.getJaxbQName(proxy, type, clsMap);
    if (qname == null && supportJaxbXmlType) {
        XmlType root = type.getAnnotation(XmlType.class);
        if (root != null) {
            XMLName name = AnnotationUtils.getAnnotation(annotations, XMLName.class);
            if (name == null) {
                qname = getJaxbQName(root.name(), root.namespace(), type, clsMap);
            } else {
                QName tempQName = JAXRSUtils.convertStringToQName(name.value());
                qname = new QName(tempQName.getNamespaceURI(),
                                  tempQName.getLocalPart(),
                                  getPrefix(tempQName.getNamespaceURI(), clsMap));
            }
        }
    }
    return qname;
}
 
Example #10
Source File: DeployerValidator.java    From zstack with Apache License 2.0 6 votes vote down vote up
private void validateCollection(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {
    XmlType xtype = obj.getClass().getAnnotation(XmlType.class);
    if (xtype == null) {
        return;
    }
    String elementName = xtype.name();
    logger.debug(String.format("validating %s->%s", elementName, f.getName()));

    Collection l = (Collection) f.get(obj);
    XmlElement eat = f.getAnnotation(XmlElement.class);
    if (eat != null && (eat.required() && (l == null || l.isEmpty()))) {
        throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", f.getName(), elementName));
    }
    XmlAttribute aat = f.getAnnotation(XmlAttribute.class);
    if (aat != null && (aat.required() && (l == null || l.isEmpty()))) {
        throw new IllegalArgumentException(String.format("field[%s] of element[%s] is mandatory, cannot be missed", aat.name(), elementName));
    }

    if (l != null) {
        Object val = l.iterator().next();
        if (val != null) {
            validateObject(val);
        }
    }
}
 
Example #11
Source File: AbstractJAXBProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected QName getJaxbQName(Class<?> cls, Type type, Object object, boolean pluralName)
    throws Exception {

    if (cls == JAXBElement.class) {
        return object != null ? ((JAXBElement<?>)object).getName() : null;
    }

    XmlRootElement root = cls.getAnnotation(XmlRootElement.class);
    if (root != null) {
        return getQNameFromNamespaceAndName(root.namespace(), root.name(), cls, pluralName);
    } else if (isXmlType(cls)) {
        XmlType xmlType = cls.getAnnotation(XmlType.class);
        return getQNameFromNamespaceAndName(xmlType.namespace(), xmlType.name(), cls, pluralName);
    } else {
        return new QName(getPackageNamespace(cls), cls.getSimpleName());
    }
}
 
Example #12
Source File: TypeInfoImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a (potentially-null) {@link XmlType} annotation on a class
 * and determine the actual value.
 *
 * @param clazz
 *      The class on which the XmlType annotation is checked.
 * @param t
 *      The {@link XmlType} annotation on the clazz. This value
 *      is taken as a parameter to improve the performance for the case where
 *      't' is pre-computed.
 */
protected final QName parseTypeName(ClassDeclT clazz, XmlType t) {
    String nsUri="##default";
    String local="##default";
    if(t!=null) {
        nsUri = t.namespace();
        local = t.name();
    }

    if(local.length()==0)
        return null; // anonymous


    if(local.equals("##default"))
        // if defaulted ...
        local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz));

    if(nsUri.equals("##default")) {
        // if defaulted ...
        XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class,clazz,this);
        if(xs!=null)
            nsUri = xs.namespace();
        else {
            nsUri = builder.defaultNsUri;
        }
    }

    return new QName(nsUri.intern(),local.intern());
}
 
Example #13
Source File: WebServiceWrapperGenerator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri,
                                     Collection<MemberInfo> members) {
    if (cls == null)
        return;
    JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class));
    xmlTypeAnn.param("name", typeName);
    xmlTypeAnn.param("namespace", namespaceUri);
    if (members.size() > 1) {
        JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder");
        for (MemberInfo memInfo : members) {
            paramArray.param(memInfo.getParamName());
        }
    }
}
 
Example #14
Source File: Jaxb2XmlEncoder.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) {
	if (super.canEncode(elementType, mimeType)) {
		Class<?> outputClass = elementType.toClass();
		return (outputClass.isAnnotationPresent(XmlRootElement.class) ||
				outputClass.isAnnotationPresent(XmlType.class));
	}
	else {
		return false;
	}
}
 
Example #15
Source File: WebServiceWrapperGenerator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri,
                                     Collection<MemberInfo> members) {
    if (cls == null)
        return;
    JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class));
    xmlTypeAnn.param("name", typeName);
    xmlTypeAnn.param("namespace", namespaceUri);
    if (members.size() > 1) {
        JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder");
        for (MemberInfo memInfo : members) {
            paramArray.param(memInfo.getParamName());
        }
    }
}
 
Example #16
Source File: WebServiceWrapperGenerator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri,
                                     Collection<MemberInfo> members) {
    if (cls == null)
        return;
    JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class));
    xmlTypeAnn.param("name", typeName);
    xmlTypeAnn.param("namespace", namespaceUri);
    if (members.size() > 1) {
        JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder");
        for (MemberInfo memInfo : members) {
            paramArray.param(memInfo.getParamName());
        }
    }
}
 
Example #17
Source File: WebServiceWrapperGenerator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri,
                                     Collection<MemberInfo> members) {
    if (cls == null)
        return;
    JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class));
    xmlTypeAnn.param("name", typeName);
    xmlTypeAnn.param("namespace", namespaceUri);
    if (members.size() > 1) {
        JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder");
        for (MemberInfo memInfo : members) {
            paramArray.param(memInfo.getParamName());
        }
    }
}
 
Example #18
Source File: Jaxb2XmlEncoder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) {
	if (super.canEncode(elementType, mimeType)) {
		Class<?> outputClass = elementType.toClass();
		return (outputClass.isAnnotationPresent(XmlRootElement.class) ||
				outputClass.isAnnotationPresent(XmlType.class));
	}
	else {
		return false;
	}
}
 
Example #19
Source File: PluginContext.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private static String getNamespaceUri(final Class<?> boundClass) {
	final XmlRootElement elementAnnotation = boundClass.getAnnotation(XmlRootElement.class);
	if (elementAnnotation != null && !"##default".equals(elementAnnotation.namespace())) {
		return elementAnnotation.namespace();
	} else {
		final XmlType xmlTypeAnnotation = boundClass.getAnnotation(XmlType.class);
		if (xmlTypeAnnotation != null && !"##default".equals(xmlTypeAnnotation.namespace())) {
			return xmlTypeAnnotation.namespace();
		} else {
			return getNamespaceUri(boundClass.getPackage());
		}
	}
}
 
Example #20
Source File: RestControllerProcessor.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the XmlType annotation from the specified class. If the XmlType doesn't exist, an exception will be thrown.
 *
 * @param clazz the class with the XmlType annotation.
 *
 * @return the XmlType.
 * @throws MojoExecutionException if the class isn't an XmlType.
 */
private XmlType getXmlType(Class<?> clazz) throws MojoExecutionException
{
    XmlType xmlType = clazz.getAnnotation(XmlType.class);
    if (xmlType == null)
    {
        throw new MojoExecutionException("Class \"" + clazz.getName() + "\" is not of XmlType.");
    }
    return xmlType;
}
 
Example #21
Source File: ClassInfoImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 *  XmlType allows specification of factoryClass and
 *  factoryMethod.  There are to be used if no default
 *  constructor is found.
 *
 * @return
 *      true if the factory method was found. False if not.
 */
private  boolean hasFactoryConstructor(XmlType t){
    if (t == null) return false;

    String method = t.factoryMethod();
    T fClass = reader().getClassValue(t, "factoryClass");
    if (method.length() > 0){
        if(nav().isSameType(fClass, nav().ref(XmlType.DEFAULT.class))){
            fClass = nav().use(clazz);
        }
        for(M m: nav().getDeclaredMethods(nav().asDecl(fClass))){
            //- Find the zero-arg public static method with the required return type
            if (nav().getMethodName(m).equals(method) &&
                nav().isSameType(nav().getReturnType(m), nav().use(clazz)) &&
                nav().getMethodParameters(m).length == 0 &&
                nav().isStaticMethod(m)){
                factoryMethod = m;
                break;
            }
        }
        if (factoryMethod == null){
            builder.reportError(new IllegalAnnotationException(
            Messages.NO_FACTORY_METHOD.format(nav().getClassName(nav().asDecl(fClass)), method), this ));
        }
    } else if(!nav().isSameType(fClass, nav().ref(XmlType.DEFAULT.class))){
        builder.reportError(new IllegalAnnotationException(
            Messages.FACTORY_CLASS_NEEDS_FACTORY_METHOD.format(nav().getClassName(nav().asDecl(fClass))), this ));
    }
    return factoryMethod != null;
}
 
Example #22
Source File: TypeInfoImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a (potentially-null) {@link XmlType} annotation on a class
 * and determine the actual value.
 *
 * @param clazz
 *      The class on which the XmlType annotation is checked.
 * @param t
 *      The {@link XmlType} annotation on the clazz. This value
 *      is taken as a parameter to improve the performance for the case where
 *      't' is pre-computed.
 */
protected final QName parseTypeName(ClassDeclT clazz, XmlType t) {
    String nsUri="##default";
    String local="##default";
    if(t!=null) {
        nsUri = t.namespace();
        local = t.name();
    }

    if(local.length()==0)
        return null; // anonymous


    if(local.equals("##default"))
        // if defaulted ...
        local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz));

    if(nsUri.equals("##default")) {
        // if defaulted ...
        XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class,clazz,this);
        if(xs!=null)
            nsUri = xs.namespace();
        else {
            nsUri = builder.defaultNsUri;
        }
    }

    return new QName(nsUri.intern(),local.intern());
}
 
Example #23
Source File: WebServiceWrapperGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri,
                                     Collection<MemberInfo> members) {
    if (cls == null)
        return;
    JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class));
    xmlTypeAnn.param("name", typeName);
    xmlTypeAnn.param("namespace", namespaceUri);
    if (members.size() > 1) {
        JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder");
        for (MemberInfo memInfo : members) {
            paramArray.param(memInfo.getParamName());
        }
    }
}
 
Example #24
Source File: ReflectionHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isComplexType(String className, ClassLoader runtimeClassLoader) {
    try {
        Class<?> type = Class.forName(className, true, runtimeClassLoader);
        Class xmlType = Class.forName(XmlType.class.getName(), true, runtimeClassLoader);

        Annotation xmlAnnotation = type.getAnnotation(xmlType);
        boolean isEnumeration = isEnumeration(className, runtimeClassLoader);

        return xmlAnnotation != null && !isEnumeration;
    } catch (ClassNotFoundException cnfe) {
        return false;
    }
}
 
Example #25
Source File: ClassInfoImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 *  XmlType allows specification of factoryClass and
 *  factoryMethod.  There are to be used if no default
 *  constructor is found.
 *
 * @return
 *      true if the factory method was found. False if not.
 */
private  boolean hasFactoryConstructor(XmlType t){
    if (t == null) return false;

    String method = t.factoryMethod();
    T fClass = reader().getClassValue(t, "factoryClass");
    if (method.length() > 0){
        if(nav().isSameType(fClass, nav().ref(XmlType.DEFAULT.class))){
            fClass = nav().use(clazz);
        }
        for(M m: nav().getDeclaredMethods(nav().asDecl(fClass))){
            //- Find the zero-arg public static method with the required return type
            if (nav().getMethodName(m).equals(method) &&
                nav().isSameType(nav().getReturnType(m), nav().use(clazz)) &&
                nav().getMethodParameters(m).length == 0 &&
                nav().isStaticMethod(m)){
                factoryMethod = m;
                break;
            }
        }
        if (factoryMethod == null){
            builder.reportError(new IllegalAnnotationException(
            Messages.NO_FACTORY_METHOD.format(nav().getClassName(nav().asDecl(fClass)), method), this ));
        }
    } else if(!nav().isSameType(fClass, nav().ref(XmlType.DEFAULT.class))){
        builder.reportError(new IllegalAnnotationException(
            Messages.FACTORY_CLASS_NEEDS_FACTORY_METHOD.format(nav().getClassName(nav().asDecl(fClass))), this ));
    }
    return factoryMethod != null;
}
 
Example #26
Source File: TypeInfoImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses a (potentially-null) {@link XmlType} annotation on a class
 * and determine the actual value.
 *
 * @param clazz
 *      The class on which the XmlType annotation is checked.
 * @param t
 *      The {@link XmlType} annotation on the clazz. This value
 *      is taken as a parameter to improve the performance for the case where
 *      't' is pre-computed.
 */
protected final QName parseTypeName(ClassDeclT clazz, XmlType t) {
    String nsUri="##default";
    String local="##default";
    if(t!=null) {
        nsUri = t.namespace();
        local = t.name();
    }

    if(local.length()==0)
        return null; // anonymous


    if(local.equals("##default"))
        // if defaulted ...
        local = NameConverter.standard.toVariableName(nav().getClassShortName(clazz));

    if(nsUri.equals("##default")) {
        // if defaulted ...
        XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class,clazz,this);
        if(xs!=null)
            nsUri = xs.namespace();
        else {
            nsUri = builder.defaultNsUri;
        }
    }

    return new QName(nsUri.intern(),local.intern());
}
 
Example #27
Source File: ClassInfoImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 *  XmlType allows specification of factoryClass and
 *  factoryMethod.  There are to be used if no default
 *  constructor is found.
 *
 * @return
 *      true if the factory method was found. False if not.
 */
private  boolean hasFactoryConstructor(XmlType t){
    if (t == null) return false;

    String method = t.factoryMethod();
    T fClass = reader().getClassValue(t, "factoryClass");
    if (method.length() > 0){
        if(nav().isSameType(fClass, nav().ref(XmlType.DEFAULT.class))){
            fClass = nav().use(clazz);
        }
        for(M m: nav().getDeclaredMethods(nav().asDecl(fClass))){
            //- Find the zero-arg public static method with the required return type
            if (nav().getMethodName(m).equals(method) &&
                nav().isSameType(nav().getReturnType(m), nav().use(clazz)) &&
                nav().getMethodParameters(m).length == 0 &&
                nav().isStaticMethod(m)){
                factoryMethod = m;
                break;
            }
        }
        if (factoryMethod == null){
            builder.reportError(new IllegalAnnotationException(
            Messages.NO_FACTORY_METHOD.format(nav().getClassName(nav().asDecl(fClass)), method), this ));
        }
    } else if(!nav().isSameType(fClass, nav().ref(XmlType.DEFAULT.class))){
        builder.reportError(new IllegalAnnotationException(
            Messages.FACTORY_CLASS_NEEDS_FACTORY_METHOD.format(nav().getClassName(nav().asDecl(fClass))), this ));
    }
    return factoryMethod != null;
}
 
Example #28
Source File: STIXSchema.java    From java-stix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Return the name from the JAXB model object.
 * 
 * @param obj
 *            Expects a JAXB model object.
 * @return element name
 */
public static String getName(Object obj) {
	try {
		return obj.getClass().getAnnotation(XmlRootElement.class).name();
	} catch (NullPointerException e) {
		return obj.getClass().getAnnotation(XmlType.class).name();
	}
}
 
Example #29
Source File: WebServiceWrapperGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlTypeDeclaration(JDefinedClass cls, String typeName, String namespaceUri,
                                     Collection<MemberInfo> members) {
    if (cls == null)
        return;
    JAnnotationUse xmlTypeAnn = cls.annotate(cm.ref(XmlType.class));
    xmlTypeAnn.param("name", typeName);
    xmlTypeAnn.param("namespace", namespaceUri);
    if (members.size() > 1) {
        JAnnotationArrayMember paramArray = xmlTypeAnn.paramArray("propOrder");
        for (MemberInfo memInfo : members) {
            paramArray.param(memInfo.getParamName());
        }
    }
}
 
Example #30
Source File: PluginContext.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private static String getLocalName(final Class<?> boundClass) {
	final XmlRootElement elementAnnotation = boundClass.getAnnotation(XmlRootElement.class);
	if (elementAnnotation != null && !"##default".equals(elementAnnotation.name())) {
		return elementAnnotation.name();
	} else {
		final XmlType xmlTypeAnnotation = boundClass.getAnnotation(XmlType.class);
		if (xmlTypeAnnotation != null && !"##default".equals(xmlTypeAnnotation.name())) {
			return xmlTypeAnnotation.name();
		} else {
			return boundClass.getSimpleName();
		}
	}
}