Java标准注释 [英] Java Standard Annotations

查看:131
本文介绍了Java标准注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始阅读注解。我在这里弃用了armStrong()方法,我需要禁止弃用警告,但是无论我放在哪里,它都会显示不必要的@SuppressWarnings(deprecation)。



可以任何人都告诉我放在哪里,以便方法已被弃用警告不会再来了?

  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 a armstrong number);
}
else
{
System.out.println(It does not a armstrong number);
}
}

public static void main(String [] args)
{

try
{
Ch10LU2Ex4 obj = new Ch10LU2Ex4();
obj.invokeDeprecatedMethod();
方法方法= obj.getClass()。getMethod(armStrong,Integer.TYPE);
注释[] annos = method.getAnnotations(); (int i = 0; i< annos.length; i ++)
{
System.out.println(annos [i]);

}
}
catch(异常e)
{
e.printStackTrace();
}
}
@SuppressWarnings(deprecation)
public void invokeDeprecatedMethod()
{
try
{
系统.out.println(输入介于100和999之间的数字:);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x = Integer.parseInt(br.readLine());
Ch10LU2Ex4.armStrong(x);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}


解决方案

这是功能而不是错误。您不需要一个 @SuppressWarnings 来调用该类本身中类的已弃用方法,因为这样的调用不首先产生废弃警告。来自其他类的已弃用方法的调用将需要 @SuppressWarnings 注释。


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")".

Can anyone tell me where to place it so that the method is deprecated warning would not come anymore?

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();
        }
    }
  }  

解决方案

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. Calls to the deprecated method from other classes will need the @SuppressWarnings annotation.

这篇关于Java标准注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆