langtools/src/share/classes/com/sun/tools/sjavac/JavacState.java
changeset 22163 3651128c74eb
parent 19500 26d88d483764
child 22165 ec53c8946fc2
equal deleted inserted replaced
22162:3b3e23e67329 22163:3651128c74eb
   155             }
   155             }
   156             needsSaving = true;
   156             needsSaving = true;
   157         }
   157         }
   158         prev = new BuildState();
   158         prev = new BuildState();
   159         now = new BuildState();
   159         now = new BuildState();
   160         taintedPackages = new HashSet<String>();
   160         taintedPackages = new HashSet<>();
   161         recompiledPackages = new HashSet<String>();
   161         recompiledPackages = new HashSet<>();
   162         packagesWithChangedPublicApis = new HashSet<String>();
   162         packagesWithChangedPublicApis = new HashSet<>();
   163     }
   163     }
   164 
   164 
   165     public BuildState prev() { return prev; }
   165     public BuildState prev() { return prev; }
   166     public BuildState now() { return now; }
   166     public BuildState now() { return now; }
   167 
   167 
   195 
   195 
   196     /**
   196     /**
   197      * Specify which sources are visible to the compiler through -sourcepath.
   197      * Specify which sources are visible to the compiler through -sourcepath.
   198      */
   198      */
   199     public void setVisibleSources(Map<String,Source> vs) {
   199     public void setVisibleSources(Map<String,Source> vs) {
   200         visibleSrcs = new HashSet<URI>();
   200         visibleSrcs = new HashSet<>();
   201         for (String s : vs.keySet()) {
   201         for (String s : vs.keySet()) {
   202             Source src = vs.get(s);
   202             Source src = vs.get(s);
   203             visibleSrcs.add(src.file().toURI());
   203             visibleSrcs.add(src.file().toURI());
   204         }
   204         }
   205     }
   205     }
   206 
   206 
   207     /**
   207     /**
   208      * Specify which classes are visible to the compiler through -classpath.
   208      * Specify which classes are visible to the compiler through -classpath.
   209      */
   209      */
   210     public void setVisibleClasses(Map<String,Source> vs) {
   210     public void setVisibleClasses(Map<String,Source> vs) {
   211         visibleSrcs = new HashSet<URI>();
   211         visibleSrcs = new HashSet<>();
   212         for (String s : vs.keySet()) {
   212         for (String s : vs.keySet()) {
   213             Source src = vs.get(s);
   213             Source src = vs.get(s);
   214             visibleSrcs.add(src.file().toURI());
   214             visibleSrcs.add(src.file().toURI());
   215         }
   215         }
   216     }
   216     }
   236     private Map<String,File> fetchPrevArtifacts(String pkg) {
   236     private Map<String,File> fetchPrevArtifacts(String pkg) {
   237         Package p = prev.packages().get(pkg);
   237         Package p = prev.packages().get(pkg);
   238         if (p != null) {
   238         if (p != null) {
   239             return p.artifacts();
   239             return p.artifacts();
   240         }
   240         }
   241         return new HashMap<String,File>();
   241         return new HashMap<>();
   242     }
   242     }
   243 
   243 
   244     /**
   244     /**
   245      * Delete all prev artifacts in the currently tainted packages.
   245      * Delete all prev artifacts in the currently tainted packages.
   246      */
   246      */
   286             b.append("# I pubapi\n");
   286             b.append("# I pubapi\n");
   287             b.append("# R arguments\n");
   287             b.append("# R arguments\n");
   288             b.append("R ").append(theArgs).append("\n");
   288             b.append("R ").append(theArgs).append("\n");
   289 
   289 
   290             // Copy over the javac_state for the packages that did not need recompilation.
   290             // Copy over the javac_state for the packages that did not need recompilation.
   291             now.copyPackagesExcept(prev, recompiledPackages, new HashSet<String>());
   291             now.copyPackagesExcept(prev, recompiledPackages, new HashSet<>());
   292             // Save the packages, ie package names, dependencies, pubapis and artifacts!
   292             // Save the packages, ie package names, dependencies, pubapis and artifacts!
   293             // I.e. the lot.
   293             // I.e. the lot.
   294             Module.saveModules(now.modules(), b);
   294             Module.saveModules(now.modules(), b);
   295 
   295 
   296             String s = b.toString();
   296             String s = b.toString();
   419     /**
   419     /**
   420      * Clean out the tainted package set, used after the first round of compiles,
   420      * Clean out the tainted package set, used after the first round of compiles,
   421      * prior to propagating dependencies.
   421      * prior to propagating dependencies.
   422      */
   422      */
   423     public void clearTaintedPackages() {
   423     public void clearTaintedPackages() {
   424         taintedPackages = new HashSet<String>();
   424         taintedPackages = new HashSet<>();
   425     }
   425     }
   426 
   426 
   427     /**
   427     /**
   428      * Go through all sources and check which have been removed, added or modified
   428      * Go through all sources and check which have been removed, added or modified
   429      * and taint the corresponding packages.
   429      * and taint the corresponding packages.
   460 
   460 
   461     /**
   461     /**
   462      * Acquire the compile_java_packages suffix rule for .java files.
   462      * Acquire the compile_java_packages suffix rule for .java files.
   463      */
   463      */
   464     public Map<String,Transformer> getJavaSuffixRule() {
   464     public Map<String,Transformer> getJavaSuffixRule() {
   465         Map<String,Transformer> sr = new HashMap<String,Transformer>();
   465         Map<String,Transformer> sr = new HashMap<>();
   466         sr.put(".java", compileJavaPackages);
   466         sr.put(".java", compileJavaPackages);
   467         return sr;
   467         return sr;
   468     }
   468     }
   469 
   469 
   470     /**
   470     /**
   507     /**
   507     /**
   508      * Scan all output dirs for artifacts and remove those files (artifacts?)
   508      * Scan all output dirs for artifacts and remove those files (artifacts?)
   509      * that are not recognized as such, in the javac_state file.
   509      * that are not recognized as such, in the javac_state file.
   510      */
   510      */
   511     public void removeUnidentifiedArtifacts() {
   511     public void removeUnidentifiedArtifacts() {
   512         Set<File> allKnownArtifacts = new HashSet<File>();
   512         Set<File> allKnownArtifacts = new HashSet<>();
   513         for (Package pkg : prev.packages().values()) {
   513         for (Package pkg : prev.packages().values()) {
   514             for (File f : pkg.artifacts().values()) {
   514             for (File f : pkg.artifacts().values()) {
   515                 allKnownArtifacts.add(f);
   515                 allKnownArtifacts.add(f);
   516             }
   516             }
   517         }
   517         }
   560 
   560 
   561     /**
   561     /**
   562      * Return those files belonging to prev, but not now.
   562      * Return those files belonging to prev, but not now.
   563      */
   563      */
   564     private Set<Source> calculateRemovedSources() {
   564     private Set<Source> calculateRemovedSources() {
   565         Set<Source> removed = new HashSet<Source>();
   565         Set<Source> removed = new HashSet<>();
   566         for (String src : prev.sources().keySet()) {
   566         for (String src : prev.sources().keySet()) {
   567             if (now.sources().get(src) == null) {
   567             if (now.sources().get(src) == null) {
   568                 removed.add(prev.sources().get(src));
   568                 removed.add(prev.sources().get(src));
   569             }
   569             }
   570         }
   570         }
   573 
   573 
   574     /**
   574     /**
   575      * Return those files belonging to now, but not prev.
   575      * Return those files belonging to now, but not prev.
   576      */
   576      */
   577     private Set<Source> calculateAddedSources() {
   577     private Set<Source> calculateAddedSources() {
   578         Set<Source> added = new HashSet<Source>();
   578         Set<Source> added = new HashSet<>();
   579         for (String src : now.sources().keySet()) {
   579         for (String src : now.sources().keySet()) {
   580             if (prev.sources().get(src) == null) {
   580             if (prev.sources().get(src) == null) {
   581                 added.add(now.sources().get(src));
   581                 added.add(now.sources().get(src));
   582             }
   582             }
   583         }
   583         }
   589      * If a source file timestamp suddenly is older than what is known
   589      * If a source file timestamp suddenly is older than what is known
   590      * about it in javac_state, then consider it modified, but print
   590      * about it in javac_state, then consider it modified, but print
   591      * a warning!
   591      * a warning!
   592      */
   592      */
   593     private Set<Source> calculateModifiedSources() {
   593     private Set<Source> calculateModifiedSources() {
   594         Set<Source> modified = new HashSet<Source>();
   594         Set<Source> modified = new HashSet<>();
   595         for (String src : now.sources().keySet()) {
   595         for (String src : now.sources().keySet()) {
   596             Source n = now.sources().get(src);
   596             Source n = now.sources().get(src);
   597             Source t = prev.sources().get(src);
   597             Source t = prev.sources().get(src);
   598             if (prev.sources().get(src) != null) {
   598             if (prev.sources().get(src) != null) {
   599                 if (t != null) {
   599                 if (t != null) {
   625 
   625 
   626     /**
   626     /**
   627      * Run the copy translator only.
   627      * Run the copy translator only.
   628      */
   628      */
   629     public void performCopying(File binDir, Map<String,Transformer> suffixRules) {
   629     public void performCopying(File binDir, Map<String,Transformer> suffixRules) {
   630         Map<String,Transformer> sr = new HashMap<String,Transformer>();
   630         Map<String,Transformer> sr = new HashMap<>();
   631         for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) {
   631         for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) {
   632             if (e.getValue() == copyFiles) {
   632             if (e.getValue() == copyFiles) {
   633                 sr.put(e.getKey(), e.getValue());
   633                 sr.put(e.getKey(), e.getValue());
   634             }
   634             }
   635         }
   635         }
   639     /**
   639     /**
   640      * Run all the translators that translate into java source code.
   640      * Run all the translators that translate into java source code.
   641      * I.e. all translators that are not copy nor compile_java_source.
   641      * I.e. all translators that are not copy nor compile_java_source.
   642      */
   642      */
   643     public void performTranslation(File gensrcDir, Map<String,Transformer> suffixRules) {
   643     public void performTranslation(File gensrcDir, Map<String,Transformer> suffixRules) {
   644         Map<String,Transformer> sr = new HashMap<String,Transformer>();
   644         Map<String,Transformer> sr = new HashMap<>();
   645         for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) {
   645         for (Map.Entry<String,Transformer> e : suffixRules.entrySet()) {
   646             if (e.getValue() != copyFiles &&
   646             if (e.getValue() != copyFiles &&
   647                 e.getValue() != compileJavaPackages) {
   647                 e.getValue() != compileJavaPackages) {
   648                 sr.put(e.getKey(), e.getValue());
   648                 sr.put(e.getKey(), e.getValue());
   649             }
   649             }
   657     public boolean performJavaCompilations(File binDir,
   657     public boolean performJavaCompilations(File binDir,
   658                                            String serverSettings,
   658                                            String serverSettings,
   659                                            String[] args,
   659                                            String[] args,
   660                                            Set<String> recentlyCompiled,
   660                                            Set<String> recentlyCompiled,
   661                                            boolean[] rcValue) {
   661                                            boolean[] rcValue) {
   662         Map<String,Transformer> suffixRules = new HashMap<String,Transformer>();
   662         Map<String,Transformer> suffixRules = new HashMap<>();
   663         suffixRules.put(".java", compileJavaPackages);
   663         suffixRules.put(".java", compileJavaPackages);
   664         compileJavaPackages.setExtra(serverSettings);
   664         compileJavaPackages.setExtra(serverSettings);
   665         compileJavaPackages.setExtra(args);
   665         compileJavaPackages.setExtra(args);
   666 
   666 
   667         rcValue[0] = perform(binDir, suffixRules);
   667         rcValue[0] = perform(binDir, suffixRules);
   668         recentlyCompiled.addAll(taintedPackages());
   668         recentlyCompiled.addAll(taintedPackages());
   669         clearTaintedPackages();
   669         clearTaintedPackages();
   670         boolean again = !packagesWithChangedPublicApis.isEmpty();
   670         boolean again = !packagesWithChangedPublicApis.isEmpty();
   671         taintPackagesDependingOnChangedPackages(packagesWithChangedPublicApis, recentlyCompiled);
   671         taintPackagesDependingOnChangedPackages(packagesWithChangedPublicApis, recentlyCompiled);
   672         packagesWithChangedPublicApis = new HashSet<String>();
   672         packagesWithChangedPublicApis = new HashSet<>();
   673         return again && rcValue[0];
   673         return again && rcValue[0];
   674     }
   674     }
   675 
   675 
   676     /**
   676     /**
   677      * Store the source into the set of sources belonging to the given transform.
   677      * Store the source into the set of sources belonging to the given transform.
   678      */
   678      */
   679     private void addFileToTransform(Map<Transformer,Map<String,Set<URI>>> gs, Transformer t, Source s) {
   679     private void addFileToTransform(Map<Transformer,Map<String,Set<URI>>> gs, Transformer t, Source s) {
   680         Map<String,Set<URI>> fs = gs.get(t);
   680         Map<String,Set<URI>> fs = gs.get(t);
   681         if (fs == null) {
   681         if (fs == null) {
   682             fs = new HashMap<String,Set<URI>>();
   682             fs = new HashMap<>();
   683             gs.put(t, fs);
   683             gs.put(t, fs);
   684         }
   684         }
   685         Set<URI> ss = fs.get(s.pkg().name());
   685         Set<URI> ss = fs.get(s.pkg().name());
   686         if (ss == null) {
   686         if (ss == null) {
   687             ss = new HashSet<URI>();
   687             ss = new HashSet<>();
   688             fs.put(s.pkg().name(), ss);
   688             fs.put(s.pkg().name(), ss);
   689         }
   689         }
   690         ss.add(s.file().toURI());
   690         ss.add(s.file().toURI());
   691     }
   691     }
   692 
   692 
   696      */
   696      */
   697     private boolean perform(File outputDir, Map<String,Transformer> suffixRules)
   697     private boolean perform(File outputDir, Map<String,Transformer> suffixRules)
   698     {
   698     {
   699         boolean rc = true;
   699         boolean rc = true;
   700         // Group sources based on transforms. A source file can only belong to a single transform.
   700         // Group sources based on transforms. A source file can only belong to a single transform.
   701         Map<Transformer,Map<String,Set<URI>>> groupedSources = new HashMap<Transformer,Map<String,Set<URI>>>();
   701         Map<Transformer,Map<String,Set<URI>>> groupedSources = new HashMap<>();
   702         for (Source src : now.sources().values()) {
   702         for (Source src : now.sources().values()) {
   703             Transformer t = suffixRules.get(src.suffix());
   703             Transformer t = suffixRules.get(src.suffix());
   704                if (t != null) {
   704                if (t != null) {
   705                 if (taintedPackages.contains(src.pkg().name()) && !src.isLinkedOnly()) {
   705                 if (taintedPackages.contains(src.pkg().name()) && !src.isLinkedOnly()) {
   706                     addFileToTransform(groupedSources, t, src);
   706                     addFileToTransform(groupedSources, t, src);
   710         // Go through the transforms and transform them.
   710         // Go through the transforms and transform them.
   711         for (Map.Entry<Transformer,Map<String,Set<URI>>> e : groupedSources.entrySet()) {
   711         for (Map.Entry<Transformer,Map<String,Set<URI>>> e : groupedSources.entrySet()) {
   712             Transformer t = e.getKey();
   712             Transformer t = e.getKey();
   713             Map<String,Set<URI>> srcs = e.getValue();
   713             Map<String,Set<URI>> srcs = e.getValue();
   714             // These maps need to be synchronized since multiple threads will be writing results into them.
   714             // These maps need to be synchronized since multiple threads will be writing results into them.
   715             Map<String,Set<URI>> packageArtifacts = Collections.synchronizedMap(new HashMap<String,Set<URI>>());
   715             Map<String,Set<URI>> packageArtifacts = Collections.synchronizedMap(new HashMap<>());
   716             Map<String,Set<String>> packageDependencies = Collections.synchronizedMap(new HashMap<String,Set<String>>());
   716             Map<String,Set<String>> packageDependencies = Collections.synchronizedMap(new HashMap<>());
   717             Map<String,String> packagePublicApis = Collections.synchronizedMap(new HashMap<String,String>());
   717             Map<String,String> packagePublicApis = Collections.synchronizedMap(new HashMap<>());
   718 
   718 
   719             boolean  r = t.transform(srcs,
   719             boolean  r = t.transform(srcs,
   720                                      visibleSrcs,
   720                                      visibleSrcs,
   721                                      visibleClasses,
   721                                      visibleClasses,
   722                                      prev.dependents(),
   722                                      prev.dependents(),
   768 
   768 
   769     /**
   769     /**
   770      * Utility method to recursively find all files below a directory.
   770      * Utility method to recursively find all files below a directory.
   771      */
   771      */
   772     private static Set<File> findAllFiles(File dir) {
   772     private static Set<File> findAllFiles(File dir) {
   773         Set<File> foundFiles = new HashSet<File>();
   773         Set<File> foundFiles = new HashSet<>();
   774         if (dir == null) {
   774         if (dir == null) {
   775             return foundFiles;
   775             return foundFiles;
   776         }
   776         }
   777         recurse(dir, foundFiles);
   777         recurse(dir, foundFiles);
   778         return foundFiles;
   778         return foundFiles;
   801         // We need to adjust our calculated sources to be identical, if necessary.
   801         // We need to adjust our calculated sources to be identical, if necessary.
   802         boolean mightNeedRewriting = File.pathSeparatorChar == ';';
   802         boolean mightNeedRewriting = File.pathSeparatorChar == ';';
   803 
   803 
   804         if (makefileSourceList == null) return;
   804         if (makefileSourceList == null) return;
   805 
   805 
   806         Set<String> calculatedSources = new HashSet<String>();
   806         Set<String> calculatedSources = new HashSet<>();
   807         Set<String> listedSources = new HashSet<String>();
   807         Set<String> listedSources = new HashSet<>();
   808 
   808 
   809         // Create a set of filenames with full paths.
   809         // Create a set of filenames with full paths.
   810         for (Source s : now.sources().values()) {
   810         for (Source s : now.sources().values()) {
   811             // Don't include link only sources when comparing sources to compile
   811             // Don't include link only sources when comparing sources to compile
   812             if (!s.isLinkedOnly()) {
   812             if (!s.isLinkedOnly()) {