How to use the function implemented in Kotlin Interface introduced in Maven as the default implementation from Java 8

Overview

You can define a default implementation for Kotlin I / F, but you need to set it in advance to use the default implementation from Java side. This time I will write down the setting method.

Premise

Setting method

① Kotlin plug-in settings

Add -Xjvm-default = compatibility to the argument of the part that defines to compile kotlin in pom.xml Example:

pom.xml


<plugin>
	<groupId>org.jetbrains.kotlin</groupId>
	<artifactId>kotlin-maven-plugin</artifactId>
	<version>${kotlin.version}</version>
	<executions>
		<execution>
			<id>compile</id>
			<goals> <goal>compile</goal> </goals>
			<configuration>
				<sourceDirs>
					<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
					<sourceDir>${project.basedir}/src/main/java</sourceDir>
				</sourceDirs>
				<args>
					<arg>-Xjvm-default=compatibility</arg>
				</args>
			</configuration>
		</execution>
		<execution>
			<id>test-compile</id>
			<goals> <goal>test-compile</goal> </goals>
			<configuration>
				<sourceDirs>
					<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
					<sourceDir>${project.basedir}/src/test/java</sourceDir>
				</sourceDirs>
				<args>
					<arg>-Xjvm-default=compatibility</arg>
				</args>
			</configuration>
		</execution>
	</executions>
</plugin>

Describe jvmTarget in properties Example:

pom.xml


<properties>
	<jdk.version>1.8</jdk.version>
	<encoding>UTF-8</encoding>
	<kotlin.version>1.3.60</kotlin.version>
	<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
</properties>

(2) Add @JvmDefault annotation to the method that you want Java to recognize as the default implementation.

That is all.

Recommended Posts

How to use the function implemented in Kotlin Interface introduced in Maven as the default implementation from Java 8
[Java] How to use the hasNext function
[Processing × Java] How to use the function
How to use ArgumentMatchers such as Mockito's any () in Kotlin
How to use classes in Java?
How to use MinIO with the same function as S3 Use docker-compose
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to get the date in java
[Java] How to use the Calendar class
How to use credentials.yml.enc introduced in Rails 5.2
What I don't like when using interface of a function with default arguments in Kotlin from Java
[Java] How to use Thread.sleep to pause the program
How to get Class from Element in Java
How to use the replace () method (Java Silver)
How to write Java String # getBytes in Kotlin?
[Java] How to omit the private constructor in Lombok
How to write Scala from the perspective of Java
[Java] How to extract the file name from the path
Summary of how to implement default arguments in Java
Notes on how to use regular expressions in Java
Summary of how to use the proxy set in IE when connecting with Java
How to get the class name / method name running in Java
How to use the getter / setter method (in object orientation)
How to use Maven to place resource files outside the JAR
How to use trained model of tensorflow2.0 with Kotlin / Java
How to use JSON data in WebSocket communication (Java, JavaScript)
How to create a placeholder part to use in the IN clause
How to store a string from ArrayList to String in Java (Personal)
How to get the longest information from Twitter as of 12/12/2016
How to call and use API in Java (Spring Boot)
How to use Java enums (Enum) in MyBatis Mapper XML
Implement the same function as C, C ++ system ("cls"); in Java
How to derive the last day of the month in Java
How to switch Java in the OpenJDK era on Mac
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
Understand how functional programming was introduced to Java in one shot
How to try Java preview features (such as Java 15 Record) in IntelliJ
To use the "java" command line tool ... How to avoid popping up
How to run a Kotlin Coroutine sample from the command line
[Behavior confirmed in December 2020] How to implement the alert display function
How to solve the unknown error when using slf4j in Java
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~
[Java] How to convert from String to Path type and get the path
kotlin & Java: How to hide the toolbar only for specific fragments
How to get the length of an audio file in java
How to increment the value of Map in one line in Java