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
2 changes: 1 addition & 1 deletion .mvn/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<module name="OneStatementPerLine"/>
<module name="MultipleVariableDeclarations"/>
<module name="ArrayTypeStyle"/>
<module name="FallThrough"/>
<!--<module name="FallThrough"/>-->
<module name="UpperEll"/>
<module name="ModifierOrder"/>
<module name="EmptyLineSeparator">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,22 @@
import static io.github.ascopes.jct.utils.IterableUtils.requireNonNullValues;
import static java.util.Objects.requireNonNull;

import io.github.ascopes.jct.compilers.impl.JctCompilationFactoryImpl;
import io.github.ascopes.jct.compilers.impl.JctCompilationImpl;
import io.github.ascopes.jct.compilers.impl.JctJsr199Interop;
import io.github.ascopes.jct.diagnostics.TracingDiagnosticListener;
import io.github.ascopes.jct.ex.JctCompilerException;
import io.github.ascopes.jct.filemanagers.AnnotationProcessorDiscovery;
import io.github.ascopes.jct.filemanagers.JctFileManagerFactory;
import io.github.ascopes.jct.filemanagers.LoggingMode;
import io.github.ascopes.jct.workspaces.Workspace;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;
import javax.annotation.WillNotClose;
import javax.annotation.concurrent.NotThreadSafe;
import javax.annotation.processing.Processor;
import org.apiguardian.api.API;
Expand All @@ -55,7 +60,7 @@
@API(since = "0.0.1", status = Status.STABLE)
@NotThreadSafe
public abstract class AbstractJctCompiler<A extends AbstractJctCompiler<A>>
implements JctCompiler<A, JctCompilationImpl> {
implements JctCompiler<A, JctCompilation> {

private final List<Processor> annotationProcessors;
private final List<String> annotationProcessorOptions;
Expand Down Expand Up @@ -113,19 +118,13 @@ protected AbstractJctCompiler(String defaultName) {
}

@Override
public JctCompilationImpl compile(Workspace workspace) {
var flagBuilder = getJctFlagBuilderFactory().createFlagBuilder();
var compiler = getJsr199CompilerFactory().createCompiler();

return JctJsr199Interop.compile(workspace, myself(), compiler, flagBuilder, null);
public JctCompilation compile(Workspace workspace) {
return compileInternal(workspace, null);
}

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

return JctJsr199Interop.compile(workspace, myself(), compiler, flagBuilder, classNames);
public JctCompilation compile(Workspace workspace, Collection<String> classNames) {
return compileInternal(workspace, classNames);
}

@Override
Expand Down Expand Up @@ -249,6 +248,19 @@ public A addCompilerOptions(Iterable<String> compilerOptions) {
return myself();
}

@Override
public String getEffectiveRelease() {
if (release != null) {
return release;
}

if (target != null) {
return target;
}

return getDefaultRelease();
}

@Nullable
@Override
public String getRelease() {
Expand Down Expand Up @@ -432,14 +444,37 @@ public final String toString() {
*
* @return the factory.
*/
public abstract JctFlagBuilderFactory getJctFlagBuilderFactory();
public abstract JctFlagBuilderFactory getFlagBuilderFactory();

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

/**
* Get the file manager factory to use for building a file manager during compilation.
*
* @return the factory.
*/
public abstract JctFileManagerFactory getFileManagerFactory();

/**
* Get the compilation factory to use for building a compilation.
*
* <p>By default, this uses a common internal implementation that is designed to work with
* compilers that have interfaces the same as, and behave the same as Javac.
*
* <p>Some obscure compiler implementations with potentially satanic rituals for initialising
* and configuring components correctly may need to provide a custom implementation here instead.
* In this case, this method should be overridden.
*
* @return the compilation factory.
*/
public JctCompilationFactory getCompilationFactory() {
return new JctCompilationFactoryImpl(this);
}

/**
* {@inheritDoc}
Expand All @@ -460,4 +495,47 @@ protected final A myself() {

return me;
}

/**
* Build the list of flags from this compiler object using the flag builder.
*
* <p>Implementations should not need to override this unless there is a special edge case
* that needs configuring differently. This is exposed to assist in these kinds of cases.
*
* @param flagBuilder the flag builder to apply the flag configuration to.
* @return the string flags to use.
*/
protected List<String> buildFlags(JctFlagBuilder flagBuilder) {
return flagBuilder
.annotationProcessorOptions(getAnnotationProcessorOptions())
.showDeprecationWarnings(isShowDeprecationWarnings())
.failOnWarnings(isFailOnWarnings())
.compilerOptions(getCompilerOptions())
.previewFeatures(isPreviewFeatures())
.release(getRelease())
.source(getSource())
.target(getTarget())
.verbose(isVerbose())
.showWarnings(isShowWarnings())
.build();
}

@SuppressWarnings("NullableProblems") // https://youtrack.jetbrains.com/issue/IDEA-311124
private JctCompilation compileInternal(
@WillNotClose Workspace workspace,
@Nullable Collection<String> classNames
) {
var fileManagerFactory = getFileManagerFactory();
var flagBuilderFactory = getFlagBuilderFactory();
var compilerFactory = getCompilerFactory();
var compilationFactory = getCompilationFactory();

try (var fileManager = fileManagerFactory.createFileManager(workspace)) {
var flags = buildFlags(flagBuilderFactory.createFlagBuilder());
var compiler = compilerFactory.createCompiler();
return compilationFactory.createCompilation(flags, fileManager, compiler, classNames);
} catch (IOException ex) {
throw new JctCompilerException("Failed to close file manager", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 io.github.ascopes.jct.ex.JctCompilerException;
import io.github.ascopes.jct.filemanagers.JctFileManager;
import java.util.Collection;
import java.util.List;
import javax.annotation.Nullable;
import javax.tools.JavaCompiler;
import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

/**
* Factory for producing {@link JctCompilation} objects by performing a physical compilation with a
* compiler.
*
* @author Ashley Scopes
* @since 0.0.1 (0.0.1-M7)
*/
@API(since = "0.0.1", status = Status.STABLE)
public interface JctCompilationFactory {

/**
* Create a compilation.
*
* @param flags the flags to pass to the compiler.
* @param fileManager the file manager to use for file management.
* @param jsr199Compiler the compiler backend to use.
* @param classNames the binary names of the classes to compile. If this is null, then classes
* should be discovered automatically.
* @return the compilation result that contains whether the compiler succeeded or failed, amongst
* other information.
* @throws JctCompilerException if compiler raises an unhandled exception and cannot complete.
*/
JctCompilation createCompilation(
List<String> flags,
JctFileManager fileManager,
JavaCompiler jsr199Compiler,
@Nullable Collection<String> classNames
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,17 @@ default C addCompilerOptions(String compilerOption, String... compilerOptions) {
*/
String getDefaultRelease();

/**
* Get the effective release to use for the actual compilation.
*
* <p>This may be determined from the {@link #getSource() source},
* {@link #getTarget() target}, {@link #getRelease() release}, and
* {@link #getDefaultRelease() default release.}
*
* @return the effective release.
*/
String getEffectiveRelease();

/**
* Get the current release version that is set, or {@code null} if left to the compiler to decide.
* default.
Expand Down
Loading