All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glassfish.jaxb.runtime.v2.model.impl.ElementPropertyInfoImpl Maven / Gradle / Ivy

There is a newer version: 62
Show newest version
/*
 * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.glassfish.jaxb.runtime.v2.model.impl;

import com.sun.istack.FinalArrayList;
import org.glassfish.jaxb.core.v2.model.core.*;
import org.glassfish.jaxb.core.v2.runtime.IllegalAnnotationException;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElements;
import jakarta.xml.bind.annotation.XmlList;

import javax.xml.namespace.QName;
import java.util.AbstractList;
import java.util.Collections;
import java.util.List;

/**
 * Common {@link ElementPropertyInfo} implementation used for both
 * Annotation Processing and runtime.
 * 
 * @author Kohsuke Kawaguchi
 */
class ElementPropertyInfoImpl
    extends ERPropertyInfoImpl
    implements ElementPropertyInfo
{
    /**
     * Lazily computed.
     * @see #getTypes()
     */
    private List> types;

    private final List> ref = new AbstractList>() {
        public TypeInfo get(int index) {
            return getTypes().get(index).getTarget();
        }

        public int size() {
            return getTypes().size();
        }
    };

    /**
     * Lazily computed.
     * @see #isRequired()
     */
    private Boolean isRequired;

    /**
     * @see #isValueList()
     */
    private final boolean isValueList;

    ElementPropertyInfoImpl(
        ClassInfoImpl parent,
        PropertySeed propertySeed) {
        super(parent, propertySeed);

        isValueList = seed.hasAnnotation(XmlList.class);

    }

    public List> getTypes() {
        if(types==null) {
            types = new FinalArrayList>();
            XmlElement[] ann=null;

            XmlElement xe = seed.readAnnotation(XmlElement.class);
            XmlElements xes = seed.readAnnotation(XmlElements.class);

            if(xe!=null && xes!=null) {
                parent.builder.reportError(new IllegalAnnotationException(
                        Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format(
                                nav().getClassName(parent.getClazz())+'#'+seed.getName(),
                                xe.annotationType().getName(), xes.annotationType().getName()),
                        xe, xes ));
            }

            isRequired = true;

            if(xe!=null)
                ann = new XmlElement[]{xe};
            else
            if(xes!=null)
                ann = xes.value();

            if(ann==null) {
                // default
                TypeT t = getIndividualType();
                if(!nav().isPrimitive(t) || isCollection())
                    isRequired = false;
                // nillableness defaults to true if it's collection
                types.add(createTypeRef(calcXmlName((XmlElement)null),t,isCollection(),null));
            } else {
                for( XmlElement item : ann ) {
                    // TODO: handle defaulting in names.
                    QName name = calcXmlName(item);
                    TypeT type = reader().getClassValue(item, "type");
                    if (nav().isSameType(type, nav().ref(XmlElement.DEFAULT.class)))
                        type = getIndividualType();
                    if((!nav().isPrimitive(type) || isCollection()) && !item.required())
                        isRequired = false;
                    types.add(createTypeRef(name, type, item.nillable(), getDefaultValue(item.defaultValue()) ));
                }
            }
            types = Collections.unmodifiableList(types);
            assert !types.contains(null);
        }
        return types;
    }

    private String getDefaultValue(String value) {
        if(value.equals("\u0000"))
            return null;
        else
            return value;
    }

    /**
     * Used by {@link PropertyInfoImpl} to create new instances of {@link TypeRef}
     */
    protected TypeRefImpl createTypeRef(QName name,TypeT type,boolean isNillable,String defaultValue) {
        return new TypeRefImpl(this,name,type,isNillable,defaultValue);
    }

    public boolean isValueList() {
        return isValueList;
    }

    public boolean isRequired() {
        if(isRequired==null)
            getTypes(); // compute the value
        return isRequired;
    }

    public List> ref() {
        return ref;
    }

    public final PropertyKind kind() {
        return PropertyKind.ELEMENT;
    }

    protected void link() {
        super.link();
        for (TypeRefImpl ref : getTypes() ) {
            ref.link();
        }

        if(isValueList()) {
            // ugly test, because IDREF's are represented as text on the wire,
            // it's OK to be a value list in that case.
            if(id()!= ID.IDREF) {
                // check if all the item types are simple types
                // this can't be done when we compute types because
                // not all TypeInfos are available yet
                for (TypeRefImpl ref : types) {
                    if(!ref.getTarget().isSimpleType()) {
                        parent.builder.reportError(new IllegalAnnotationException(
                        Messages.XMLLIST_NEEDS_SIMPLETYPE.format(
                            nav().getTypeName(ref.getTarget().getType())), this ));
                        break;
                    }
                }
            }

            if(!isCollection())
                parent.builder.reportError(new IllegalAnnotationException(
                    Messages.XMLLIST_ON_SINGLE_PROPERTY.format(), this
                ));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy