-
Notifications
You must be signed in to change notification settings - Fork 51
Revapi JMPS compilation support #301
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
Open
IvanAndresFritzler
wants to merge
7
commits into
revapi:main
Choose a base branch
from
IvanAndresFritzler:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
963b9d2
Compilation is now java 9+ and jpms aware (thoug we might need to bri…
IvanAndresFritzler dfe9e9d
Bumping the build to Java 9+ code (thoug we might need to bring back …
IvanAndresFritzler f44f0c8
Fix tests
fsgonz 46b7636
More fixes
fsgonz 52b3053
More fixes
fsgonz f3ef5ff
Multi-release jpms compilation.
IvanAndresFritzler b05ebf7
Split package test (#5)
fsgonz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
241 changes: 241 additions & 0 deletions
241
revapi-java/src/main/java-mr/9/org/revapi/java/compilation/Compiler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| /* | ||
| * Copyright 2014-2025 Lukas Krejci | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed 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.revapi.java.compilation; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.UnsupportedEncodingException; | ||
| import java.io.Writer; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Future; | ||
|
|
||
| import javax.tools.JavaCompiler; | ||
| import javax.tools.JavaFileObject; | ||
| import javax.tools.StandardJavaFileManager; | ||
| import javax.tools.ToolProvider; | ||
|
|
||
| import org.revapi.Archive; | ||
| import org.revapi.TreeFilter; | ||
| import org.revapi.java.AnalysisConfiguration; | ||
| import org.revapi.java.Timing; | ||
| import org.revapi.java.spi.JarExtractor; | ||
| import org.revapi.java.spi.JavaElement; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * @author Lukas Krejci | ||
| * | ||
| * @since 0.1 | ||
| */ | ||
| public final class Compiler { | ||
| private static final Logger LOG = LoggerFactory.getLogger(Compiler.class); | ||
|
|
||
| private final JavaCompiler compiler; | ||
| private final Writer output; | ||
| private final Iterable<? extends Archive> classPath; | ||
| private final Iterable<? extends Archive> additionalClassPath; | ||
| private final ExecutorService executor; | ||
| private final TreeFilter<JavaElement> filter; | ||
| private final Iterable<JarExtractor> jarExtractors; | ||
|
|
||
| public Compiler(ExecutorService executor, Writer reportingOutput, Iterable<JarExtractor> jarExtractors, | ||
| Iterable<? extends Archive> classPath, Iterable<? extends Archive> additionalClassPath, | ||
| TreeFilter<JavaElement> filter) { | ||
| this.jarExtractors = jarExtractors; | ||
|
|
||
| compiler = ToolProvider.getSystemJavaCompiler(); | ||
| if (compiler == null) { | ||
| throw new AssertionError("Could not obtain the system compiler. Is tools.jar on the classpath?"); | ||
| } | ||
|
|
||
| this.executor = executor; | ||
| this.output = reportingOutput; | ||
| this.classPath = classPath; | ||
| this.additionalClassPath = additionalClassPath; | ||
| this.filter = filter; | ||
| } | ||
|
|
||
| public CompilationValve compile(final ProbingEnvironment environment, | ||
| final AnalysisConfiguration.MissingClassReporting missingClassReporting, | ||
| final boolean ignoreMissingAnnotations) throws Exception { | ||
|
|
||
| File targetPath = Files.createTempDirectory("revapi-java").toAbsolutePath().toFile(); | ||
|
|
||
| File sourceDir = new File(targetPath, "sources"); | ||
| sourceDir.mkdir(); | ||
|
|
||
| File lib = new File(targetPath, "lib"); | ||
| lib.mkdir(); | ||
|
|
||
| // make sure the classpath is in the same order as passed in | ||
| int classPathSize = size(classPath); | ||
| int nofArchives = classPathSize + size(additionalClassPath); | ||
|
|
||
| int prefixLength = (int) Math.log10(nofArchives) + 1; | ||
|
|
||
| IdentityHashMap<Archive, File> classPathFiles = copyArchives(classPath, lib, 0, prefixLength); | ||
| IdentityHashMap<Archive, File> additionClassPathFiles = copyArchives(additionalClassPath, lib, classPathSize, | ||
| prefixLength); | ||
|
|
||
| List<String> options = Arrays.asList("-d", sourceDir.toString(), "--module-path", lib.getAbsolutePath(), | ||
| "--add-modules", "ALL-MODULE-PATH"); | ||
|
|
||
| List<JavaFileObject> sources = Arrays.<JavaFileObject> asList(new MarkerAnnotationObject(), | ||
| new ArchiveProbeObject()); | ||
|
|
||
| // the locale and charset are actually not important, because the only sources we're providing | ||
| // are not file-based. The rest of the stuff the compiler will be touching is already compiled | ||
| // and therefore not affected by the charset. | ||
| StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, Locale.getDefault(), | ||
| Charset.forName("UTF-8")); | ||
|
|
||
| final JavaCompiler.CompilationTask task = compiler.getTask(output, fileManager, null, options, | ||
| Collections.singletonList(ArchiveProbeObject.CLASS_NAME), sources); | ||
|
|
||
| ProbingAnnotationProcessor processor = new ProbingAnnotationProcessor(environment); | ||
|
|
||
| task.setProcessors(Collections.singletonList(processor)); | ||
|
|
||
| Future<Boolean> future = processor.submitWithCompilationAwareness(executor, task, () -> { | ||
| if (Timing.LOG.isDebugEnabled()) { | ||
| Timing.LOG.debug("About to crawl " + environment.getApi()); | ||
| } | ||
|
|
||
| try { | ||
| new ClasspathScanner(fileManager, environment, classPathFiles, additionClassPathFiles, | ||
| missingClassReporting, ignoreMissingAnnotations, filter).initTree(); | ||
| } catch (IOException e) { | ||
| throw new IllegalStateException("Failed to scan the classpath.", e); | ||
| } | ||
|
|
||
| if (Timing.LOG.isDebugEnabled()) { | ||
| Timing.LOG.debug("Crawl finished for " + environment.getApi()); | ||
| } | ||
| }); | ||
|
|
||
| return new CompilationValve(future, targetPath, environment, fileManager); | ||
| } | ||
|
|
||
| private String composeClassPath(File classPathDir) { | ||
| StringBuilder bld = new StringBuilder(); | ||
|
|
||
| File[] jars = classPathDir.listFiles(); | ||
|
|
||
| if (jars == null || jars.length == 0) { | ||
| return ""; | ||
| } | ||
|
|
||
| List<File> sortedJars = new ArrayList<>(Arrays.asList(jars)); | ||
| Collections.sort(sortedJars, new Comparator<File>() { | ||
| @Override | ||
| public int compare(File o1, File o2) { | ||
| return o1.getName().compareTo(o2.getName()); | ||
| } | ||
| }); | ||
|
|
||
| Iterator<File> it = sortedJars.iterator(); | ||
|
|
||
| bld.append(it.next().getAbsolutePath()); | ||
| while (it.hasNext()) { | ||
| bld.append(File.pathSeparator).append(it.next().getAbsolutePath()); | ||
| } | ||
|
|
||
| return bld.toString(); | ||
| } | ||
|
|
||
| private IdentityHashMap<Archive, File> copyArchives(Iterable<? extends Archive> archives, File parentDir, | ||
| int startIdx, int prefixLength) { | ||
| IdentityHashMap<Archive, File> ret = new IdentityHashMap<>(); | ||
| if (archives == null) { | ||
| return ret; | ||
| } | ||
|
|
||
| for (Archive a : archives) { | ||
| String name = formatName(startIdx++, prefixLength, a.getName()); | ||
| File f = new File(parentDir, name); | ||
|
|
||
| ret.put(a, f); | ||
|
|
||
| if (f.exists()) { | ||
| LOG.warn("File " + f.getAbsolutePath() + " with the data of archive '" + a.getName() | ||
| + "' already exists." + " Assume it already contains the bits we need."); | ||
| continue; | ||
| } | ||
|
|
||
| Path target = new File(parentDir, name).toPath(); | ||
|
|
||
| try (InputStream data = extracted(a)) { | ||
| Files.copy(data, target); | ||
| } catch (IOException e) { | ||
| throw new IllegalStateException( | ||
| "Failed to copy class path element: " + a.getName() + " to " + f.getAbsolutePath(), e); | ||
| } | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| private InputStream extracted(Archive archive) throws IOException { | ||
| for (JarExtractor t : jarExtractors) { | ||
| Optional<InputStream> extracted = t.extract(archive); | ||
| if (extracted.isPresent()) { | ||
| return extracted.get(); | ||
| } | ||
| } | ||
|
|
||
| return archive.openStream(); | ||
| } | ||
|
|
||
| private int size(Iterable<?> collection) { | ||
| if (collection == null) { | ||
| return 0; | ||
| } | ||
|
|
||
| int ret = 0; | ||
| Iterator<?> it = collection.iterator(); | ||
| while (it.hasNext()) { | ||
| ret++; | ||
| it.next(); | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| private String formatName(int idx, int prefixLength, String rootName) { | ||
| try { | ||
| return String.format("%0" + prefixLength + "d-%s.jar", idx, | ||
| UUID.nameUUIDFromBytes(rootName.getBytes("UTF-8"))); | ||
| } catch (UnsupportedEncodingException e) { | ||
| throw new IllegalStateException("UTF-8 not supported."); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of duplicating the whole class and changing just the way the compiler options are composed, I think it'd be better to introduce a sort of package-private base class that would implement all of the common logic (i.e. basically a copy of what Compiler class currently is) and just define a new package-private abstract method for getting the compiler options that each of the Compiler classes would implement according to the java compiler version requirements (i.e. the Compiler in the main sources would use the classpath the old way and the Compiler in 9 would use the module path as you changed it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree. We will refactor this in order to implement it in that way.