Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import javax.annotation.processing.Processor;
import javax.tools.JavaCompiler;
import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

Expand All @@ -58,8 +57,6 @@
public abstract class AbstractJctCompiler<A extends AbstractJctCompiler<A>>
implements JctCompiler<A, JctCompilationImpl> {

private final JavaCompiler jsr199Compiler;
private final JctFlagBuilder flagBuilder;
private final List<Processor> annotationProcessors;
private final List<String> annotationProcessorOptions;
private final List<String> compilerOptions;
Expand Down Expand Up @@ -87,19 +84,10 @@ public abstract class AbstractJctCompiler<A extends AbstractJctCompiler<A>>
/**
* Initialize this compiler.
*
* @param name the friendly name of the compiler to default to.
* @param jsr199Compiler the JSR-199 compiler implementation to use.
* @param flagBuilder the flag builder to use.
* @param defaultName the printable default name to use for the compiler.
*/
protected AbstractJctCompiler(
String name,
JavaCompiler jsr199Compiler,
JctFlagBuilder flagBuilder
) {
this.name = requireNonNull(name, "name");
this.jsr199Compiler = requireNonNull(jsr199Compiler, "jsr199Compiler");
this.flagBuilder = requireNonNull(flagBuilder, "flagBuilder");

protected AbstractJctCompiler(String defaultName) {
name = requireNonNull(defaultName, "name");
annotationProcessors = new ArrayList<>();
annotationProcessorOptions = new ArrayList<>();
compilerOptions = new ArrayList<>();
Expand All @@ -126,12 +114,18 @@ protected AbstractJctCompiler(

@Override
public JctCompilationImpl compile(Workspace workspace) {
return JctJsr199Interop.compile(workspace, myself(), jsr199Compiler, flagBuilder, null);
var flagBuilder = getJctFlagBuilderFactory().createFlagBuilder();
var compiler = getJsr199CompilerFactory().createCompiler();

return JctJsr199Interop.compile(workspace, myself(), compiler, flagBuilder, null);
}

@Override
public JctCompilationImpl compile(Workspace workspace, Collection<String> classNames) {
return JctJsr199Interop.compile(workspace, myself(), jsr199Compiler, flagBuilder, classNames);
var flagBuilder = getJctFlagBuilderFactory().createFlagBuilder();
var compiler = getJsr199CompilerFactory().createCompiler();

return JctJsr199Interop.compile(workspace, myself(), compiler, flagBuilder, classNames);
}

@Override
Expand All @@ -152,24 +146,6 @@ public A name(String name) {
return myself();
}

/**
* Get the flag builder to use.
*
* @return the flag builder.
*/
public JctFlagBuilder getFlagBuilder() {
return flagBuilder;
}

/**
* Get the JSR-199 compiler to use.
*
* @return the JSR-199 compiler.
*/
public JavaCompiler getJsr199Compiler() {
return jsr199Compiler;
}

@Override
public boolean isVerbose() {
return verbose;
Expand Down Expand Up @@ -451,6 +427,28 @@ public final String toString() {
return name;
}

/**
* Get the flag builder factory to use for building flags.
*
* @return the factory.
*/
public abstract JctFlagBuilderFactory getJctFlagBuilderFactory();

/**
* Get the JSR-199 compiler factory to use for initialising an internal compiler.
*
* @return the factory.
*/
public abstract Jsr199CompilerFactory getJsr199CompilerFactory();

/**
* {@inheritDoc}
*
* @return the default release version to use when no version is specified by the user.
*/
@Override
public abstract String getDefaultRelease();

/**
* Get this implementation of {@link AbstractJctCompiler}, cast to the type parameter {@link A}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2022 - 2023, the original author or authors.
*
* 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 io.github.ascopes.jct.compilers;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

/**
* Factory that creates a flag builder.
*
* @author Ashley Scopes
* @since 0.0.1 (0.0.1-M7)
*/
@API(since = "0.0.1", status = Status.STABLE)
@FunctionalInterface
public interface JctFlagBuilderFactory {

/**
* Create a new flag builder.
*
* @return the flag builder.
*/
JctFlagBuilder createFlagBuilder();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2022 - 2023, the original author or authors.
*
* 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 io.github.ascopes.jct.compilers;

import javax.tools.JavaCompiler;
import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

/**
* Factory that creates an instance of a JSR-199 compiler.
*
* @author Ashley Scopes
* @since 0.0.1 (0.0.1-M7)
*/
@API(since = "0.0.1", status = Status.STABLE)
@FunctionalInterface
public interface Jsr199CompilerFactory {

/**
* Create a new instance of a JSR-199 {@link JavaCompiler}.
*
* @return the JSR-199 Java compiler.
*/
JavaCompiler createCompiler();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ public final class JctCompilationImpl implements JctCompilation {
private final List<TraceDiagnostic<JavaFileObject>> diagnostics;
private final JctFileManager fileManager;

@SuppressWarnings("ConstantConditions")
private JctCompilationImpl(Builder builder) {
success = requireNonNull(builder.success, "success");
failOnWarnings = requireNonNull(builder.failOnWarnings, "failOnWarnings");
success = requireNonNull(
builder.success, "success"
);
failOnWarnings = requireNonNull(
builder.failOnWarnings, "failOnWarnings"
);
outputLines = unmodifiableList(
requireNonNullValues(builder.outputLines, "outputLines")
);
Expand All @@ -64,7 +69,9 @@ private JctCompilationImpl(Builder builder) {
diagnostics = unmodifiableList(
requireNonNullValues(builder.diagnostics, "diagnostics")
);
fileManager = requireNonNull(builder.fileManager, "fileManager");
fileManager = requireNonNull(
builder.fileManager, "fileManager"
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package io.github.ascopes.jct.compilers.javac;

import static java.util.Objects.requireNonNull;

import io.github.ascopes.jct.compilers.AbstractJctCompiler;
import io.github.ascopes.jct.compilers.JctFlagBuilderFactory;
import io.github.ascopes.jct.compilers.Jsr199CompilerFactory;
import javax.annotation.concurrent.NotThreadSafe;
import javax.lang.model.SourceVersion;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;
Expand All @@ -39,40 +42,24 @@ public final class JavacJctCompilerImpl extends AbstractJctCompiler<JavacJctComp
* Initialize a new Java compiler.
*/
public JavacJctCompilerImpl() {
this(ToolProvider.getSystemJavaCompiler());
super(NAME);
}

/**
* Initialize a new Java compiler.
*
* @param jsr199Compiler the JSR-199 compiler backend to use.
*/
public JavacJctCompilerImpl(JavaCompiler jsr199Compiler) {
this(NAME, jsr199Compiler);
}

/**
* Initialize a new Java compiler.
*
* @param name the name to give the compiler.
*/
public JavacJctCompilerImpl(String name) {
this(name, ToolProvider.getSystemJavaCompiler());
@Override
public String getDefaultRelease() {
return Integer.toString(getLatestSupportedVersionInt(false));
}

/**
* Initialize a new Java compiler.
*
* @param name the name to give the compiler.
* @param jsr199Compiler the JSR-199 compiler backend to use.
*/
public JavacJctCompilerImpl(String name, JavaCompiler jsr199Compiler) {
super(name, jsr199Compiler, new JavacJctFlagBuilderImpl());
@Override
public JctFlagBuilderFactory getJctFlagBuilderFactory() {
return JavacJctFlagBuilderImpl::new;
}

@Override
public String getDefaultRelease() {
return Integer.toString(getLatestSupportedVersionInt(false));
public Jsr199CompilerFactory getJsr199CompilerFactory() {
// RequireNonNull to ensure the return result is non-null, since the ToolProvider
// method is not annotated.
return () -> requireNonNull(ToolProvider.getSystemJavaCompiler());
}

/**
Expand Down
Loading