javax.xml.bind.annotation.XmlAccessType Java Examples

The following examples show how to use javax.xml.bind.annotation.XmlAccessType. 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: Utils.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static Collection<Method> getMethodsInternal(Class<?> cls, XmlAccessType accessType,
        boolean acceptSetters) {
    Set<Method> methods = new HashSet<>();
    Class<?> superClass = cls.getSuperclass();
    if (superClass != null && !superClass.equals(Object.class) && !superClass.equals(Throwable.class)) {
        // process super class until java.lang.Object or java.lang.Throwable is not reached
        methods.addAll(getMethodsInternal(superClass, accessType, acceptSetters));
    }
    // process current class
    for (Method method : cls.getDeclaredMethods()) {
        if (isMethodAccepted(method, accessType, acceptSetters)) {
            methods.add(method);
        }
    }
    return methods;
}
 
Example #2
Source File: JavaTypeAnalyzer.java    From jaxrs-analyzer with Apache License 2.0 6 votes vote down vote up
private Map<String, TypeIdentifier> analyzeClass(final String type, final Class<?> clazz) {
    if (clazz == null || isJDKType(type))
        return Collections.emptyMap();

    final XmlAccessType value = getXmlAccessType(clazz);

    // TODO analyze & test annotation inheritance
    ignoredFieldNames.clear();
    final List<Field> relevantFields = Stream.of(clazz.getDeclaredFields()).filter(f -> isRelevant(f, value)).collect(Collectors.toList());
    final List<Method> relevantGetters = Stream.of(clazz.getDeclaredMethods()).filter(m -> isRelevant(m, value)).collect(Collectors.toList());

    final Map<String, TypeIdentifier> properties = new HashMap<>();

    final Stream<Class<?>> allSuperTypes = Stream.concat(Stream.of(clazz.getInterfaces()), Stream.of(clazz.getSuperclass()));
    allSuperTypes.filter(Objects::nonNull).map(Type::getDescriptor).map(t -> analyzeClass(t, loadClassFromType(t))).forEach(properties::putAll);

    Stream.concat(relevantFields.stream().map(f -> mapField(f, type)), relevantGetters.stream().map(g -> mapGetter(g, type)))
            .filter(Objects::nonNull).forEach(p -> {
        properties.put(p.getLeft(), TypeIdentifier.ofType(p.getRight()));
        analyze(p.getRight());
    });

    return properties;
}
 
Example #3
Source File: JAXBContextInitializer.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the field is accepted as a JAXB property.
 */
static boolean isFieldAccepted(Field field, XmlAccessType accessType) {
    // We only accept non static fields which are not marked @XmlTransient or has transient modifier
    if (field.isAnnotationPresent(XmlTransient.class)
        || Modifier.isTransient(field.getModifiers())) {
        return false;
    }
    if (Modifier.isStatic(field.getModifiers())) {
        return field.isAnnotationPresent(XmlAttribute.class);
    }
    if (accessType == XmlAccessType.PUBLIC_MEMBER
        && !Modifier.isPublic(field.getModifiers())) {
        return false;
    }
    if (accessType == XmlAccessType.NONE
        || accessType == XmlAccessType.PROPERTY) {
        return checkJaxbAnnotation(field.getAnnotations());
    }
    return true;
}
 
Example #4
Source File: JavaTypeAnalyzer.java    From jaxrs-analyzer with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the method is public and non-static and that the method is a Getter.
 * Does not allow methods with ignored names.
 * Does also not take methods annotated with {@link XmlTransient}.
 *
 * @param method The method
 * @return {@code true} if the method should be analyzed further
 */
private static boolean isRelevant(final Method method, final XmlAccessType accessType) {
    if (method.isSynthetic() || !isGetter(method))
        return false;

    final boolean propertyIgnored = ignoredFieldNames.contains(extractPropertyName(method.getName()));
    if (propertyIgnored || hasIgnoreAnnotation(method) || isTypeIgnored(method.getReturnType())) {
        return false;
    }

    if (isAnnotationPresent(method, XmlElement.class))
        return true;

    if (accessType == XmlAccessType.PROPERTY)
        return !isAnnotationPresent(method, XmlTransient.class);
    else if (accessType == XmlAccessType.PUBLIC_MEMBER)
        return Modifier.isPublic(method.getModifiers()) && !isAnnotationPresent(method, XmlTransient.class);

    return false;
}
 
Example #5
Source File: JavaTypeAnalyzer.java    From jaxrs-analyzer with Apache License 2.0 6 votes vote down vote up
private static boolean isRelevant(final Field field, final XmlAccessType accessType) {
    if (field.isSynthetic())
        return false;

    if (hasIgnoreAnnotation(field) || isTypeIgnored(field.getType())) {
        ignoredFieldNames.add(field.getName());
        return false;
    }

    if (isAnnotationPresent(field, XmlElement.class))
        return true;

    final int modifiers = field.getModifiers();
    if (accessType == XmlAccessType.FIELD)
        // always take, unless static or transient
        return !Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers) && !isAnnotationPresent(field, XmlTransient.class);
    else if (accessType == XmlAccessType.PUBLIC_MEMBER)
        // only for public, non-static
        return Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) && !isAnnotationPresent(field, XmlTransient.class);

    return false;
}
 
Example #6
Source File: ImmutableJaxbGenerator.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private void renderClassLevelAnnotations(JDefinedClass classModel, List<FieldModel> fields) throws Exception {
	JFieldRef constantsClass = classModel.staticRef(Util.CONSTANTS_CLASS_NAME);
	JFieldRef elementsClass = classModel.staticRef(Util.ELEMENTS_CLASS_NAME);
	JClass coreConstants = codeModel.ref(CoreConstants.class);
	JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements");
	
	// XmlRootElement
	JAnnotationUse rootElementAnnotation = classModel.annotate(XmlRootElement.class);
	rootElementAnnotation.param("name", constantsClass.ref(Util.ROOT_ELEMENT_NAME_FIELD));
	
	// XmlAccessorType
	JAnnotationUse xmlAccessorTypeAnnotation = classModel.annotate(XmlAccessorType.class);
	xmlAccessorTypeAnnotation.param("value", XmlAccessType.NONE);
	
	// XmlType
	JAnnotationUse xmlTypeAnnotation = classModel.annotate(XmlType.class);
	xmlTypeAnnotation.param("name", constantsClass.ref(Util.TYPE_NAME_FIELD));
	JAnnotationArrayMember propOrderMember = xmlTypeAnnotation.paramArray("propOrder");
	for (FieldModel field : fields) {
		if (Util.isCommonElement(field.fieldName)) {
			propOrderMember.param(commonElementsRef.ref(Util.toConstantsVariable(field.fieldName)));
		} else {
			propOrderMember.param(elementsClass.ref(Util.toConstantsVariable(field.fieldName)));
		}
	}
	propOrderMember.param(commonElementsRef.ref("FUTURE_ELEMENTS"));
}
 
Example #7
Source File: ClassInfoImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the {@link XmlAccessType} on this class by looking at {@link XmlAccessorType}
 * annotations.
 */
private XmlAccessType getAccessType() {
    XmlAccessorType xat = getClassOrPackageAnnotation(XmlAccessorType.class);
    if(xat!=null)
        return xat.value();
    else
        return XmlAccessType.PUBLIC_MEMBER;
}
 
Example #8
Source File: WebServiceWrapperGenerator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) {

       if (cls == null)
            return;
        JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class);
        xmlRootElementAnn.param("name", elementName);
        if (namespaceUri.length() > 0) {
            xmlRootElementAnn.param("namespace", namespaceUri);
        }
        JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class));
        xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD);
    }
 
Example #9
Source File: WebServiceWrapperGenerator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) {

       if (cls == null)
            return;
        JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class);
        xmlRootElementAnn.param("name", elementName);
        if (namespaceUri.length() > 0) {
            xmlRootElementAnn.param("namespace", namespaceUri);
        }
        JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class));
        xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD);
    }
 
Example #10
Source File: ClassInfoImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the {@link XmlAccessType} on this class by looking at {@link XmlAccessorType}
 * annotations.
 */
private XmlAccessType getAccessType() {
    XmlAccessorType xat = getClassOrPackageAnnotation(XmlAccessorType.class);
    if(xat!=null)
        return xat.value();
    else
        return XmlAccessType.PUBLIC_MEMBER;
}
 
Example #11
Source File: WebServiceWrapperGenerator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) {

       if (cls == null)
            return;
        JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class);
        xmlRootElementAnn.param("name", elementName);
        if (namespaceUri.length() > 0) {
            xmlRootElementAnn.param("namespace", namespaceUri);
        }
        JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class));
        xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD);
    }
 
Example #12
Source File: JavaTypeAnalyzer.java    From jaxrs-analyzer with Apache License 2.0 5 votes vote down vote up
private XmlAccessType getXmlAccessType(final Class<?> clazz) {
    Class<?> current = clazz;

    while (current != null) {
        if (isAnnotationPresent(current, XmlAccessorType.class))
            return getAnnotation(current, XmlAccessorType.class).value();
        current = current.getSuperclass();
    }

    return XmlAccessType.PUBLIC_MEMBER;
}
 
Example #13
Source File: ClassInfoImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the {@link XmlAccessType} on this class by looking at {@link XmlAccessorType}
 * annotations.
 */
private XmlAccessType getAccessType() {
    XmlAccessorType xat = getClassOrPackageAnnotation(XmlAccessorType.class);
    if(xat!=null)
        return xat.value();
    else
        return XmlAccessType.PUBLIC_MEMBER;
}
 
Example #14
Source File: Utils.java    From cxf with Apache License 2.0 5 votes vote down vote up
static XmlAccessType getXmlAccessType(Class<?> cls) {
    XmlAccessorType accessorType = cls.getAnnotation(XmlAccessorType.class);
    if (accessorType == null && cls.getPackage() != null) {
        accessorType = cls.getPackage().getAnnotation(XmlAccessorType.class);
    }
    return accessorType != null
        ? accessorType.value() : XmlAccessType.PUBLIC_MEMBER;
}
 
Example #15
Source File: Utils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static Collection<Field> getFieldsInternal(Class<?> cls, XmlAccessType accessType) {
    Set<Field> fields = new HashSet<>();
    Class<?> superClass = cls.getSuperclass();
    if (superClass != null && !superClass.equals(Object.class) && !superClass.equals(Throwable.class)) {
        // process super class until java.lang.Object or java.lang.Throwable is not reached
        fields.addAll(getFieldsInternal(superClass, accessType));
    }
    // process current class
    for (Field field : cls.getDeclaredFields()) {
        if (JAXBContextInitializer.isFieldAccepted(field, accessType)) {
            fields.add(field);
        }
    }
    return fields;
}
 
Example #16
Source File: Utils.java    From cxf with Apache License 2.0 5 votes vote down vote up
static Method getMethod(Class<?> cls, XmlAccessType accessType, String methodName,
        Class<?>... paramTypes) {
    for (Method m : getMethods(cls, accessType, true)) {
        if (m.getName().equals(methodName) && Arrays.equals(m.getParameterTypes(), paramTypes)) {
            return m;
        }
    }
    return null;
}
 
Example #17
Source File: Utils.java    From cxf with Apache License 2.0 5 votes vote down vote up
static Field getField(Class<?> cls, XmlAccessType accessType, String fieldName) {
    for (final Field f : getFields(cls, accessType)) {
        if (f.getName().equals(fieldName)) {
            return f;
        }
    }
    return null;
}
 
Example #18
Source File: Utils.java    From cxf with Apache License 2.0 5 votes vote down vote up
static boolean isMethodAccepted(Method method, XmlAccessType accessType, boolean acceptSetters) {
    // ignore bridge, static, @XmlTransient methods plus methods declared in Throwable
    if (method.isBridge()
            || Modifier.isStatic(method.getModifiers())
            || method.isAnnotationPresent(XmlTransient.class)
            || method.getDeclaringClass().equals(Throwable.class)
            || "getClass".equals(method.getName())) {
        return false;
    }
    // Allow only public methods if PUBLIC_MEMBER access is requested
    if (accessType == XmlAccessType.PUBLIC_MEMBER && !Modifier.isPublic(method.getModifiers())) {
        return false;
    }
    if (isGetter(method)) {
        // does nothing
    } else if (isSetter(method)) {
        if (!acceptSetters) {
            return false;
        }
    } else {
        // we accept only getters and setters
        return false;
    }
    // let JAXB annotations decide if NONE or FIELD access is requested
    if (accessType == XmlAccessType.NONE || accessType == XmlAccessType.FIELD) {
        return JAXBContextInitializer.checkJaxbAnnotation(method.getAnnotations());
    }
    // method accepted
    return true;
}
 
Example #19
Source File: UtilsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMethod() {
    for (Method method : Utils.getGetters(MyException.class, XmlAccessType.PUBLIC_MEMBER)) {
        if ("toString".equals(method.getName())) {
            fail("toString should not be included in get methods list");
        }
    }
}
 
Example #20
Source File: WebServiceWrapperGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) {

       if (cls == null)
            return;
        JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class);
        xmlRootElementAnn.param("name", elementName);
        if (namespaceUri.length() > 0) {
            xmlRootElementAnn.param("namespace", namespaceUri);
        }
        JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class));
        xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD);
    }
 
Example #21
Source File: ClassInfoImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the {@link XmlAccessType} on this class by looking at {@link XmlAccessorType}
 * annotations.
 */
private XmlAccessType getAccessType() {
    XmlAccessorType xat = getClassOrPackageAnnotation(XmlAccessorType.class);
    if(xat!=null)
        return xat.value();
    else
        return XmlAccessType.PUBLIC_MEMBER;
}
 
Example #22
Source File: WebServiceWrapperGenerator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) {

       if (cls == null)
            return;
        JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class);
        xmlRootElementAnn.param("name", elementName);
        if (namespaceUri.length() > 0) {
            xmlRootElementAnn.param("namespace", namespaceUri);
        }
        JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class));
        xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD);
    }
 
Example #23
Source File: ClassInfoImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the {@link XmlAccessType} on this class by looking at {@link XmlAccessorType}
 * annotations.
 */
private XmlAccessType getAccessType() {
    XmlAccessorType xat = getClassOrPackageAnnotation(XmlAccessorType.class);
    if(xat!=null)
        return xat.value();
    else
        return XmlAccessType.PUBLIC_MEMBER;
}
 
Example #24
Source File: WebServiceWrapperGenerator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) {

       if (cls == null)
            return;
        JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class);
        xmlRootElementAnn.param("name", elementName);
        if (namespaceUri.length() > 0) {
            xmlRootElementAnn.param("namespace", namespaceUri);
        }
        JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class));
        xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD);
    }
 
Example #25
Source File: ClassInfoImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the {@link XmlAccessType} on this class by looking at {@link XmlAccessorType}
 * annotations.
 */
private XmlAccessType getAccessType() {
    XmlAccessorType xat = getClassOrPackageAnnotation(XmlAccessorType.class);
    if(xat!=null)
        return xat.value();
    else
        return XmlAccessType.PUBLIC_MEMBER;
}
 
Example #26
Source File: ClassInfoImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the {@link XmlAccessType} on this class by looking at {@link XmlAccessorType}
 * annotations.
 */
private XmlAccessType getAccessType() {
    XmlAccessorType xat = getClassOrPackageAnnotation(XmlAccessorType.class);
    if(xat!=null)
        return xat.value();
    else
        return XmlAccessType.PUBLIC_MEMBER;
}
 
Example #27
Source File: WebServiceWrapperGenerator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) {

       if (cls == null)
            return;
        JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class);
        xmlRootElementAnn.param("name", elementName);
        if (namespaceUri.length() > 0) {
            xmlRootElementAnn.param("namespace", namespaceUri);
        }
        JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class));
        xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD);
    }
 
Example #28
Source File: ClassInfoImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the {@link XmlAccessType} on this class by looking at {@link XmlAccessorType}
 * annotations.
 */
private XmlAccessType getAccessType() {
    XmlAccessorType xat = getClassOrPackageAnnotation(XmlAccessorType.class);
    if(xat!=null)
        return xat.value();
    else
        return XmlAccessType.PUBLIC_MEMBER;
}
 
Example #29
Source File: WebServiceWrapperGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void writeXmlElementDeclaration(JDefinedClass cls, String elementName, String namespaceUri) {

       if (cls == null)
            return;
        JAnnotationUse xmlRootElementAnn = cls.annotate(XmlRootElement.class);
        xmlRootElementAnn.param("name", elementName);
        if (namespaceUri.length() > 0) {
            xmlRootElementAnn.param("namespace", namespaceUri);
        }
        JAnnotationUse xmlAccessorTypeAnn = cls.annotate(cm.ref(XmlAccessorType.class));
        xmlAccessorTypeAnn.param("value", XmlAccessType.FIELD);
    }
 
Example #30
Source File: ClassInfoImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private void findFieldProperties(C c, XmlAccessType at) {

        // always find properties from the super class first
        C sc = nav().getSuperClass(c);
        if (shouldRecurseSuperClass(sc)) {
            findFieldProperties(sc,at);
        }

        for( F f : nav().getDeclaredFields(c) ) {
            Annotation[] annotations = reader().getAllFieldAnnotations(f,this);
            boolean isDummy = reader().hasFieldAnnotation(OverrideAnnotationOf.class, f);

            if( nav().isTransient(f) ) {
                // it's an error for transient field to have any binding annotation
                if(hasJAXBAnnotation(annotations))
                    builder.reportError(new IllegalAnnotationException(
                        Messages.TRANSIENT_FIELD_NOT_BINDABLE.format(nav().getFieldName(f)),
                            getSomeJAXBAnnotation(annotations)));
            } else
            if( nav().isStaticField(f) ) {
                // static fields are bound only when there's explicit annotation.
                if(hasJAXBAnnotation(annotations))
                    addProperty(createFieldSeed(f),annotations, false);
            } else {
                if(at==XmlAccessType.FIELD
                ||(at==XmlAccessType.PUBLIC_MEMBER && nav().isPublicField(f))
                || hasJAXBAnnotation(annotations)) {
                    if (isDummy) {
                        ClassInfo<T, C> top = getBaseClass();
                        while ((top != null) && (top.getProperty("content") == null)) {
                            top = top.getBaseClass();
                        }
                        DummyPropertyInfo prop = (DummyPropertyInfo) top.getProperty("content");
                        PropertySeed seed = createFieldSeed(f);
                        ((DummyPropertyInfo)prop).addType(createReferenceProperty(seed));
                    } else {
                        addProperty(createFieldSeed(f), annotations, false);
                    }
                }
                checkFieldXmlLocation(f);
            }
        }
    }