Skip to content

Improve Kotlin support #5537

Closed
Closed
@philwebb

Description

@philwebb
Member

Placeholder issue for things we can do to improve Kotlin support

@value

Using @Value("${thing}") is a pain because you need to escape $ (e.g. @Value("\${thing}"). Perhaps a different key like @Value("#{thing}").

Open Classes

Open classes are a pain, can we use a classloader to remove final.
Also all @Bean methods

perhaps we can use a custom bean classloader

Default constructor arguments with @Autowired

Give a constructor with a default param:

open class Foo @Autowired constructor(val restTemplate: RestOperation = NoOpRestOperation());

Kotlin will create multiple constructors, this makes Spring fall over because there's multiple annotated constructors. A method to be smart about which to pick would be nice.

Main method

@JvmStatic is needed for main methods. Check if we can find them automatically. Also the name of the class is a bit mad ThePackageKt so perhaps we can replace it with something nice.

Add Kotlin Module

Look at Jackson for an example.

Activity

wilkinsona

wilkinsona commented on Mar 31, 2016

@wilkinsona
Member

Gradle plugin

Our plugin applies the Java plugin automatically. That doesn't make sense when you're building a pure Kotlin project and leads to unnecessary noise as the Java-related tasks are still run as part of the build:

$ ./gradlew classes
:compileKotlin
:compileJava UP-TO-DATE
:processResources
:classes
philwebb

philwebb commented on Mar 31, 2016

@philwebb
MemberAuthor
philwebb

philwebb commented on Mar 31, 2016

@philwebb
MemberAuthor

If looks like @Value might already support #{...} expressions as an alternative to ${...}.

jkschneider

jkschneider commented on Apr 2, 2016

@jkschneider
Contributor

Spring MVC

Reified extension functions for the RestOperations interface. There are probably similar better-typing gains to be made elsewhere.

jkschneider

jkschneider commented on Apr 2, 2016

@jkschneider
Contributor

Spring HATEOAS

Extension functions to improve the transformation of objects and collections of objects into Resource and PagedResources, respectively. Also better link generation for Resource and PagedResources.

sdeleuze

sdeleuze commented on Apr 2, 2016

@sdeleuze
Contributor

Thanks for creating this issue @philwebb 👍, I am especially interested by removing the need for the open qualifier.

Maybe we could discuss Monday with the team if they would be ok to automatically register Kotlin Jackson module in Jackson2ObjectMapperBuilder if it is in the classpath like we do for various other modules (I could add this to Spring Framework 4.3 easily).

@jkschneider Good idea about Spring HATEOAS. Be aware that I also plan to experiment on allowing to specify relations thanks to annotations + regular data classes, see spring-projects/spring-hateoas#401 for more details. Nothing specific to Kotlin, but that will make it easier to deal with data classes in both Kotlin and Java.

sdeleuze

sdeleuze commented on Apr 4, 2016

@sdeleuze
Contributor

@philwebb SPR-14108 + related pull request for Jackson Kotlin module automatic registration submitted for inclusion in Spring Framework 4.3.

sdeleuze

sdeleuze commented on Apr 4, 2016

@sdeleuze
Contributor

I have just merged Jackson Kotlin module automatic registration, so it will be available in 4.3.0.RC1 upcoming release tomorrow and then in Spring Boot 1.4.

About @Value I did a quick test some times ago, and I dont remember # was supported out of the box as a dropin replacement for $, I think only @Value("#{config['foo']}) and @Value("\${foo}") were. That's why I described in this StackOverflow answer how to customize ${...} to something else. Is there a better solution available out of the box?

About extensions, Kotlin Primavera contains some examples of Kotlin extensions applied to Spring (I am sure there a lot of others to imagine even more useful).

I have tried to reproduce the "Default constructor arguments with @Autowired" described above in this test project but did not succeed. Any chance someone send a PR on this test project to show how to reproduce this issue?

You can find bellow some issues from Kotlin bugtracker related to Spring:

jkschneider

jkschneider commented on Apr 4, 2016

@jkschneider
Contributor

@lkogler - Hadi Hariri mentioned you as a Spek contributor. Thought you might be interested in this effort as well.

jkschneider

jkschneider commented on Apr 4, 2016

@jkschneider
Contributor

@sdeleuze Sent a PR for "Default constructor arguments with @Autowired". Adding required=false was a valid workaround as of some pre-1.0 release, but no longer works.

Thanks for putting together all of the links!

@philwebb or @sdeleuze if you could create a spring-kotlin project in spring-projects and give me commit access, I'll start pouring in the work I have here.

sdeleuze

sdeleuze commented on Apr 4, 2016

@sdeleuze
Contributor

@jkschneider As a first step I have created the spring-kotlin in my own sdeleuze account. Maybe we could both of us (and any interested people) contribute anything useful for building Spring + Kotlin applications, including documentation. When the project will be mature enough, we will evaluate moving it to https://github.com/spring-projects.

I already gave you the push rights, please sign the CLA as explained in the README. I will setup the build process using http://build.spring.io and publish the snapshots in https://repo.spring.io/snapshot/ as soon as we have the first bits committed.

Does it sound ok for you?

spencergibb

spencergibb commented on Apr 4, 2016

@spencergibb
Member

@sdeleuze He's signed the CLA for spring cloud previously.

jkschneider

jkschneider commented on Apr 4, 2016

@jkschneider
Contributor

@sdeleuze thanks, sounds like a plan!

nfrankel

nfrankel commented on Apr 4, 2016

@nfrankel

I'm interested as well, and have just signed (again?) the CLA - just to be sure.

37 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cemo@wendigo@mikegehard@sdeleuze@snicoll

        Issue actions

          Improve Kotlin support · Issue #5537 · spring-projects/spring-boot