4

I want developers to be able to use the function but I want them to be aware that it's not intended for use in most cases.

Is there a way to warn other developers about function. Similar to what lint does. Is there something like @Warning("Prefer using #fooo() if possible) annotation for the method? I want to know how to do it java or kotlin. Something similar to @Deprecated but making more sense in my context.

I want my IDE to aware of that warning too, not only compiler.

Elio Lako
  • 1,333
  • 2
  • 16
  • 26
svkaka
  • 3,942
  • 2
  • 31
  • 55
  • 1
    IDE integration is usually always based on the linter, which the compiler usually calls too (unless it has its own). Either way it builds on the same source set, so you'd have to find an IDE that builds on top of the source in a way that makes it able to process it – Zoe Apr 10 '19 at 14:50
  • If the `@Deprecated` annotation (or javadoc tag) does not match your need, try a warning message in the javadoc, this is a good start ! – Highbrainer Apr 10 '19 at 14:51
  • 2
    Possible duplicate of [How to intentionally cause a custom java compiler warning message?](https://stackoverflow.com/questions/1752607/how-to-intentionally-cause-a-custom-java-compiler-warning-message) – Laurence Apr 10 '19 at 15:05
  • @Laurence thanks I will check that – svkaka Apr 10 '19 at 15:09
  • @Highbrainer is there something like warning tag? or you mean just raw text into comment – svkaka Apr 10 '19 at 15:10
  • @svkaka - ya, I know it is not kotlin, but should not be too difficult to do something similar with pure kotlin annotations. – Laurence Apr 10 '19 at 15:12
  • @svakaka AFAIK there is no standard warning tag. Yet you can write your own ! – Highbrainer Apr 11 '19 at 13:15

4 Answers4

1

Here is my take, it might be considered as totally incorrect. You can use @Deprecated to achieve the same result:

@Deprecated("test_only")
fun getNumberFormatterInstanceForTesting(context: Context, localeCode: String): NumberFormat {
        if (!::numberFormat.isInitialized) numberFormat = NumberFormat.getCurrencyInstance(parseLocaleCode(localeCode))
        return numberFormat
}

Some more Example:

@VisibleForTesting
@Deprecated("Test ONLY, DO NOT use in production")
constructor(application: Application) : this(null, application)
Haomin
  • 1,265
  • 13
  • 12
0

Sadly I think thats not possible, but it depends on the IDE that you are using, there is no native java way to have the method display a warning, you have to make changes to the IDE's code analyser to get what you want

Starmixcraft
  • 389
  • 1
  • 14
0

Say you have a foo.bar() Make a delegation instead:

foo.special.bar()
foo.special().bar()

In this way can mark the "special" member, find usages. Refactoring required.

This is a bit like a getter for a specific Business Object:

foo.getBazBO().bar()

An alternative would be to use your own annotation, use Doxygen or some other annotation processing, invest effort in IDE programming. More effort, less immediate results.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thanks, I see that this won't be very easy to fix. I will try to create an issue on Kotlin issue tracker, as I think it can be very helpful. – svkaka Apr 10 '19 at 15:08
0

You can use Kotlin's @Experimental annotation:

https://kotlinlang.org/docs/reference/experimental.html#experimental-status-of-experimental-api-markers

This would make consumers of such functions require explicit opt-in, and you can chose between propagation and non-propagation of the marker.

Note that as of Kotlin 1.3, the @Experimental annotation is itself experimental.

Can_of_awe
  • 1,431
  • 1
  • 11
  • 17