src/share/jaxws_classes/com/sun/tools/internal/ws/processor/modeler/annotation/TypeModeler.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2013, 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         }
    70         if (retClass == null) {
    71             Collection<? extends ExecutableElement> methods = ElementFilter.methodsIn(theClass.getEnclosedElements());
    72             for (ExecutableElement method : methods) {
    73                 if (method.getSimpleName().toString().equals(methodName)) {
    74                     retClass = theClass;
    75                     break;
    76                 }
    77             }
    78         }
    79         return retClass;
    80     }
    82     public static Collection<DeclaredType> collectInterfaces(TypeElement type) {
    83         @SuppressWarnings({"unchecked"})
    84         Collection<DeclaredType> interfaces = (Collection<DeclaredType>) type.getInterfaces();
    85         for (TypeMirror interfaceType : type.getInterfaces()) {
    86             interfaces.addAll(collectInterfaces(getDeclaration(interfaceType)));
    87         }
    88         return interfaces;
    89     }
    91     public static boolean isSubclass(String subTypeName, String superTypeName, ProcessingEnvironment env) {
    92         return isSubclass(env.getElementUtils().getTypeElement(subTypeName), env.getElementUtils().getTypeElement(superTypeName), env);
    93     }
    95     public static boolean isSubclass(TypeElement subType, TypeElement superType, ProcessingEnvironment env) {
    96         return !subType.equals(superType) && isSubElement(subType, superType);
    97     }
    99     public static TypeMirror getHolderValueType(TypeMirror type, TypeElement defHolder, ProcessingEnvironment env) {
   100         TypeElement typeElement = getDeclaration(type);
   101         if (typeElement == null)
   102             return null;
   104         if (isSubElement(typeElement, defHolder)) {
   105             if (type.getKind().equals(TypeKind.DECLARED)) {
   106                 Collection<? extends TypeMirror> argTypes = ((DeclaredType) type).getTypeArguments();
   107                 if (argTypes.size() == 1) {
   108                     return argTypes.iterator().next();
   109                 } else if (argTypes.isEmpty()) {
   110                     VariableElement member = getValueMember(typeElement);
   111                     if (member != null) {
   112                         return member.asType();
   113                     }
   114                 }
   115             }
   116         }
   117         return null;
   118     }
   120     public static VariableElement getValueMember(TypeMirror classType) {
   121         return getValueMember(getDeclaration(classType));
   122     }
   124     public static VariableElement getValueMember(TypeElement type) {
   125         VariableElement member = null;
   126         for (VariableElement field : ElementFilter.fieldsIn(type.getEnclosedElements())) {
   127             if ("value".equals(field.getSimpleName().toString())) {
   128                 member = field;
   129                 break;
   130             }
   131         }
   132         if (member == null && type.getKind().equals(ElementKind.CLASS))
   133             member = getValueMember(type.getSuperclass());
   134         return member;
   135     }
   137     public static boolean isSubElement(TypeElement d1, TypeElement d2) {
   138         if (d1.equals(d2))
   139             return true;
   140         TypeElement superClassDecl = null;
   141         if (d1.getKind().equals(ElementKind.CLASS)) {
   142             TypeMirror superClass = d1.getSuperclass();
   143             if (!superClass.getKind().equals(TypeKind.NONE)) {
   144                 superClassDecl = (TypeElement) ((DeclaredType) superClass).asElement();
   145                 if (superClassDecl.equals(d2))
   146                     return true;
   147             }
   148         }
   149         for (TypeMirror superIntf : d1.getInterfaces()) {
   150             DeclaredType declaredSuperIntf = (DeclaredType) superIntf;
   151             if (declaredSuperIntf.asElement().equals(d2)) {
   152                 return true;
   153             }
   154             if (isSubElement((TypeElement) declaredSuperIntf.asElement(), d2)) {
   155                 return true;
   156             } else if (superClassDecl != null && isSubElement(superClassDecl, d2)) {
   157                 return true;
   158             }
   159         }
   160         return false;
   161     }
   163 }

mercurial