Skip to content

Commit

Permalink
Java 9+ compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
whiskeysierra committed Sep 5, 2020
1 parent ea0e907 commit 7745f6c
Show file tree
Hide file tree
Showing 53 changed files with 508 additions and 173 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ jobs:
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 1.8
# See comment on moditect-maven-plugin in pom.xml
java-version: 12
- name: Compile
run: ./mvnw clean test-compile -B
- name: Test
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
*.iml
.idea/

### VS Code ###
.vscode
.project
.settings
.classpath
.factorypath

### Maven ###
target/
/.mvn/wrapper/*.jar
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ It's decoupled from any JSON library, but contains a separate module for Jackson
- Java 8
- Any build tool using Maven Central, or direct download
- Jackson (optional)
- Gson (optional)

## Installation

Expand All @@ -44,6 +45,24 @@ Add the following dependency to your project:
<artifactId>jackson-datatype-problem</artifactId>
<version>${problem.version}</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-gson</artifactId>
<version>${problem.version}</version>
</dependency>
```

### Java Modules

Even though the minimum requirement is still Java 8, all modules are Java 9 compatible:

```java
module org.example {
requires org.zalando.problem;
// pick needed dependencies
requires org.zalando.problem.jackson;
requires org.zalando.problem.gson;
}
```

## Configuration
Expand Down
4 changes: 4 additions & 0 deletions jackson-datatype-problem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<artifactId>jackson-datatype-problem</artifactId>
<properties>
<jackson.version>2.10.1</jackson.version>
<module.name>org.zalando.problem.jackson</module.name>
<module.exports>
org.zalando.problem.jackson;
</module.exports>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.annotation.Nullable;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.zalando.problem.AbstractThrowableProblem;
import org.zalando.problem.StatusType;
import org.zalando.problem.ThrowableProblem;

import java.net.URI;

abstract class AbstractThrowableProblemMixIn {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand All @@ -7,6 +7,8 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import org.zalando.problem.ThrowableProblem;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;

@JsonIgnoreProperties(ignoreUnknown = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import org.zalando.problem.DefaultProblem;
import org.zalando.problem.Problem;
import org.zalando.problem.StatusType;

import java.net.URI;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.util.VersionUtil;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.apiguardian.api.API;

import org.zalando.problem.DefaultProblem;
import org.zalando.problem.Exceptional;
import org.zalando.problem.Problem;
import org.zalando.problem.Status;
import org.zalando.problem.StatusType;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -34,7 +40,7 @@ public ProblemModule() {
* @throws IllegalArgumentException if there are duplicate status codes across all status types
*/
@SafeVarargs
public <E extends Enum & StatusType> ProblemModule(final Class<? extends E>... types)
public <E extends Enum<?> & StatusType> ProblemModule(final Class<? extends E>... types)
throws IllegalArgumentException {

this(false, buildIndex(types));
Expand Down Expand Up @@ -76,7 +82,7 @@ public void setupModule(final SetupContext context) {
}

@SafeVarargs
private static <E extends Enum & StatusType> Map<Integer, StatusType> buildIndex(
private static <E extends Enum<?> & StatusType> Map<Integer, StatusType> buildIndex(
final Class<? extends E>... types) {
final Map<Integer, StatusType> index = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.databind.util.StdConverter;

import org.zalando.problem.Problem;

import java.net.URI;

final class ProblemTypeConverter extends StdConverter<URI, URI> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import javax.annotation.Nullable;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.zalando.problem.StatusType;

import java.io.IOException;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import org.zalando.problem.StatusType;

import java.io.IOException;

final class StatusTypeSerializer extends JsonSerializer<StatusType> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import javax.annotation.concurrent.Immutable;
import org.zalando.problem.StatusType;

@Immutable
final class UnknownStatus implements StatusType {

private final int statusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.zalando.problem.ProblemModule
org.zalando.problem.jackson.ProblemModule
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

abstract class BusinessException extends Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import org.zalando.problem.StatusType;

enum CustomStatus implements StatusType {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.google.gag.annotation.remark.Hack;
import com.google.gag.annotation.remark.OhNoYouDidnt;
import org.junit.jupiter.api.Test;
import org.zalando.problem.AbstractThrowableProblem;

import java.net.URI;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.zalando.problem.Status.BAD_REQUEST;

@Hack
@OhNoYouDidnt
final class EnforceCoverageTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import org.zalando.problem.Exceptional;
import org.zalando.problem.StatusType;
import org.zalando.problem.ThrowableProblem;

import java.io.IOException;
import java.net.URI;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;

import org.zalando.problem.AbstractThrowableProblem;

import java.net.URI;

import static org.zalando.problem.Status.BAD_REQUEST;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import org.zalando.problem.spi.StackTraceProcessor;

Expand All @@ -16,7 +16,11 @@ public Collection<StackTraceElement> process(final Collection<StackTraceElement>
final ArrayList<StackTraceElement> elements = new ArrayList<>(collection);

return elements.stream()
.filter(startsWith("sun.reflect", "java.lang.reflect", "com.fasterxml.jackson").negate())
.filter(startsWith(
"sun.reflect",
"java.lang.reflect",
"jdk.internal.reflect",
"com.fasterxml.jackson").negate())
.findFirst()
.map(elements::indexOf)
.map(subList(elements))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;

import org.zalando.problem.Exceptional;
import org.zalando.problem.StatusType;
import org.zalando.problem.ThrowableProblem;

import java.net.URI;

import static org.zalando.problem.Status.BAD_REQUEST;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.zalando.problem.DefaultProblem;
import org.zalando.problem.Exceptional;
import org.zalando.problem.Problem;
import org.zalando.problem.Status;
import org.zalando.problem.StatusType;
import org.zalando.problem.ThrowableProblem;

import java.io.IOException;
import java.io.PrintWriter;
Expand Down Expand Up @@ -245,7 +251,7 @@ void shouldDeserializeWithProcessedStackTrace() throws IOException {
final String stackTrace = getStackTrace(problem);
final String[] stackTraceElements = stackTrace.split("\n");

assertThat(stackTraceElements[1], startsWith("\tat org.zalando.problem.ProblemMixInTest"));
assertThat(stackTraceElements[1], startsWith("\tat " + ProblemMixInTest.class.getName()));
}

private String getStackTrace(final Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.zalando.problem;
package org.zalando.problem.jackson;

import org.junit.jupiter.api.Test;
import org.zalando.problem.Status;

import static org.junit.jupiter.api.Assertions.assertThrows;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.zalando.problem.jackson;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class UnknownStatusTest {

@Test
void shouldReturnCodeAndPhrase() {
final int code = 8080;
final UnknownStatus status = new UnknownStatus(code);
assertEquals(8080, status.getStatusCode());
assertEquals("Unknown", status.getReasonPhrase());
}

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.zalando.problem.JacksonStackTraceProcessor
org.zalando.problem.jackson.JacksonStackTraceProcessor
Loading

0 comments on commit 7745f6c

Please sign in to comment.