Search in sources :

Example 1 with ClasspathJsr199

use of org.eclipse.jdt.internal.compiler.batch.ClasspathJsr199 in project bazel-jdt-java-toolchain by salesforce.

the class EclipseCompilerImpl method handleLocations.

protected void handleLocations() {
    ArrayList<FileSystem.Classpath> fileSystemClasspaths = new ArrayList<>();
    EclipseFileManager eclipseJavaFileManager = null;
    StandardJavaFileManager standardJavaFileManager = null;
    JavaFileManager javaFileManager = null;
    boolean havePlatformPaths = false;
    boolean haveClassPaths = false;
    if (this.fileManager instanceof EclipseFileManager) {
        eclipseJavaFileManager = (EclipseFileManager) this.fileManager;
    }
    if (this.fileManager instanceof StandardJavaFileManager) {
        standardJavaFileManager = (StandardJavaFileManager) this.fileManager;
    }
    javaFileManager = this.fileManager;
    if (eclipseJavaFileManager != null) {
        if ((eclipseJavaFileManager.flags & EclipseFileManager.HAS_ENDORSED_DIRS) == 0 && (eclipseJavaFileManager.flags & EclipseFileManager.HAS_BOOTCLASSPATH) != 0) {
            fileSystemClasspaths.addAll(this.handleEndorseddirs(null));
        }
    }
    Iterable<? extends File> locationFiles = null;
    if (standardJavaFileManager != null) {
        locationFiles = standardJavaFileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH);
        if (locationFiles != null) {
            for (File file : locationFiles) {
                if (file.isDirectory()) {
                    List<Classpath> platformLocations = getPlatformLocations(file);
                    if (standardJavaFileManager instanceof EclipseFileManager) {
                        if (platformLocations.size() == 1) {
                            Classpath jrt = platformLocations.get(0);
                            if (jrt instanceof ClasspathJrt) {
                                ClasspathJrt classpathJrt = (ClasspathJrt) jrt;
                                // TODO: double check, should it be platform or system module?
                                try {
                                    EclipseFileManager efm = (EclipseFileManager) standardJavaFileManager;
                                    // XXX EclipseFileManager should close jrtfs but it looks like standardJavaFileManager is never closed
                                    @SuppressWarnings("resource") JrtFileSystem // Was leaking new JrtFileSystem(classpathJrt.file):
                                    jrtfs = efm.getJrtFileSystem(classpathJrt.file);
                                    efm.locationHandler.newSystemLocation(StandardLocation.SYSTEM_MODULES, jrtfs);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                    fileSystemClasspaths.addAll(platformLocations);
                    // Only possible scenario is, we have one and only entry representing the Java home.
                    break;
                } else {
                    Classpath classpath = FileSystem.getClasspath(file.getAbsolutePath(), null, null, this.options, this.releaseVersion);
                    if (classpath != null) {
                        fileSystemClasspaths.add(classpath);
                        havePlatformPaths = true;
                    }
                }
            }
        }
    } else if (javaFileManager != null) {
        File javaHome = Util.getJavaHome();
        long jdkLevel = Util.getJDKLevel(javaHome);
        if (jdkLevel >= ClassFileConstants.JDK9) {
            Classpath systemClasspath = getSystemClasspath(javaHome, jdkLevel);
            Classpath classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.SYSTEM_MODULES);
            fileSystemClasspaths.add(classpath);
            classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.PLATFORM_CLASS_PATH);
            fileSystemClasspaths.add(classpath);
        } else {
            Classpath classpath = new ClasspathJsr199(this.fileManager, StandardLocation.PLATFORM_CLASS_PATH);
            fileSystemClasspaths.add(classpath);
        }
        havePlatformPaths = true;
    }
    if (eclipseJavaFileManager != null) {
        if ((eclipseJavaFileManager.flags & EclipseFileManager.HAS_EXT_DIRS) == 0 && (eclipseJavaFileManager.flags & EclipseFileManager.HAS_BOOTCLASSPATH) != 0) {
            fileSystemClasspaths.addAll(this.handleExtdirs(null));
        }
    }
    if (standardJavaFileManager != null) {
        locationFiles = standardJavaFileManager.getLocation(StandardLocation.SOURCE_PATH);
        if (locationFiles != null) {
            for (File file : locationFiles) {
                Classpath classpath = FileSystem.getClasspath(file.getAbsolutePath(), null, null, this.options, this.releaseVersion);
                if (classpath != null) {
                    fileSystemClasspaths.add(classpath);
                }
            }
        }
        locationFiles = standardJavaFileManager.getLocation(StandardLocation.CLASS_PATH);
        if (locationFiles != null) {
            for (File file : locationFiles) {
                Classpath classpath = FileSystem.getClasspath(file.getAbsolutePath(), null, null, this.options, this.releaseVersion);
                if (classpath != null) {
                    fileSystemClasspaths.add(classpath);
                    haveClassPaths = true;
                }
            }
        }
        locationFiles = standardJavaFileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH);
        if (locationFiles != null) {
            for (File file : locationFiles) {
                if (file.isDirectory()) {
                    String javaVersion = getJavaVersion(file);
                    // $NON-NLS-1$
                    long jdkLevel = javaVersion.equals("") ? this.complianceLevel : CompilerOptions.versionToJdkLevel(javaVersion);
                    Classpath systemClasspath = getSystemClasspath(file, jdkLevel);
                    Classpath classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.PLATFORM_CLASS_PATH);
                    fileSystemClasspaths.add(classpath);
                    // Copy over to modules location as well
                    if (standardJavaFileManager.getLocation(StandardLocation.SYSTEM_MODULES) == null) {
                        classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.SYSTEM_MODULES);
                        fileSystemClasspaths.add(classpath);
                    }
                    haveClassPaths = true;
                    // unlikely to have more than one path
                    break;
                }
            }
        }
        locationFiles = standardJavaFileManager.getLocation(StandardLocation.SYSTEM_MODULES);
        if (locationFiles != null) {
            for (File file : locationFiles) {
                if (file.isDirectory()) {
                    String javaVersion = getJavaVersion(file);
                    // $NON-NLS-1$
                    long jdkLevel = javaVersion.equals("") ? this.complianceLevel : CompilerOptions.versionToJdkLevel(javaVersion);
                    Classpath systemClasspath = getSystemClasspath(file, jdkLevel);
                    Classpath classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.SYSTEM_MODULES);
                    fileSystemClasspaths.add(classpath);
                    // Copy over to platform location as well
                    if (standardJavaFileManager.getLocation(StandardLocation.PLATFORM_CLASS_PATH) == null) {
                        classpath = new ClasspathJsr199(systemClasspath, this.fileManager, StandardLocation.PLATFORM_CLASS_PATH);
                        fileSystemClasspaths.add(classpath);
                    }
                    haveClassPaths = true;
                    // unlikely to have more than one path
                    break;
                }
            }
        }
        try {
            Iterable<? extends Path> locationAsPaths = standardJavaFileManager.getLocationAsPaths(StandardLocation.MODULE_SOURCE_PATH);
            if (locationAsPaths != null) {
                StringBuilder builder = new StringBuilder();
                for (Path path : locationAsPaths) {
                    // Append all of them
                    builder.append(path.toFile().getCanonicalPath());
                    builder.append(File.pathSeparator);
                }
                ArrayList<Classpath> modulepaths = handleModuleSourcepath(builder.toString());
                for (Classpath classpath : modulepaths) {
                    Collection<String> moduleNames = classpath.getModuleNames(null);
                    for (String modName : moduleNames) {
                        Path p = Paths.get(classpath.getPath());
                        standardJavaFileManager.setLocationForModule(StandardLocation.MODULE_SOURCE_PATH, modName, Collections.singletonList(p));
                        p = Paths.get(classpath.getDestinationPath());
                    }
                }
                fileSystemClasspaths.addAll(modulepaths);
            }
        } catch (IllegalStateException e) {
        // Ignore this as JRE 9 throws IllegalStateException for getLocation returning null
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Exception e) {
            this.logger.logException(e);
        }
        try {
            locationFiles = standardJavaFileManager.getLocation(StandardLocation.MODULE_PATH);
            if (locationFiles != null) {
                for (File file : locationFiles) {
                    try {
                        ArrayList<Classpath> modulepaths = handleModulepath(file.getCanonicalPath());
                        for (Classpath classpath : modulepaths) {
                            Collection<String> moduleNames = classpath.getModuleNames(null);
                            for (String string : moduleNames) {
                                Path path = Paths.get(classpath.getPath());
                                standardJavaFileManager.setLocationForModule(StandardLocation.MODULE_PATH, string, Collections.singletonList(path));
                            }
                        }
                        fileSystemClasspaths.addAll(modulepaths);
                    } catch (IOException e) {
                        throw new AbortCompilationUnit(null, e, null);
                    }
                }
            }
        } catch (IllegalStateException e) {
        // Ignore this as JRE 9 throws IllegalStateException for getLocation returning null
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Exception e) {
            this.logger.logException(e);
        }
    } else if (javaFileManager != null) {
        Classpath classpath = null;
        if (this.fileManager.hasLocation(StandardLocation.SOURCE_PATH)) {
            classpath = new ClasspathJsr199(this.fileManager, StandardLocation.SOURCE_PATH);
            fileSystemClasspaths.add(classpath);
        }
        // Add the locations to search for in specific order
        if (this.fileManager.hasLocation(StandardLocation.UPGRADE_MODULE_PATH)) {
            classpath = new ClasspathJsr199(this.fileManager, StandardLocation.UPGRADE_MODULE_PATH);
        }
        if (this.fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH)) {
            classpath = new ClasspathJsr199(this.fileManager, StandardLocation.PATCH_MODULE_PATH);
            fileSystemClasspaths.add(classpath);
        }
        if (this.fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH)) {
            classpath = new ClasspathJsr199(this.fileManager, StandardLocation.MODULE_SOURCE_PATH);
            fileSystemClasspaths.add(classpath);
        }
        if (this.fileManager.hasLocation(StandardLocation.MODULE_PATH)) {
            classpath = new ClasspathJsr199(this.fileManager, StandardLocation.MODULE_PATH);
            fileSystemClasspaths.add(classpath);
        }
        classpath = new ClasspathJsr199(this.fileManager, StandardLocation.CLASS_PATH);
        fileSystemClasspaths.add(classpath);
        haveClassPaths = true;
    }
    if (this.checkedClasspaths == null) {
        // not sufficient to resolve all standard classes.
        if (!havePlatformPaths)
            fileSystemClasspaths.addAll(this.handleBootclasspath(null, null));
        if (!haveClassPaths)
            fileSystemClasspaths.addAll(this.handleClasspath(null, null));
    }
    fileSystemClasspaths = FileSystem.ClasspathNormalizer.normalize(fileSystemClasspaths);
    final int size = fileSystemClasspaths.size();
    if (size != 0) {
        this.checkedClasspaths = new FileSystem.Classpath[size];
        int i = 0;
        for (FileSystem.Classpath classpath : fileSystemClasspaths) {
            this.checkedClasspaths[i++] = classpath;
        }
    }
}
Also used : AbortCompilationUnit(org.eclipse.jdt.internal.compiler.problem.AbortCompilationUnit) ArrayList(java.util.ArrayList) Classpath(org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath) FileSystem(org.eclipse.jdt.internal.compiler.batch.FileSystem) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ClasspathJsr199(org.eclipse.jdt.internal.compiler.batch.ClasspathJsr199) Path(java.nio.file.Path) JavaFileManager(javax.tools.JavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ClasspathJrt(org.eclipse.jdt.internal.compiler.batch.ClasspathJrt) IOException(java.io.IOException) IOException(java.io.IOException) Classpath(org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath) ClassFile(org.eclipse.jdt.internal.compiler.ClassFile) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 JavaFileManager (javax.tools.JavaFileManager)1 StandardJavaFileManager (javax.tools.StandardJavaFileManager)1 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)1 ClasspathJrt (org.eclipse.jdt.internal.compiler.batch.ClasspathJrt)1 ClasspathJsr199 (org.eclipse.jdt.internal.compiler.batch.ClasspathJsr199)1 FileSystem (org.eclipse.jdt.internal.compiler.batch.FileSystem)1 Classpath (org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath)1 AbortCompilationUnit (org.eclipse.jdt.internal.compiler.problem.AbortCompilationUnit)1