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

Print this page




1767         }
1768         return bestSoFar;
1769     }
1770 
1771     /** Load toplevel or member class with given fully qualified name and
1772      *  verify that it is accessible.
1773      *  @param env       The current environment.
1774      *  @param name      The fully qualified name of the class to be loaded.
1775      */
1776     Symbol loadClass(Env<AttrContext> env, Name name) {
1777         try {
1778             ClassSymbol c = reader.loadClass(name);
1779             return isAccessible(env, c) ? c : new AccessError(c);
1780         } catch (ClassReader.BadClassFile err) {
1781             throw err;
1782         } catch (CompletionFailure ex) {
1783             return typeNotFound;
1784         }
1785     }
1786 
1787     /** Find qualified member type.



1788      *  @param env       The current environment.
1789      *  @param site      The original type from where the selection takes
1790      *                   place.
1791      *  @param name      The type's name.
1792      *  @param c         The class to search for the member type. This is
1793      *                   always a superclass or implemented interface of
1794      *                   site's class.
1795      */
1796     Symbol findMemberType(Env<AttrContext> env,
1797                           Type site,
1798                           Name name,
1799                           TypeSymbol c) {
1800         Symbol bestSoFar = typeNotFound;
1801         Symbol sym;
1802         Scope.Entry e = c.members().lookup(name);
1803         while (e.scope != null) {
1804             if (e.sym.kind == TYP) {
1805                 return isAccessible(env, site, e.sym)
1806                     ? e.sym
1807                     : new AccessError(env, site, e.sym);
1808             }
1809             e = e.next();
1810         }


















1811         Type st = types.supertype(c.type);
1812         if (st != null && st.hasTag(CLASS)) {
1813             sym = findMemberType(env, site, name, st.tsym);
1814             if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1815         }
1816         for (List<Type> l = types.interfaces(c.type);
1817              bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
1818              l = l.tail) {
1819             sym = findMemberType(env, site, name, l.head.tsym);
1820             if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
1821                 sym.owner != bestSoFar.owner)
1822                 bestSoFar = new AmbiguityError(bestSoFar, sym);
1823             else if (sym.kind < bestSoFar.kind)
1824                 bestSoFar = sym;
1825         }
1826         return bestSoFar;
1827     }
1828 






















1829     /** Find a global type in given scope and load corresponding class.
1830      *  @param env       The current environment.
1831      *  @param scope     The scope in which to look for the type.
1832      *  @param name      The type's name.
1833      */
1834     Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
1835         Symbol bestSoFar = typeNotFound;
1836         for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
1837             Symbol sym = loadClass(env, e.sym.flatName());
1838             if (bestSoFar.kind == TYP && sym.kind == TYP &&
1839                 bestSoFar != sym)
1840                 return new AmbiguityError(bestSoFar, sym);
1841             else if (sym.kind < bestSoFar.kind)
1842                 bestSoFar = sym;
1843         }
1844         return bestSoFar;
1845     }
1846 
1847     /** Find an unqualified type symbol.
1848      *  @param env       The current environment.
1849      *  @param name      The type's name.
1850      */
1851     Symbol findType(Env<AttrContext> env, Name name) {
1852         Symbol bestSoFar = typeNotFound;
1853         Symbol sym;
1854         boolean staticOnly = false;
1855         for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {





1856             if (isStatic(env1)) staticOnly = true;
1857             for (Scope.Entry e = env1.info.scope.lookup(name);
1858                  e.scope != null;
1859                  e = e.next()) {
1860                 if (e.sym.kind == TYP) {
1861                     if (staticOnly &&
1862                         e.sym.type.hasTag(TYPEVAR) &&
1863                         e.sym.owner.kind == TYP) return new StaticError(e.sym);

1864                     return e.sym;
1865                 }
1866             }



1867 
1868             sym = findMemberType(env1, env1.enclClass.sym.type, name,
1869                                  env1.enclClass.sym);
1870             if (staticOnly && sym.kind == TYP &&
1871                 sym.type.hasTag(CLASS) &&
1872                 sym.type.getEnclosingType().hasTag(CLASS) &&
1873                 env1.enclClass.sym.type.isParameterized() &&
1874                 sym.type.getEnclosingType().isParameterized())
1875                 return new StaticError(sym);
1876             else if (sym.exists()) return sym;
1877             else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1878 
1879             JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
1880             if ((encl.sym.flags() & STATIC) != 0)
1881                 staticOnly = true;
1882         }
1883 
1884         if (!env.tree.hasTag(IMPORT)) {
1885             sym = findGlobalType(env, env.toplevel.namedImportScope, name);
1886             if (sym.exists()) return sym;
1887             else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1888 
1889             sym = findGlobalType(env, env.toplevel.packge.members(), name);




1767         }
1768         return bestSoFar;
1769     }
1770 
1771     /** Load toplevel or member class with given fully qualified name and
1772      *  verify that it is accessible.
1773      *  @param env       The current environment.
1774      *  @param name      The fully qualified name of the class to be loaded.
1775      */
1776     Symbol loadClass(Env<AttrContext> env, Name name) {
1777         try {
1778             ClassSymbol c = reader.loadClass(name);
1779             return isAccessible(env, c) ? c : new AccessError(c);
1780         } catch (ClassReader.BadClassFile err) {
1781             throw err;
1782         } catch (CompletionFailure ex) {
1783             return typeNotFound;
1784         }
1785     }
1786 
1787 
1788     /**
1789      * Find a type declared in a scope (not inherited).  Return null
1790      * if none is found.
1791      *  @param env       The current environment.
1792      *  @param site      The original type from where the selection takes
1793      *                   place.
1794      *  @param name      The type's name.
1795      *  @param c         The class to search for the member type. This is
1796      *                   always a superclass or implemented interface of
1797      *                   site's class.
1798      */
1799     Symbol findImmediateMemberType(Env<AttrContext> env,
1800                                    Type site,
1801                                    Name name,
1802                                    TypeSymbol c) {


1803         Scope.Entry e = c.members().lookup(name);
1804         while (e.scope != null) {
1805             if (e.sym.kind == TYP) {
1806                 return isAccessible(env, site, e.sym)
1807                     ? e.sym
1808                     : new AccessError(env, site, e.sym);
1809             }
1810             e = e.next();
1811         }
1812         return null;
1813     }
1814 
1815     /** Find a member type inherited from a superclass or interface.
1816      *  @param env       The current environment.
1817      *  @param site      The original type from where the selection takes
1818      *                   place.
1819      *  @param name      The type's name.
1820      *  @param c         The class to search for the member type. This is
1821      *                   always a superclass or implemented interface of
1822      *                   site's class.
1823      */
1824     Symbol findInheritedMemberType(Env<AttrContext> env,
1825                                    Type site,
1826                                    Name name,
1827                                    TypeSymbol c) {
1828         Symbol bestSoFar = typeNotFound;
1829         Symbol sym;
1830         Type st = types.supertype(c.type);
1831         if (st != null && st.hasTag(CLASS)) {
1832             sym = findMemberType(env, site, name, st.tsym);
1833             if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1834         }
1835         for (List<Type> l = types.interfaces(c.type);
1836              bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
1837              l = l.tail) {
1838             sym = findMemberType(env, site, name, l.head.tsym);
1839             if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
1840                 sym.owner != bestSoFar.owner)
1841                 bestSoFar = new AmbiguityError(bestSoFar, sym);
1842             else if (sym.kind < bestSoFar.kind)
1843                 bestSoFar = sym;
1844         }
1845         return bestSoFar;
1846     }
1847 
1848     /** Find qualified member type.
1849      *  @param env       The current environment.
1850      *  @param site      The original type from where the selection takes
1851      *                   place.
1852      *  @param name      The type's name.
1853      *  @param c         The class to search for the member type. This is
1854      *                   always a superclass or implemented interface of
1855      *                   site's class.
1856      */
1857     Symbol findMemberType(Env<AttrContext> env,
1858                           Type site,
1859                           Name name,
1860                           TypeSymbol c) {
1861         Symbol sym = findImmediateMemberType(env, site, name, c);
1862 
1863         if (sym != null)
1864             return sym;
1865 
1866         return findInheritedMemberType(env, site, name, c);
1867 
1868     }
1869 
1870     /** Find a global type in given scope and load corresponding class.
1871      *  @param env       The current environment.
1872      *  @param scope     The scope in which to look for the type.
1873      *  @param name      The type's name.
1874      */
1875     Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
1876         Symbol bestSoFar = typeNotFound;
1877         for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
1878             Symbol sym = loadClass(env, e.sym.flatName());
1879             if (bestSoFar.kind == TYP && sym.kind == TYP &&
1880                 bestSoFar != sym)
1881                 return new AmbiguityError(bestSoFar, sym);
1882             else if (sym.kind < bestSoFar.kind)
1883                 bestSoFar = sym;
1884         }
1885         return bestSoFar;
1886     }
1887 
1888     /** Find an unqualified type symbol.
1889      *  @param env       The current environment.
1890      *  @param name      The type's name.
1891      */
1892     Symbol findType(Env<AttrContext> env, Name name) {
1893         Symbol bestSoFar = typeNotFound;
1894         Symbol sym;
1895         boolean staticOnly = false;
1896         for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
1897             // First, try the members declared in the class
1898             sym = findImmediateMemberType(env1, env1.enclClass.sym.type,
1899                                           name, env1.enclClass.sym);
1900             // Then try the type parameters
1901             if (sym == null) {
1902                 if (isStatic(env1)) staticOnly = true;
1903                 for (Scope.Entry e = env1.info.scope.lookup(name);
1904                      e.scope != null;
1905                      e = e.next()) {
1906                     if (e.sym.kind == TYP) {
1907                         if (staticOnly &&
1908                             e.sym.type.hasTag(TYPEVAR) &&
1909                             e.sym.owner.kind == TYP)
1910                             return new StaticError(e.sym);
1911                         return e.sym;
1912                     }
1913                 }
1914                 sym = findInheritedMemberType(env1, env1.enclClass.sym.type,
1915                                               name, env1.enclClass.sym);
1916             }
1917 


1918             if (staticOnly && sym.kind == TYP &&
1919                 sym.type.hasTag(CLASS) &&
1920                 sym.type.getEnclosingType().hasTag(CLASS) &&
1921                 env1.enclClass.sym.type.isParameterized() &&
1922                 sym.type.getEnclosingType().isParameterized())
1923                 return new StaticError(sym);
1924             else if (sym.exists()) return sym;
1925             else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1926 
1927             JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
1928             if ((encl.sym.flags() & STATIC) != 0)
1929                 staticOnly = true;
1930         }
1931 
1932         if (!env.tree.hasTag(IMPORT)) {
1933             sym = findGlobalType(env, env.toplevel.namedImportScope, name);
1934             if (sym.exists()) return sym;
1935             else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1936 
1937             sym = findGlobalType(env, env.toplevel.packge.members(), name);