langtools/src/share/classes/com/sun/tools/javac/comp/MemberEnter.java
changeset 22163 3651128c74eb
parent 21498 58c2486db8d0
child 22442 8fd30fc4e3a3
equal deleted inserted replaced
22162:3b3e23e67329 22163:3651128c74eb
    60  *  If you write code that depends on this, you do so at your own risk.
    60  *  If you write code that depends on this, you do so at your own risk.
    61  *  This code and its internal interfaces are subject to change or
    61  *  This code and its internal interfaces are subject to change or
    62  *  deletion without notice.</b>
    62  *  deletion without notice.</b>
    63  */
    63  */
    64 public class MemberEnter extends JCTree.Visitor implements Completer {
    64 public class MemberEnter extends JCTree.Visitor implements Completer {
    65     protected static final Context.Key<MemberEnter> memberEnterKey =
    65     protected static final Context.Key<MemberEnter> memberEnterKey = new Context.Key<>();
    66         new Context.Key<MemberEnter>();
       
    67 
    66 
    68     /** A switch to determine whether we check for package/class conflicts
    67     /** A switch to determine whether we check for package/class conflicts
    69      */
    68      */
    70     final static boolean checkClash = true;
    69     final static boolean checkClash = true;
    71 
    70 
   124     boolean allowRepeatedAnnos;
   123     boolean allowRepeatedAnnos;
   125 
   124 
   126     /** A queue for classes whose members still need to be entered into the
   125     /** A queue for classes whose members still need to be entered into the
   127      *  symbol table.
   126      *  symbol table.
   128      */
   127      */
   129     ListBuffer<Env<AttrContext>> halfcompleted = new ListBuffer<Env<AttrContext>>();
   128     ListBuffer<Env<AttrContext>> halfcompleted = new ListBuffer<>();
   130 
   129 
   131     /** Set to true only when the first of a set of classes is
   130     /** Set to true only when the first of a set of classes is
   132      *  processed from the half completed queue.
   131      *  processed from the half completed queue.
   133      */
   132      */
   134     boolean isFirst = true;
   133     boolean isFirst = true;
   176         final PackageSymbol packge = env.toplevel.packge;
   175         final PackageSymbol packge = env.toplevel.packge;
   177         final TypeSymbol origin = tsym;
   176         final TypeSymbol origin = tsym;
   178 
   177 
   179         // enter imported types immediately
   178         // enter imported types immediately
   180         new Object() {
   179         new Object() {
   181             Set<Symbol> processed = new HashSet<Symbol>();
   180             Set<Symbol> processed = new HashSet<>();
   182             void importFrom(TypeSymbol tsym) {
   181             void importFrom(TypeSymbol tsym) {
   183                 if (tsym == null || !processed.add(tsym))
   182                 if (tsym == null || !processed.add(tsym))
   184                     return;
   183                     return;
   185 
   184 
   186                 // also import inherited names
   185                 // also import inherited names
   201             }
   200             }
   202         }.importFrom(tsym);
   201         }.importFrom(tsym);
   203 
   202 
   204         // enter non-types before annotations that might use them
   203         // enter non-types before annotations that might use them
   205         annotate.earlier(new Annotate.Worker() {
   204         annotate.earlier(new Annotate.Worker() {
   206             Set<Symbol> processed = new HashSet<Symbol>();
   205             Set<Symbol> processed = new HashSet<>();
   207 
   206 
   208             public String toString() {
   207             public String toString() {
   209                 return "import static " + tsym + ".*" + " in " + sourcefile;
   208                 return "import static " + tsym + ".*" + " in " + sourcefile;
   210             }
   209             }
   211             void importFrom(TypeSymbol tsym) {
   210             void importFrom(TypeSymbol tsym) {
   269         final PackageSymbol packge = env.toplevel.packge;
   268         final PackageSymbol packge = env.toplevel.packge;
   270         final TypeSymbol origin = tsym;
   269         final TypeSymbol origin = tsym;
   271 
   270 
   272         // enter imported types immediately
   271         // enter imported types immediately
   273         new Object() {
   272         new Object() {
   274             Set<Symbol> processed = new HashSet<Symbol>();
   273             Set<Symbol> processed = new HashSet<>();
   275             void importFrom(TypeSymbol tsym) {
   274             void importFrom(TypeSymbol tsym) {
   276                 if (tsym == null || !processed.add(tsym))
   275                 if (tsym == null || !processed.add(tsym))
   277                     return;
   276                     return;
   278 
   277 
   279                 // also import inherited names
   278                 // also import inherited names
   295             }
   294             }
   296         }.importFrom(tsym);
   295         }.importFrom(tsym);
   297 
   296 
   298         // enter non-types before annotations that might use them
   297         // enter non-types before annotations that might use them
   299         annotate.earlier(new Annotate.Worker() {
   298         annotate.earlier(new Annotate.Worker() {
   300             Set<Symbol> processed = new HashSet<Symbol>();
   299             Set<Symbol> processed = new HashSet<>();
   301             boolean found = false;
   300             boolean found = false;
   302 
   301 
   303             public String toString() {
   302             public String toString() {
   304                 return "import static " + tsym + "." + name;
   303                 return "import static " + tsym + "." + name;
   305             }
   304             }
   376         // Enter and attribute type parameters.
   375         // Enter and attribute type parameters.
   377         List<Type> tvars = enter.classEnter(typarams, env);
   376         List<Type> tvars = enter.classEnter(typarams, env);
   378         attr.attribTypeVariables(typarams, env);
   377         attr.attribTypeVariables(typarams, env);
   379 
   378 
   380         // Enter and attribute value parameters.
   379         // Enter and attribute value parameters.
   381         ListBuffer<Type> argbuf = new ListBuffer<Type>();
   380         ListBuffer<Type> argbuf = new ListBuffer<>();
   382         for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) {
   381         for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) {
   383             memberEnter(l.head, env);
   382             memberEnter(l.head, env);
   384             argbuf.append(l.head.vartype.type);
   383             argbuf.append(l.head.vartype.type);
   385         }
   384         }
   386 
   385 
   395         } else {
   394         } else {
   396             recvtype = null;
   395             recvtype = null;
   397         }
   396         }
   398 
   397 
   399         // Attribute thrown exceptions.
   398         // Attribute thrown exceptions.
   400         ListBuffer<Type> thrownbuf = new ListBuffer<Type>();
   399         ListBuffer<Type> thrownbuf = new ListBuffer<>();
   401         for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) {
   400         for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) {
   402             Type exc = attr.attribType(l.head, env);
   401             Type exc = attr.attribType(l.head, env);
   403             if (!exc.hasTag(TYPEVAR)) {
   402             if (!exc.hasTag(TYPEVAR)) {
   404                 exc = chk.checkClassType(l.head.pos(), exc);
   403                 exc = chk.checkClassType(l.head.pos(), exc);
   405             } else if (exc.tsym.owner == msym) {
   404             } else if (exc.tsym.owner == msym) {
   589             if (types.isSignaturePolymorphic(m)) {
   588             if (types.isSignaturePolymorphic(m)) {
   590                 m.flags_field |= SIGNATURE_POLYMORPHIC;
   589                 m.flags_field |= SIGNATURE_POLYMORPHIC;
   591             }
   590             }
   592 
   591 
   593             // Set m.params
   592             // Set m.params
   594             ListBuffer<VarSymbol> params = new ListBuffer<VarSymbol>();
   593             ListBuffer<VarSymbol> params = new ListBuffer<>();
   595             JCVariableDecl lastParam = null;
   594             JCVariableDecl lastParam = null;
   596             for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
   595             for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
   597                 JCVariableDecl param = lastParam = l.head;
   596                 JCVariableDecl param = lastParam = l.head;
   598                 params.append(Assert.checkNonNull(param.sym));
   597                 params.append(Assert.checkNonNull(param.sym));
   599             }
   598             }
   913 
   912 
   914     /** Enter a set of annotations. */
   913     /** Enter a set of annotations. */
   915     private void actualEnterAnnotations(List<JCAnnotation> annotations,
   914     private void actualEnterAnnotations(List<JCAnnotation> annotations,
   916                           Env<AttrContext> env,
   915                           Env<AttrContext> env,
   917                           Symbol s) {
   916                           Symbol s) {
   918         Map<TypeSymbol, ListBuffer<Attribute.Compound>> annotated =
   917         Map<TypeSymbol, ListBuffer<Attribute.Compound>> annotated = new LinkedHashMap<>();
   919                 new LinkedHashMap<TypeSymbol, ListBuffer<Attribute.Compound>>();
   918         Map<Attribute.Compound, DiagnosticPosition> pos = new HashMap<>();
   920         Map<Attribute.Compound, DiagnosticPosition> pos =
       
   921                 new HashMap<Attribute.Compound, DiagnosticPosition>();
       
   922 
   919 
   923         for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
   920         for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
   924             JCAnnotation a = al.head;
   921             JCAnnotation a = al.head;
   925             Attribute.Compound c = annotate.enterAnnotation(a,
   922             Attribute.Compound c = annotate.enterAnnotation(a,
   926                                                             syms.annotationType,
   923                                                             syms.annotationType,
   950                 s.flags_field |= Flags.DEPRECATED;
   947                 s.flags_field |= Flags.DEPRECATED;
   951             }
   948             }
   952         }
   949         }
   953 
   950 
   954         s.setDeclarationAttributesWithCompletion(
   951         s.setDeclarationAttributesWithCompletion(
   955                 annotate.new AnnotateRepeatedContext<Attribute.Compound>(env, annotated, pos, log, false));
   952                 annotate.new AnnotateRepeatedContext<>(env, annotated, pos, log, false));
   956     }
   953     }
   957 
   954 
   958     /** Queue processing of an attribute default value. */
   955     /** Queue processing of an attribute default value. */
   959     void annotateDefaultValueLater(final JCExpression defaultValue,
   956     void annotateDefaultValueLater(final JCExpression defaultValue,
   960                                    final Env<AttrContext> localEnv,
   957                                    final Env<AttrContext> localEnv,
  1062                 ? Type.noType
  1059                 ? Type.noType
  1063                 : syms.objectType;
  1060                 : syms.objectType;
  1064             ct.supertype_field = modelMissingTypes(supertype, tree.extending, false);
  1061             ct.supertype_field = modelMissingTypes(supertype, tree.extending, false);
  1065 
  1062 
  1066             // Determine interfaces.
  1063             // Determine interfaces.
  1067             ListBuffer<Type> interfaces = new ListBuffer<Type>();
  1064             ListBuffer<Type> interfaces = new ListBuffer<>();
  1068             ListBuffer<Type> all_interfaces = null; // lazy init
  1065             ListBuffer<Type> all_interfaces = null; // lazy init
  1069             Set<Type> interfaceSet = new HashSet<Type>();
  1066             Set<Type> interfaceSet = new HashSet<>();
  1070             List<JCExpression> interfaceTrees = tree.implementing;
  1067             List<JCExpression> interfaceTrees = tree.implementing;
  1071             for (JCExpression iface : interfaceTrees) {
  1068             for (JCExpression iface : interfaceTrees) {
  1072                 Type i = attr.attribBase(iface, baseEnv, false, true, true);
  1069                 Type i = attr.attribBase(iface, baseEnv, false, true, true);
  1073                 if (i.hasTag(CLASS)) {
  1070                 if (i.hasTag(CLASS)) {
  1074                     interfaces.append(i);
  1071                     interfaces.append(i);
  1212      * If the symbol is non-null, attach the type annotation to it.
  1209      * If the symbol is non-null, attach the type annotation to it.
  1213      */
  1210      */
  1214     private void actualEnterTypeAnnotations(final List<JCAnnotation> annotations,
  1211     private void actualEnterTypeAnnotations(final List<JCAnnotation> annotations,
  1215             final Env<AttrContext> env,
  1212             final Env<AttrContext> env,
  1216             final Symbol s) {
  1213             final Symbol s) {
  1217         Map<TypeSymbol, ListBuffer<Attribute.TypeCompound>> annotated =
  1214         Map<TypeSymbol, ListBuffer<Attribute.TypeCompound>> annotated = new LinkedHashMap<>();
  1218                 new LinkedHashMap<TypeSymbol, ListBuffer<Attribute.TypeCompound>>();
  1215         Map<Attribute.TypeCompound, DiagnosticPosition> pos = new HashMap<>();
  1219         Map<Attribute.TypeCompound, DiagnosticPosition> pos =
       
  1220                 new HashMap<Attribute.TypeCompound, DiagnosticPosition>();
       
  1221 
  1216 
  1222         for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
  1217         for (List<JCAnnotation> al = annotations; !al.isEmpty(); al = al.tail) {
  1223             JCAnnotation a = al.head;
  1218             JCAnnotation a = al.head;
  1224             Attribute.TypeCompound tc = annotate.enterTypeAnnotation(a,
  1219             Attribute.TypeCompound tc = annotate.enterTypeAnnotation(a,
  1225                     syms.annotationType,
  1220                     syms.annotationType,
  1243             }
  1238             }
  1244         }
  1239         }
  1245 
  1240 
  1246         if (s != null) {
  1241         if (s != null) {
  1247             s.appendTypeAttributesWithCompletion(
  1242             s.appendTypeAttributesWithCompletion(
  1248                     annotate.new AnnotateRepeatedContext<Attribute.TypeCompound>(env, annotated, pos, log, true));
  1243                     annotate.new AnnotateRepeatedContext<>(env, annotated, pos, log, true));
  1249         }
  1244         }
  1250     }
  1245     }
  1251 
  1246 
  1252     public void typeAnnotate(final JCTree tree, final Env<AttrContext> env, final Symbol sym, DiagnosticPosition deferPos) {
  1247     public void typeAnnotate(final JCTree tree, final Env<AttrContext> env, final Symbol sym, DiagnosticPosition deferPos) {
  1253         if (allowTypeAnnos) {
  1248         if (allowTypeAnnos) {
  1446             tree.accept(this);
  1441             tree.accept(this);
  1447             return result;
  1442             return result;
  1448         }
  1443         }
  1449 
  1444 
  1450         List<Type> visit(List<? extends JCTree> trees) {
  1445         List<Type> visit(List<? extends JCTree> trees) {
  1451             ListBuffer<Type> lb = new ListBuffer<Type>();
  1446             ListBuffer<Type> lb = new ListBuffer<>();
  1452             for (JCTree t: trees)
  1447             for (JCTree t: trees)
  1453                 lb.append(visit(t));
  1448                 lb.append(visit(t));
  1454             return lb.toList();
  1449             return lb.toList();
  1455         }
  1450         }
  1456 
  1451