langtools/src/share/classes/com/sun/tools/apt/mirror/util/DeclarationsImpl.java
changeset 11864 116173ff7d77
parent 11711 3a9d57fab406
child 11865 8dc106e1925e
equal deleted inserted replaced
11711:3a9d57fab406 11864:116173ff7d77
     1 /*
       
     2  * Copyright (c) 2004, 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  */
       
    25 
       
    26 package com.sun.tools.apt.mirror.util;
       
    27 
       
    28 
       
    29 import com.sun.mirror.declaration.*;
       
    30 import com.sun.mirror.util.Declarations;
       
    31 import com.sun.tools.apt.mirror.declaration.DeclarationImpl;
       
    32 import com.sun.tools.apt.mirror.declaration.MethodDeclarationImpl;
       
    33 import com.sun.tools.apt.mirror.util.DeclarationsImpl;
       
    34 import com.sun.tools.apt.mirror.AptEnv;
       
    35 import com.sun.tools.javac.code.*;
       
    36 import com.sun.tools.javac.code.Symbol.*;
       
    37 import com.sun.tools.javac.util.Context;
       
    38 
       
    39 import static com.sun.tools.javac.code.Kinds.*;
       
    40 
       
    41 
       
    42 /**
       
    43  * Implementation of Declarations utility methods for annotation processors
       
    44  */
       
    45 @SuppressWarnings("deprecation")
       
    46 public class DeclarationsImpl implements Declarations {
       
    47 
       
    48     private final AptEnv env;
       
    49 
       
    50 
       
    51     private static final Context.Key<Declarations> declarationsKey =
       
    52             new Context.Key<Declarations>();
       
    53 
       
    54     public static Declarations instance(Context context) {
       
    55         Declarations instance = context.get(declarationsKey);
       
    56         if (instance == null) {
       
    57             instance = new DeclarationsImpl(context);
       
    58         }
       
    59         return instance;
       
    60     }
       
    61 
       
    62     private DeclarationsImpl(Context context) {
       
    63         context.put(declarationsKey, this);
       
    64         env = AptEnv.instance(context);
       
    65     }
       
    66 
       
    67 
       
    68     /**
       
    69      * {@inheritDoc}
       
    70      * See sections 8.3 and 8.4.6 of
       
    71      * <cite>The Java&trade; Language Specification</cite>
       
    72      */
       
    73     public boolean hides(MemberDeclaration sub, MemberDeclaration sup) {
       
    74         Symbol hider = ((DeclarationImpl) sub).sym;
       
    75         Symbol hidee = ((DeclarationImpl) sup).sym;
       
    76 
       
    77         // Fields only hide fields; methods only methods; types only types.
       
    78         // Names must match.  Nothing hides itself (just try it).
       
    79         if (hider == hidee ||
       
    80                 hider.kind != hidee.kind ||
       
    81                 hider.name != hidee.name) {
       
    82             return false;
       
    83         }
       
    84 
       
    85         // Only static methods can hide other methods.
       
    86         // Methods only hide methods with matching signatures.
       
    87         if (hider.kind == MTH) {
       
    88             if ((hider.flags() & Flags.STATIC) == 0 ||
       
    89                         !env.jctypes.isSubSignature(hider.type, hidee.type)) {
       
    90                 return false;
       
    91             }
       
    92         }
       
    93 
       
    94         // Hider must be in a subclass of hidee's class.
       
    95         // Note that if M1 hides M2, and M2 hides M3, and M3 is accessible
       
    96         // in M1's class, then M1 and M2 both hide M3.
       
    97         ClassSymbol hiderClass = hider.owner.enclClass();
       
    98         ClassSymbol hideeClass = hidee.owner.enclClass();
       
    99         if (hiderClass == null || hideeClass == null ||
       
   100                 !hiderClass.isSubClass(hideeClass, env.jctypes)) {
       
   101             return false;
       
   102         }
       
   103 
       
   104         // Hidee must be accessible in hider's class.
       
   105         // The method isInheritedIn is poorly named:  it checks only access.
       
   106         return hidee.isInheritedIn(hiderClass, env.jctypes);
       
   107     }
       
   108 
       
   109     /**
       
   110      * {@inheritDoc}
       
   111      * See section 8.4.6.1 of
       
   112      * <cite>The Java&trade; Language Specification</cite>
       
   113      */
       
   114     public boolean overrides(MethodDeclaration sub, MethodDeclaration sup) {
       
   115         MethodSymbol overrider = ((MethodDeclarationImpl) sub).sym;
       
   116         MethodSymbol overridee = ((MethodDeclarationImpl) sup).sym;
       
   117         ClassSymbol origin = (ClassSymbol) overrider.owner;
       
   118 
       
   119         return overrider.name == overridee.name &&
       
   120 
       
   121                // not reflexive as per JLS
       
   122                overrider != overridee &&
       
   123 
       
   124                // we don't care if overridee is static, though that wouldn't
       
   125                // compile
       
   126                !overrider.isStatic() &&
       
   127 
       
   128                // overrider, whose declaring type is the origin, must be
       
   129                // in a subtype of overridee's type
       
   130                env.jctypes.asSuper(origin.type, overridee.owner) != null &&
       
   131 
       
   132                // check access and signatures; don't check return types
       
   133                overrider.overrides(overridee, origin, env.jctypes, false);
       
   134     }
       
   135 }