Skip to content

Commit 27eaf51

Browse files
committed
GH-414: Make output container groups only allow a single package
1 parent 20901ad commit 27eaf51

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/OutputContainerGroup.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package io.github.ascopes.jct.containers;
1717

18+
import io.github.ascopes.jct.workspaces.PathRoot;
19+
import java.util.List;
1820
import javax.tools.JavaFileManager.Location;
1921
import org.apiguardian.api.API;
2022
import org.apiguardian.api.API.Status;
@@ -29,17 +31,58 @@
2931
* {@link #getOrCreateModule(String) create} the module, and then operate on that sub-container
3032
* group. Operations on non-module packages should operate on this container group directly.
3133
*
34+
* <p>Note that each container group will usually only support one package container group
35+
* in the outputs. This is due to the JSR-199 API not providing a method for specifying which
36+
* output location to write files to for legacy-style packages.
37+
*
3238
* @author Ashley Scopes
3339
* @since 0.0.1
3440
*/
3541
@API(since = "0.0.1", status = Status.STABLE)
3642
public interface OutputContainerGroup extends PackageContainerGroup, ModuleContainerGroup {
3743

44+
/**
45+
* {@inheritDoc}
46+
*
47+
* <p>Note that this implementation will only ever allow a single container in the package
48+
* container groups. If a container is already present, then an exception will be thrown.
49+
*
50+
* @param path the path to add.
51+
* @throws IllegalStateException if a package already exists in this location.
52+
*/
53+
@Override
54+
void addPackage(PathRoot path);
55+
56+
57+
/**
58+
* {@inheritDoc}
59+
*
60+
* <p>Note that this implementation will only ever allow a single container in the package
61+
* container groups. If a container is already present, then an exception will be thrown.
62+
*
63+
* @param container the container to add.
64+
* @throws IllegalStateException if a package already exists in this location.
65+
*/
66+
@Override
67+
void addPackage(Container container);
68+
3869
/**
3970
* Get the output-oriented location.
4071
*
4172
* @return the output-oriented location.
4273
*/
4374
@Override
4475
Location getLocation();
76+
77+
/**
78+
* {@inheritDoc}
79+
*
80+
* <p>Note that this implementation will only ever return one container in the list due to
81+
* JSR-199 limitations.
82+
*
83+
* @return the list containing the package container, if it exists. Otherwise, an empty list is
84+
* returned instead.
85+
*/
86+
@Override
87+
List<Container> getPackages();
4588
}

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/OutputContainerGroupImpl.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ public OutputContainerGroupImpl(Location location, String release) {
8181
}
8282
}
8383

84+
@Override
85+
public void addPackage(PathRoot path) {
86+
if (super.isEmpty()) {
87+
super.addPackage(path);
88+
} else {
89+
throw packageAlreadySpecified(path);
90+
}
91+
}
92+
93+
@Override
94+
public void addPackage(Container container) {
95+
if (super.isEmpty()) {
96+
super.addPackage(container);
97+
} else {
98+
throw packageAlreadySpecified(container.getPathRoot());
99+
}
100+
}
101+
84102
@Override
85103
public void addModule(String module, Container container) {
86104
getOrCreateModule(module).addPackage(container);
@@ -173,4 +191,13 @@ private PackageContainerGroup newPackageGroup(ModuleLocation moduleLocation) {
173191
group.addPackage(pathWrapper);
174192
return group;
175193
}
194+
195+
@SuppressWarnings("resource")
196+
private IllegalStateException packageAlreadySpecified(PathRoot newPathRoot) {
197+
var existingPathRoot = getPackages().iterator().next().getPathRoot();
198+
return new IllegalStateException(
199+
"Cannot add a new package (" + newPathRoot + ") to this output container group because " +
200+
"a package has already been specified (" + existingPathRoot + ")"
201+
);
202+
}
176203
}

0 commit comments

Comments
 (0)