Compare commits

...

3 Commits

Author SHA1 Message Date
Stephan Schroevers
689eac7c8d Ugh, classpath issues 2022-06-06 09:42:53 +02:00
Stephan Schroevers
7c3abaec21 Sharing: yes we can 2022-06-06 09:42:53 +02:00
Stephan Schroevers
d260112bed WIP: Crazy idea; approach to be validated 2022-06-06 09:42:53 +02:00
7 changed files with 296 additions and 3 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

@@ -0,0 +1,16 @@
package tech.picnic.errorprone.bugpatterns;
import com.google.common.hash.Hashing;
import com.google.common.reflect.ClassPath;
import java.io.IOException;
final class CodeTransformers {
// XXX: Use.
private static void foo(ClassPath.ResourceInfo resource) {
try {
resource.asByteSource().hash(Hashing.murmur3_32_fixed(0)).toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -31,6 +31,7 @@ import com.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher;
import com.google.errorprone.fixes.Replacement;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import java.io.IOException;
@@ -43,9 +44,12 @@ 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 javax.naming.Context;
import tech.picnic.errorprone.plexus.compiler.javac.caching.CachingJavacCompiler;
/**
* A {@link BugChecker} which flags code which can be simplified using Refaster templates located on
@@ -71,7 +75,8 @@ public final class RefasterCheck extends BugChecker implements CompilationUnitTr
static final Supplier<ImmutableListMultimap<String, CodeTransformer>> ALL_CODE_TRANSFORMERS =
Suppliers.memoize(RefasterCheck::loadAllCodeTransformers);
private final CodeTransformer codeTransformer;
private final ErrorProneFlags errorProneFlags;
private final Supplier<CodeTransformer> codeTransformer;
/** Instantiates the default {@link RefasterCheck}. */
public RefasterCheck() {
@@ -84,15 +89,37 @@ public final class RefasterCheck extends BugChecker implements CompilationUnitTr
* @param flags Any provided command line flags.
*/
public RefasterCheck(ErrorProneFlags flags) {
codeTransformer = createCompositeCodeTransformer(flags);
errorProneFlags = flags;
codeTransformer = Suppliers.memoize(() -> createCompositeCodeTransformer(flags));
}
// XXX: Need to somehow convert `Description` to handle classloading issue.
// private BiFunction<TreePath, Context, List<Description>>
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
// XXX Use the `codeTransformer` field only if `cache` is empty. In that case, populate the
// cache.
// XXX: Current code is wrong: doesn't respect flags.
System.out.println("XXXX :" + CachingJavacCompiler.class.toString());
System.out.println("XXXX Path :" + TreePath.class.hashCode());
System.out.println("XXXX Context:" + Context.class.hashCode());
ConcurrentMap<String, Object> cache = state.context.get(ConcurrentMap.class);
System.err.printf("XXX %s%n", cache);
CodeTransformer transformer;
if (cache != null) {
cache.compute("x", (a, b) -> b instanceof Integer ? ((Integer) b) + 1 : 1);
transformer = (CodeTransformer) cache.computeIfAbsent("c", k -> codeTransformer.get());
} else {
// We're not using the hacked compiler
System.err.printf("XXX NOP!%n");
transformer = codeTransformer.get();
}
/* First, collect all matches. */
List<Description> matches = new ArrayList<>();
try {
codeTransformer.apply(state.getPath(), new SubContext(state.context), matches::add);
transformer.apply(state.getPath(), new SubContext(state.context), matches::add);
} catch (LinkageError e) {
// XXX: This `try/catch` block handles the issue described and resolved in
// https://github.com/google/error-prone/pull/2456. Drop this block once that change is

View File

@@ -0,0 +1,67 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>tech.picnic.error-prone-support</groupId>
<artifactId>error-prone-support</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>plexus-compiler-javac-caching</artifactId>
<name>Picnic :: Error Prone Support :: Caching Plexus Compiler</name>
<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>
<!-- XXX: <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-javac</artifactId>
<!-- XXX: <scope>provided</scope> -->
</dependency>
<!--<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
</dependency>-->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</dependency>
<!--<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>-->
</dependencies>
<!--<build>
<plugins>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
</plugin>
</plugins>
</build>-->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>javac</compilerId>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@@ -0,0 +1,94 @@
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.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;
import org.codehaus.plexus.compiler.javac.JavaxToolsCompiler;
/** A {@link JavaxToolsCompiler} implementation which... */
// XXX: Extend documentation. Make it reality.
public final class CachingJavacCompiler extends JavaxToolsCompiler {
@Override
protected JavaCompiler newJavaCompiler() {
// XXX: Tweak!
// 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?
@Override
public CompilerResult compileInProcess(
String[] args, CompilerConfiguration config, String[] sourceFiles) throws CompilerException {
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.
// XXX: This hack should be hidden in a utility class, exposed by a separate Maven module (to
// be used both here and within RefasterCheck).
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

@@ -0,0 +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>
</component-set>

58
pom.xml
View File

@@ -40,6 +40,8 @@
<modules>
<module>error-prone-contrib</module>
<!-- XXX: Drop? -->
<module>plexus-compiler-javac-caching</module>
<module>refaster-compiler</module>
<module>refaster-support</module>
</modules>
@@ -183,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>
@@ -348,6 +355,33 @@
<artifactId>checker-qual</artifactId>
<version>3.22.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-api</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-javac</artifactId>
<!-- XXX: Dedup. -->
<version>2.12.0</version>
</dependency>
<!--<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>2.1.1</version>
</dependency>-->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<!-- XXX: dedup? -->
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
@@ -749,7 +783,16 @@
<!-- Erroneously inverted logic... for details, see
https://issues.apache.org/jira/browse/MCOMPILER-209. -->
<useIncrementalCompilation>false</useIncrementalCompilation>
<!--<compilerId>javac-with-caching</compilerId>-->
<compilerId>javac-with-caching</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>plexus-compiler-javac-caching</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -1087,6 +1130,21 @@
<artifactId>versions-maven-plugin</artifactId>
<version>2.11.0</version>
</plugin>
<!-- XXX: Drop if unused! -->
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<!-- XXX: Dedup -->
<version>2.1.1</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
<goal>merge-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.gaul</groupId>
<artifactId>modernizer-maven-plugin</artifactId>