Commit Graph

1086 Commits

Author SHA1 Message Date
Zac Sweers
336ca952b0 Use char literals for array and object start/end (#944)
Use char literals for array and object start/end
2019-09-30 15:27:38 -04:00
Jake Wharton
ecb7083abf Use char literals for array and object start/end 2019-09-30 14:40:12 -04:00
Jesse Wilson
52aaf9cc84 Merge pull request #933 from square/z/throwOnMissingTypeVars
Throw on missing type variables in Types.newParameterizedType* methods
2019-09-26 21:57:23 -04:00
Jesse Wilson
20aa604489 Merge pull request #940 from square/jakew/skip-name-message/2019-09-26
Point at the offending name when failing on unknown
2019-09-26 21:56:49 -04:00
Jake Wharton
12d37ffdc3 Point at the offending name when failing on unknown 2019-09-26 20:37:34 -04:00
Zac Sweers
86736461c6 Fix misspelled codgen package in tests (#937)
* Fix misspelled codgen package in tests

This has always bothered me

* Remove leftover import

* Fix import one more time

* Imports are the worst
2019-09-25 23:48:31 -04:00
Zac Sweers
902a935f89 Shade kotlinpoet-metadata and kotlinx-metadata (#936)
* Shade kotlinpoet-metadata and kotlinx-metadata

* Relocate KotlinPoet's shaded autocommon dep too
2019-09-25 23:15:06 -04:00
Zac Sweers
54d07b6cbf Migrate to KotlinPoet-metadata (#903)
* Add kotlinpoet version to properties

* Swap in kotlinpoet-metadata for kotlin-metadata in dependencies

* Don't use KotlinMetadataUtils and KotlinAbstractProcessor anymore

* Upcast to TypeElement

* Temporarily add direct kotlinx-metadata dependency

Something is wrong with packaging in my local kotlinpoet, will remove before

* Remove tags API from TargetParameter

No longer needed

* Add PropertySpec directly to TargetProperty, remove holder funs

Simplifies some things!

* Move generated annotation gen into type callback

Removes JsonClassCodegenProcessor completely from codegen API

* Remove unstable autocommon dependency

Won't be using this anymore after this change

* Manually put quotes jsonName in AdapterGenerator

Otherwise we could incur double escaping from kotlinpoet since we directly reuse possible-escaped parsed `@Json` name values

* Opt in to use `@UseExperimental` annotation for preview

* Rework and simplify metadata to just use kotlinpoet-metadata

This is hard to do in broken down 1:1 steps, but the net result is a much smaller implementation footprint, better error messages, and a simpler API interaction with the code gen API. There is some raw parsing of kotlinpoet types required (mostly around annotations), but otherwise it's pretty smooth sailing and a good test of the upcoming kotlinpoet-metadata support

* Declare KotlinPoetMetadataPreview directly

* Try to point to snapshots for CI build

* Update to classinspector API

* Remove TypeResolver API

No longer needed

* Kotlinpoet 1.4.0 final

* Fix missing import from rebase

* Remove old kotlin-metadata version
2019-09-25 22:22:44 -04:00
Zac Sweers
5f98e93698 Switch to kotlin-compile-testing for JsonClassCodegenProcessorTe… (#928)
* Add kotlin-compile-testing dependency

* Add Okio dependency to tests to match kotlin-compile-testing

Without this, Okio.sink() fails to resolve at runtime

* Try converting privateConstructor() to kotlin-compile-testing

* Space

* Update to 1.2.2

* Use new kotlin source file API

* Use temporaryFolder as workingDir

* Extract prepareCompilation helper method

This allows for modifying the compilation with extra bits as needed

* Migrate tests entirely to new API

* Remove incorrect error message

This passed before, but was picked up in https://github.com/square/moshi/pull/903 before and observed to be incorrect.

* Remove custom kotlin compiler implementations

* Add an OK exit code test

This wasn't possible before!

* Remove jitpack dependency now that it's on mavencentral

* Move versions to parent pom

* Fix new test from rebase
2019-09-25 16:41:31 -04:00
Zac Sweers
96fe3e1417 Throw on missing type variables in Types.newParameterizedType* methods
It's a programmer error to omit this, but vararg declaration allows you to. This checks that.

We could improve it to ensure matching lengths or bounds, but this is a good start.
2019-09-24 18:27:36 -04:00
Zac Sweers
c0639316b1 Don't try to access unsafe APIs on android 29+ (#913)
* Don't try to access unsafe APIs on android 29+

Resolves #872. Not sure if we want to try caching some of these lookups

* Checkstyle

* Use correct inner class naming

Co-Authored-By: Jake Wharton <jakew@google.com>

* Name variable ignored

* Cache android check
2019-09-24 16:05:12 -04:00
Zac Sweers
1e541fc66c Use raw type for kclass references in dynamic invocation code gen (#911)
* Use raw type for kclass references in dynamic invocation code gen

Added a compile-only regression test
Resolves #905

* Make it actually generic

* Mostly fix generation

Will need to wait for inlined instantiation to merge

* Fix rebase issues and use newInstance
2019-09-12 21:55:47 -04:00
Zac Sweers
dd0e3807d3 Fail on sealed classes in KotlinJsonAdapterFactory and code gen (#914)
Resolves #906
2019-09-12 20:38:46 -04:00
Zac Sweers
a67b4d6a72 Inline mask generation and constructor invocation (#908)
* Differentiate local naming from properties with constructor defaults

* Remove constructor invocation and mask creation methods

We're inlining all this

* Add explanatory comment for why we use the default primitive value

* Inline mask generation and constructor invocation to generated adapters

* Remove unused argument

Co-Authored-By: Jake Wharton <jakew@google.com>

* Compute masks directly during code gen

* Opportunistic: remove extraneous double space from control flow

* Just mask and invert directly
2019-09-12 16:42:31 -04:00
Zac Sweers
7a4f3513a1 Don't generate generic type information in adapter lookup (#919)
This is redundant since the property it's being assigned to already defines it, so we can omit this and save some generated code bloat
2019-09-12 16:29:53 -04:00
Zac Sweers
489b178a9b Only add constructor property if it will be used (#918)
Before - we would always add the constructor property even if the class had no default constructor parameters. Apparently -Werror doesn't flag this as a warning either, so we didn't catch it before
2019-09-12 16:07:41 -04:00
Zac Sweers
71cf745852 Don't re-wrap nullsafe/nonnull adapters if they are already one (#909)
* Don't re-wrap nullsafe/nonnull adapters if they are already one

nullSafe() is often called defensively, which can lead to a lot of layers of wrapping and checking. This avoids that overhead where possible.

* Add tests
2019-09-12 14:14:49 -04:00
Zac Sweers
98092d0727 Update KotlinPoet to 1.3.0 (#915)
* Update to KotlinPoet 1.3.0

* Add hotwords test case

* Use %N for toJson parameter names to ensure escaping
2019-09-12 14:13:23 -04:00
Zac Sweers
1978581b3d Make Rfc3339DateJsonAdapter null-safe (#910)
* Move delegate to private final

* Make Rfc3339DateJsonAdapter null-safe

Resolves #723
2019-09-11 07:22:02 -04:00
Zac Sweers
c820314107 Followups from #896 (#907)
* Simplify parameterProperties and parameterProperties

* Match only on synthetic constructors

* Limit synthetic constructor keeps to kotlin classes
2019-09-11 04:55:24 -04:00
Zac Sweers
ea300997a1 Decouple code gen API from apt and kotlin-metadata (#816)
* Decouple AdapterGenerator from kotlin-metadata

* Decouple AdapterGenerator from javax elements API

* Extract checkIsVisibility helper

* Remove elements and kotlinmetadata from property, constructor, type

* Extract kotlinpoet-only parts to new API package for better separation

* Add mirroring APIs

* Finish extraction of PropertyGenerator to no-elements/no-kotlin-metadata

* Update for new tag API in KotlinPoet

* Update for new tag API in KotlinPoet

* Move rest of elements utils out of API

We should split this up better as metadata.kt has become a dumping ground

* Move type handling to JsonClassCodegenProcessor

* Rebase fix

* Opportunistic idiomatic require()
2019-09-08 21:24:10 -04:00
Zac Sweers
5a4530b7d9 Fast followups from #899 (#901)
Followup fixes from the previous PR
2019-09-08 19:15:02 -04:00
Zac Sweers
619be26e95 Refactor missing/null properties to helper utils instead (#900)
* Create missing property and unexpected null templates

* Fix rebase conflicts
2019-09-08 17:36:58 -04:00
Zac Sweers
329d0e14b0 Invoke defaults constructor in kotlin code gen (#896)
* Add Util#invokeDefaultConstructor

* Add defaultPrimitiveValue

This will be used to initialize required properties for later invocation of the default constructor

* Move isTransient into PropertyGenerator

We will need this in order to know how to invoke constructors even if they have transient parameters

* Add notion of hasLocalIsPresentName to PropertyGenerator

* Switch to using invokeDefaultConstructor for any default property types

* Add code gen versions of default constructor test

* Fix mismatched names

* Use Arrays.copyOf

* Unwrap InvocationTargetException

* Use name allocator

* Rename createMask to createDefaultValuesParametersMask, use it directly

* Opportunistically clean up result variable holder

Only needs to be made if we have non-parameter instances, otherwise we can just return directly

* Fix mask name

* Remove unnecessary mod

* Switch to local lazily-initialized constructor reference

Not working because of some issue in kotlinpoet I don't understand

* Fix named usage

* Clean up debugging dots

* Add proguard/R8 rule for keeping defaults constructor in targets

* Make constructor lookup property private

* Add another defensive dot

* Rework invokeDefaultConstructor to accept vararg args

A little more idiomatic

* Update proguard rules
2019-09-08 17:24:25 -04:00
Zac Sweers
711de52ae1 Clean up warnings in KotlinJsonAdapter (#899)
* Use isNotEmpty()

* Use Metadata import directly

* Tighten visibility of IndexedParameterMap

* Use idiomatic require() checks

Didn't use it on a couple other places where readability would suffer

* Remove unnecessary type arg

* Remove unnecessary curly braces

* Use mutable list rather than allocate new lists

IntelliJ thinks this is a better approach

* Tighten KotlinJsonAdapter property visibility

* Suppress existing extraneous warnings
2019-09-04 21:39:35 -04:00
Jake Wharton
7f720c03c9 Merge pull request #898 from ZacSweers/z/excludeMore
Don't reflect on kotlinx and update doc
2019-09-04 21:12:58 -04:00
Zac Sweers
319d7c8bd5 Don't reflect on kotlinx and update doc
This adds kotlinx to the exclusion list, and also updates the doc on ClassJsonAdapter
2019-09-04 20:58:56 -04:00
Zac Sweers
9db91f2dce Kotlin-reflect QoL APIs (#875)
* Start MoshiKotlinExtensions

* Add Moshi.Builder.add() with reified typeOf

* Use non-shadowing name for addAdapter

* Implement KType.toType()

.javaType isn't implemented

* Don't re-wrap wrapped adapters

* Use new adapter functions in tests to smoke test

* Don't enable uses experimental in kotlin-reflect, only the test

* Use internal leniency API

* Use toType in addAdapter too

* Update error message in test
2019-09-04 20:24:59 -04:00
Jake Wharton
73cc9656ba Merge pull request #876 from ZacSweers/z/rawTypeMessage
Pass raw type rather than possibly-generic type in platform type error
2019-09-04 14:16:56 -04:00
Zac Sweers
6ffb94647b Switch to openjdk8 for travis builds (#885)
* Switch to openjdk8 for travis builds

oraclejdk8 isn't available unless we ust trusty dist, which is also an option

* Update deploy_shapshot.sh
2019-08-07 22:56:03 -04:00
Zac Sweers
25fcbb2637 Improve message and match in tests 2019-07-08 12:40:55 -04:00
Zac Sweers
aea8675252 Pass raw type rather than possibly-generic type in platform type error
We only check the raw type here, so the message should match
2019-07-02 18:22:39 -04:00
Nuno Gomes
687acba760 removed limitation where subtypes should be unique (#856)
* removed limitation where subtypes should be unique

There can be use cases where different type labels should match with the same subtype

* added test for PolymorphicJsonAdapter non unique subtypes
2019-06-20 10:39:19 -05:00
Jesse Wilson
54c44c5570 Merge pull request #871 from ZacSweers/z/kotlin1340
Update to Kotlin 1.3.40
2019-06-19 21:47:41 -04:00
Zac Sweers
6a1a15b974 Update to Kotlin 1.3.40 2019-06-19 20:06:50 -05:00
Jesse Wilson
10ac30b0ed Merge pull request #868 from sullis/maven-compiler-plugin-3.8.1
maven-compiler-plugin 3.8.1
2019-06-19 06:38:45 -04:00
Sean Sullivan
0142e2aaf0 maven-compiler-plugin 3.8.1 2019-06-18 17:00:31 -04:00
Jesse Wilson
f1965f0f46 Merge pull request #821 from Syeberman/sye/covariant-map-values
Support covariant Map values, which are used by Kotlin
2019-06-06 10:49:51 -04:00
Sye van der Veen
7e417840e2 Support covariant Map values, which are used by Kotlin 2019-05-28 10:51:55 -04:00
Zac Sweers
0943ef5a61 Allow custom generators (#847)
* Extract generatedJsonAdapterName to public API for other generators/consumers

* Fix kapt location in tests

* Add IDE-generated dependency-reduced-pom.xml to gitignore

This always bites me

* Add generator property to JsonClass and skip in processor

* Opportunistically fix formatting for generateAdapter doc

* Extract NullSafeJsonAdapter for delegate testing

* Add custom adapter tests

* Allow no-moshi constructors for generated adapters

* Fix rebase issue

* Use something other than nullSafe() for lenient check

This no longer propagates lenient

* Add missing copyrights

* Add top-level class note

* Add note about working against Moshi's generated signature

* Add missing bit to "requirements for"

* Note kotlin requirement relaxed in custom generators

* Style
2019-05-15 20:42:08 -04:00
Zac Sweers
a5020ddb3c Support gradle incremental processing in code gen (#824)
* Support gradle incremental processing in code gen

This adds support for incremental compilation in gradle via incap helper and marking the code gen as `ISOLATING`.

Depends on a newer version of KotlinPoet that has https://github.com/square/kotlinpoet/pull/647

Resolves #589

* Opportunistically update to auto-service 1.0-rc5

Supports incremental compilation and moves annotations to a separate artifact

* 1.2.0 final!

* Mark compiler embeddales as test only
2019-04-17 18:12:11 -07:00
Jesse Wilson
11a547023c Merge pull request #834 from square/jakew/dont-wrap/2019-04-10
Don't wrap between throw and exception type
2019-04-10 08:38:38 -07:00
Jake Wharton
bcb6fd4a4d Don't wrap between throw and exception type 2019-04-10 10:28:30 -04:00
Oliver Bestmann
3994723c3c Use StringBuilder to deduplicate String constants 2019-04-02 11:49:40 -04:00
Zac Sweers
30c973e494 Merge pull request #825 from square/z/kotlin13
Update to Kotlin 1.3.21
2019-03-25 20:00:40 -07:00
Zac Sweers
1c3dc89da1 Update to Kotlin 1.3.21
Resolves #777
2019-03-25 19:44:03 -07:00
Eric Cochran
3e848c0cdd Use KotlinPoet's MemberName for emptySet import. (#818)
* Update to KotlinPoet 1.1.0.

* Use KotlinPoet's MemberName for emptySet import.
2019-03-14 02:57:08 -07:00
Eric Cochran
be6f3eb2af Unconditionally close the peeked JsonReader. (#810)
It doesn't have an effect now, but this is for the future when closing the peeked source also clears buffers.
2019-02-17 17:00:29 -08:00
Eric Cochran
a83a9b79e4 Use peekJson in the DefaultOnDataMismatchAdapter example. (#809) 2019-02-15 17:09:50 -08:00
Eric Cochran
d843950731 Make checkstyle happy on JsonReader column width. (#811) 2019-02-15 17:08:46 -08:00