Sharing: yes we can

This commit is contained in:
Stephan Schroevers
2022-05-23 22:51:25 +02:00
parent d260112bed
commit 7c3abaec21
6 changed files with 119 additions and 26 deletions

View File

@@ -39,6 +39,11 @@
<artifactId>error_prone_test_helpers</artifactId>
<scope>test</scope>
</dependency>
<!-- XXX: dubious. -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>plexus-compiler-javac-caching</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>refaster-compiler</artifactId>

View File

@@ -43,9 +43,11 @@ import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import tech.picnic.errorprone.plexus.compiler.javac.caching.CachingJavacCompiler;
/**
* A {@link BugChecker} which flags code which can be simplified using Refaster templates located on
@@ -89,6 +91,16 @@ public final class RefasterCheck extends BugChecker implements CompilationUnitTr
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
System.out.println("XXXX :" + CachingJavacCompiler.class.toString());
ConcurrentMap<String, Integer> cache = state.context.get(ConcurrentMap.class);
System.err.printf("XXX %s%n", cache);
if (cache != null) {
cache.compute("x", (a, b) -> b == null ? 1 : b + 1);
} else {
// We're not using the hacked compiler
System.err.printf("XXX NOP!%n");
}
/* First, collect all matches. */
List<Description> matches = new ArrayList<>();
try {

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@@ -14,6 +14,11 @@
<description>Caching Javac compiler Plexus Compiler component.</description>
<dependencies>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>javac</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-api</artifactId>

View File

@@ -1,7 +1,21 @@
package tech.picnic.errorprone.plexus.compiler.javac.caching;
import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.util.Context;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.lang.model.SourceVersion;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import org.codehaus.plexus.compiler.CompilerConfiguration;
import org.codehaus.plexus.compiler.CompilerException;
import org.codehaus.plexus.compiler.CompilerResult;
@@ -13,7 +27,10 @@ public final class CachingJavacCompiler extends JavaxToolsCompiler {
@Override
protected JavaCompiler newJavaCompiler() {
// XXX: Tweak!
return ToolProvider.getSystemJavaCompiler();
// XXX: Could we instead simply provide another `JavaCompiler` to be service-loaded? Would
// certainly be simpler.
// return ToolProvider.getSystemJavaCompiler();
return new XxxJavaCompiler(JavacTool.create());
}
// XXX: Drop?
@@ -23,4 +40,53 @@ public final class CachingJavacCompiler extends JavaxToolsCompiler {
getLogger().error("XXXX I'm invoked!");
return super.compileInProcess(args, config, sourceFiles);
}
// XXX: Name!
private static final class XxxJavaCompiler implements JavaCompiler {
private final ConcurrentHashMap<?, ?> cache = new ConcurrentHashMap<>();
private final JavacTool delegate;
private XxxJavaCompiler(JavacTool delegate) {
this.delegate = delegate;
}
@Override
public CompilationTask getTask(
Writer out,
JavaFileManager fileManager,
DiagnosticListener<? super JavaFileObject> diagnosticListener,
Iterable<String> options,
Iterable<String> classes,
Iterable<? extends JavaFileObject> compilationUnits) {
System.out.println("XXXX I'm invoked!");
Context context = new Context();
// XXX: Explain.
context.put(ConcurrentMap.class, cache);
return delegate.getTask(
out, fileManager, diagnosticListener, options, classes, compilationUnits, context);
}
@Override
public StandardJavaFileManager getStandardFileManager(
DiagnosticListener<? super JavaFileObject> diagnosticListener,
Locale locale,
Charset charset) {
return delegate.getStandardFileManager(diagnosticListener, locale, charset);
}
@Override
public int isSupportedOption(String option) {
return delegate.isSupportedOption(option);
}
@Override
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
return delegate.run(in, out, err, arguments);
}
@Override
public Set<SourceVersion> getSourceVersions() {
return delegate.getSourceVersions();
}
}
}

View File

@@ -1,26 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<component-set>
<components>
<component>
<role>org.codehaus.plexus.compiler.Compiler</role>
<role-hint>javac-with-caching</role-hint>
<implementation>org.codehaus.plexus.compiler.javac.JavacCompiler</implementation>
<description />
<isolated-realm>false</isolated-realm>
<requirements>
<requirement>
<role>org.codehaus.plexus.compiler.javac.InProcessCompiler</role>
<role-hint>javac-with-caching</role-hint>
<field-name>inProcessCompiler</field-name>
</requirement>
</requirements>
</component>
<component>
<role>org.codehaus.plexus.compiler.javac.InProcessCompiler</role>
<role-hint>javac-with-caching</role-hint>
<implementation>tech.picnic.errorprone.plexus.compiler.javac.caching.CachingJavacCompiler</implementation>
<description />
<isolated-realm>false</isolated-realm>
</component>
</components>
<components>
<component>
<role>org.codehaus.plexus.compiler.Compiler</role>
<role-hint>javac-with-caching</role-hint>
<implementation>org.codehaus.plexus.compiler.javac.JavacCompiler</implementation>
<description/>
<isolated-realm>false</isolated-realm>
<requirements>
<requirement>
<role>org.codehaus.plexus.compiler.javac.InProcessCompiler</role>
<role-hint>javac-with-caching</role-hint>
<field-name>inProcessCompiler</field-name>
</requirement>
</requirements>
</component>
<component>
<role>org.codehaus.plexus.compiler.javac.InProcessCompiler</role>
<role-hint>javac-with-caching</role-hint>
<implementation>tech.picnic.errorprone.plexus.compiler.javac.caching.CachingJavacCompiler</implementation>
<description/>
<isolated-realm>false</isolated-realm>
</component>
</components>
</component-set>

View File

@@ -185,6 +185,11 @@
<artifactId>error_prone_test_helpers</artifactId>
<version>${version.error-prone}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>plexus-compiler-javac-caching</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>refaster-compiler</artifactId>