Use of deprecated and suppress annotations in kotlin basic learning

preface

In Java, some methods are usually annotated, but many annotations have no problems in Java code. If you switch to kotlin, there will be some problems if you continue to use these annotations. This article mainly compares some common Java annotations with kotlin annotations.

Deprecated

In Java methods, if you need to discard a method, just add @ deprecated to the method money. For example:

@Deprecated
public void test(){

}

However, if this annotation is used directly in kotlin code, there is a problem:

Deprecated

Looking at the source code, it is found that the annotation deprecated has been strengthened in kotlin:

@Target(CLASS,FUNCTION,PROPERTY,ANNOTATION_CLASS,CONSTRUCTOR,PROPERTY_SETTER,PROPERTY_GETTER,TYPEALIAS)
@MustBeDocumented
public annotation class Deprecated(
 val message: String,val replaceWith: ReplaceWith = ReplaceWith(""),val level: DeprecationLevel = DeprecationLevel.WARNING
)

There are three parameters of deprecated in the source code, of which the second and third parameters have default values, and the first parameter message does not. In other words, message information must be brought when you want to use it normally. The normal writing method is as follows:

@Deprecated("xxx")
fun testKt(){

}

Three parameter descriptions:

Message: a message that explains the deprecation and suggests the use of an alternative API

Level: specifies how deprecated element usage is reported in code. Level has three enumerations

public enum class DeprecationLevel {
/** Usage of the deprecated element will be reported as a warning. */
WARNING,/** Usage of the deprecated element will be reported as an error. */
ERROR,/** Deprecated element will not be accessible from code. */
HIDDEN
}

Replacewith: Specifies the code snippet that can be used to replace deprecated functions, properties, or classes. don't get it? One picture:

replace

Suppresswarnings and suppress

In Java code, if you need to eliminate some compile time warnings, you usually use @ suppresswarnings ("XXX") to solve them. In kotlin, you can't use this annotation. Instead, you need to use the built-in @ suppress ("XXX") band in kotlin.

For example, during SDK development, there are usually many external APIs. Under normal circumstances, when they are not called, there will be the following similar warnings:

hello

It's hard for people with obsessive-compulsive disorder. In order to eliminate this warning, we can:

@Suppress("unused")
fun hello() {
 println("hello")
}

If it is java code, write it like this:

@SuppressWarnings("unused")
public void hello(){

}

For another example, when some type conversion is required:

hello

To eliminate this warning, we can do the following:

fun unChecked(){
 val list: List<Any> = emptyList()

 @Suppress("UNCHECKED_CAST")
 list as List<String>
}

If it is in Java code, it needs to be written as @ suppresswarnings ("unchecked")

In kotlin, there are many warnings that suppless supports to eliminate. You can view more relevant parameter information through the source code, such as:

hello

summary

In fact, these two annotations are not difficult to use, mainly because they are used to writing java code. After switching to kotlin, many APIs are not familiar and need to be understood and accumulated slowly in the development process. As the saying goes, a good memory is not as good as a bad pen.

Well, the above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>