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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial