Kotlin all open

Kotlin all open DEFAULT
Edit Page

Kotlin has classes and their members by default, which makes it inconvenient to use frameworks and libraries such as Spring AOP that require classes to be . The all-open compiler plugin adapts Kotlin to the requirements of those frameworks and makes classes annotated with a specific annotation and their members open without the explicit keyword.

For instance, when you use Spring, you don't need all the classes to be open, but only classes annotated with specific annotations like or . All-open allows to specify such annotations.

We provide all-open plugin support both for Gradle and Maven with the complete IDE integration.

Note: For Spring you can use the compiler plugin (see below).

Add the plugin artifact to the buildscript dependencies and apply the plugin:

As an alternative, you can enable it using the block:

Then specify the list of annotations that will make classes open:

If the class (or any of its superclasses) is annotated with , the class itself and all its members will become open.

It also works with meta-annotations:

is annotated with the all-open meta-annotation , so it becomes an all-open annotation as well.

Here's how to use all-open with Maven:

Please refer to the "Using in Gradle" section above for the detailed information about how all-open annotations work.

If you use Spring, you can enable the kotlin-spring compiler plugin instead of specifying Spring annotations manually. The kotlin-spring is a wrapper on top of all-open, and it behaves exactly the same way.

As with all-open, add the plugin to the buildscript dependencies:

Or using the Gradle plugins DSL:

In Maven, the plugin is provided by the plugin dependency, so to enable it:

The plugin specifies the following annotations: , , , and . Thanks to meta-annotations support classes annotated with , , , or are automatically opened since these annotations are meta-annotated with .

Of course, you can use both and in the same project.

Note that if you use the project template generated by the start.spring.io service, the plugin will be enabled by default.

All-open compiler plugin JAR is available in the binary distribution of the Kotlin compiler. You can attach the plugin by providing the path to its JAR file using the kotlinc option:

You can specify all-open annotations directly, using the plugin option, or enable the "preset". The only preset available now for all-open is .

The no-arg compiler plugin generates an additional zero-argument constructor for classes with a specific annotation.

The generated constructor is synthetic so it can’t be directly called from Java or Kotlin, but it can be called using reflection.

This allows the Java Persistence API (JPA) to instantiate a class although it doesn't have the zero-parameter constructor from Kotlin or Java point of view (see the description of plugin below).

The usage is pretty similar to all-open.

Add the plugin and specify the list of annotations that must lead to generating a no-arg constructor for the annotated classes.

Or using the Gradle plugins DSL:

Then specify the list of no-arg annotations:

Enable option if you want the plugin to run the initialization logic from the synthetic constructor. Starting from Kotlin 1.1.3-2, it is disabled by default because of and which will be addressed in the future.

As with the kotlin-spring plugin wrapped on top of all-open, kotlin-jpa is wrapped on top of no-arg. The plugin specifies , and no-arg annotations automatically.

That's how you add the plugin in Gradle:

Or using the Gradle plugins DSL:

In Maven, enable the plugin:

As with all-open, add the plugin JAR file to the compiler plugin classpath and specify annotations or presets:

The sam-with-receiver compiler plugin makes the first parameter of the annotated Java "single abstract method" (SAM) interface method a receiver in Kotlin. This conversion only works when the SAM interface is passed as a Kotlin lambda, both for SAM adapters and SAM constructors (see the documentation for more details).

Here is an example:

The usage is the same to all-open and no-arg, except the fact that sam-with-receiver does not have any built-in presets, and you need to specify your own list of special-treated annotations.

Then specify the list of SAM-with-receiver annotations:

Just add the plugin JAR file to the compiler plugin classpath and specify the list of sam-with-receiver annotations:

compiler plugin provides a implementation generator. To learn how to use it, refer to the Android documentation.

Sours: http://kotlin.liying-cn.net/docs/reference/compiler-plugins.html

All-open compiler plugin

Kotlin has classes and their members by default, which makes it inconvenient to use frameworks and libraries such as Spring AOP that require classes to be . The all-open compiler plugin adapts Kotlin to the requirements of those frameworks and makes classes annotated with a specific annotation and their members open without the explicit keyword.

For instance, when you use Spring, you don't need all the classes to be open, but only classes annotated with specific annotations like or . All-open allows to specify such annotations.

We provide all-open plugin support both for Gradle and Maven with the complete IDE integration.

For Spring, you can use the compiler plugin (see below).

Gradle

Add the plugin artifact to the build script dependencies and apply the plugin:

buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" } } apply plugin: "kotlin-allopen"

As an alternative, you can enable it using the block:

plugins { id "org.jetbrains.kotlin.plugin.allopen" version "1.5.31" }

Then specify the list of annotations that will make classes open:

allOpen { annotation("com.my.Annotation") // annotations("com.another.Annotation", "com.third.Annotation") }

If the class (or any of its superclasses) is annotated with , the class itself and all its members will become open.

It also works with meta-annotations:

@com.my.Annotation annotation class MyFrameworkAnnotation @MyFrameworkAnnotation class MyClass // will be all-open

is annotated with the all-open meta-annotation , so it becomes an all-open annotation as well.

Maven

Here's how to use all-open with Maven:

<plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <configuration> <compilerPlugins> <!-- Or "spring" for the Spring support --> <plugin>all-open</plugin> </compilerPlugins> <pluginOptions> <!-- Each annotation is placed on its own line --> <option>all-open:annotation=com.my.Annotation</option> <option>all-open:annotation=com.their.AnotherAnnotation</option> </pluginOptions> </configuration> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-allopen</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin>

Please refer to the Gradle section for the detailed information about how all-open annotations work.

Spring support

If you use Spring, you can enable the kotlin-spring compiler plugin instead of specifying Spring annotations manually. The kotlin-spring is a wrapper on top of all-open, and it behaves exactly the same way.

As with all-open, add the plugin to the build script dependencies:

buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version" } } apply plugin: "kotlin-spring" // instead of "kotlin-allopen"

Or using the Gradle plugins DSL:

plugins { id "org.jetbrains.kotlin.plugin.spring" version "1.5.31" }

In Maven, the plugin is provided by the plugin dependency, so to enable it:

<compilerPlugins> <plugin>spring</plugin> </compilerPlugins> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-allopen</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies>

The plugin specifies the following annotations:

Thanks to meta-annotations support, classes annotated with , , , or are automatically opened since these annotations are meta-annotated with .

Of course, you can use both and in the same project.

Note that if you use the project template generated by the start.spring.io service, the plugin will be enabled by default.

Command-line compiler

All-open compiler plugin JAR is available in the binary distribution of the Kotlin compiler. You can attach the plugin by providing the path to its JAR file using the kotlinc option:

-Xplugin=$KOTLIN_HOME/lib/allopen-compiler-plugin.jar

You can specify all-open annotations directly, using the plugin option, or enable the "preset". The only preset available now for all-open is .

# The plugin option format is: "-P plugin:<plugin id>:<key>=<value>". # Options can be repeated. -P plugin:org.jetbrains.kotlin.allopen:annotation=com.my.Annotation -P plugin:org.jetbrains.kotlin.allopen:preset=spring

Last modified: 28 May 2021

Kotlin compiler optionsNo-arg compiler plugin

Sours: https://kotlinlang.org/docs/all-open-plugin.html
  1. Make racists afraid again amazon
  2. Zx14r swingarm
  3. Voodoo store
  4. 1940s workwear

Using Kotlin

If the dependency and the extension (or the extension) have been added to the project, then Quarkus automatically registers the to the bean (see this guide for more details).

When using Kotlin data classes with you may experience serialization errors that do not occur with the version, despite the Kotlin Jackson Module being registered. This is especially so if you have a more complex JSON hierarchy, where an issue on a lower node causes a serialization failure. The error message displayed is a catch-all and typically displays an issue with the root object, which may not necessarily be the case.

To ensure full-compability with , it is recommended to apply the Jackson annotation, and set a nullable default, as illustrated below. You can automate the generation of Kotlin data classes for your sample JSON using Intellij plugins (such as JSON to Kotlin Class), and easily enable the Jackson annotation and select nullable parameters as part of the auto-code generation.

Sours: https://quarkus.io/guides/kotlin
Inheritance, Overriding using Open Keyword in Kotlin

allopen-annotations

CircleCIReleaseLicense

Why?

If you try to mock class in Kotlin, you will get following error.

This is because all classes are final by default in Kotlin. To fix this problem, you can use one of the following approach.

Option 1 - Make class and member function explicitly.

Option 2 - Use interface or abstract class as super type for class that you want to mock.

Option 3 - Use Mockito's inline mock maker. This works well for unit tests running on local JVM, but for for instrumentation tests, it only works on Android API 28 or above. Remember that inline mock maker has to redefine classes at runtime, which is why it is slower.

Option 4 - Use Kotlin all-open compiler plugin. plugin makes class open not only for tests but also for production code as well. To detect misuse of test only open class via lint, you can use library with plugin.

How?

Add following dependency to your .

implementation 'com.github.annypatel.allopen-annotations:all-open:1.0.1'

If you want class to be opened only for debug build and not for release, then add following dependencies to your .

debugImplementation 'com.github.annypatel.allopen-annotations:all-open:1.0.1' releaseImplementation 'com.github.annypatel.allopen-annotations:none-open:1.0.1'

Configure compiler plugin in you project.

This library provides just two annotations and . is a meta annotation, use it with compiler plugin.

Use annotation on the class that you want to make open for tests.

Lint

  • InheritingTestOnlyOpenType - Detects type that inherits test only open type.

  • TestOnlyOpenType - Detects test only open type which is being inherited.

  • NonMetaAnnotation - Detects usage of non-meta annotation with plugin in build script.

Sours: https://github.com/annypatel/allopen-annotations

All open kotlin

.

Build A Simple Android App With Kotlin

.

You will also be interested:

.



638 639 640 641 642