langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java
changeset 45682 fc3b228b9e2a
parent 44822 2f24758e7ae0
child 45744 db6aedca2c8c
equal deleted inserted replaced
45600:6589d4088eaa 45682:fc3b228b9e2a
    27 
    27 
    28 import java.io.Closeable;
    28 import java.io.Closeable;
    29 import java.io.File;
    29 import java.io.File;
    30 import java.io.FileNotFoundException;
    30 import java.io.FileNotFoundException;
    31 import java.io.IOException;
    31 import java.io.IOException;
       
    32 import java.io.InputStream;
    32 import java.io.UncheckedIOException;
    33 import java.io.UncheckedIOException;
    33 import java.net.URI;
    34 import java.net.URI;
    34 import java.net.URL;
    35 import java.net.URL;
    35 import java.net.URLClassLoader;
    36 import java.net.URLClassLoader;
    36 import java.nio.file.DirectoryIteratorException;
    37 import java.nio.file.DirectoryIteratorException;
    63 import java.util.function.Predicate;
    64 import java.util.function.Predicate;
    64 import java.util.regex.Matcher;
    65 import java.util.regex.Matcher;
    65 import java.util.regex.Pattern;
    66 import java.util.regex.Pattern;
    66 import java.util.stream.Collectors;
    67 import java.util.stream.Collectors;
    67 import java.util.stream.Stream;
    68 import java.util.stream.Stream;
       
    69 import java.util.jar.Attributes;
       
    70 import java.util.jar.Manifest;
    68 
    71 
    69 import javax.lang.model.SourceVersion;
    72 import javax.lang.model.SourceVersion;
    70 import javax.tools.JavaFileManager;
    73 import javax.tools.JavaFileManager;
    71 import javax.tools.JavaFileManager.Location;
    74 import javax.tools.JavaFileManager.Location;
    72 import javax.tools.JavaFileObject;
    75 import javax.tools.JavaFileObject;
  1338                     try (FileSystem fs = jarFSProvider.newFileSystem(p, fsEnv)) {
  1341                     try (FileSystem fs = jarFSProvider.newFileSystem(p, fsEnv)) {
  1339                         Path moduleInfoClass = fs.getPath("module-info.class");
  1342                         Path moduleInfoClass = fs.getPath("module-info.class");
  1340                         if (Files.exists(moduleInfoClass)) {
  1343                         if (Files.exists(moduleInfoClass)) {
  1341                             String moduleName = readModuleName(moduleInfoClass);
  1344                             String moduleName = readModuleName(moduleInfoClass);
  1342                             return new Pair<>(moduleName, p);
  1345                             return new Pair<>(moduleName, p);
       
  1346                         }
       
  1347                         Path mf = fs.getPath("META-INF/MANIFEST.MF");
       
  1348                         if (Files.exists(mf)) {
       
  1349                             try (InputStream in = Files.newInputStream(mf)) {
       
  1350                                 Manifest man = new Manifest(in);
       
  1351                                 Attributes attrs = man.getMainAttributes();
       
  1352                                 if (attrs != null) {
       
  1353                                     String moduleName = attrs.getValue(new Attributes.Name("Automatic-Module-Name"));
       
  1354                                     if (moduleName != null) {
       
  1355                                         if (isModuleName(moduleName)) {
       
  1356                                             return new Pair<>(moduleName, p);
       
  1357                                         } else {
       
  1358                                             log.error(Errors.LocnCantGetModuleNameForJar(p));
       
  1359                                             return null;
       
  1360                                         }
       
  1361                                     }
       
  1362                                 }
       
  1363                             }
  1343                         }
  1364                         }
  1344                     } catch (ModuleNameReader.BadClassFile e) {
  1365                     } catch (ModuleNameReader.BadClassFile e) {
  1345                         log.error(Errors.LocnBadModuleInfo(p));
  1366                         log.error(Errors.LocnBadModuleInfo(p));
  1346                         return null;
  1367                         return null;
  1347                     } catch (IOException e) {
  1368                     } catch (IOException e) {
  1426                     moduleNameReader = new ModuleNameReader();
  1447                     moduleNameReader = new ModuleNameReader();
  1427                 return moduleNameReader.readModuleName(path);
  1448                 return moduleNameReader.readModuleName(path);
  1428             }
  1449             }
  1429         }
  1450         }
  1430 
  1451 
       
  1452         //from jdk.internal.module.Checks:
       
  1453         /**
       
  1454          * Returns {@code true} if the given name is a legal module name.
       
  1455          */
       
  1456         private boolean isModuleName(String name) {
       
  1457             int next;
       
  1458             int off = 0;
       
  1459             while ((next = name.indexOf('.', off)) != -1) {
       
  1460                 String id = name.substring(off, next);
       
  1461                 if (!SourceVersion.isName(id))
       
  1462                     return false;
       
  1463                 off = next+1;
       
  1464             }
       
  1465             String last = name.substring(off);
       
  1466             return SourceVersion.isName(last);
       
  1467         }
  1431     }
  1468     }
  1432 
  1469 
  1433     private class ModuleSourcePathLocationHandler extends BasicLocationHandler {
  1470     private class ModuleSourcePathLocationHandler extends BasicLocationHandler {
  1434         private ModuleTable moduleTable;
  1471         private ModuleTable moduleTable;
  1435         private List<Path> paths;
  1472         private List<Path> paths;