src/share/jaxws_classes/com/sun/xml/internal/ws/model/AbstractWrapperBeanGenerator.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.model;
    28 import com.sun.istack.internal.NotNull;
    29 import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
    30 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
    31 import com.sun.xml.internal.ws.spi.db.BindingHelper;
    32 import com.sun.xml.internal.ws.util.StringUtils;
    34 import javax.jws.WebParam;
    35 import javax.jws.WebResult;
    36 import javax.xml.bind.annotation.*;
    37 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    38 import javax.xml.ws.WebServiceException;
    39 import java.lang.annotation.Annotation;
    40 import java.lang.reflect.InvocationHandler;
    41 import java.lang.reflect.Method;
    42 import java.lang.reflect.Proxy;
    43 import java.util.*;
    44 import java.util.logging.Logger;
    46 /**
    47  * Finds request/response wrapper and exception bean memebers.
    48  *
    49  * <p>
    50  * It uses JAXB's {@link AnnotationReader}, {@link Navigator} so that
    51  * tools can use this with annotation processing, and the runtime can use this with
    52  * reflection.
    53  *
    54  * @author Jitendra Kotamraju
    55  */
    56 public abstract class AbstractWrapperBeanGenerator<T,C,M,A extends Comparable> {
    58     private static final Logger LOGGER = Logger.getLogger(AbstractWrapperBeanGenerator.class.getName());
    60     private static final String RETURN = "return";
    61     private static final String EMTPY_NAMESPACE_ID = "";
    63     private static final Class[] jaxbAnns = new Class[] {
    64         XmlAttachmentRef.class, XmlMimeType.class, XmlJavaTypeAdapter.class,
    65         XmlList.class, XmlElement.class
    66     };
    68     private static final Set<String> skipProperties = new HashSet<String>();
    69     static{
    70         skipProperties.add("getCause");
    71         skipProperties.add("getLocalizedMessage");
    72         skipProperties.add("getClass");
    73         skipProperties.add("getStackTrace");
    74         skipProperties.add("getSuppressed");  // JDK 7 adds this
    75     }
    77     private final AnnotationReader<T,C,?,M> annReader;
    78     private final Navigator<T,C,?,M> nav;
    79     private final BeanMemberFactory<T,A> factory;
    81     protected AbstractWrapperBeanGenerator(AnnotationReader<T,C,?,M> annReader,
    82             Navigator<T,C,?,M> nav, BeanMemberFactory<T,A> factory) {
    83         this.annReader = annReader;
    84         this.nav = nav;
    85         this.factory = factory;
    86     }
    88     public static interface BeanMemberFactory<T,A> {
    89         A createWrapperBeanMember(T paramType, String paramName, List<Annotation> jaxbAnnotations);
    90     }
    92     // Collects the JAXB annotations on a method
    93     private List<Annotation> collectJAXBAnnotations(M method) {
    94         List<Annotation> jaxbAnnotation = new ArrayList<Annotation>();
    95         for(Class jaxbClass : jaxbAnns) {
    96             Annotation ann = annReader.getMethodAnnotation(jaxbClass, method, null);
    97             if (ann != null) {
    98                 jaxbAnnotation.add(ann);
    99             }
   100         }
   101         return jaxbAnnotation;
   102     }
   104     // Collects the JAXB annotations on a parameter
   105     private List<Annotation> collectJAXBAnnotations(M method, int paramIndex) {
   106         List<Annotation> jaxbAnnotation = new ArrayList<Annotation>();
   107         for(Class jaxbClass : jaxbAnns) {
   108             Annotation ann = annReader.getMethodParameterAnnotation(jaxbClass, method, paramIndex, null);
   109             if (ann != null) {
   110                 jaxbAnnotation.add(ann);
   111             }
   112         }
   113         return jaxbAnnotation;
   114     }
   116     protected abstract T getSafeType(T type);
   118     /**
   119      * Returns Holder's value type.
   120      *
   121      * @return null if it not a Holder, otherwise return Holder's value type
   122      */
   123     protected abstract T getHolderValueType(T type);
   125     protected abstract boolean isVoidType(T type);
   127     /**
   128      * Computes request bean members for a method. Collects all IN and INOUT
   129      * parameters as request bean fields. In this process, if a parameter
   130      * has any known JAXB annotations they are collected as well.
   131      * Special processing for @XmlElement annotation is done.
   132      *
   133      * @param method SEI method for which request bean members are computed
   134      * @return List of request bean members
   135      */
   136     public List<A> collectRequestBeanMembers(M method) {
   138         List<A> requestMembers = new ArrayList<A>();
   139         int paramIndex = -1;
   141         for (T param : nav.getMethodParameters(method)) {
   142             paramIndex++;
   143             WebParam webParam = annReader.getMethodParameterAnnotation(WebParam.class, method, paramIndex, null);
   144             if (webParam != null && (webParam.header() || webParam.mode().equals(WebParam.Mode.OUT))) {
   145                 continue;
   146             }
   147             T holderType = getHolderValueType(param);
   148 //            if (holderType != null && webParam != null && webParam.mode().equals(WebParam.Mode.IN)) {
   149 //                // Should we flag an error - holder cannot be IN part ??
   150 //                continue;
   151 //            }
   153             T paramType = (holderType != null) ? holderType : getSafeType(param);
   154             String paramName = (webParam != null && webParam.name().length() > 0)
   155                     ? webParam.name() : "arg"+paramIndex;
   156             String paramNamespace = (webParam != null && webParam.targetNamespace().length() > 0)
   157                     ? webParam.targetNamespace() : EMTPY_NAMESPACE_ID;
   159             // Collect JAXB annotations on a parameter
   160             List<Annotation> jaxbAnnotation = collectJAXBAnnotations(method, paramIndex);
   162             // If a parameter contains @XmlElement, process it.
   163             processXmlElement(jaxbAnnotation, paramName, paramNamespace, paramType);
   164             A member = factory.createWrapperBeanMember(paramType,
   165                     getPropertyName(paramName), jaxbAnnotation);
   166             requestMembers.add(member);
   167         }
   168         return requestMembers;
   169     }
   171     /**
   172      * Computes response bean members for a method. Collects all OUT and INOUT
   173      * parameters as response bean fields. In this process, if a parameter
   174      * has any known JAXB annotations they are collected as well.
   175      * Special processing for @XmlElement annotation is done.
   176      *
   177      * @param method SEI method for which response bean members are computed
   178      * @return List of response bean members
   179      */
   180     public List<A> collectResponseBeanMembers(M method) {
   182         List<A> responseMembers = new ArrayList<A>();
   184         // return that need to be part response wrapper bean
   185         String responseElementName = RETURN;
   186         String responseNamespace = EMTPY_NAMESPACE_ID;
   187         boolean isResultHeader = false;
   188         WebResult webResult = annReader.getMethodAnnotation(WebResult.class, method ,null);
   189         if (webResult != null) {
   190             if (webResult.name().length() > 0) {
   191                 responseElementName = webResult.name();
   192             }
   193             if (webResult.targetNamespace().length() > 0) {
   194                 responseNamespace = webResult.targetNamespace();
   195             }
   196             isResultHeader = webResult.header();
   197         }
   198         T returnType = getSafeType(nav.getReturnType(method));
   199         if (!isVoidType(returnType) && !isResultHeader) {
   200             List<Annotation> jaxbRespAnnotations = collectJAXBAnnotations(method);
   201             processXmlElement(jaxbRespAnnotations, responseElementName, responseNamespace, returnType);
   202             responseMembers.add(factory.createWrapperBeanMember(returnType, getPropertyName(responseElementName), jaxbRespAnnotations));
   203         }
   205         // Now parameters that need to be part response wrapper bean
   206         int paramIndex = -1;
   207         for (T param : nav.getMethodParameters(method)) {
   208             paramIndex++;
   210             T paramType = getHolderValueType(param);
   211             WebParam webParam = annReader.getMethodParameterAnnotation(WebParam.class, method, paramIndex, null);
   212             if (paramType == null || (webParam != null && webParam.header())) {
   213                 continue;       // not a holder or a header - so don't add it
   214             }
   216             String paramName = (webParam != null && webParam.name().length() > 0)
   217                     ? webParam.name() : "arg"+paramIndex;
   218             String paramNamespace = (webParam != null && webParam.targetNamespace().length() > 0)
   219                     ? webParam.targetNamespace() : EMTPY_NAMESPACE_ID;
   220             List<Annotation> jaxbAnnotation = collectJAXBAnnotations(method, paramIndex);
   221             processXmlElement(jaxbAnnotation, paramName, paramNamespace, paramType);
   222             A member = factory.createWrapperBeanMember(paramType,
   223                     getPropertyName(paramName), jaxbAnnotation);
   224             responseMembers.add(member);
   225         }
   227         return responseMembers;
   228     }
   230     private void processXmlElement(List<Annotation> jaxb, String elemName, String elemNS, T type) {
   231         XmlElement elemAnn = null;
   232         for (Annotation a : jaxb) {
   233             if (a.annotationType() == XmlElement.class) {
   234                 elemAnn = (XmlElement) a;
   235                 jaxb.remove(a);
   236                 break;
   237             }
   238         }
   239         String name = (elemAnn != null && !elemAnn.name().equals("##default"))
   240                 ? elemAnn.name() : elemName;
   242         String ns = (elemAnn != null && !elemAnn.namespace().equals("##default"))
   243                 ? elemAnn.namespace() : elemNS;
   245         boolean nillable = nav.isArray(type)
   246                 || (elemAnn != null && elemAnn.nillable());
   248         boolean required = elemAnn != null && elemAnn.required();
   249         XmlElementHandler handler = new XmlElementHandler(name, ns, nillable, required);
   250         XmlElement elem = (XmlElement) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[]{XmlElement.class}, handler);
   251         jaxb.add(elem);
   252     }
   255     private static class XmlElementHandler implements InvocationHandler {
   256         private String name;
   257         private String namespace;
   258         private boolean nillable;
   259         private boolean required;
   261         XmlElementHandler(String name, String namespace, boolean nillable,
   262                           boolean required) {
   263             this.name = name;
   264             this.namespace = namespace;
   265             this.nillable = nillable;
   266             this.required = required;
   267         }
   269         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
   270             String methodName = method.getName();
   271             if (methodName.equals("name")) {
   272                 return name;
   273             } else if (methodName.equals("namespace")) {
   274                 return namespace;
   275             } else if (methodName.equals("nillable")) {
   276                 return nillable;
   277             } else if (methodName.equals("required")) {
   278                 return required;
   279             } else {
   280                 throw new WebServiceException("Not handling "+methodName);
   281             }
   282         }
   283     }
   285     /**
   286      * Computes and sorts exception bean members for a given exception as per
   287      * the 3.7 section of the spec. It takes all getter properties in the
   288      * exception and its superclasses(except getCause, getLocalizedMessage,
   289      * getStackTrace, getClass). The returned collection is sorted based
   290      * on the property names.
   291      *
   292      * <p>
   293      * But if the exception has @XmlType its values are honored. Only the
   294      * propOrder properties are considered. The returned collection is sorted
   295      * as per the given propOrder.
   296      *
   297      * @param exception
   298      * @return list of properties in the correct order for an exception bean
   299      */
   300     public Collection<A> collectExceptionBeanMembers(C exception) {
   301         return collectExceptionBeanMembers(exception, true);
   302     }
   304     /**
   305      * Computes and sorts exception bean members for a given exception as per
   306      * the 3.7 section of the spec. It takes all getter properties in the
   307      * exception and its superclasses(except getCause, getLocalizedMessage,
   308      * getStackTrace, getClass). The returned collection is sorted based
   309      * on the property names.
   310      *
   311      * <p>
   312      * But if the exception has @XmlType its values are honored. Only the
   313      * propOrder properties are considered. The returned collection is sorted
   314      * as per the given propOrder.
   315      *
   316      * @param exception
   317      * @param decapitalize if true, all the property names are decapitalized
   318      *
   319      * @return list of properties in the correct order for an exception bean
   320      */
   321    public Collection<A> collectExceptionBeanMembers(C exception, boolean decapitalize ) {
   322         TreeMap<String, A> fields = new TreeMap<String, A>();
   323         getExceptionProperties(exception, fields, decapitalize);
   325         // Consider only the @XmlType(propOrder) properties
   326         XmlType xmlType = annReader.getClassAnnotation(XmlType.class, exception, null);
   327         if (xmlType != null) {
   328             String[] propOrder = xmlType.propOrder();
   329             // If not the default order of properties, use that propOrder
   330             if (propOrder.length > 0 && propOrder[0].length() != 0) {
   331                 List<A> list = new ArrayList<A>();
   332                 for(String prop : propOrder) {
   333                     A a = fields.get(prop);
   334                     if (a != null) {
   335                         list.add(a);
   336                     } else {
   337                         throw new WebServiceException("Exception "+exception+
   338                                 " has @XmlType and its propOrder contains unknown property "+prop);
   339                     }
   340                 }
   341                 return list;
   342             }
   343         }
   345         return fields.values();
   346     }
   349     private void getExceptionProperties(C exception, TreeMap<String, A> fields, boolean decapitalize) {
   350         C sc = nav.getSuperClass(exception);
   351         if (sc != null) {
   352             getExceptionProperties(sc, fields, decapitalize);
   353         }
   354         Collection<? extends M> methods = nav.getDeclaredMethods(exception);
   356         for (M method : methods) {
   358             // 2.1.x is doing the following: no final static, transient, non-public
   359             // transient cannot used as modifier for method, so not doing it now
   360             if (!nav.isPublicMethod(method)
   361                 || (nav.isStaticMethod(method) && nav.isFinalMethod(method))) {
   362                  continue;
   363             }
   365             if (!nav.isPublicMethod(method)) {
   366                 continue;
   367             }
   369             String name = nav.getMethodName(method);
   371             if (!(name.startsWith("get") || name.startsWith("is")) || skipProperties.contains(name) ||
   372                     name.equals("get") || name.equals("is")) {
   373                 // Don't bother with invalid propertyNames.
   374                 continue;
   375             }
   377             T returnType = getSafeType(nav.getReturnType(method));
   378             if (nav.getMethodParameters(method).length == 0) {
   379                 String fieldName = name.startsWith("get") ? name.substring(3) : name.substring(2);
   380                 if (decapitalize) fieldName = StringUtils.decapitalize(fieldName);
   381                 fields.put(fieldName, factory.createWrapperBeanMember(returnType, fieldName, Collections.<Annotation>emptyList()));
   382             }
   383         }
   385     }
   387     /**
   388      * Gets the property name by mangling using JAX-WS rules
   389      * @param name to be mangled
   390      * @return property name
   391      */
   392     private static String getPropertyName(String name) {
   393         String propertyName = BindingHelper.mangleNameToVariableName(name);
   394         //We wont have to do this if JAXBRIContext.mangleNameToVariableName() takes
   395         //care of mangling java identifiers
   396         return getJavaReservedVarialbeName(propertyName);
   397     }
   400     //TODO MOVE Names.java to runtime (instead of doing the following)
   401     /*
   402      * See if its a java keyword name, if so then mangle the name
   403      */
   404     private static @NotNull String getJavaReservedVarialbeName(@NotNull String name) {
   405         String reservedName = reservedWords.get(name);
   406         return reservedName == null ? name : reservedName;
   407     }
   409     private static final Map<String, String> reservedWords;
   411     static {
   412         reservedWords = new HashMap<String, String>();
   413         reservedWords.put("abstract", "_abstract");
   414         reservedWords.put("assert", "_assert");
   415         reservedWords.put("boolean", "_boolean");
   416         reservedWords.put("break", "_break");
   417         reservedWords.put("byte", "_byte");
   418         reservedWords.put("case", "_case");
   419         reservedWords.put("catch", "_catch");
   420         reservedWords.put("char", "_char");
   421         reservedWords.put("class", "_class");
   422         reservedWords.put("const", "_const");
   423         reservedWords.put("continue", "_continue");
   424         reservedWords.put("default", "_default");
   425         reservedWords.put("do", "_do");
   426         reservedWords.put("double", "_double");
   427         reservedWords.put("else", "_else");
   428         reservedWords.put("extends", "_extends");
   429         reservedWords.put("false", "_false");
   430         reservedWords.put("final", "_final");
   431         reservedWords.put("finally", "_finally");
   432         reservedWords.put("float", "_float");
   433         reservedWords.put("for", "_for");
   434         reservedWords.put("goto", "_goto");
   435         reservedWords.put("if", "_if");
   436         reservedWords.put("implements", "_implements");
   437         reservedWords.put("import", "_import");
   438         reservedWords.put("instanceof", "_instanceof");
   439         reservedWords.put("int", "_int");
   440         reservedWords.put("interface", "_interface");
   441         reservedWords.put("long", "_long");
   442         reservedWords.put("native", "_native");
   443         reservedWords.put("new", "_new");
   444         reservedWords.put("null", "_null");
   445         reservedWords.put("package", "_package");
   446         reservedWords.put("private", "_private");
   447         reservedWords.put("protected", "_protected");
   448         reservedWords.put("public", "_public");
   449         reservedWords.put("return", "_return");
   450         reservedWords.put("short", "_short");
   451         reservedWords.put("static", "_static");
   452         reservedWords.put("strictfp", "_strictfp");
   453         reservedWords.put("super", "_super");
   454         reservedWords.put("switch", "_switch");
   455         reservedWords.put("synchronized", "_synchronized");
   456         reservedWords.put("this", "_this");
   457         reservedWords.put("throw", "_throw");
   458         reservedWords.put("throws", "_throws");
   459         reservedWords.put("transient", "_transient");
   460         reservedWords.put("true", "_true");
   461         reservedWords.put("try", "_try");
   462         reservedWords.put("void", "_void");
   463         reservedWords.put("volatile", "_volatile");
   464         reservedWords.put("while", "_while");
   465         reservedWords.put("enum", "_enum");
   466     }
   468 }

mercurial