Sitelet https://github.com/revapi/revapi/pull/301/files
Skip to content
Open
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
8 changes: 7 additions & 1 deletion coverage/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2014-2023 Lukas Krejci
Copyright 2014-2025 Lukas Krejci
and other contributors as indicated by the @author tags.

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -113,6 +113,12 @@
<goal>report-aggregate</goal>
</goals>
<phase>verify</phase>
<configuration>
<!-- Exclude alternate versions for multi-release modules due to https://github.com/jacoco/jacoco/issues/407 -->
<excludes>
<exclude>META-INF/**/*</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
6 changes: 5 additions & 1 deletion revapi-build/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2014-2023 Lukas Krejci
Copyright 2014-2025 Lukas Krejci
and other contributors as indicated by the @author tags.

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -172,6 +172,10 @@
</goals>
<phase>test</phase>
<configuration>
<!-- Exclude alternate versions for multi-release modules due to https://github.com/jacoco/jacoco/issues/407 -->
<excludes>
<exclude>META-INF/**/*</exclude>
</excludes>
<dataFile>${project.build.directory}/jacoco-ut.exec</dataFile>
</configuration>
</execution>
Expand Down
3 changes: 1 addition & 2 deletions revapi-java-spi/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2014-2022 Lukas Krejci
Copyright 2014-2025 Lukas Krejci
and other contributors as indicated by the @author tags.

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -44,7 +44,6 @@
</distributionManagement>
<properties>
<automatic.module.name>org.revapi.java.spi</automatic.module.name>
<jacoco.skip>true</jacoco.skip>
</properties>
<dependencies>
<dependency>
Expand Down
16 changes: 16 additions & 0 deletions revapi-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
/*

Copy link
Copy Markdown
Member

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).

Copy link
Copy Markdown
Author

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.

* 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.");
}
}
}
Loading