简体   繁体   English

Java标准注释

[英]Java Standard Annotations

I have recently started reading into annotations. 我最近开始阅读注释。 I deprecated the armStrong() method here and I need to suppress the deprecation warning but wherever I put it it says "unnecessary @SuppressWarnings("deprecation")". 我在这里弃用了armStrong()方法,我需要取消弃用警告,但是无论放到哪里,它都说“不必要的@SuppressWarnings(“弃用”)“。

Can anyone tell me where to place it so that the method is deprecated warning would not come anymore? 谁能告诉我将其放置在何处,以便method is deprecatedmethod is deprecated警告将不再出现?

import java.io.*;
import java.lang.annotation.*;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)

@interface number
{
String arm();
}

public class Ch10LU2Ex4
{  
@Deprecated
@number(arm = "Armstrong number")
public static void armStrong(int n)
 {
    int temp, x, sum = 0;
    temp = n;
    while(temp!=0)
     {
        x = temp%10;
        sum = sum+x*x*x;
        temp = temp/10;
     }
    if(sum==n)
     {
        System.out.println("It is an armstrong number");
     }
    else
    {
        System.out.println("It is not an armstrong number");
    }
 }

public static void main(String[] args) 
  {

    try
    {   
        Ch10LU2Ex4 obj = new Ch10LU2Ex4();
        obj.invokeDeprecatedMethod();
        Method method = obj.getClass().getMethod("armStrong", Integer.TYPE);
        Annotation[] annos = method.getAnnotations();
        for(int i = 0; i<annos.length; i++)
        {
            System.out.println(annos[i]);
        }
    }
    catch(Exception e)
     {
        e.printStackTrace();
     }
  } 
    @SuppressWarnings("deprecation")
    public void invokeDeprecatedMethod()
    {
        try
        {
        System.out.println("Enter a number between 100 and 999:");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int x = Integer.parseInt(br.readLine());
        Ch10LU2Ex4.armStrong(x);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
  }  

Using a deprecated method from another method is what causes a warning. 使用 另一方法已过时的方法是什么原因导致的警告。

A typical use will look like this: 典型用法如下所示:

 @SuppressWarnings("deprecation")
 public void invokeDeprecatedMethod() {
     instanceofotherclass.armStrong(1);
 }

Within the same class, it is assumed that the programmer knows what he's doing. 在同一个类中,假定程序员知道他在做什么。

This is a feature, not a bug . 这是一个功能,而不是错误 You don't need a @SuppressWarnings for a call to a deprecated method of a class from within that class itself , because such calls don't generate a deprecation warning in the first place. 您不需要@SuppressWarnings从该类本身内部调用该类的不赞成使用的方法,因为此类调用首先不会产生不赞成使用的警告。 Calls to the deprecated method from other classes will need the @SuppressWarnings annotation. 其他类调用不推荐使用的方法将需要@SuppressWarnings批注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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