src/share/classes/com/sun/tools/javac/comp/Attr.java

changeset 2558
d560276b8a35
parent 2543
c6d5efccedc3
child 2596
fa8be3ce18fc
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Sep 03 13:20:04 2014 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Sep 10 10:50:59 2014 +0100
     1.3 @@ -287,7 +287,7 @@
     1.4       *  @param env    The current environment.
     1.5       */
     1.6      boolean isAssignableAsBlankFinal(VarSymbol v, Env<AttrContext> env) {
     1.7 -        Symbol owner = owner(env);
     1.8 +        Symbol owner = env.info.scope.owner;
     1.9             // owner refers to the innermost variable, method or
    1.10             // initializer block declaration at this point.
    1.11          return
    1.12 @@ -302,41 +302,6 @@
    1.13               ((v.flags() & STATIC) != 0) == Resolve.isStatic(env));
    1.14      }
    1.15  
    1.16 -    /**
    1.17 -     * Return the innermost enclosing owner symbol in a given attribution context
    1.18 -     */
    1.19 -    Symbol owner(Env<AttrContext> env) {
    1.20 -        while (true) {
    1.21 -            switch (env.tree.getTag()) {
    1.22 -                case VARDEF:
    1.23 -                    //a field can be owner
    1.24 -                    VarSymbol vsym = ((JCVariableDecl)env.tree).sym;
    1.25 -                    if (vsym.owner.kind == TYP) {
    1.26 -                        return vsym;
    1.27 -                    }
    1.28 -                    break;
    1.29 -                case METHODDEF:
    1.30 -                    //method def is always an owner
    1.31 -                    return ((JCMethodDecl)env.tree).sym;
    1.32 -                case CLASSDEF:
    1.33 -                    //class def is always an owner
    1.34 -                    return ((JCClassDecl)env.tree).sym;
    1.35 -                case BLOCK:
    1.36 -                    //static/instance init blocks are owner
    1.37 -                    Symbol blockSym = env.info.scope.owner;
    1.38 -                    if ((blockSym.flags() & BLOCK) != 0) {
    1.39 -                        return blockSym;
    1.40 -                    }
    1.41 -                    break;
    1.42 -                case TOPLEVEL:
    1.43 -                    //toplevel is always an owner (for pkge decls)
    1.44 -                    return env.info.scope.owner;
    1.45 -            }
    1.46 -            Assert.checkNonNull(env.next);
    1.47 -            env = env.next;
    1.48 -        }
    1.49 -    }
    1.50 -
    1.51      /** Check that variable can be assigned to.
    1.52       *  @param pos    The current source code position.
    1.53       *  @param v      The assigned varaible
    1.54 @@ -3660,7 +3625,7 @@
    1.55              // and are subject to definite assignment checking.
    1.56              if ((env.info.enclVar == v || v.pos > tree.pos) &&
    1.57                  v.owner.kind == TYP &&
    1.58 -                canOwnInitializer(owner(env)) &&
    1.59 +                enclosingInitEnv(env) != null &&
    1.60                  v.owner == env.info.scope.owner.enclClass() &&
    1.61                  ((v.flags() & STATIC) != 0) == Resolve.isStatic(env) &&
    1.62                  (!env.tree.hasTag(ASSIGN) ||
    1.63 @@ -3680,6 +3645,36 @@
    1.64          }
    1.65  
    1.66          /**
    1.67 +         * Returns the enclosing init environment associated with this env (if any). An init env
    1.68 +         * can be either a field declaration env or a static/instance initializer env.
    1.69 +         */
    1.70 +        Env<AttrContext> enclosingInitEnv(Env<AttrContext> env) {
    1.71 +            while (true) {
    1.72 +                switch (env.tree.getTag()) {
    1.73 +                    case VARDEF:
    1.74 +                        JCVariableDecl vdecl = (JCVariableDecl)env.tree;
    1.75 +                        if (vdecl.sym.owner.kind == TYP) {
    1.76 +                            //field
    1.77 +                            return env;
    1.78 +                        }
    1.79 +                        break;
    1.80 +                    case BLOCK:
    1.81 +                        if (env.next.tree.hasTag(CLASSDEF)) {
    1.82 +                            //instance/static initializer
    1.83 +                            return env;
    1.84 +                        }
    1.85 +                        break;
    1.86 +                    case METHODDEF:
    1.87 +                    case CLASSDEF:
    1.88 +                    case TOPLEVEL:
    1.89 +                        return null;
    1.90 +                }
    1.91 +                Assert.checkNonNull(env.next);
    1.92 +                env = env.next;
    1.93 +            }
    1.94 +        }
    1.95 +
    1.96 +        /**
    1.97           * Check for illegal references to static members of enum.  In
    1.98           * an enum type, constructors and initializers may not
    1.99           * reference its static members unless they are constant.
   1.100 @@ -3732,17 +3727,6 @@
   1.101                     v.name != names._class;
   1.102          }
   1.103  
   1.104 -        /** Can the given symbol be the owner of code which forms part
   1.105 -         *  if class initialization? This is the case if the symbol is
   1.106 -         *  a type or field, or if the symbol is the synthetic method.
   1.107 -         *  owning a block.
   1.108 -         */
   1.109 -        private boolean canOwnInitializer(Symbol sym) {
   1.110 -            return
   1.111 -                (sym.kind & (VAR | TYP)) != 0 ||
   1.112 -                (sym.kind == MTH && (sym.flags() & BLOCK) != 0);
   1.113 -        }
   1.114 -
   1.115      Warner noteWarner = new Warner();
   1.116  
   1.117      /**

mercurial