-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for immutable and private fields/properties
In addition to better encapsulation and increased safety, this allows LoganSquare to support Kotlin data classes. For each fields, marked for parsing from JSON, there must be a matching constructor parameter in the corresponding position. The names of parameters are not validated, instead the number and types of parameters are relied on for safety. Each model uses either setter/field injection (old behavior) or constructor injection (new behavior); mixing setters and constructor parameters within same model is not supported. Calling `parseField` on JsonMappers of models with constructor injection produces UnsupportedOperationException. The impact on existing code should be minimal — the new parsing strategy gets used only when previous version of LoganSquare would produce an error: e.g. if there are final final or private fields without setters. See pull request for full description of feature
- Loading branch information
1 parent
a063552
commit 3f2f0b3
Showing
12 changed files
with
490 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
processor/src/test/java/com/bluelinelabs/logansquare/processor/ConstructorInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.bluelinelabs.logansquare.processor; | ||
|
||
import com.google.testing.compile.JavaFileObjects; | ||
import org.junit.Test; | ||
|
||
import static com.google.common.truth.Truth.ASSERT; | ||
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; | ||
|
||
public class ConstructorInjectionTest { | ||
@Test | ||
public void testPrivateFields() { | ||
ASSERT.about(javaSource()) | ||
.that(JavaFileObjects.forResource("model/good/PrivateFieldsWithoutSettersModel.java")) | ||
.processedWith(new JsonAnnotationProcessor()) | ||
.compilesWithoutError() | ||
.and() | ||
.generatesSources(JavaFileObjects.forResource("generated/PrivateFieldsWithoutSettersModel$$JsonObjectMapper.java")); | ||
} | ||
|
||
@Test | ||
public void testImmutableFields() { | ||
ASSERT.about(javaSource()) | ||
.that(JavaFileObjects.forResource("model/good/ImmutableFieldsModel.java")) | ||
.processedWith(new JsonAnnotationProcessor()) | ||
.compilesWithoutError() | ||
.and() | ||
.generatesSources(JavaFileObjects.forResource("generated/ImmutableFieldsModel$$JsonObjectMapper.java")); | ||
} | ||
} |
Oops, something went wrong.