< prev index next >
src/java.base/share/classes/jdk/internal/module/ModulePatcher.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 29,38 ****
--- 29,39 ----
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.module.ModuleDescriptor;
+ import java.lang.module.ModuleDescriptor.Builder;
import java.lang.module.ModuleReader;
import java.lang.module.ModuleReference;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
*** 52,61 ****
--- 53,63 ----
import java.util.jar.JarFile;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.internal.loader.Resource;
+ import jdk.internal.loader.ResourceHelper;
import jdk.internal.misc.JavaLangModuleAccess;
import jdk.internal.misc.SharedSecrets;
import sun.net.www.ParseUtil;
*** 106,152 ****
String mn = descriptor.name();
List<Path> paths = map.get(mn);
if (paths == null)
return mref;
! // scan the JAR file or directory tree to get the set of packages
Set<String> packages = new HashSet<>();
try {
for (Path file : paths) {
if (Files.isRegularFile(file)) {
// JAR file - do not open as a multi-release JAR as this
// is not supported by the boot class loader
try (JarFile jf = new JarFile(file.toFile())) {
jf.stream()
.map(e -> toPackageName(file, e))
! .filter(Checks::isJavaIdentifier)
.forEach(packages::add);
}
} else if (Files.isDirectory(file)) {
// exploded directory without following sym links
Path top = file;
Files.find(top, Integer.MAX_VALUE,
((path, attrs) -> attrs.isRegularFile()))
.map(path -> toPackageName(top, path))
! .filter(Checks::isJavaIdentifier)
.forEach(packages::add);
}
}
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
// if there are new packages then we need a new ModuleDescriptor
! Set<String> original = descriptor.packages();
! packages.addAll(original);
! if (packages.size() > original.size()) {
! descriptor = JLMA.newModuleDescriptor(descriptor, packages);
}
// return a module reference to the patched module
URI location = mref.location().orElse(null);
--- 108,181 ----
String mn = descriptor.name();
List<Path> paths = map.get(mn);
if (paths == null)
return mref;
! // Scan the JAR file or directory tree to get the set of packages.
! // For automatic modules then packages that do not contain class files
! // must be ignored.
Set<String> packages = new HashSet<>();
+ boolean isAutomatic = descriptor.isAutomatic();
try {
for (Path file : paths) {
if (Files.isRegularFile(file)) {
// JAR file - do not open as a multi-release JAR as this
// is not supported by the boot class loader
try (JarFile jf = new JarFile(file.toFile())) {
jf.stream()
+ .filter(e -> !e.isDirectory()
+ && (!isAutomatic || e.getName().endsWith(".class")))
.map(e -> toPackageName(file, e))
! .filter(Checks::isPackageName)
.forEach(packages::add);
}
} else if (Files.isDirectory(file)) {
// exploded directory without following sym links
Path top = file;
Files.find(top, Integer.MAX_VALUE,
((path, attrs) -> attrs.isRegularFile()))
+ .filter(path -> !isAutomatic
+ || path.toString().endsWith(".class"))
.map(path -> toPackageName(top, path))
! .filter(Checks::isPackageName)
.forEach(packages::add);
}
}
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
// if there are new packages then we need a new ModuleDescriptor
! packages.removeAll(descriptor.packages());
! if (!packages.isEmpty()) {
! Builder builder = JLMA.newModuleBuilder(descriptor.name(),
! /*strict*/ false,
! descriptor.modifiers());
! if (!descriptor.isAutomatic()) {
! descriptor.requires().forEach(builder::requires);
! descriptor.exports().forEach(builder::exports);
! descriptor.opens().forEach(builder::opens);
! descriptor.uses().forEach(builder::uses);
! }
! descriptor.provides().forEach(builder::provides);
!
! descriptor.version().ifPresent(builder::version);
! descriptor.mainClass().ifPresent(builder::mainClass);
! descriptor.osName().ifPresent(builder::osName);
! descriptor.osArch().ifPresent(builder::osArch);
! descriptor.osVersion().ifPresent(builder::osVersion);
!
! // original + new packages
! builder.packages(descriptor.packages());
! builder.packages(packages);
!
! descriptor = builder.build();
}
// return a module reference to the patched module
URI location = mref.location().orElse(null);
*** 469,496 ****
@Override
public void close() { }
@Override
public Resource find(String name) throws IOException {
! Path file = Paths.get(name.replace('/', File.separatorChar));
! if (file.getRoot() == null) {
! file = dir.resolve(file);
! } else {
! // drop the root component so that the resource is
! // located relative to the module directory
! int n = file.getNameCount();
! if (n == 0)
! return null;
! file = dir.resolve(file.subpath(0, n));
! }
!
if (Files.isRegularFile(file)) {
return newResource(name, dir, file);
- } else {
- return null;
}
}
private Resource newResource(String name, Path top, Path file) {
return new Resource() {
@Override
public String getName() {
--- 498,516 ----
@Override
public void close() { }
@Override
public Resource find(String name) throws IOException {
! Path path = ResourceHelper.toFilePath(name);
! if (path != null) {
! Path file = dir.resolve(path);
if (Files.isRegularFile(file)) {
return newResource(name, dir, file);
}
}
+ return null;
+ }
private Resource newResource(String name, Path top, Path file) {
return new Resource() {
@Override
public String getName() {
< prev index next >