简体   繁体   中英

Suppress “Use property access syntax” for method or class

In my project, I have a delegate(written in Java) for fetching data by RPC, most of it methods start with 'get' and some require no parameters, so Kotlin suggests to replace calls to them with property access syntax while they are not fulfilling property semantics (well, because they perform network requests), so I'd like to suppress all such inspections for all these methods usages by default and not in place of every call (yes, I'm aware of @Suppress annotation for blocks, and it works exactly opposite to what I need).

Is there any solution for this except renaming them so they won't start with 'get'?

I suppose that you use IntelliJ IDEA.

You can go to Preferences, Editor, Inspection, Kotlin, Accessor call that can... and uncheck it. (Or type property syntax in search filter).

This will disable this warning on your whole project. You may also change the severity to something else that suit your needs.

EDIT : You can use the @Suppress annotation.

Suppose you have a Java class named A exposing methods:

  • getA() this one is a regular Java bean access,
  • getB() this one is a network call for example.

You can write:

fun main(args: Array<String>) {
    val a = A()
    println(a.a)
    @Suppress("UsePropertyAccessSyntax")
    print(a.getB())
}

@Suppress can be used at several levels. See Documentation .

IntelliJ IDEA has a nice help to suppress those warnings. 想法屏幕截图

EDIT2 :
Disclaimer : There is an other way to do that, but I will not use it myself as it is ugly and I prefer having a warning than using it.

You can use Kotlin extensions and rename your network getters like this.

The Java class:

public class A {
    private int a;

    int getA() {
        return a;
    }

    A _getB() {   // Was getB()
        return this;
    }
}

The Kotlin file:

fun A.getB() : A {       // Kotlin extension
    return this._getB()
}

fun main(args: Array<String>) {
    val a = A()
    println(a.a)
    print(a.getB()) // No warning here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM