Skip to content

Commit e14fd0a

Browse files
committed
Migrate to JSpecify annotations
1 parent e8ce124 commit e14fd0a

File tree

13 files changed

+211
-102
lines changed

13 files changed

+211
-102
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ jobs:
2525
java-version: ${{ matrix.java-version }}
2626

2727
- name: Validate Gradle Wrapper
28-
uses: gradle/wrapper-validation-action@v3
28+
uses: gradle/actions/wrapper-validation@v3
2929

3030
- name: Setup Gradle
31-
uses: gradle/gradle-build-action@v3
31+
uses: gradle/actions/setup-gradle@v3
3232

3333
- name: Build
3434
run: ./gradlew build

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# AzAuth
22

33
[![Java CI](https://img.shields.io/github/actions/workflow/status/Azuriom/AzAuth/build.yml?branch=master&style=flat-square)](https://github.com/Azuriom/AzAuth/actions/workflows/build.yml)
4-
[![Maven Central](https://img.shields.io/maven-central/v/com.azuriom/azauth.svg?label=Maven%20Central&style=flat-square)](https://search.maven.org/search?q=g:%22com.azuriom%22%20AND%20a:%22azauth%22)
4+
[![Maven Central](https://img.shields.io/maven-central/v/com.azuriom/azauth.svg?label=Maven%20Central&style=flat-square)](https://central.sonatype.com/artifact/com.azuriom/azauth)
55
[![Chat](https://img.shields.io/discord/625774284823986183?color=5865f2&label=Discord&logo=discord&logoColor=fff&style=flat-square)](https://azuriom.com/discord)
66

77
A Java implementation of the Azuriom Auth API.
@@ -38,4 +38,4 @@ You can find how to use AzAuth on our [documentation](https://azuriom.com/docs/a
3838

3939
## Dependencies
4040

41-
AzAuth uses [Google Gson](https://github.com/google/gson) to deserialize Json.
41+
AzAuth uses [Google Gson](https://github.com/google/gson) to deserialize JSON, and [JSpecify](https://jspecify.dev/) annotations to provide nullability information.

build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ plugins {
55
}
66

77
group 'com.azuriom'
8-
version '1.0.0-SNAPSHOT'
8+
version '1.1.0'
99

1010
ext {
1111
isReleaseVersion = !version.endsWith('SNAPSHOT')
1212
}
1313

1414
java {
15-
sourceCompatibility = 1.8
16-
targetCompatibility = 1.8
15+
sourceCompatibility = JavaVersion.VERSION_1_8
16+
targetCompatibility = JavaVersion.VERSION_1_8
1717
}
1818

1919
tasks.withType(JavaCompile) {
@@ -26,7 +26,7 @@ repositories {
2626

2727
dependencies {
2828
api 'com.google.code.gson:gson:2.10.1'
29-
compileOnlyApi 'org.jetbrains:annotations:24.1.0'
29+
api 'org.jspecify:jspecify:1.0.0'
3030

3131
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2'
3232
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
@@ -82,8 +82,8 @@ publishing {
8282

8383
repositories {
8484
maven {
85-
def releasesRepoUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
86-
def snapshotsRepoUrl = 'https://oss.sonatype.org/content/repositories/snapshots/'
85+
def releasesRepoUrl = 'https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/'
86+
def snapshotsRepoUrl = 'https://central.sonatype.com/repository/maven-snapshots/'
8787
url = isReleaseVersion ? releasesRepoUrl : snapshotsRepoUrl
8888

8989
credentials {

src/main/java/com/azuriom/azauth/AuthClient.java

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import com.azuriom.azauth.model.ErrorResponse;
88
import com.azuriom.azauth.model.User;
99
import com.google.gson.*;
10-
import org.jetbrains.annotations.Blocking;
11-
import org.jetbrains.annotations.Contract;
12-
import org.jetbrains.annotations.NotNull;
13-
import org.jetbrains.annotations.Nullable;
10+
import org.jspecify.annotations.NullMarked;
11+
import org.jspecify.annotations.Nullable;
1412

1513
import java.awt.*;
1614
import java.io.BufferedReader;
@@ -28,7 +26,9 @@
2826

2927
/**
3028
* The authentication client for Azuriom.
29+
* All methods perform blocking operations and should not be called on a non-blocking thread.
3130
*/
31+
@NullMarked
3232
public class AuthClient {
3333

3434
private static final Logger LOGGER = Logger.getLogger(AuthClient.class.getName());
@@ -40,14 +40,14 @@ public class AuthClient {
4040
.registerTypeAdapter(UUID.class, new UuidAdapter())
4141
.create();
4242

43-
private final @NotNull String url;
43+
private final String url;
4444

4545
/**
46-
* Construct a new AzAuthenticator instance.
46+
* Construct a new AzAuthenticator instance, with the given website URL.
4747
*
4848
* @param url the website url
4949
*/
50-
public AuthClient(@NotNull String url) {
50+
public AuthClient(String url) {
5151
Objects.requireNonNull(url, "url");
5252

5353
this.url = url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
@@ -62,7 +62,7 @@ public AuthClient(@NotNull String url) {
6262
*
6363
* @return the website url
6464
*/
65-
public @NotNull String getUrl() {
65+
public String getUrl() {
6666
return this.url;
6767
}
6868

@@ -74,9 +74,7 @@ public AuthClient(@NotNull String url) {
7474
* @return the user profile
7575
* @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
7676
*/
77-
@Blocking
78-
public @NotNull AuthResult<@NotNull User> login(@NotNull String email,
79-
@NotNull String password) throws AuthException {
77+
public AuthResult<User> login(String email, String password) throws AuthException {
8078
return this.login(email, password, User.class);
8179
}
8280

@@ -89,10 +87,9 @@ public AuthClient(@NotNull String url) {
8987
* @return the user profile
9088
* @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
9189
*/
92-
@Blocking
93-
public @NotNull AuthResult<@NotNull User> login(@NotNull String email,
94-
@NotNull String password,
95-
@Nullable String code2fa) throws AuthException {
90+
public AuthResult<User> login(String email,
91+
String password,
92+
@Nullable String code2fa) throws AuthException {
9693
return this.login(email, password, code2fa, User.class);
9794
}
9895

@@ -106,10 +103,9 @@ public AuthClient(@NotNull String url) {
106103
* @return the user profile
107104
* @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
108105
*/
109-
@Blocking
110-
public <T> @NotNull AuthResult<@NotNull T> login(@NotNull String email,
111-
@NotNull String password,
112-
@NotNull Class<T> responseType) throws AuthException {
106+
public <T> AuthResult<T> login(String email,
107+
String password,
108+
Class<T> responseType) throws AuthException {
113109
return login(email, password, (String) null, responseType);
114110
}
115111

@@ -123,10 +119,9 @@ public AuthClient(@NotNull String url) {
123119
* @return the user profile
124120
* @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
125121
*/
126-
@Blocking
127-
public @NotNull User login(@NotNull String email,
128-
@NotNull String password,
129-
@NotNull Supplier<String> codeSupplier) throws AuthException {
122+
public User login(String email,
123+
String password,
124+
Supplier<String> codeSupplier) throws AuthException {
130125
return login(email, password, codeSupplier, User.class);
131126
}
132127

@@ -142,11 +137,10 @@ public AuthClient(@NotNull String url) {
142137
* @return the user profile
143138
* @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
144139
*/
145-
@Blocking
146-
public <T> @NotNull T login(@NotNull String email,
147-
@NotNull String password,
148-
@NotNull Supplier<String> codeSupplier,
149-
@NotNull Class<T> responseType) throws AuthException {
140+
public <T> T login(String email,
141+
String password,
142+
Supplier<@Nullable String> codeSupplier,
143+
Class<T> responseType) throws AuthException {
150144
AuthResult<T> result = login(email, password, responseType);
151145

152146
if (result.isSuccess()) {
@@ -183,11 +177,10 @@ public AuthClient(@NotNull String url) {
183177
* @return the user profile
184178
* @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
185179
*/
186-
@Blocking
187-
public <T> @NotNull AuthResult<@NotNull T> login(@NotNull String email,
188-
@NotNull String password,
189-
@Nullable String code2fa,
190-
@NotNull Class<T> responseType) throws AuthException {
180+
public <T> AuthResult<T> login(String email,
181+
String password,
182+
@Nullable String code2fa,
183+
Class<T> responseType) throws AuthException {
191184
JsonObject body = new JsonObject();
192185
body.addProperty("email", email);
193186
body.addProperty("password", password);
@@ -203,8 +196,7 @@ public AuthClient(@NotNull String url) {
203196
* @return the user profile
204197
* @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
205198
*/
206-
@Blocking
207-
public @NotNull User verify(@NotNull String accessToken) throws AuthException {
199+
public User verify(String accessToken) throws AuthException {
208200
return this.verify(accessToken, User.class);
209201
}
210202

@@ -217,9 +209,7 @@ public AuthClient(@NotNull String url) {
217209
* @return the user profile
218210
* @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
219211
*/
220-
@Blocking
221-
public <T> @NotNull T verify(@NotNull String accessToken, @NotNull Class<T> responseType)
222-
throws AuthException {
212+
public <T> T verify(String accessToken, Class<T> responseType) throws AuthException {
223213
JsonObject body = new JsonObject();
224214
body.addProperty("access_token", accessToken);
225215

@@ -234,24 +224,20 @@ public AuthClient(@NotNull String url) {
234224

235225
/**
236226
* Invalidate the given access token.
237-
* To get a new valid access token you need to use {@link #login(String, String)} again.
227+
* To get a new valid access token, you need to use {@link #login(String, String)} again.
238228
*
239229
* @param accessToken the user access token
240230
* @throws AuthException if an error occurs (IO exception, invalid credentials, etc)
241231
*/
242-
@Blocking
243-
public void logout(@NotNull String accessToken) throws AuthException {
232+
public void logout(String accessToken) throws AuthException {
244233
JsonObject body = new JsonObject();
245234
body.addProperty("access_token", accessToken);
246235

247-
this.post("logout", body, null);
236+
this.post("logout", body, JsonNull.class);
248237
}
249238

250-
@Blocking
251-
@Contract("_, _, null -> null; _, _, !null -> !null")
252-
private <T> AuthResult<T> post(@NotNull String endpoint,
253-
@NotNull JsonObject body,
254-
@Nullable Class<T> responseType) throws AuthException {
239+
private <T> AuthResult<T> post(String endpoint, JsonObject body, Class<T> responseType)
240+
throws AuthException {
255241
try {
256242
URI api = URI.create(this.url + "/api/auth/" + endpoint);
257243
HttpURLConnection connection = (HttpURLConnection) api.toURL().openConnection();
@@ -271,18 +257,18 @@ private <T> AuthResult<T> post(@NotNull String endpoint,
271257
return this.handleClientError(connection);
272258
}
273259

274-
if (responseType == null) {
275-
return null;
276-
}
277-
278260
return handleResponse(connection, responseType);
279261
} catch (IOException e) {
280262
throw new AuthException(e);
281263
}
282264
}
283265

284-
private <T> AuthResult<T> handleResponse(HttpURLConnection connection, Class<T> type)
266+
private <T> AuthResult<T> handleResponse(HttpURLConnection connection, Class<@Nullable T> type)
285267
throws AuthException, IOException {
268+
if (type == JsonNull.class) {
269+
return new AuthResult.Success<>(type.cast(JsonNull.INSTANCE));
270+
}
271+
286272
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
287273
T response = GSON.fromJson(reader, type);
288274

0 commit comments

Comments
 (0)