skip to Main Content

I have this Java method:

/**
 * Old method is deprecated.
 * @deprecated Use {@link #newMethod()} instead.
 * @return boolean
 */
@Deprecated
public static boolean oldMethod() {
  return false;
}

and then later on:

boolean result = oldMethod();

In the VS Code editor, the oldMethod() doesn’t show with strikethough like it does in the NetBeans (and other decent IDEs). Oddly, it does show as strikethrough in the popup code completion, but not in the final code.

I also tried calling the method from a different class, and it still has the same issue:

public class Class2 {
  void example() {
    Class1.oldMethod();
  }
}

I’m using the Apache NetBeans language server extension – however I have the same behaviour when using the ones from Oracle, Red Hat and Microsoft. Also, I checked the "Editor: Show Deprecated" setting and it is turned on. Why is it not shown?

2

Answers


  1. The deprecated tag will only apply the strikethrough on the final code if you are accessing the method from a different class/module. The java Deprecated description states, "A module being deprecated does not cause warnings to be issued for uses of types within the module."(Oracle Documentation) For example, if I were to create a class with a deprecated method like you said,

    public class Class1 {
    
    @Deprecated
    public static boolean oldMethod() {
        return false;
    }
    public static void run() {
        boolean result = oldMethod();
    }
    

    then calling oldMethod() would not result in a visual strikethrough. However, if I were to create another class as such,

    public class Class2 {
    
    public static void run() {
        boolean result = Class1.oldMethod();
    }
    

    }

    then it will visually result in a strikethrough: Class1.OldMethod().

    Login or Signup to reply.
  2. I have also tested "editor.showDeprecated": true was not working for me.

    After playing around with VS Code configurations, I have found the solution.

    Solution: Update the .vscode/settings.json of your Java Project to have editor.semanticTokenColorCustomizations that you can use to do the customization on the semantics.

    Screenshot of the settings.json location in your project:

    enter image description here

    settings.json:

    {
      ......
    
      "editor.showDeprecated": true,
      "editor.suggest.showDeprecated": true,
      "editor.semanticTokenColorCustomizations": {
        "enabled": true,
        "rules": {
          "*.deprecated": { "strikethrough": true }
        }
      }
    }
    

    Screenshot of the Java code:

    enter image description here

    As soon as you use @Deprecated annotation, it will put strikethrough.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search