introduce JpsJavacFileProvider

GitOrigin-RevId: f4074cb69853cf982ece36eb0cff14f9c406ac8a
This commit is contained in:
Vladimir Krivosheev
2025-02-08 10:42:41 +01:00
committed by intellij-monorepo-bot
parent 3599f1909f
commit e69c97a244
7 changed files with 70 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.compiler;
import com.intellij.openapi.module.Module;
@@ -66,7 +66,7 @@ public class JavaCompilerBasicTest extends BaseCompilerTestCase {
}
}, Locale.US, null);
try (final JpsJavacFileManager fileManager = new JpsJavacFileManager(new DummyContext(stdFileManager), true, Collections.emptyList())) {
try (final JpsJavacFileManager fileManager = new JpsJavacFileManager(new DummyContext(stdFileManager), true, Collections.emptyList(), null)) {
fileManager.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(jarFile));
fileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.emptyList());
@@ -100,7 +100,7 @@ public class JavaCompilerBasicTest extends BaseCompilerTestCase {
}
}, Locale.US, null);
try (final JpsJavacFileManager fileManager = new JpsJavacFileManager(new DummyContext(stdFileManager), true, Collections.emptyList())) {
try (final JpsJavacFileManager fileManager = new JpsJavacFileManager(new DummyContext(stdFileManager), true, Collections.emptyList(), null)) {
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(outputRoot));
final Iterable<JavaFileObject> files = fileManager.list(StandardLocation.CLASS_OUTPUT, "ppp", Set.of(JavaFileObject.Kind.CLASS, JavaFileObject.Kind.OTHER), false);
@@ -135,7 +135,7 @@ public class JavaCompilerBasicTest extends BaseCompilerTestCase {
}
}, Locale.US, null);
try (final JpsJavacFileManager fileManager = new JpsJavacFileManager(new DummyContext(stdFileManager), true, Collections.emptyList())) {
try (final JpsJavacFileManager fileManager = new JpsJavacFileManager(new DummyContext(stdFileManager), true, Collections.emptyList(), null)) {
fileManager.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(jarFile));
fileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.emptyList());
@@ -188,7 +188,7 @@ public class JavaCompilerBasicTest extends BaseCompilerTestCase {
}
}, Locale.US, null);
try (final JpsJavacFileManager fileManager = new JpsJavacFileManager(new DummyContext(stdFileManager), true, Collections.emptyList())) {
try (final JpsJavacFileManager fileManager = new JpsJavacFileManager(new DummyContext(stdFileManager), true, Collections.emptyList(), null)) {
fileManager.setLocation(StandardLocation.SOURCE_PATH, Collections.emptyList());
fileManager.handleOption("--patch-module", Arrays.asList("java.desktop=" + srcRoot.getPath()).iterator());

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.jps.javac;
import io.netty.bootstrap.Bootstrap;
@@ -190,7 +190,7 @@ public final class ExternalJavacProcess {
try {
JavaCompilingTool tool = getCompilingTool();
final boolean rc = JavacMain.compile(
options, files, classpath, platformCp, modulePath, upgradeModulePath, sourcePath, outs, diagnostic, outputSink, canceledStatus, tool
options, files, classpath, platformCp, modulePath, upgradeModulePath, sourcePath, outs, diagnostic, outputSink, canceledStatus, tool, null
);
return JavacProtoUtil.toMessage(sessionId, JavacProtoUtil.createBuildCompletedResponse(rc));
}

View File

@@ -1,13 +1,15 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.jps.javac;
import com.intellij.openapi.util.io.FileUtilRt;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import javax.tools.*;
import java.io.*;
final class InputFileObject extends JpsFileObject {
@ApiStatus.Internal
public final class InputFileObject extends JpsFileObject {
private final File myFile;
private final String myEncoding;
private final ValueSupplier<File, RuntimeException> myAbsFile;

View File

@@ -58,7 +58,8 @@ public final class JavacMain {
final DiagnosticOutputConsumer diagnosticConsumer,
final OutputFileConsumer outputSink,
CanceledStatus canceledStatus,
@NotNull JavaCompilingTool compilingTool) {
@NotNull JavaCompilingTool compilingTool,
@Nullable JpsJavacFileProvider jpsJavacFileProvider) {
JavaCompiler compiler;
try {
compiler = compilingTool.createCompiler();
@@ -75,7 +76,8 @@ public final class JavacMain {
final boolean usingJavac = compilingTool instanceof JavacCompilerTool;
final boolean javacBefore9 = usingJavac && JAVA_RUNTIME_PRE_9; // since java 9 internal API's used by the optimizedFileManager have changed
final JpsJavacFileManager fileManager = new JpsJavacFileManager(
new ContextImpl(compiler, diagnosticConsumer, outputSink, modulePath, canceledStatus), javacBefore9, JavaSourceTransformer.getTransformers()
new ContextImpl(compiler, diagnosticConsumer, outputSink, modulePath, canceledStatus), javacBefore9, JavaSourceTransformer.getTransformers(),
jpsJavacFileProvider
);
if (javacBefore9 && !Iterators.isEmpty(platformClasspath)) {
// for javac6 this will prevent lazy initialization of Paths.bootClassPathRtJar

View File

@@ -1,11 +1,8 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.jps.javac;
import com.intellij.openapi.util.io.FileUtilRt;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.*;
import org.jetbrains.jps.builders.java.JavaSourceTransformer;
import org.jetbrains.jps.javac.Iterators.BooleanFunction;
import org.jetbrains.jps.javac.Iterators.Function;
@@ -44,6 +41,7 @@ public final class JpsJavacFileManager extends ForwardingJavaFileManager<Standar
});
private final Context myContext;
private final @Nullable JpsJavacFileProvider myJpsJavacFileProvider;
private final boolean myJavacBefore9;
private final Collection<? extends JavaSourceTransformer> mySourceTransformers;
private final FileOperations myFileOperations = new DefaultFileOperations();
@@ -77,10 +75,14 @@ public final class JpsJavacFileManager extends ForwardingJavaFileManager<Standar
private final Map<String, JavaFileObject> myInputSourcesIndex = new HashMap<>();
private final List<Closeable> myCloseables = new ArrayList<>();
public JpsJavacFileManager(final Context context, boolean javacBefore9, Collection<? extends JavaSourceTransformer> transformers) {
public JpsJavacFileManager(final Context context,
boolean javacBefore9,
Collection<? extends JavaSourceTransformer> transformers,
@Nullable final JpsJavacFileProvider javacFileProvider) {
super(context.getStandardFileManager());
myJavacBefore9 = javacBefore9;
mySourceTransformers = transformers;
myJpsJavacFileProvider = javacFileProvider;
myContext = new Context() {
@Nullable
@Override
@@ -169,9 +171,16 @@ public final class JpsJavacFileManager extends ForwardingJavaFileManager<Standar
return getFileForOutput(location, JpsFileObject.findKind(fileName), fileName, null, sibling);
}
private OutputFileObject getFileForOutput(Location location, JavaFileObject.Kind kind, String fileName, @Nullable String className, FileObject sibling) throws IOException {
private JavaFileObject getFileForOutput(Location location, JavaFileObject.Kind kind, String fileName, @Nullable String className, FileObject sibling) throws IOException {
checkCanceled();
if (myJpsJavacFileProvider != null && kind == JavaFileObject.Kind.CLASS) {
JavaFileObject result = myJpsJavacFileProvider.getFileForOutput(fileName, className, sibling);
if (result != null) {
return result;
}
}
Iterable<URI> originatingSources = null;
if (sibling instanceof JavaFileObject) {
final JavaFileObject javaFileObject = (JavaFileObject)sibling;
@@ -402,6 +411,12 @@ public final class JpsJavacFileManager extends ForwardingJavaFileManager<Standar
return inferred;
}
}
else if (myJpsJavacFileProvider != null) {
String inferred = myJpsJavacFileProvider.inferBinaryName(location, file);
if (inferred != null) {
return inferred;
}
}
return super.inferBinaryName(location, _fo);
}
@@ -491,9 +506,16 @@ public final class JpsJavacFileManager extends ForwardingJavaFileManager<Standar
// we consider here only locations that are known to be file-based
final Iterable<? extends File> locationRoots = getLocation(location);
if (Iterators.isEmpty(locationRoots)) {
return Collections.emptyList();
if (myJpsJavacFileProvider == null) {
return Collections.emptyList();
}
else {
return myJpsJavacFileProvider.list(location, packageName, kinds, recurse);
}
}
result = Iterators.flat(Iterators.map(locationRoots, new Function<File, Iterable<JavaFileObject>>() {
result = Iterators.flat(
myJpsJavacFileProvider == null ? null : myJpsJavacFileProvider.list(location, packageName, kinds, recurse),
Iterators.flat(Iterators.map(locationRoots, new Function<File, Iterable<JavaFileObject>>() {
@Override
public Iterable<JavaFileObject> fun(File root) {
try {
@@ -544,7 +566,8 @@ public final class JpsJavacFileManager extends ForwardingJavaFileManager<Standar
throw new RuntimeException(e);
}
}
}));
}))
);
}
else {
// locations, not supported by this class should be handled by default javac file manager

View File

@@ -0,0 +1,21 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.jps.javac;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import javax.tools.*;
import java.util.Set;
@ApiStatus.Internal
public interface JpsJavacFileProvider {
@Nullable
Iterable<JavaFileObject> list(JavaFileManager.Location location,
String packageName,
Set<JavaFileObject.Kind> kinds,
boolean recurse);
String inferBinaryName(JavaFileManager.Location location, JavaFileObject file);
@Nullable JavaFileObject getFileForOutput(String fileName, String className, FileObject sibling);
}

View File

@@ -532,7 +532,7 @@ public final class JavaBuilder extends ModuleLevelBuilder {
return invokeJavac(compilerSdkVersion, context, chunk, compilingTool, options, files, classesConsumer, (_options, _files, _outSink) -> {
logJavacCall(chunk, _options, "in-process");
return JavacMain.compile(
_options, _files, classPath, platformCp, modulePath, upgradeModulePath, sourcePath, outs, diagnosticSink, _outSink, context.getCancelStatus(), compilingTool
_options, _files, classPath, platformCp, modulePath, upgradeModulePath, sourcePath, outs, diagnosticSink, _outSink, context.getCancelStatus(), compilingTool, null
);
});
}