2013-01-04 152 views
0

我最近开始阅读注释。我在这里弃用了armStrong()方法,我需要抑制弃用警告,但无论我把它放在哪里,它都会说“不必要的@SuppressWarnings(”deprecation“)”。Java标准注释

任何人都可以告诉我在哪里放置它,以便method 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(); 
     } 
    } 
    } 
+0

你把那个注释放在哪里? –

+0

我把它放在很多地方,但它并没有在任何地方工作,所以我只是把它取出来了,没有帮助。 – Robin

+2

@Robin ..只需在您调用Deprecated方法的地方使用它。 –

回答

3

这是a feature, not a bug。您不需要@SuppressWarnings来调用类中已弃用的方法本身,因为此类调用首先不会生成弃用警告。从其他类调​​用不推荐使用的方法将需要@SuppressWarnings注释。

4

使用弃用的方法从另一个方法是什么原因导致的警告。

一个典型的应用将是这样的:

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

在同一个班级,假设程序员知道自己在做什么。

+0

我改变了代码,但它仍然没有帮助:( – Robin