2013-01-04 79 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