Skip to content

[NETBEANS-4044] Fixing patching of modular libraries #4391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
Expand All @@ -29,6 +30,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -43,6 +45,7 @@
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.queries.BinaryForSourceQuery;
import org.netbeans.modules.java.source.indexing.JavaIndex;
import org.netbeans.modules.java.source.util.Iterators;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
Expand All @@ -64,6 +67,7 @@ final class PatchModuleFileManager implements JavaFileManager {
private final Map<String,List<URL>> patches;
private final Map<URL, String> roots;
private Set<PatchLocation> moduleLocations;
private String overrideModuleName;

PatchModuleFileManager(
@NonNull final JavaFileManager binDelegate,
Expand Down Expand Up @@ -162,7 +166,8 @@ public boolean handleOption(String head, Iterator<String> tail) {
return true;
}
} else if (head.startsWith(JavacParser.NB_X_MODULE)) {
addModulePatches(head.substring(JavacParser.NB_X_MODULE.length()),
overrideModuleName = head.substring(JavacParser.NB_X_MODULE.length());
addModulePatches(overrideModuleName,
src.entries().stream().map(e -> e.getURL()).collect(Collectors.toList()));
return true;
}
Expand Down Expand Up @@ -214,6 +219,8 @@ public ClassLoader getClassLoader(Location location) {

@Override
public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
location = fixLocation(location);

if (PatchLocation.isInstance(location)) {
final PatchLocation pl = PatchLocation.cast(location);
final ModuleLocation bin = pl.getBin();
Expand Down Expand Up @@ -249,6 +256,8 @@ public String inferBinaryName(Location location, JavaFileObject file) {

@Override
public JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind) throws IOException {
location = fixLocation(location);

if (PatchLocation.isInstance(location)) {
final PatchLocation pl = PatchLocation.cast(location);
final ModuleLocation bin = pl.getBin();
Expand Down Expand Up @@ -285,14 +294,22 @@ public FileObject getFileForOutput(Location location, String packageName, String
}
//</editor-fold>

private Location fixLocation(Location input) throws IOException {
if (input == StandardLocation.CLASS_OUTPUT && overrideModuleName != null) {
return this.moduleLocations.stream().filter(pl -> overrideModuleName.equals(pl.getModuleName())).map(pl -> (Location) pl).findAny().orElse(input);
} else {
return input;
}
}

private Set<PatchLocation> moduleLocations(final Location baseLocation) throws IOException {
if (baseLocation != StandardLocation.PATCH_MODULE_PATH) {
throw new IllegalStateException(baseLocation.toString());
}
if (moduleLocations == null) {
Set<PatchLocation> res = new HashSet<>();
for (Map.Entry<String,List<URL>> patch : patches.entrySet()) {
res.add(createPatchLocation(patch.getKey(), patch.getValue()));
res.add(createPatchLocation(patch.getKey(), patch.getValue(), patch.getKey().equals(overrideModuleName)));
}
moduleLocations = Collections.unmodifiableSet(res);
}
Expand All @@ -302,15 +319,20 @@ private Set<PatchLocation> moduleLocations(final Location baseLocation) throws I
@NonNull
private static PatchLocation createPatchLocation(
@NonNull final String modName,
@NonNull final List<? extends URL> roots) throws IOException {
@NonNull final List<? extends URL> roots,
final boolean sourceOverride) throws IOException {
Collection<URL> bin = new ArrayList<>(roots.size());
Collection<URL> src = new ArrayList<>(roots.size());
for (URL root : roots) {
if (JavaIndex.hasSourceCache(root, false)) {
src.add(root);
bin.add(FileUtil.urlForArchiveOrDir(JavaIndex.getClassFolder(root)));
} else {
bin.add(root);
if (sourceOverride) {
bin.addAll(Arrays.asList(BinaryForSourceQuery.findBinaryRoots(root).getRoots()));
} else {
bin.add(root);
}
}
}
return new PatchLocation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.netbeans.modules.java.source.parsing;

import com.sun.tools.javac.api.JavacTool;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.swing.event.ChangeListener;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileManager.Location;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
import static junit.framework.TestCase.assertEquals;
import org.netbeans.api.java.queries.BinaryForSourceQuery;
import org.netbeans.api.java.source.ClasspathInfo;
import org.netbeans.api.java.source.CompilationController;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.java.source.JavaSource.Phase;
import org.netbeans.api.java.source.SourceUtilsTestUtil;
import org.netbeans.api.java.source.Task;
import org.netbeans.api.java.source.TestUtilities;
import org.netbeans.junit.NbTestCase;
import org.netbeans.modules.java.source.tasklist.CompilerSettings;
import org.netbeans.modules.java.source.usages.ClasspathInfoAccessor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation;

public class PatchModuleTest extends NbTestCase {

public PatchModuleTest(String name) {
super(name);
}

private FileObject sourceRoot;
private FileObject classRoot;
private FileObject cp;

protected void setUp() throws Exception {
SourceUtilsTestUtil.prepareTest(new String[0], new Object[] {settings, binaryForSource});
clearWorkDir();
prepareTest();
}

public void testNETBEANS_4044() throws Exception {
settings.commandLine = "-Xnb-Xmodule:patch";
binaryForSource.result = new BinaryForSourceQuery.Result() {
@Override
public URL[] getRoots() {
return new URL[] {classRoot.toURL()};
}
@Override
public void addChangeListener(ChangeListener l) {}
@Override
public void removeChangeListener(ChangeListener l) {}
};
FileObject sourceModule = createFile("module-info.java", "module patch {}"); SourceUtilsTestUtil.setSourceLevel(sourceModule, "11");
FileObject source1 = createFile("patch/Patch.java", "package patch; class Patch { Dep dep; }"); SourceUtilsTestUtil.setSourceLevel(source1, "11");
FileObject source2 = createFile("patch/Dep.java", "package patch; class Dep { }"); SourceUtilsTestUtil.setSourceLevel(source2, "11");
ClasspathInfo cpInfo = ClasspathInfo.create(sourceModule);
try (JavaFileManager fm = ClasspathInfoAccessor.getINSTANCE().createFileManager(cpInfo, "11");
JavaFileManager output = new OutputFileManager(fm)) {
List<JavaFileObject> files = new ArrayList<>();
Enumeration<? extends FileObject> en = sourceRoot.getChildren(true);
while (en.hasMoreElements()) {
FileObject f = en.nextElement();
if (f.isData()) {
files.add(FileObjects.fileObjectFileObject(f, sourceRoot, null, null));
}
}
assertTrue(JavacTool.create().getTask(null, output, null, null, null, files).call());
}
JavaSource js = JavaSource.forFileObject(source1);

js.runUserActionTask(new Task<CompilationController>() {
public void run(CompilationController parameter) throws Exception {
assertTrue(Phase.RESOLVED.compareTo(parameter.toPhase(Phase.RESOLVED)) <= 0);
assertEquals(parameter.getDiagnostics().toString(), 0, parameter.getDiagnostics().size());
}
}, true);
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
settings.commandLine = null;
binaryForSource.result = null;
}

private FileObject createFile(String path, String content) throws Exception {
FileObject file = FileUtil.createData(sourceRoot, path);
TestUtilities.copyStringToFile(file, content);

return file;
}

private void prepareTest() throws Exception {
File work = getWorkDir();
FileObject workFO = FileUtil.toFileObject(work);

assertNotNull(workFO);

sourceRoot = workFO.createFolder("src");
classRoot = workFO.createFolder("class");

FileObject buildRoot = workFO.createFolder("build");
FileObject cache = workFO.createFolder("cache");

SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache, new FileObject[] {cp});
}

private static final CompilerSettingsImpl settings = new CompilerSettingsImpl();

private static final class CompilerSettingsImpl extends CompilerSettings {
private String commandLine;
@Override
protected String buildCommandLine(FileObject file) {
return commandLine;
}

}

private static final BinaryForSourceQueryImpl binaryForSource = new BinaryForSourceQueryImpl();

private static final class BinaryForSourceQueryImpl implements BinaryForSourceQueryImplementation {

private BinaryForSourceQuery.Result result;

@Override
public BinaryForSourceQuery.Result findBinaryRoots(URL sourceRoot) {
return result;
}

}

private class OutputFileManager extends ForwardingJavaFileManager {

public OutputFileManager(JavaFileManager fileManager) {
super(fileManager);
}

@Override
public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, javax.tools.FileObject sibling) throws IOException {
try {
return new SimpleJavaFileObject(new URI("mem://" + className + kind.extension), kind) {
@Override
public OutputStream openOutputStream() throws IOException {
return FileUtil.createData(classRoot, className.replace(".", "/") + kind.extension).getOutputStream();
}
};
} catch (URISyntaxException ex) {
throw new IOException(ex);
}
}
}
}