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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/processor/modeler/annotation/TypeModeler.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,163 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.ws.processor.modeler.annotation;
    1.30 +
    1.31 +import javax.annotation.processing.ProcessingEnvironment;
    1.32 +import javax.lang.model.element.ElementKind;
    1.33 +import javax.lang.model.element.ExecutableElement;
    1.34 +import javax.lang.model.element.TypeElement;
    1.35 +import javax.lang.model.element.VariableElement;
    1.36 +import javax.lang.model.type.DeclaredType;
    1.37 +import javax.lang.model.type.TypeKind;
    1.38 +import javax.lang.model.type.TypeMirror;
    1.39 +import javax.lang.model.util.ElementFilter;
    1.40 +import java.util.Collection;
    1.41 +
    1.42 +/**
    1.43 + * @author WS Development Team
    1.44 + */
    1.45 +final class TypeModeler {
    1.46 +
    1.47 +    private TypeModeler() {
    1.48 +    }
    1.49 +
    1.50 +    public static TypeElement getDeclaration(TypeMirror typeMirror) {
    1.51 +        if (typeMirror != null && typeMirror.getKind().equals(TypeKind.DECLARED))
    1.52 +            return (TypeElement) ((DeclaredType) typeMirror).asElement();
    1.53 +        return null;
    1.54 +    }
    1.55 +
    1.56 +    public static TypeElement getDeclaringClassMethod(TypeMirror theClass, String methodName, TypeMirror[] args) {
    1.57 +        return getDeclaringClassMethod(getDeclaration(theClass), methodName, args);
    1.58 +    }
    1.59 +
    1.60 +    public static TypeElement getDeclaringClassMethod(TypeElement theClass, String methodName, TypeMirror[] args) {
    1.61 +
    1.62 +        TypeElement retClass = null;
    1.63 +        if (theClass.getKind().equals(ElementKind.CLASS)) {
    1.64 +            TypeMirror superClass = theClass.getSuperclass();
    1.65 +            if (!superClass.getKind().equals(TypeKind.NONE))
    1.66 +                retClass = getDeclaringClassMethod(superClass, methodName, args);
    1.67 +        }
    1.68 +        if (retClass == null) {
    1.69 +            for (TypeMirror interfaceType : theClass.getInterfaces()) {
    1.70 +                retClass = getDeclaringClassMethod(interfaceType, methodName, args);
    1.71 +            }
    1.72 +        }
    1.73 +        if (retClass == null) {
    1.74 +            Collection<? extends ExecutableElement> methods = ElementFilter.methodsIn(theClass.getEnclosedElements());
    1.75 +            for (ExecutableElement method : methods) {
    1.76 +                if (method.getSimpleName().toString().equals(methodName)) {
    1.77 +                    retClass = theClass;
    1.78 +                    break;
    1.79 +                }
    1.80 +            }
    1.81 +        }
    1.82 +        return retClass;
    1.83 +    }
    1.84 +
    1.85 +    public static Collection<DeclaredType> collectInterfaces(TypeElement type) {
    1.86 +        @SuppressWarnings({"unchecked"})
    1.87 +        Collection<DeclaredType> interfaces = (Collection<DeclaredType>) type.getInterfaces();
    1.88 +        for (TypeMirror interfaceType : type.getInterfaces()) {
    1.89 +            interfaces.addAll(collectInterfaces(getDeclaration(interfaceType)));
    1.90 +        }
    1.91 +        return interfaces;
    1.92 +    }
    1.93 +
    1.94 +    public static boolean isSubclass(String subTypeName, String superTypeName, ProcessingEnvironment env) {
    1.95 +        return isSubclass(env.getElementUtils().getTypeElement(subTypeName), env.getElementUtils().getTypeElement(superTypeName), env);
    1.96 +    }
    1.97 +
    1.98 +    public static boolean isSubclass(TypeElement subType, TypeElement superType, ProcessingEnvironment env) {
    1.99 +        return !subType.equals(superType) && isSubElement(subType, superType);
   1.100 +    }
   1.101 +
   1.102 +    public static TypeMirror getHolderValueType(TypeMirror type, TypeElement defHolder, ProcessingEnvironment env) {
   1.103 +        TypeElement typeElement = getDeclaration(type);
   1.104 +        if (typeElement == null)
   1.105 +            return null;
   1.106 +
   1.107 +        if (isSubElement(typeElement, defHolder)) {
   1.108 +            if (type.getKind().equals(TypeKind.DECLARED)) {
   1.109 +                Collection<? extends TypeMirror> argTypes = ((DeclaredType) type).getTypeArguments();
   1.110 +                if (argTypes.size() == 1) {
   1.111 +                    return argTypes.iterator().next();
   1.112 +                } else if (argTypes.isEmpty()) {
   1.113 +                    VariableElement member = getValueMember(typeElement);
   1.114 +                    if (member != null) {
   1.115 +                        return member.asType();
   1.116 +                    }
   1.117 +                }
   1.118 +            }
   1.119 +        }
   1.120 +        return null;
   1.121 +    }
   1.122 +
   1.123 +    public static VariableElement getValueMember(TypeMirror classType) {
   1.124 +        return getValueMember(getDeclaration(classType));
   1.125 +    }
   1.126 +
   1.127 +    public static VariableElement getValueMember(TypeElement type) {
   1.128 +        VariableElement member = null;
   1.129 +        for (VariableElement field : ElementFilter.fieldsIn(type.getEnclosedElements())) {
   1.130 +            if ("value".equals(field.getSimpleName().toString())) {
   1.131 +                member = field;
   1.132 +                break;
   1.133 +            }
   1.134 +        }
   1.135 +        if (member == null && type.getKind().equals(ElementKind.CLASS))
   1.136 +            member = getValueMember(type.getSuperclass());
   1.137 +        return member;
   1.138 +    }
   1.139 +
   1.140 +    public static boolean isSubElement(TypeElement d1, TypeElement d2) {
   1.141 +        if (d1.equals(d2))
   1.142 +            return true;
   1.143 +        TypeElement superClassDecl = null;
   1.144 +        if (d1.getKind().equals(ElementKind.CLASS)) {
   1.145 +            TypeMirror superClass = d1.getSuperclass();
   1.146 +            if (!superClass.getKind().equals(TypeKind.NONE)) {
   1.147 +                superClassDecl = (TypeElement) ((DeclaredType) superClass).asElement();
   1.148 +                if (superClassDecl.equals(d2))
   1.149 +                    return true;
   1.150 +            }
   1.151 +        }
   1.152 +        for (TypeMirror superIntf : d1.getInterfaces()) {
   1.153 +            DeclaredType declaredSuperIntf = (DeclaredType) superIntf;
   1.154 +            if (declaredSuperIntf.asElement().equals(d2)) {
   1.155 +                return true;
   1.156 +            }
   1.157 +            if (isSubElement((TypeElement) declaredSuperIntf.asElement(), d2)) {
   1.158 +                return true;
   1.159 +            } else if (superClassDecl != null && isSubElement(superClassDecl, d2)) {
   1.160 +                return true;
   1.161 +            }
   1.162 +        }
   1.163 +        return false;
   1.164 +    }
   1.165 +
   1.166 +}

mercurial