aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.ws.processor.modeler.annotation; aoqi@0: aoqi@0: import javax.annotation.processing.ProcessingEnvironment; aoqi@0: import javax.lang.model.element.ElementKind; aoqi@0: import javax.lang.model.element.ExecutableElement; aoqi@0: import javax.lang.model.element.TypeElement; aoqi@0: import javax.lang.model.element.VariableElement; aoqi@0: import javax.lang.model.type.DeclaredType; aoqi@0: import javax.lang.model.type.TypeKind; aoqi@0: import javax.lang.model.type.TypeMirror; aoqi@0: import javax.lang.model.util.ElementFilter; aoqi@0: import java.util.Collection; aoqi@0: aoqi@0: /** aoqi@0: * @author WS Development Team aoqi@0: */ aoqi@0: final class TypeModeler { aoqi@0: aoqi@0: private TypeModeler() { aoqi@0: } aoqi@0: aoqi@0: public static TypeElement getDeclaration(TypeMirror typeMirror) { aoqi@0: if (typeMirror != null && typeMirror.getKind().equals(TypeKind.DECLARED)) aoqi@0: return (TypeElement) ((DeclaredType) typeMirror).asElement(); aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public static TypeElement getDeclaringClassMethod(TypeMirror theClass, String methodName, TypeMirror[] args) { aoqi@0: return getDeclaringClassMethod(getDeclaration(theClass), methodName, args); aoqi@0: } aoqi@0: aoqi@0: public static TypeElement getDeclaringClassMethod(TypeElement theClass, String methodName, TypeMirror[] args) { aoqi@0: aoqi@0: TypeElement retClass = null; aoqi@0: if (theClass.getKind().equals(ElementKind.CLASS)) { aoqi@0: TypeMirror superClass = theClass.getSuperclass(); aoqi@0: if (!superClass.getKind().equals(TypeKind.NONE)) aoqi@0: retClass = getDeclaringClassMethod(superClass, methodName, args); aoqi@0: } aoqi@0: if (retClass == null) { aoqi@0: for (TypeMirror interfaceType : theClass.getInterfaces()) { aoqi@0: retClass = getDeclaringClassMethod(interfaceType, methodName, args); aoqi@0: } aoqi@0: } aoqi@0: if (retClass == null) { aoqi@0: Collection methods = ElementFilter.methodsIn(theClass.getEnclosedElements()); aoqi@0: for (ExecutableElement method : methods) { aoqi@0: if (method.getSimpleName().toString().equals(methodName)) { aoqi@0: retClass = theClass; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return retClass; aoqi@0: } aoqi@0: aoqi@0: public static Collection collectInterfaces(TypeElement type) { aoqi@0: @SuppressWarnings({"unchecked"}) aoqi@0: Collection interfaces = (Collection) type.getInterfaces(); aoqi@0: for (TypeMirror interfaceType : type.getInterfaces()) { aoqi@0: interfaces.addAll(collectInterfaces(getDeclaration(interfaceType))); aoqi@0: } aoqi@0: return interfaces; aoqi@0: } aoqi@0: aoqi@0: public static boolean isSubclass(String subTypeName, String superTypeName, ProcessingEnvironment env) { aoqi@0: return isSubclass(env.getElementUtils().getTypeElement(subTypeName), env.getElementUtils().getTypeElement(superTypeName), env); aoqi@0: } aoqi@0: aoqi@0: public static boolean isSubclass(TypeElement subType, TypeElement superType, ProcessingEnvironment env) { aoqi@0: return !subType.equals(superType) && isSubElement(subType, superType); aoqi@0: } aoqi@0: aoqi@0: public static TypeMirror getHolderValueType(TypeMirror type, TypeElement defHolder, ProcessingEnvironment env) { aoqi@0: TypeElement typeElement = getDeclaration(type); aoqi@0: if (typeElement == null) aoqi@0: return null; aoqi@0: aoqi@0: if (isSubElement(typeElement, defHolder)) { aoqi@0: if (type.getKind().equals(TypeKind.DECLARED)) { aoqi@0: Collection argTypes = ((DeclaredType) type).getTypeArguments(); aoqi@0: if (argTypes.size() == 1) { aoqi@0: return argTypes.iterator().next(); aoqi@0: } else if (argTypes.isEmpty()) { aoqi@0: VariableElement member = getValueMember(typeElement); aoqi@0: if (member != null) { aoqi@0: return member.asType(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: public static VariableElement getValueMember(TypeMirror classType) { aoqi@0: return getValueMember(getDeclaration(classType)); aoqi@0: } aoqi@0: aoqi@0: public static VariableElement getValueMember(TypeElement type) { aoqi@0: VariableElement member = null; aoqi@0: for (VariableElement field : ElementFilter.fieldsIn(type.getEnclosedElements())) { aoqi@0: if ("value".equals(field.getSimpleName().toString())) { aoqi@0: member = field; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: if (member == null && type.getKind().equals(ElementKind.CLASS)) aoqi@0: member = getValueMember(type.getSuperclass()); aoqi@0: return member; aoqi@0: } aoqi@0: aoqi@0: public static boolean isSubElement(TypeElement d1, TypeElement d2) { aoqi@0: if (d1.equals(d2)) aoqi@0: return true; aoqi@0: TypeElement superClassDecl = null; aoqi@0: if (d1.getKind().equals(ElementKind.CLASS)) { aoqi@0: TypeMirror superClass = d1.getSuperclass(); aoqi@0: if (!superClass.getKind().equals(TypeKind.NONE)) { aoqi@0: superClassDecl = (TypeElement) ((DeclaredType) superClass).asElement(); aoqi@0: if (superClassDecl.equals(d2)) aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: for (TypeMirror superIntf : d1.getInterfaces()) { aoqi@0: DeclaredType declaredSuperIntf = (DeclaredType) superIntf; aoqi@0: if (declaredSuperIntf.asElement().equals(d2)) { aoqi@0: return true; aoqi@0: } aoqi@0: if (isSubElement((TypeElement) declaredSuperIntf.asElement(), d2)) { aoqi@0: return true; aoqi@0: } else if (superClassDecl != null && isSubElement(superClassDecl, d2)) { aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: }