langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/ClassFinder.java
changeset 27579 d1a63c99cdd5
parent 27224 228abfa87080
child 27858 443efec4bf09
equal deleted inserted replaced
27578:d61af14a5cf7 27579:d1a63c99cdd5
    25 
    25 
    26 package com.sun.tools.javac.code;
    26 package com.sun.tools.javac.code;
    27 
    27 
    28 import java.io.*;
    28 import java.io.*;
    29 import java.util.EnumSet;
    29 import java.util.EnumSet;
       
    30 import java.util.HashMap;
       
    31 import java.util.Map;
    30 import java.util.Set;
    32 import java.util.Set;
       
    33 
    31 import javax.lang.model.SourceVersion;
    34 import javax.lang.model.SourceVersion;
    32 import javax.tools.JavaFileObject;
       
    33 import javax.tools.JavaFileManager;
    35 import javax.tools.JavaFileManager;
    34 import javax.tools.JavaFileManager.Location;
    36 import javax.tools.JavaFileManager.Location;
       
    37 import javax.tools.JavaFileObject;
    35 import javax.tools.StandardJavaFileManager;
    38 import javax.tools.StandardJavaFileManager;
    36 
    39 
    37 import static javax.tools.StandardLocation.*;
       
    38 
       
    39 import com.sun.tools.javac.comp.Annotate;
       
    40 import com.sun.tools.javac.code.Scope.WriteableScope;
    40 import com.sun.tools.javac.code.Scope.WriteableScope;
    41 import com.sun.tools.javac.code.Symbol.*;
    41 import com.sun.tools.javac.code.Symbol.*;
       
    42 import com.sun.tools.javac.code.Symbol.ClassSymbol;
       
    43 import com.sun.tools.javac.code.Symbol.Completer;
       
    44 import com.sun.tools.javac.code.Symbol.CompletionFailure;
       
    45 import com.sun.tools.javac.code.Symbol.PackageSymbol;
       
    46 import com.sun.tools.javac.code.Symbol.TypeSymbol;
       
    47 import com.sun.tools.javac.comp.Annotate;
       
    48 import com.sun.tools.javac.file.JRTIndex;
       
    49 import com.sun.tools.javac.file.JavacFileManager;
       
    50 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
    42 import com.sun.tools.javac.jvm.ClassReader;
    51 import com.sun.tools.javac.jvm.ClassReader;
       
    52 import com.sun.tools.javac.jvm.Profile;
    43 import com.sun.tools.javac.util.*;
    53 import com.sun.tools.javac.util.*;
       
    54 
       
    55 import static javax.tools.StandardLocation.*;
    44 
    56 
    45 import static com.sun.tools.javac.code.Flags.*;
    57 import static com.sun.tools.javac.code.Flags.*;
    46 import static com.sun.tools.javac.code.Kinds.Kind.*;
    58 import static com.sun.tools.javac.code.Kinds.Kind.*;
    47 
    59 
    48 import static com.sun.tools.javac.main.Option.*;
    60 import static com.sun.tools.javac.main.Option.*;
   124     /** The class or method currently being read.
   136     /** The class or method currently being read.
   125      */
   137      */
   126     protected Symbol currentOwner = null;
   138     protected Symbol currentOwner = null;
   127 
   139 
   128     /**
   140     /**
       
   141      * The currently selected profile.
       
   142      */
       
   143     private final Profile profile;
       
   144 
       
   145     /**
       
   146      * Use direct access to the JRTIndex to access the temporary
       
   147      * replacement for the info that used to be in ct.sym.
       
   148      * In time, this will go away and be replaced by the module system.
       
   149      */
       
   150     private final JRTIndex jrtIndex;
       
   151 
       
   152     /**
   129      * Completer that delegates to the complete-method of this class.
   153      * Completer that delegates to the complete-method of this class.
   130      */
   154      */
   131     private final Completer thisCompleter = new Completer() {
   155     private final Completer thisCompleter = new Completer() {
   132         @Override
   156         @Override
   133         public void complete(Symbol sym) throws CompletionFailure {
   157         public void complete(Symbol sym) throws CompletionFailure {
   166         verbose = options.isSet(VERBOSE);
   190         verbose = options.isSet(VERBOSE);
   167         cacheCompletionFailure = options.isUnset("dev");
   191         cacheCompletionFailure = options.isUnset("dev");
   168         preferSource = "source".equals(options.get("-Xprefer"));
   192         preferSource = "source".equals(options.get("-Xprefer"));
   169         userPathsFirst = options.isSet(XXUSERPATHSFIRST);
   193         userPathsFirst = options.isSet(XXUSERPATHSFIRST);
   170 
   194 
   171 
       
   172         completionFailureName =
   195         completionFailureName =
   173             options.isSet("failcomplete")
   196             options.isSet("failcomplete")
   174             ? names.fromString(options.get("failcomplete"))
   197             ? names.fromString(options.get("failcomplete"))
   175             : null;
   198             : null;
   176     }
   199 
       
   200         // Temporary, until more info is available from the module system.
       
   201         boolean useCtProps;
       
   202         JavaFileManager fm = context.get(JavaFileManager.class);
       
   203         if (fm instanceof JavacFileManager) {
       
   204             JavacFileManager jfm = (JavacFileManager) fm;
       
   205             useCtProps = jfm.isDefaultBootClassPath() && jfm.isSymbolFileEnabled();
       
   206         } else if (fm.getClass().getName().equals("com.sun.tools.sjavac.comp.SmartFileManager")) {
       
   207             useCtProps = !options.isSet("ignore.symbol.file");
       
   208         } else {
       
   209             useCtProps = false;
       
   210         }
       
   211         jrtIndex = useCtProps && JRTIndex.isAvailable() ? JRTIndex.getSharedInstance() : null;
       
   212 
       
   213         profile = Profile.instance(context);
       
   214     }
       
   215 
       
   216 
       
   217 /************************************************************************
       
   218  * Temporary ct.sym replacement
       
   219  *
       
   220  * The following code is a temporary substitute for the ct.sym mechanism
       
   221  * used in JDK 6 thru JDK 8.
       
   222  * This mechanism will eventually be superseded by the Jigsaw module system.
       
   223  ***********************************************************************/
       
   224 
       
   225     /**
       
   226      * Returns any extra flags for a class symbol.
       
   227      * This information used to be provided using private annotations
       
   228      * in the class file in ct.sym; in time, this information will be
       
   229      * available from the module system.
       
   230      */
       
   231     long getSupplementaryFlags(ClassSymbol c) {
       
   232         if (jrtIndex == null || !jrtIndex.isInJRT(c.classfile)) {
       
   233             return 0;
       
   234         }
       
   235 
       
   236         if (supplementaryFlags == null) {
       
   237             supplementaryFlags = new HashMap<>();
       
   238         }
       
   239 
       
   240         Long flags = supplementaryFlags.get(c.packge());
       
   241         if (flags == null) {
       
   242             long newFlags = 0;
       
   243             try {
       
   244                 JRTIndex.CtSym ctSym = jrtIndex.getCtSym(c.packge().flatName());
       
   245                 Profile minProfile = Profile.DEFAULT;
       
   246                 if (ctSym.proprietary)
       
   247                     newFlags |= PROPRIETARY;
       
   248                 if (ctSym.minProfile != null)
       
   249                     minProfile = Profile.lookup(ctSym.minProfile);
       
   250                 if (profile != Profile.DEFAULT && minProfile.value > profile.value) {
       
   251                     newFlags |= NOT_IN_PROFILE;
       
   252                 }
       
   253             } catch (IOException ignore) {
       
   254             }
       
   255             supplementaryFlags.put(c.packge(), flags = newFlags);
       
   256         }
       
   257         return flags;
       
   258     }
       
   259 
       
   260     private Map<PackageSymbol, Long> supplementaryFlags;
   177 
   261 
   178 /************************************************************************
   262 /************************************************************************
   179  * Loading Classes
   263  * Loading Classes
   180  ***********************************************************************/
   264  ***********************************************************************/
   181 
   265 
   257                 if (verbose) {
   341                 if (verbose) {
   258                     log.printVerbose("loading", currentClassFile.toString());
   342                     log.printVerbose("loading", currentClassFile.toString());
   259                 }
   343                 }
   260                 if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
   344                 if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
   261                     reader.readClassFile(c);
   345                     reader.readClassFile(c);
       
   346                     c.flags_field |= getSupplementaryFlags(c);
   262                 } else {
   347                 } else {
   263                     if (sourceCompleter != null) {
   348                     if (sourceCompleter != null) {
   264                         sourceCompleter.complete(c);
   349                         sourceCompleter.complete(c);
   265                     } else {
   350                     } else {
   266                         throw new IllegalStateException("Source completer required to read "
   351                         throw new IllegalStateException("Source completer required to read "
   269                 }
   354                 }
   270             } finally {
   355             } finally {
   271                 currentClassFile = previousClassFile;
   356                 currentClassFile = previousClassFile;
   272             }
   357             }
   273         } else {
   358         } else {
       
   359             throw classFileNotFound(c);
       
   360         }
       
   361     }
       
   362     // where
       
   363         private CompletionFailure classFileNotFound(ClassSymbol c) {
   274             JCDiagnostic diag =
   364             JCDiagnostic diag =
   275                 diagFactory.fragment("class.file.not.found", c.flatname);
   365                 diagFactory.fragment("class.file.not.found", c.flatname);
   276             throw
   366             return newCompletionFailure(c, diag);
   277                 newCompletionFailure(c, diag);
   367         }
   278         }
       
   279     }
       
   280     // where
       
   281         /** Static factory for CompletionFailure objects.
   368         /** Static factory for CompletionFailure objects.
   282          *  In practice, only one can be used at a time, so we share one
   369          *  In practice, only one can be used at a time, so we share one
   283          *  to reduce the expense of allocating new exception objects.
   370          *  to reduce the expense of allocating new exception objects.
   284          */
   371          */
   285         private CompletionFailure newCompletionFailure(TypeSymbol c,
   372         private CompletionFailure newCompletionFailure(TypeSymbol c,
   294                 result.sym = c;
   381                 result.sym = c;
   295                 result.diag = diag;
   382                 result.diag = diag;
   296                 return result;
   383                 return result;
   297             }
   384             }
   298         }
   385         }
   299         private CompletionFailure cachedCompletionFailure =
   386         private final CompletionFailure cachedCompletionFailure =
   300             new CompletionFailure(null, (JCDiagnostic) null);
   387             new CompletionFailure(null, (JCDiagnostic) null);
   301         {
   388         {
   302             cachedCompletionFailure.setStackTrace(new StackTraceElement[0]);
   389             cachedCompletionFailure.setStackTrace(new StackTraceElement[0]);
   303         }
   390         }
   304 
   391