Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
1 /*
2 * Copyright (c) 1997, 2010, 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.tools.internal.ws.processor.modeler.annotation;
28 import javax.annotation.processing.ProcessingEnvironment;
29 import javax.lang.model.element.ElementKind;
30 import javax.lang.model.element.ExecutableElement;
31 import javax.lang.model.element.TypeElement;
32 import javax.lang.model.element.VariableElement;
33 import javax.lang.model.type.DeclaredType;
34 import javax.lang.model.type.TypeKind;
35 import javax.lang.model.type.TypeMirror;
36 import javax.lang.model.util.ElementFilter;
37 import java.util.Collection;
39 /**
40 * @author WS Development Team
41 */
42 final class TypeModeler {
44 private TypeModeler() {
45 }
47 public static TypeElement getDeclaration(TypeMirror typeMirror) {
48 if (typeMirror != null && typeMirror.getKind().equals(TypeKind.DECLARED))
49 return (TypeElement) ((DeclaredType) typeMirror).asElement();
50 return null;
51 }
53 public static TypeElement getDeclaringClassMethod(TypeMirror theClass, String methodName, TypeMirror[] args) {
54 return getDeclaringClassMethod(getDeclaration(theClass), methodName, args);
55 }
57 public static TypeElement getDeclaringClassMethod(TypeElement theClass, String methodName, TypeMirror[] args) {
59 TypeElement retClass = null;
60 if (theClass.getKind().equals(ElementKind.CLASS)) {
61 TypeMirror superClass = theClass.getSuperclass();
62 if (!superClass.getKind().equals(TypeKind.NONE))
63 retClass = getDeclaringClassMethod(superClass, methodName, args);
64 }
65 if (retClass == null) {
66 for (TypeMirror interfaceType : theClass.getInterfaces())
67 retClass = getDeclaringClassMethod(interfaceType, methodName, args);
68 }
69 if (retClass == null) {
70 Collection<? extends ExecutableElement> methods = ElementFilter.methodsIn(theClass.getEnclosedElements());
71 for (ExecutableElement method : methods) {
72 if (method.getSimpleName().toString().equals(methodName)) {
73 retClass = theClass;
74 break;
75 }
76 }
77 }
78 return retClass;
79 }
81 public static Collection<DeclaredType> collectInterfaces(TypeElement type) {
82 @SuppressWarnings({"unchecked"})
83 Collection<DeclaredType> interfaces = (Collection<DeclaredType>) type.getInterfaces();
84 for (TypeMirror interfaceType : type.getInterfaces()) {
85 interfaces.addAll(collectInterfaces(getDeclaration(interfaceType)));
86 }
87 return interfaces;
88 }
90 public static boolean isSubclass(String subTypeName, String superTypeName, ProcessingEnvironment env) {
91 return isSubclass(env.getElementUtils().getTypeElement(subTypeName), env.getElementUtils().getTypeElement(superTypeName), env);
92 }
94 public static boolean isSubclass(TypeElement subType, TypeElement superType, ProcessingEnvironment env) {
95 return !subType.equals(superType) && isSubElement(subType, superType);
96 }
98 public static TypeMirror getHolderValueType(TypeMirror type, TypeElement defHolder, ProcessingEnvironment env) {
99 TypeElement typeElement = getDeclaration(type);
100 if (typeElement == null)
101 return null;
103 if (isSubElement(typeElement, defHolder)) {
104 if (type.getKind().equals(TypeKind.DECLARED)) {
105 Collection<? extends TypeMirror> argTypes = ((DeclaredType) type).getTypeArguments();
106 if (argTypes.size() == 1) {
107 return argTypes.iterator().next();
108 } else if (argTypes.size() == 0) {
109 VariableElement member = getValueMember(typeElement);
110 if (member != null) {
111 return member.asType();
112 }
113 }
114 }
115 }
116 return null;
117 }
119 public static VariableElement getValueMember(TypeMirror classType) {
120 return getValueMember(getDeclaration(classType));
121 }
123 public static VariableElement getValueMember(TypeElement type) {
124 VariableElement member = null;
125 for (VariableElement field : ElementFilter.fieldsIn(type.getEnclosedElements())) {
126 if ("value".equals(field.getSimpleName().toString())) {
127 member = field;
128 break;
129 }
130 }
131 if (member == null && type.getKind().equals(ElementKind.CLASS))
132 member = getValueMember(type.getSuperclass());
133 return member;
134 }
136 public static boolean isSubElement(TypeElement d1, TypeElement d2) {
137 if (d1.equals(d2))
138 return true;
139 TypeElement superClassDecl = null;
140 if (d1.getKind().equals(ElementKind.CLASS)) {
141 TypeMirror superClass = d1.getSuperclass();
142 if (!superClass.getKind().equals(TypeKind.NONE)) {
143 superClassDecl = (TypeElement) ((DeclaredType) superClass).asElement();
144 if (superClassDecl.equals(d2))
145 return true;
146 }
147 }
148 for (TypeMirror superIntf : d1.getInterfaces()) {
149 DeclaredType declaredSuperIntf = (DeclaredType) superIntf;
150 if (declaredSuperIntf.asElement().equals(d2)) {
151 return true;
152 }
153 if (isSubElement((TypeElement) declaredSuperIntf.asElement(), d2)) {
154 return true;
155 } else if (superClassDecl != null && isSubElement(superClassDecl, d2)) {
156 return true;
157 }
158 }
159 return false;
160 }
162 }