DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Metal and the Simulated Annealing Algorithm
  • Reactive Kafka With Spring Boot
  • Why You Should Migrate Microservices From Java to Kotlin: Experience and Insights
  • How To Create a Homescreen Widget in Android

Trending

  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Teradata Performance and Skew Prevention Tips
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  1. DZone
  2. Coding
  3. Languages
  4. @Deprecated in Kotlin

@Deprecated in Kotlin

This quick primer on the @Deprecated annotation shows how it works with Kotlin code and how you can customize the deprecation level to suit your needs.

By 
Nakul Mishra user avatar
Nakul Mishra
·
Oct. 25, 17 · Tutorial
Likes (17)
Comment
Save
Tweet
Share
34.7K Views

Join the DZone community and get the full member experience.

Join For Free

In Kotlin, we can use the @Deprecated annotation to mark a class, function, property, variable, or parameter as deprecated. What makes this annotation interesting is not only the possibility of deprecation, but also of providing replacements with all the necessary imports. This comes in handy for clients upgrading source code without digging into documentation. Apart from this, we can also control the level of depreciation. Let's see it in action.

To mark a function as deprecated, all you need to do is annotate it and provide the deprecation message. Thanks to Kotlin's Java interoperability, we can use this Annotation within existing Java code bases.

@Deprecated(message = "we are going to replace with StringUtils.isEmpty)
public static boolean isEmpty(String input) {
    return input.equals("");
}

Client view when invoking the method

It would be nice if we could also provide the replacement code as part of an IDE suggestion to help a client to upgrade their code base. @ReplaceWith aims to achieve exactly that. It takes two arguments :

  1. expression: method call to replace with.
  2. imports: necessary import to make an expression compile.

E.g.

@Deprecated(message = "we are going to replace with StringUtils.isEmpty",
        replaceWith = @ReplaceWith(
                expression = "StringUtils.isEmpty(input)",
                imports = {"org.apache.commons.lang3.StringUtils"})
)
public static boolean isEmpty(String input) {
    return input.equals("");
}

IDE can now suggest the replacement method

Depending on the use case, we can also tweak the deprecation level for an immediate upgrade. Supported levels are:

  1. Warning: will result in a compilation warning. This is the default level.
  2. Error: will result in a compilation error.
  3. Hidden: will be hidden in the code base (non-existing for the caller), but will be present at the bytecode level. This is useful in scenarios where we want to pretend the method doesn't exist at the source code level but still want to keep it at bytecode level (for compatibility reasons).

E.g. DeprecationLevel.ERROR

@Deprecated(level = DeprecationLevel.ERROR,
        message = "we are going to replace with StringUtils",
        replaceWith = @ReplaceWith(
                expression = "StringUtils.isEmpty(input)",
                imports = {"org.apache.commons.lang3.StringUtils"})
)
public static boolean isEmpty(String input) {
    return input.equals("");
}

Deprecation level – Error
E.g. DeprecationLevel.HIDDEN 

@Deprecated(level = DeprecationLevel.HIDDEN,
        message = "we are going to replace with StringUtils",
        replaceWith = @ReplaceWith(
                expression = "StringUtils.isEmpty(input)",
                imports = {"org.apache.commons.lang3.StringUtils"})
)
public static boolean isEmpty(String input) {
    return input.equals("");
}

Deprecation Level – Hidden


Kotlin (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Metal and the Simulated Annealing Algorithm
  • Reactive Kafka With Spring Boot
  • Why You Should Migrate Microservices From Java to Kotlin: Experience and Insights
  • How To Create a Homescreen Widget in Android

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!