diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/OutputContainerGroup.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/OutputContainerGroup.java index 5ced1461a..08faa4392 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/OutputContainerGroup.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/OutputContainerGroup.java @@ -15,6 +15,8 @@ */ package io.github.ascopes.jct.containers; +import io.github.ascopes.jct.workspaces.PathRoot; +import java.util.List; import javax.tools.JavaFileManager.Location; import org.apiguardian.api.API; import org.apiguardian.api.API.Status; @@ -29,12 +31,41 @@ * {@link #getOrCreateModule(String) create} the module, and then operate on that sub-container * group. Operations on non-module packages should operate on this container group directly. * + *

Note that each container group will usually only support one package container group + * in the outputs. This is due to the JSR-199 API not providing a method for specifying which + * output location to write files to for legacy-style packages. + * * @author Ashley Scopes * @since 0.0.1 */ @API(since = "0.0.1", status = Status.STABLE) public interface OutputContainerGroup extends PackageContainerGroup, ModuleContainerGroup { + /** + * {@inheritDoc} + * + *

Note that this implementation will only ever allow a single container in the package + * container groups. If a container is already present, then an exception will be thrown. + * + * @param path the path to add. + * @throws IllegalStateException if a package already exists in this location. + */ + @Override + void addPackage(PathRoot path); + + + /** + * {@inheritDoc} + * + *

Note that this implementation will only ever allow a single container in the package + * container groups. If a container is already present, then an exception will be thrown. + * + * @param container the container to add. + * @throws IllegalStateException if a package already exists in this location. + */ + @Override + void addPackage(Container container); + /** * Get the output-oriented location. * @@ -42,4 +73,16 @@ public interface OutputContainerGroup extends PackageContainerGroup, ModuleConta */ @Override Location getLocation(); + + /** + * {@inheritDoc} + * + *

Note that this implementation will only ever return one container in the list due to + * JSR-199 limitations. + * + * @return the list containing the package container, if it exists. Otherwise, an empty list is + * returned instead. + */ + @Override + List getPackages(); } diff --git a/java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/OutputContainerGroupImpl.java b/java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/OutputContainerGroupImpl.java index 3724089e1..33479ea8b 100644 --- a/java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/OutputContainerGroupImpl.java +++ b/java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/OutputContainerGroupImpl.java @@ -81,6 +81,24 @@ public OutputContainerGroupImpl(Location location, String release) { } } + @Override + public void addPackage(PathRoot path) { + if (super.isEmpty()) { + super.addPackage(path); + } else { + throw packageAlreadySpecified(path); + } + } + + @Override + public void addPackage(Container container) { + if (super.isEmpty()) { + super.addPackage(container); + } else { + throw packageAlreadySpecified(container.getPathRoot()); + } + } + @Override public void addModule(String module, Container container) { getOrCreateModule(module).addPackage(container); @@ -173,4 +191,13 @@ private PackageContainerGroup newPackageGroup(ModuleLocation moduleLocation) { group.addPackage(pathWrapper); return group; } + + @SuppressWarnings("resource") + private IllegalStateException packageAlreadySpecified(PathRoot newPathRoot) { + var existingPathRoot = getPackages().iterator().next().getPathRoot(); + return new IllegalStateException( + "Cannot add a new package (" + newPathRoot + ") to this output container group because " + + "a package has already been specified (" + existingPathRoot + ")" + ); + } }