Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start #1

Merged
merged 8 commits into from
Aug 31, 2023
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
36 changes: 36 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build

on: [push, pull_request, workflow_dispatch]

jobs:
build:

runs-on: ${{ matrix.os }}
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix:
java_version: [17]
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v2
with:
java-version: ${{ matrix.java_version }}
distribution: 'zulu'
- name: Maven cache
uses: actions/cache@v2
env:
cache-name: maven-cache
with:
path:
~/.m2
key: build-${{ env.cache-name }}
- name: Maven version
run: mvn --version
- name: Build with Maven
run: mvn clean test
39 changes: 39 additions & 0 deletions .github/workflows/jdk-ea.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

name: JDK EA

on:
workflow_dispatch:
schedule:
- cron: '39 6 * * 1,3,5'

jobs:
build:

runs-on: ${{ matrix.os }}
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix:
java_version: [GA,EA] ## valhalla,metropolis don't support java 17
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v2
- name: Set up Java
uses: oracle-actions/setup-java@v1
with:
website: jdk.java.net
release: ${{ matrix.java_version }}
- name: Maven cache
uses: actions/cache@v2
env:
cache-name: maven-cache
with:
path:
~/.m2
key: build-${{ env.cache-name }}
- name: Build with Maven
run: mvn package

13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target/
build/
.idea/
*.iml
.gradle
*.prefs
*.class*
*.factorypath
*.project
*.processors
*/bin/
.DS_Store

64 changes: 63 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
# avaje-record-builder
[![Build](https://github.com/avaje/avaje-spi-service/actions/workflows/build.yml/badge.svg)](https://github.com/avaje/avaje-spi-service/actions/workflows/build.yml)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/avaje/avaje-spi-service/blob/master/LICENSE)
[![Maven Central : avaje-record-builder](https://maven-badges.herokuapp.com/maven-central/io.avaje/avaje-record-builder/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.avaje/avaje-record-builder)
[![Discord](https://img.shields.io/discord/1074074312421683250?color=%237289da&label=discord)](https://discord.gg/Qcqf9R27BR)
# avaje-record-builder
Uses Annotation processing to automatically adds `META-INF/services` entries for classes

## Usage
### 1. Add dependency:
```xml
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-spi-service</artifactId>
<version>${spi.version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
```

When working with Java modules you need to add the annotation module as a static dependency.
```java
module my.module {
requires static io.avaje.spi;
}
```
### 2. Add `@ServiceProvider`

On classes that you'd like registered, put the `@ServiceProvider` annotation. As long as you only have one interface or one superclass, that type is assumed to be the spi interface. So given the example below:
```java
@ServiceProvider
public class MyProvider implements SomeSPI {
...
}
```
You get the `META-INF/services/com.example.SomeSPI` file whose content is `org.acme.MyProvider`.

If you have multiple interfaces and/or base type, the library cannot infer the contract type. In such a case, specify the contract type explicitly by giving it to `@ServiceProvider` like this:

```java
@ServiceProvider(SomeSPI.class)
public class MyExtendedProvider extends AbstractSet implements Comparable, Serializable, SomeSPI {
...
}
```

### 3. `module-info` validation
For modular projects, the processor will throw a compile error describing what `provides` statements you have missed. So if you define the SPI like the the previous steps, and have a module setup like the following:
```java
module my.module {

requires static io.avaje.spi;

}
```
You'll get the following compile error:
```
Compilation failure /src/main/java/module-info.java:[1,1]
Missing `provides SomeSPI with MyProvider, MyExtendedProvider;`
```

## Related Works
- [Pistachio](https://github.com/jstachio/pistachio)

37 changes: 37 additions & 0 deletions avaje-record-builder-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.avaje</groupId>
<artifactId>avaje-record-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>avaje-record-builder-core</artifactId>
<name>avaje-record-builder-core</name>
<description>Record Builder Generator</description>
<scm>
<developerConnection>scm:git:[email protected]:avaje/avaje-spi.git</developerConnection>
<tag>HEAD</tag>
</scm>

<properties>
<avaje.prisms.version>1.14</avaje.prisms.version>
</properties>

<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-prisms</artifactId>
<version>${avaje.prisms.version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.avaje.recordbuilder.internal;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;

/** Helper that wraps a writer with some useful methods to append content. */
public class Append implements AutoCloseable {

private final Writer writer;

public Append(Writer writer) {
this.writer = writer;
}

public Append append(String content) {
try {
writer.append(content);
return this;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public void close() {
try {
writer.flush();
writer.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public Append eol() {
try {
writer.append("\n");
return this;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/** Append content with formatted arguments. */
public Append append(String format, Object... args) {
return append(String.format(format, args));
}
}
Loading