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;
}
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;
}
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]);
}
}
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;
}
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);
}
Aggregations