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

com.sun.xml.ws.model.AbstractWrapperBeanGenerator Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright (c) 1997, 2022 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 com.sun.xml.ws.model;

import com.sun.istack.NotNull;
import org.glassfish.jaxb.core.v2.model.annotation.AnnotationReader;
import org.glassfish.jaxb.core.v2.model.nav.Navigator;
import com.sun.xml.ws.spi.db.BindingHelper;
import com.sun.xml.ws.util.StringUtils;

import jakarta.jws.WebParam;
import jakarta.jws.WebResult;
import jakarta.xml.bind.annotation.*;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import jakarta.xml.ws.WebServiceException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
import java.util.logging.Logger;
import java.security.AccessController;

/**
 * Finds request/response wrapper and exception bean memebers.
 *
 * 

* It uses JAXB's {@link AnnotationReader}, {@link Navigator} so that * tools can use this with annotation processing, and the runtime can use this with * reflection. * * @author Jitendra Kotamraju */ public abstract class AbstractWrapperBeanGenerator { private static final Logger LOGGER = Logger.getLogger(AbstractWrapperBeanGenerator.class.getName()); private static final String RETURN = "return"; private static final String EMTPY_NAMESPACE_ID = ""; private static final Class[] jaxbAnns = new Class[] { XmlAttachmentRef.class, XmlMimeType.class, XmlJavaTypeAdapter.class, XmlList.class, XmlElement.class }; private static final Set skipProperties = new HashSet(); static{ skipProperties.add("getCause"); skipProperties.add("getLocalizedMessage"); skipProperties.add("getClass"); skipProperties.add("getStackTrace"); skipProperties.add("getSuppressed"); // JDK 7 adds this } private final AnnotationReader annReader; private final Navigator nav; private final BeanMemberFactory factory; protected AbstractWrapperBeanGenerator(AnnotationReader annReader, Navigator nav, BeanMemberFactory factory) { this.annReader = annReader; this.nav = nav; this.factory = factory; } public static interface BeanMemberFactory { A createWrapperBeanMember(T paramType, String paramName, List jaxbAnnotations); } // Collects the JAXB annotations on a method private List collectJAXBAnnotations(M method) { List jaxbAnnotation = new ArrayList(); for(Class jaxbClass : jaxbAnns) { Annotation ann = annReader.getMethodAnnotation(jaxbClass, method, null); if (ann != null) { jaxbAnnotation.add(ann); } } return jaxbAnnotation; } // Collects the JAXB annotations on a parameter private List collectJAXBAnnotations(M method, int paramIndex) { List jaxbAnnotation = new ArrayList(); for(Class jaxbClass : jaxbAnns) { Annotation ann = annReader.getMethodParameterAnnotation(jaxbClass, method, paramIndex, null); if (ann != null) { jaxbAnnotation.add(ann); } } return jaxbAnnotation; } protected abstract T getSafeType(T type); /** * Returns Holder's value type. * * @return null if it not a Holder, otherwise return Holder's value type */ protected abstract T getHolderValueType(T type); protected abstract boolean isVoidType(T type); /** * Computes request bean members for a method. Collects all IN and INOUT * parameters as request bean fields. In this process, if a parameter * has any known JAXB annotations they are collected as well. * Special processing for @XmlElement annotation is done. * * @param method SEI method for which request bean members are computed * @return List of request bean members */ public List collectRequestBeanMembers(M method) { List requestMembers = new ArrayList(); int paramIndex = -1; for (T param : nav.getMethodParameters(method)) { paramIndex++; WebParam webParam = annReader.getMethodParameterAnnotation(WebParam.class, method, paramIndex, null); if (webParam != null && (webParam.header() || webParam.mode().equals(WebParam.Mode.OUT))) { continue; } T holderType = getHolderValueType(param); // if (holderType != null && webParam != null && webParam.mode().equals(WebParam.Mode.IN)) { // // Should we flag an error - holder cannot be IN part ?? // continue; // } T paramType = (holderType != null) ? holderType : getSafeType(param); String paramName = (webParam != null && webParam.name().length() > 0) ? webParam.name() : "arg"+paramIndex; String paramNamespace = (webParam != null && webParam.targetNamespace().length() > 0) ? webParam.targetNamespace() : EMTPY_NAMESPACE_ID; // Collect JAXB annotations on a parameter List jaxbAnnotation = collectJAXBAnnotations(method, paramIndex); // If a parameter contains @XmlElement, process it. processXmlElement(jaxbAnnotation, paramName, paramNamespace, paramType); A member = factory.createWrapperBeanMember(paramType, getPropertyName(paramName), jaxbAnnotation); requestMembers.add(member); } return requestMembers; } /** * Computes response bean members for a method. Collects all OUT and INOUT * parameters as response bean fields. In this process, if a parameter * has any known JAXB annotations they are collected as well. * Special processing for @XmlElement annotation is done. * * @param method SEI method for which response bean members are computed * @return List of response bean members */ public List collectResponseBeanMembers(M method) { List responseMembers = new ArrayList(); // return that need to be part response wrapper bean String responseElementName = RETURN; String responseNamespace = EMTPY_NAMESPACE_ID; boolean isResultHeader = false; WebResult webResult = annReader.getMethodAnnotation(WebResult.class, method ,null); if (webResult != null) { if (webResult.name().length() > 0) { responseElementName = webResult.name(); } if (webResult.targetNamespace().length() > 0) { responseNamespace = webResult.targetNamespace(); } isResultHeader = webResult.header(); } T returnType = getSafeType(nav.getReturnType(method)); if (!isVoidType(returnType) && !isResultHeader) { List jaxbRespAnnotations = collectJAXBAnnotations(method); processXmlElement(jaxbRespAnnotations, responseElementName, responseNamespace, returnType); responseMembers.add(factory.createWrapperBeanMember(returnType, getPropertyName(responseElementName), jaxbRespAnnotations)); } // Now parameters that need to be part response wrapper bean int paramIndex = -1; for (T param : nav.getMethodParameters(method)) { paramIndex++; T paramType = getHolderValueType(param); WebParam webParam = annReader.getMethodParameterAnnotation(WebParam.class, method, paramIndex, null); if (paramType == null || (webParam != null && webParam.header())) { continue; // not a holder or a header - so don't add it } String paramName = (webParam != null && webParam.name().length() > 0) ? webParam.name() : "arg"+paramIndex; String paramNamespace = (webParam != null && webParam.targetNamespace().length() > 0) ? webParam.targetNamespace() : EMTPY_NAMESPACE_ID; List jaxbAnnotation = collectJAXBAnnotations(method, paramIndex); processXmlElement(jaxbAnnotation, paramName, paramNamespace, paramType); A member = factory.createWrapperBeanMember(paramType, getPropertyName(paramName), jaxbAnnotation); responseMembers.add(member); } return responseMembers; } // When an element is of an array type, the software // currently sets nillable to true regardless of the value of the // related annotation. A customer has complained that nillable="false" // should be allowed for such a type. // // Since the current behavior was specifically placed in the code, // there may be a good reason for it, and we do not want to break // compatibility. // Therefore, we are adding a new system property // -Dcom.sun.xml.ws.jaxb.allowNonNillableArray=true // to implement the behavior requested by the customer. private final boolean JAXB_ALLOWNONNILLABLEARRAY = getBooleanSystemProperty("com.sun.xml.ws.jaxb.allowNonNillableArray").booleanValue(); /* * Process an individual XML element. * * @param jaxb List of annotations to search * @param elemName The element to be processed * @param elemNS Namespace for the element * @param type Type of the parameter. If this is an array type, then the default behavior is to always consider the parameter nillable. However, if -Dcom.sun.xml.ws.jaxb.allowNonNillableArray=true is set, then an annotation setting nillable to false will be honored. */ private void processXmlElement(List jaxb, String elemName, String elemNS, T type) { XmlElement elemAnn = null; for (Annotation a : jaxb) { if (a.annotationType() == XmlElement.class) { elemAnn = (XmlElement) a; jaxb.remove(a); break; } } String name = (elemAnn != null && !elemAnn.name().equals("##default")) ? elemAnn.name() : elemName; String ns = (elemAnn != null && !elemAnn.namespace().equals("##default")) ? elemAnn.namespace() : elemNS; boolean nillable = (nav.isArray(type) && !JAXB_ALLOWNONNILLABLEARRAY) || (nav.isArray(type) && elemAnn == null) || (elemAnn != null && elemAnn.nillable()); boolean required = elemAnn != null && elemAnn.required(); XmlElementHandler handler = new XmlElementHandler(name, ns, nillable, required); XmlElement elem = (XmlElement) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{XmlElement.class}, handler); jaxb.add(elem); } private static class XmlElementHandler implements InvocationHandler { private String name; private String namespace; private boolean nillable; private boolean required; XmlElementHandler(String name, String namespace, boolean nillable, boolean required) { this.name = name; this.namespace = namespace; this.nillable = nillable; this.required = required; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); if (methodName.equals("name")) { return name; } else if (methodName.equals("namespace")) { return namespace; } else if (methodName.equals("nillable")) { return nillable; } else if (methodName.equals("required")) { return required; } else { throw new WebServiceException("Not handling "+methodName); } } } /** * Computes and sorts exception bean members for a given exception as per * the 3.7 section of the spec. It takes all getter properties in the * exception and its superclasses(except getCause, getLocalizedMessage, * getStackTrace, getClass). The returned collection is sorted based * on the property names. * *

* But if the exception has @XmlType its values are honored. Only the * propOrder properties are considered. The returned collection is sorted * as per the given propOrder. * * @return list of properties in the correct order for an exception bean */ public Collection collectExceptionBeanMembers(C exception) { return collectExceptionBeanMembers(exception, true); } /** * Computes and sorts exception bean members for a given exception as per * the 3.7 section of the spec. It takes all getter properties in the * exception and its superclasses(except getCause, getLocalizedMessage, * getStackTrace, getClass). The returned collection is sorted based * on the property names. * *

* But if the exception has @XmlType its values are honored. Only the * propOrder properties are considered. The returned collection is sorted * as per the given propOrder. * * @param decapitalize if true, all the property names are decapitalized * * @return list of properties in the correct order for an exception bean */ public Collection collectExceptionBeanMembers(C exception, boolean decapitalize ) { TreeMap fields = new TreeMap(); getExceptionProperties(exception, fields, decapitalize); // Consider only the @XmlType(propOrder) properties XmlType xmlType = annReader.getClassAnnotation(XmlType.class, exception, null); if (xmlType != null) { String[] propOrder = xmlType.propOrder(); // If not the default order of properties, use that propOrder if (propOrder.length > 0 && propOrder[0].length() != 0) { List list = new ArrayList(); for(String prop : propOrder) { A a = fields.get(prop); if (a != null) { list.add(a); } else { throw new WebServiceException("Exception "+exception+ " has @XmlType and its propOrder contains unknown property "+prop); } } return list; } } return fields.values(); } private void getExceptionProperties(C exception, TreeMap fields, boolean decapitalize) { C sc = nav.getSuperClass(exception); if (sc != null) { getExceptionProperties(sc, fields, decapitalize); } Collection methods = nav.getDeclaredMethods(exception); for (M method : methods) { // 2.1.x is doing the following: no final static, transient, non-public // transient cannot used as modifier for method, so not doing it now if (!nav.isPublicMethod(method) || (nav.isStaticMethod(method) && nav.isFinalMethod(method))) { continue; } if (!nav.isPublicMethod(method)) { continue; } String name = nav.getMethodName(method); if (!(name.startsWith("get") || name.startsWith("is")) || skipProperties.contains(name) || name.equals("get") || name.equals("is")) { // Don't bother with invalid propertyNames. continue; } T returnType = getSafeType(nav.getReturnType(method)); if (nav.getMethodParameters(method).length == 0) { String fieldName = name.startsWith("get") ? name.substring(3) : name.substring(2); if (decapitalize) fieldName = StringUtils.decapitalize(fieldName); fields.put(fieldName, factory.createWrapperBeanMember(returnType, fieldName, Collections.emptyList())); } } } /** * Gets the property name by mangling using JAX-WS rules * @param name to be mangled * @return property name */ private static String getPropertyName(String name) { String propertyName = BindingHelper.mangleNameToVariableName(name); //We wont have to do this if JAXBRIContext.mangleNameToVariableName() takes //care of mangling java identifiers return getJavaReservedVarialbeName(propertyName); } //TODO MOVE Names.java to runtime (instead of doing the following) /* * See if its a java keyword name, if so then mangle the name */ private static @NotNull String getJavaReservedVarialbeName(@NotNull String name) { String reservedName = reservedWords.get(name); return reservedName == null ? name : reservedName; } private static final Map reservedWords; static { reservedWords = new HashMap(); reservedWords.put("abstract", "_abstract"); reservedWords.put("assert", "_assert"); reservedWords.put("boolean", "_boolean"); reservedWords.put("break", "_break"); reservedWords.put("byte", "_byte"); reservedWords.put("case", "_case"); reservedWords.put("catch", "_catch"); reservedWords.put("char", "_char"); reservedWords.put("class", "_class"); reservedWords.put("const", "_const"); reservedWords.put("continue", "_continue"); reservedWords.put("default", "_default"); reservedWords.put("do", "_do"); reservedWords.put("double", "_double"); reservedWords.put("else", "_else"); reservedWords.put("extends", "_extends"); reservedWords.put("false", "_false"); reservedWords.put("final", "_final"); reservedWords.put("finally", "_finally"); reservedWords.put("float", "_float"); reservedWords.put("for", "_for"); reservedWords.put("goto", "_goto"); reservedWords.put("if", "_if"); reservedWords.put("implements", "_implements"); reservedWords.put("import", "_import"); reservedWords.put("instanceof", "_instanceof"); reservedWords.put("int", "_int"); reservedWords.put("interface", "_interface"); reservedWords.put("long", "_long"); reservedWords.put("native", "_native"); reservedWords.put("new", "_new"); reservedWords.put("null", "_null"); reservedWords.put("package", "_package"); reservedWords.put("private", "_private"); reservedWords.put("protected", "_protected"); reservedWords.put("public", "_public"); reservedWords.put("return", "_return"); reservedWords.put("short", "_short"); reservedWords.put("static", "_static"); reservedWords.put("strictfp", "_strictfp"); reservedWords.put("super", "_super"); reservedWords.put("switch", "_switch"); reservedWords.put("synchronized", "_synchronized"); reservedWords.put("this", "_this"); reservedWords.put("throw", "_throw"); reservedWords.put("throws", "_throws"); reservedWords.put("transient", "_transient"); reservedWords.put("true", "_true"); reservedWords.put("try", "_try"); reservedWords.put("void", "_void"); reservedWords.put("volatile", "_volatile"); reservedWords.put("while", "_while"); reservedWords.put("enum", "_enum"); } private static Boolean getBooleanSystemProperty(final String prop) { return AccessController.doPrivileged( new java.security.PrivilegedAction() { public Boolean run() { return Boolean.getBoolean(prop); } } ); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy