Search in sources :

Example 1 with IModule

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

the class Main method extractModuleDesc.

private IModule extractModuleDesc(String fileName) {
    IModule mod = null;
    if (fileName.toLowerCase().endsWith(IModule.MODULE_INFO_JAVA)) {
        // this.options may not be completely populated yet, and definitely not
        // validated. Make sure the source level is set for the parser
        Map<String, String> opts = new HashMap<String, String>(this.options);
        opts.put(CompilerOptions.OPTION_Source, this.options.get(CompilerOptions.OPTION_Compliance));
        Parser parser = new Parser(new ProblemReporter(getHandlingPolicy(), new CompilerOptions(opts), getProblemFactory()), false);
        ICompilationUnit cu = new CompilationUnit(null, fileName, null);
        CompilationResult compilationResult = new CompilationResult(cu, 0, 1, 10);
        CompilationUnitDeclaration unit = parser.parse(cu, compilationResult);
        if (unit.isModuleInfo() && unit.moduleDeclaration != null) {
            mod = new BasicModule(unit.moduleDeclaration, null);
        }
    } else if (fileName.toLowerCase().endsWith(IModule.MODULE_INFO_CLASS)) {
        try {
            // Check the absolute path?
            ClassFileReader reader = ClassFileReader.read(fileName);
            mod = reader.getModuleDeclaration();
        } catch (ClassFormatException | IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException(// $NON-NLS-1$
            this.bind("configure.invalidModuleDescriptor", fileName));
        }
    }
    return mod;
}
Also used : ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) IModule(org.eclipse.jdt.internal.compiler.env.IModule) ProblemReporter(org.eclipse.jdt.internal.compiler.problem.ProblemReporter) HashMap(java.util.HashMap) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) Parser(org.eclipse.jdt.internal.compiler.parser.Parser) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult)

Example 2 with IModule

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

the class Main method handleModuleSourcepath.

protected ArrayList<FileSystem.Classpath> handleModuleSourcepath(String arg) {
    ArrayList<String> modulePaths = processModulePathEntries(arg);
    ArrayList<FileSystem.Classpath> result = new ArrayList<>();
    if ((modulePaths != null) && (modulePaths.size() != 0)) {
        if (this.destinationPath == null) {
            // $NON-NLS-1$
            addPendingErrors(this.bind("configure.missingDestinationPath"));
        }
        String[] paths = new String[modulePaths.size()];
        modulePaths.toArray(paths);
        for (int i = 0; i < paths.length; i++) {
            File dir = new File(paths[i]);
            if (dir.isDirectory()) {
                // 1. Create FileSystem.Classpath for each module
                // 2. Iterator each module in case of directory for source files and add to this.fileNames
                List<Classpath> modules = ModuleFinder.findModules(dir, this.destinationPath, getNewParser(), this.options, false, this.releaseVersion);
                for (Classpath classpath : modules) {
                    result.add(classpath);
                    Path modLocation = Paths.get(classpath.getPath()).toAbsolutePath();
                    String destPath = classpath.getDestinationPath();
                    IModule mod = classpath.getModule();
                    String moduleName = mod == null ? null : new String(mod.name());
                    for (int j = 0; j < this.filenames.length; j++) {
                        Path filePath;
                        try {
                            // Get canonical path just as the classpath location is stored with the same.
                            // To avoid mismatch of /USER_JAY and /USE~1 in windows systems.
                            filePath = new File(this.filenames[j]).getCanonicalFile().toPath();
                            if (filePath.startsWith(modLocation)) {
                                this.modNames[j] = moduleName;
                                this.destinationPaths[j] = destPath;
                            }
                        } catch (IOException e) {
                            // Files doesn't exist and perhaps doesn't belong in a module, move on to other files
                            // Use empty module name to distinguish from missing module case
                            // $NON-NLS-1$
                            this.modNames[j] = "";
                        }
                    }
                }
            }
        }
        for (int j = 0; j < this.filenames.length; j++) {
            if (this.modNames[j] == null) {
                // $NON-NLS-1$
                throw new IllegalArgumentException(this.bind("configure.notOnModuleSourcePath", new String[] { this.filenames[j] }));
            }
        }
    }
    return result;
}
Also used : Path(java.nio.file.Path) IModule(org.eclipse.jdt.internal.compiler.env.IModule) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Classpath(org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath) ClassFile(org.eclipse.jdt.internal.compiler.ClassFile) File(java.io.File)

Example 3 with IModule

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

the class FileSystem method scanForModules.

/**
 * TESTS ONLY
 */
public void scanForModules(Parser parser) {
    for (int i = 0, max = this.classpaths.length; i < max; i++) {
        File file = new File(this.classpaths[i].getPath());
        IModule iModule = ModuleFinder.scanForModule(this.classpaths[i], file, parser, false, null);
        if (iModule != null)
            this.moduleLocations.put(String.valueOf(iModule.name()), this.classpaths[i]);
    }
}
Also used : IModule(org.eclipse.jdt.internal.compiler.env.IModule) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 4 with IModule

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

the class ModuleFinder method extractModuleFromClass.

private static IModule extractModuleFromClass(File classfilePath, Classpath pathEntry) {
    ClassFileReader reader;
    try {
        reader = ClassFileReader.read(classfilePath);
        IModule module = getModule(reader);
        if (module != null) {
            return reader.getModuleDeclaration();
        }
        return null;
    } catch (ClassFormatException | IOException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : IModule(org.eclipse.jdt.internal.compiler.env.IModule) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) IOException(java.io.IOException) ClassFormatException(org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException)

Example 5 with IModule

use of org.eclipse.jdt.internal.compiler.env.IModule in project m2e-core by eclipse-m2e.

the class ModuleSupport method getModuleInfo.

private static InternalModuleInfo getModuleInfo(File file, int targetCompliance) {
    if (!file.isFile()) {
        return null;
    }
    try (JarFile jar = new JarFile(file, false)) {
        Manifest manifest = jar.getManifest();
        boolean isMultiRelease = false;
        if (manifest != null) {
            isMultiRelease = "true".equalsIgnoreCase(manifest.getMainAttributes().getValue("Multi-Release"));
        }
        int compliance = isMultiRelease ? targetCompliance : 8;
        for (int i = compliance; i >= 8; i--) {
            String filename;
            if (i == 8) {
                // 8 represents unversioned module-info.class
                filename = IModule.MODULE_INFO_CLASS;
            } else {
                filename = "META-INF/versions/" + i + "/" + IModule.MODULE_INFO_CLASS;
            }
            ClassFileReader reader = ClassFileReader.read(jar, filename);
            if (reader != null) {
                IModule module = reader.getModuleDeclaration();
                if (module != null) {
                    return InternalModuleInfo.fromDeclaration(module);
                }
            }
        }
        if (manifest != null) {
            // optimization: we already have the manifest, so directly check for Automatic-Module-Name
            // rather than using AutomaticModuleNaming.determineAutomaticModuleName(String)
            String automaticModuleName = manifest.getMainAttributes().getValue("Automatic-Module-Name");
            if (automaticModuleName != null) {
                return InternalModuleInfo.withAutomaticName(automaticModuleName);
            }
        }
    } catch (ClassFormatException | IOException ex) {
        log.error(ex.getMessage(), ex);
    }
    return InternalModuleInfo.withAutomaticNameFromFile(file);
}
Also used : IModule(org.eclipse.jdt.internal.compiler.env.IModule) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) ClassFormatException(org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException)

Aggregations

IModule (org.eclipse.jdt.internal.compiler.env.IModule)11 IOException (java.io.IOException)6 ClassFileReader (org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader)4 File (java.io.File)3 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 ZipFile (java.util.zip.ZipFile)3 ClassFormatException (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException)3 FileVisitResult (java.nio.file.FileVisitResult)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 ArrayList (java.util.ArrayList)2 JarFile (java.util.jar.JarFile)2 FilenameFilter (java.io.FilenameFilter)1 Manifest (java.util.jar.Manifest)1 ZipEntry (java.util.zip.ZipEntry)1 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 Classpath (org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath)1 ICompilationUnit (org.eclipse.jdt.internal.compiler.env.ICompilationUnit)1