Skip to content

Commit 889b46a

Browse files
committed
Update to 1.1.0
- moved to com.github package and group id - package repository is now jitpack - updated source to java 14 - down-compiling to java 8 using jabel
1 parent 8651911 commit 889b46a

File tree

100 files changed

+386
-296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+386
-296
lines changed

.github/workflows/build.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: gradle build
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Set up JDK 14
17+
uses: actions/setup-java@v1
18+
with:
19+
java-version: 14
20+
- name: Grant execute permission for gradlew
21+
run: chmod +x gradlew
22+
- name: Build with Gradle
23+
run: ./gradlew build

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
moshi-java-codegen
22
=====
33

4+
[![](https://jitpack.io/v/nename0/moshi-java-codegen.svg)](https://jitpack.io/#nename0/moshi-java-codegen/1.1.0)
5+
46
This annotation processor generates small and fast [JsonAdapters](https://square.github.io/moshi/1.x/moshi/com/squareup/moshi/JsonAdapter.html)
57
for Moshi from Java classes, just like the [moshi-kotlin-codegen](https://github.com/square/moshi#codegen) for Kotlin.
68
To use it just annotate your class like this:
@@ -18,16 +20,13 @@ and add the annotation processor to your project. For Gradle:
1820

1921
```groovy
2022
repositories {
21-
maven {
22-
name = "GitHubPackages - moshi-java-codegen"
23-
url = uri("https://maven.pkg.github.com/nename0/moshi-java-codegen")
24-
}
2523
mavenCentral()
24+
maven { url 'https://jitpack.io' }
2625
...
2726
}
2827
2928
dependencies {
30-
annotationProcessor "io.github.nename0:moshi-java-codegen:1.0.0-SNAPSHOT"
29+
annotationProcessor "com.github.nename0:moshi-java-codegen:1.1.0"
3130
}
3231
```
3332

build.gradle

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
subprojects {
2-
apply plugin: "java"
1+
buildscript {
2+
ext {
3+
moshiVersion = "1.9.2"
4+
}
5+
}
36

4-
group 'io.github.nename0'
5-
version '1.0.0-SNAPSHOT'
7+
allprojects {
8+
apply plugin: "java"
69

7-
sourceCompatibility = 1.8
10+
group 'com.github.nename0'
11+
version '1.1.0'
812

913
repositories {
1014
mavenCentral()
1115
}
1216

13-
ext {
14-
moshiVersion = "1.9.2"
15-
}
16-
1717
dependencies {
18-
testCompile "org.junit.jupiter:junit-jupiter:5.6.1"
18+
testImplementation "org.junit.jupiter:junit-jupiter:5.6.1"
1919
}
2020

2121
test {
@@ -24,5 +24,5 @@ subprojects {
2424
}
2525

2626
wrapper {
27-
gradleVersion = '5.6.4' //version required
27+
gradleVersion = '6.3' //version required
2828
}

codegen/build.gradle

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import org.gradle.internal.jvm.Jvm
1+
// import org.gradle.internal.jvm.Jvm
22

33
plugins {
44
id 'java-library'
@@ -8,28 +8,55 @@ plugins {
88
ext {
99
autoServiceVersion = "1.0-rc6"
1010
incapVersion = "0.2"
11+
jabelVersion = "records-SNAPSHOT"
1112
}
1213

14+
repositories {
15+
maven { url 'https://jitpack.io' }
16+
}
17+
18+
sourceCompatibility = 14
19+
targetCompatibility = 8
20+
1321
dependencies {
1422
api "com.squareup.moshi:moshi:$moshiVersion"
1523
implementation "com.squareup:javapoet:1.12.1"
24+
1625
implementation "com.google.auto.service:auto-service-annotations:$autoServiceVersion"
1726
annotationProcessor "com.google.auto.service:auto-service:$autoServiceVersion"
27+
1828
implementation "net.ltgt.gradle.incap:incap:$incapVersion"
1929
annotationProcessor "net.ltgt.gradle.incap:incap-processor:$incapVersion"
2030

21-
testCompile "com.google.testing.compile:compile-testing:0.18"
22-
testCompile files(Jvm.current().getToolsJar()) // we need the tools.jar from the jdk for google compile-testing
31+
// use java 14 but compile to java 8
32+
annotationProcessor "com.github.bsideup:jabel:$jabelVersion"
33+
compileOnly "com.github.bsideup:jabel:$jabelVersion"
34+
35+
testImplementation "com.google.testing.compile:compile-testing:0.18"
36+
// testCompile files(Jvm.current().getToolsJar()) // we need the tools.jar from the jdk for google compile-testing
2337
testAnnotationProcessor project // use self to process tests
2438
}
2539

40+
tasks.withType(JavaCompile).all {
41+
options.compilerArgs = [
42+
"--release", "8", // Avoid using Java 9+ APIs
43+
'--enable-preview',
44+
]
45+
46+
doFirst {
47+
options.compilerArgs = options.compilerArgs.findAll {
48+
it != '--enable-preview'
49+
}
50+
}
51+
}
52+
2653
def defaultResourceDir = sourceSets.test.resources.srcDirs[0]
2754

2855
sourceSets {
2956
test {
3057
resources {
3158
// add java source of test-adapters tests to out test resources so we can do regression testing with them
32-
srcDirs defaultResourceDir, project(':test-adapters').sourceSets.test.java.srcDirs
59+
srcDirs project(':test-adapters').sourceSets.test.java.srcDirs
3360
}
3461
}
3562
}
@@ -38,6 +65,11 @@ sourceSets {
3865
task updateRegressionTestExpectedOutput(type: Copy, dependsOn: project(':test-adapters').compileTestJava) {
3966
from(project(':test-adapters').sourceSets.test.output.generatedSourcesDirs)
4067
into(file(defaultResourceDir.path + '/regression'))
68+
69+
doLast {
70+
println "From: " + project(':test-adapters').sourceSets.test.output.generatedSourcesDirs.join(", ")
71+
println "To: " + file(defaultResourceDir.path + '/regression')
72+
}
4173
}
4274

4375

codegen/src/main/java/io/github/nename0/moshi/java/codegen/JsonClassCodegenProcessor.java renamed to codegen/src/main/java/com/github/nename0/moshi/java/codegen/JsonClassCodegenProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
* See the License for the specific language governing permissions and
1919
* limitations under the License.
2020
*/
21-
package io.github.nename0.moshi.java.codegen;
21+
package com.github.nename0.moshi.java.codegen;
2222

2323
import com.google.auto.service.AutoService;
24-
import io.github.nename0.moshi.java.codegen.renderer.JsonAdapterRenderer;
24+
import com.github.nename0.moshi.java.codegen.renderer.JsonAdapterRenderer;
2525
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
2626
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;
2727

codegen/src/main/java/io/github/nename0/moshi/java/codegen/JsonClassHolder.java renamed to codegen/src/main/java/com/github/nename0/moshi/java/codegen/JsonClassHolder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* See the License for the specific language governing permissions and
1919
* limitations under the License.
2020
*/
21-
package io.github.nename0.moshi.java.codegen;
21+
package com.github.nename0.moshi.java.codegen;
2222

2323
import com.squareup.javapoet.ClassName;
2424
import com.squareup.javapoet.TypeName;
@@ -28,7 +28,6 @@
2828
import java.util.TreeMap;
2929

3030
import javax.lang.model.element.TypeElement;
31-
import javax.lang.model.element.TypeParameterElement;
3231

3332
/**
3433
* contains information about a parsed json class

codegen/src/main/java/io/github/nename0/moshi/java/codegen/JsonClassParser.java renamed to codegen/src/main/java/com/github/nename0/moshi/java/codegen/JsonClassParser.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* See the License for the specific language governing permissions and
1919
* limitations under the License.
2020
*/
21-
package io.github.nename0.moshi.java.codegen;
21+
package com.github.nename0.moshi.java.codegen;
2222

2323
import com.squareup.javapoet.ClassName;
2424
import com.squareup.javapoet.TypeName;
@@ -28,18 +28,18 @@
2828

2929
import javax.annotation.processing.ProcessingEnvironment;
3030
import javax.annotation.processing.RoundEnvironment;
31-
import javax.lang.model.element.*;
32-
import javax.lang.model.type.DeclaredType;
31+
import javax.lang.model.element.Element;
32+
import javax.lang.model.element.ElementKind;
33+
import javax.lang.model.element.ExecutableElement;
34+
import javax.lang.model.element.TypeElement;
3335
import javax.lang.model.type.TypeKind;
34-
import javax.lang.model.type.TypeMirror;
3536
import javax.lang.model.util.ElementFilter;
3637
import javax.lang.model.util.Elements;
3738
import java.io.PrintWriter;
3839
import java.io.StringWriter;
3940
import java.lang.annotation.Annotation;
4041
import java.util.List;
4142
import java.util.Map;
42-
import java.util.Set;
4343
import java.util.stream.Collectors;
4444

4545
import static javax.lang.model.element.Modifier.*;
@@ -72,12 +72,12 @@ public void findAndParseObjects(RoundEnvironment env, Map<String, JsonClassHolde
7272
if (!annotation.generateAdapter() || !JsonClassCodegenProcessor.GENERATOR_NAME.equals(annotation.generator())) {
7373
continue;
7474
}
75-
if (elem.getKind() != ElementKind.CLASS || !(elem instanceof TypeElement)) {
75+
if (elem.getKind() != ElementKind.CLASS || !(elem instanceof TypeElement typeElement)) {
7676
error(elem, "@JsonClass can't be applied to %s - %s: must be a Class type.", elem.getEnclosingElement().toString(), elem.toString());
7777
continue;
7878
}
7979
try {
80-
processJsonClassAnnotation((TypeElement) elem, jsonClassMap);
80+
processJsonClassAnnotation(typeElement, jsonClassMap);
8181
} catch (Exception e) {
8282
StringWriter stackTrace = new StringWriter();
8383
e.printStackTrace(new PrintWriter(stackTrace));
@@ -135,11 +135,11 @@ private boolean checkClassAccessibleFromPackage(TypeElement element) {
135135
return false;
136136
}
137137
Element parent = current.getEnclosingElement();
138-
if (parent.asType().getKind() != TypeKind.DECLARED || !(parent instanceof TypeElement)) {
138+
if (parent.asType().getKind() != TypeKind.DECLARED || !(parent instanceof TypeElement child)) {
139139
error(parent, "Expected %s(%s) nesting %s to be a class/interface", parent.getSimpleName(), parent.getKind().toString(), current.getQualifiedName());
140140
return false;
141141
}
142-
current = (TypeElement) parent;
142+
current = child;
143143
}
144144
return true;
145145
}

codegen/src/main/java/io/github/nename0/moshi/java/codegen/JsonFieldHolder.java renamed to codegen/src/main/java/com/github/nename0/moshi/java/codegen/JsonFieldHolder.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
1-
package io.github.nename0.moshi.java.codegen;
1+
package com.github.nename0.moshi.java.codegen;
22

33
import com.squareup.javapoet.ClassName;
44
import com.squareup.javapoet.FieldSpec;
55
import com.squareup.javapoet.TypeName;
6-
import com.squareup.moshi.Json;
7-
import com.squareup.moshi.JsonQualifier;
86

9-
import javax.lang.model.element.*;
10-
import javax.lang.model.type.DeclaredType;
11-
import javax.lang.model.type.ExecutableType;
12-
import javax.lang.model.type.TypeKind;
13-
import javax.lang.model.type.TypeMirror;
14-
import javax.lang.model.util.ElementFilter;
15-
import javax.lang.model.util.Elements;
16-
import javax.lang.model.util.Types;
17-
import java.lang.annotation.ElementType;
18-
import java.lang.annotation.Retention;
19-
import java.lang.annotation.RetentionPolicy;
20-
import java.lang.annotation.Target;
217
import java.util.*;
22-
import java.util.stream.Collectors;
238

249
/**
2510
* contains information about a parsed json field

codegen/src/main/java/io/github/nename0/moshi/java/codegen/JsonFieldParser.java renamed to codegen/src/main/java/com/github/nename0/moshi/java/codegen/JsonFieldParser.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* See the License for the specific language governing permissions and
1919
* limitations under the License.
2020
*/
21-
package io.github.nename0.moshi.java.codegen;
21+
package com.github.nename0.moshi.java.codegen;
2222

2323
import com.squareup.javapoet.ClassName;
2424
import com.squareup.javapoet.TypeName;
@@ -111,12 +111,15 @@ private JsonFieldHolder getGetterAndSetter(TypeElement originClass, TypeElement
111111

112112
List<String> possibleGetterMethodNames = new ArrayList<>();
113113
possibleGetterMethodNames.add("get" + elementNameLowerCase);
114+
// in preparation for Java 14 JEP 359 Records where getters have the field name
115+
possibleGetterMethodNames.add(elementNameLowerCase);
114116
if (elementTypeKind == TypeKind.BOOLEAN) {
115117
possibleGetterMethodNames.add("is" + elementNameLowerCase);
116118
possibleGetterMethodNames.add("has" + elementNameLowerCase);
117-
possibleGetterMethodNames.add(elementNameLowerCase);
118119
}
119120

121+
// TODO allow constructor arguments instead of setters especially for JEP 359 Records
122+
120123
List<String> possibleSetterMethodNames = new ArrayList<>();
121124
possibleSetterMethodNames.add("set" + elementNameLowerCase);
122125

codegen/src/main/java/io/github/nename0/moshi/java/codegen/renderer/DelegateAdapterFieldRenderer.java renamed to codegen/src/main/java/com/github/nename0/moshi/java/codegen/renderer/DelegateAdapterFieldRenderer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package io.github.nename0.moshi.java.codegen.renderer;
1+
package com.github.nename0.moshi.java.codegen.renderer;
22

33
import com.squareup.javapoet.*;
44
import com.squareup.moshi.JsonAdapter;
55
import com.squareup.moshi.Types;
6-
import io.github.nename0.moshi.java.codegen.JsonClassHolder;
7-
import io.github.nename0.moshi.java.codegen.JsonFieldHolder;
6+
import com.github.nename0.moshi.java.codegen.JsonClassHolder;
7+
import com.github.nename0.moshi.java.codegen.JsonFieldHolder;
88

99
import javax.lang.model.element.Modifier;
1010
import java.lang.annotation.Annotation;
@@ -38,6 +38,8 @@ public void addAdapterInitializers(MethodSpec.Builder constructorBuilder, TypeRe
3838
List<CodeBlock> parameters = new ArrayList<>();
3939
parameters.add(typeRenderer.render(fieldHolder.type));
4040

41+
// TODO implement the annotations to prevent using reflection in Types.getFieldJsonQualifierAnnotations()
42+
4143
if (fieldHolder.jsonAnnotations.size() > 0) {
4244
ParameterizedTypeName fieldType = ParameterizedTypeName.get(Set.class, Types.subtypeOf(Annotation.class));
4345
String fieldName = "ANNOTATIONS_" + fieldHolder.fieldName.toUpperCase() + "_" + (annotationCount++);

0 commit comments

Comments
 (0)