Skip to content

Commit

Permalink
Write SPIs to META-INF/generated-services so that they can be merged …
Browse files Browse the repository at this point in the history
…later via avaje-spi-service (#616)

* write spis to a different folder

* Update InjectProcessorTest.java

* Update InjectProcessorTest.java

* remove hard spi dependency

* Update ProcessingContext.java

* Remove ProcessingContext.testInit() and adjust test

---------

Co-authored-by: Rob Bygrave <robin.bygrave@gmail.com>
  • Loading branch information
SentryMan and rbygrave authored Jun 23, 2024
1 parent c10e2a9 commit 53e0fce
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion inject-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-spi-service</artifactId>
<version>1.9</version>
<version>1.10</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ static final class Ctx {
private final List<ModuleData> modules = new ArrayList<>();
private final List<TypeElement> delayQueue = new ArrayList<>();
private final List<String> spiServices = new ArrayList<>();
private final boolean spiPresent = APContext.typeElement("io.avaje.spi.internal.ServiceProcessor") != null;
private boolean strictWiring;

Ctx() {}

void registerProvidedTypes(Set<String> moduleFileProvided) {
ExternalProvider.registerModuleProvidedTypes(providedTypes);
providedTypes.addAll(moduleFileProvided);
Expand All @@ -44,10 +43,6 @@ static void init(Set<String> moduleFileProvided, boolean performModuleValidation
CTX.get().registerProvidedTypes(moduleFileProvided);
}

static void testInit() {
CTX.set(new Ctx());
}

static String loadMetaInfServices() {
return loadMetaInf(Constants.META_INF_SPI).stream()
.filter(ProcessingContext::isInjectModule)
Expand Down Expand Up @@ -96,7 +91,12 @@ static void addInjectSPI(String type) {
}

static FileObject createMetaInfWriterFor(String interfaceType) throws IOException {
return filer().createResource(StandardLocation.CLASS_OUTPUT, "", interfaceType);
var serviceFile =
CTX.get().spiPresent
? interfaceType.replace("META-INF/services/", "META-INF/generated-services/")
: interfaceType;

return filer().createResource(StandardLocation.CLASS_OUTPUT, "", serviceFile);
}

static TypeElement elementMaybe(String rawType) {
Expand Down Expand Up @@ -198,7 +198,6 @@ static void writeSPIServicesFile() {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
logError("Failed to write services file " + e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
Expand All @@ -26,6 +25,8 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import io.avaje.spi.internal.ServiceProcessor;

class InjectProcessorTest {

@AfterEach
Expand All @@ -52,7 +53,7 @@ void testGeneration() throws Exception {
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final StandardJavaFileManager manager = compiler.getStandardFileManager(null, null, null);

manager.setLocation(StandardLocation.SOURCE_PATH, Arrays.asList(new File(source)));
manager.setLocation(StandardLocation.SOURCE_PATH, List.of(new File(source)));

final Set<Kind> fileKinds = Collections.singleton(Kind.SOURCE);

Expand All @@ -67,7 +68,7 @@ void testGeneration() throws Exception {
List.of("--release=" + Integer.getInteger("java.specification.version")),
null,
files);
task.setProcessors(Arrays.asList(new InjectProcessor()));
task.setProcessors(List.of(new InjectProcessor(), new ServiceProcessor()));

assertThat(task.call()).isTrue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ void nestedShortName() {

@Test
void nestedPackageOf() {
ProcessingContext.testInit();
assertEquals(Util.nestedPackageOf("com.example.Foo.Bar"), "com.example");
assertEquals(Util.nestedPackageOf("com.example.other.foo.Bar"), "com.example.other");
}
Expand Down

0 comments on commit 53e0fce

Please sign in to comment.