2009-04-23 86 views
3

編譯我的程序後,我得到了一堆警告語句,我知道我可以將它們關閉,但我想讓它們正確,但我不知道修復它們的正確方法,對於其中的一些,抱怨,無論如何,像這樣一個:如何獲得Java語法正確?

BS=(TreeSet)B.next() 
BS=(TreeSet<Object>)B.next() 

任何幫助嗎?這裏有一些:

170: warning: [unchecked] unchecked call to getMethod(java.lang.String,java.lang.Class<?>...) as a member of the raw type java.lang.Class 
     Method createMethod=factory.getMethod("create"+currentValue, parameters); 
--------------------------------------------------------------------------------------------------------------------------------- 
168  Class factory=Class.forName("net.sourceforge.barbecue.BarcodeFactory"); 
169  Class [] parameters={ "".getClass() }; 
170  Method createMethod=factory.getMethod("create"+currentValue, parameters); 
======================================================================================================= 
76: warning: [unchecked] unchecked conversion 
found : java.util.Vector[] 
required: java.util.Vector<Stock_Region_Element>[] 
    static Vector<Stock_Region_Element> Region[]=new Vector[Nm_Lib.Region_Num]; 
--------------------------------------------------------------------------------------------------------------------------------- 
76  static Vector<Stock_Region_Element> Region[]=new Vector[Nm_Lib.Region_Num]; 
======================================================================================================= 
1365: warning: [unchecked] unchecked cast 
found : java.lang.Object 
required: java.util.Vector<Contact_Info_Entry> 
     Loaded_Contact_Info_Entry_Vector=(Vector<Contact_Info_Entry>)XML_Decoder.readObject(); 
--------------------------------------------------------------------------------------------------------------------------------- 
1358 Vector<Contact_Info_Entry> Loaded_Contact_Info_Entry_Vector=new Vector<Contact_Info_Entry>(); 
1359  
1360 if (new File(A_Path).exists()) 
1361  try 
1362  { 
1363//  Out("A_Path="+A_Path); 
1364  XML_Decoder=new XMLDecoder(new BufferedInputStream(new FileInputStream(A_Path))); 
1365  Loaded_Contact_Info_Entry_Vector=(Vector<Contact_Info_Entry>)XML_Decoder.readObject(); 
======================================================================================================= 
1902: warning: [unchecked] unchecked cast 
found : java.lang.Object 
required: java.util.Vector<java.awt.font.TextLayout> 
     if (textToUse == FILE_TEXT) backupLineBreakTLs=(Vector<TextLayout>) lineBreakTLs.clone(); 
--------------------------------------------------------------------------------------------------------------------------------- 
1692 private Vector<TextLayout> lineBreakTLs=null; 
1693 for (int i=0; i < fileText.length; i++) 
1694 { 
1695  AttributedString as=new AttributedString(fileText[i],g2.getFont().getAttributes()); 
1696  LineBreakMeasurer lbm=new LineBreakMeasurer(as.getIterator(),g2.getFontRenderContext()); 
1697  while (lbm.getPosition() < fileText[i].length()) lineBreakTLs.add(lbm.nextLayout((float) w)); 
1698 } 
...... 
1901 Vector<TextLayout> backupLineBreakTLs=null; 
1902 if (textToUse == FILE_TEXT) backupLineBreakTLs=(Vector<TextLayout>) lineBreakTLs.clone(); 
======================================================================================================= 
143: warning: [unchecked] unchecked conversion 
found : java.util.TreeSet 
required: java.util.TreeSet<java.lang.Object> 
      BS=(TreeSet)B.next(); 
--------------------------------------------------------------------------------------------------------------------------------- 
139   TreeSet<Object> Sub_Set=new TreeSet<Object>(TreeSet_Order); 
140   Sub_Set.add(First); 
141   if (Debug) System.out.println("=7.20= Sub_Set="+Sub_Set); 
142   TreeSet<Object> BS=new TreeSet<Object>(TreeSet_Order); 
143   BS=(TreeSet)B.next(); 
======================================================================================================= 
813: warning: [unchecked] unchecked cast 
found : java.lang.Object 
required: java.util.Vector<java.lang.Object> 
    Vector<Object> dataRow=(Vector<Object>)rows.elementAt(row); 
--------------------------------------------------------------------------------------------------------------------------------- 
813 Vector<Object> dataRow=(Vector<Object>)rows.elementAt(row); 
======================================================================================================= 
199: warning: [unchecked] unchecked cast 
found : java.lang.Object 
required: java.util.Vector<java.lang.String> 
     comboModel=(Vector<String>)(ois.readObject()); 
--------------------------------------------------------------------------------------------------------------------------------- 
19 private static Vector<String> comboModel; 
...... 
199  comboModel=(Vector<String>)(ois.readObject()); 
======================================================================================================= 
71: warning: [unchecked] unchecked conversion 
found : LoadLibraryAction 
required: java.security.PrivilegedAction<T> 
    static { java.security.AccessController.doPrivileged(new LoadLibraryAction("jpeg")); } 
71: warning: [unchecked] unchecked method invocation: <T>doPrivileged(java.security.PrivilegedAction<T>) in java.security.AccessController is applied to (LoadLibraryAction) 
    static { java.security.AccessController.doPrivileged(new LoadLibraryAction("jpeg")); } 
--------------------------------------------------------------------------------------------------------------------------------- 
70 // load the jpeg lib when created. 
71 static { java.security.AccessController.doPrivileged(new LoadLibraryAction("jpeg")); } 
======================================================================================================= 
184: warning: [unchecked] unchecked cast 
found : java.lang.Object 
required: java.util.Vector<java.lang.Object> 
     trans_frame.Events_Vector=(Vector<Object>)s.readObject(); 
--------------------------------------------------------------------------------------------------------------------------------- 
183  ObjectInputStream s=new ObjectInputStream(in); 
184  trans_frame.Events_Vector=(Vector<Object>)s.readObject();  
======================================================================================================= 
3473: warning: [unchecked] unchecked call to getConstructor(java.lang.Class<?>...) as a member of the raw type java.lang.Class 
     java.lang.reflect.Constructor constructor=cl.getConstructor(new Class[] {String.class});  // get the constructor with one parameter 
--------------------------------------------------------------------------------------------------------------------------------- 
3473  java.lang.reflect.Constructor constructor=cl.getConstructor(new Class[] {String.class});  // get the constructor with one parameter 
======================================================================================================= 
3476: warning: [unchecked] unchecked call to getMethod(java.lang.String,java.lang.Class<?>...) as a member of the raw type java.lang.Class 
     java.lang.reflect.Method objMethod=cl.getMethod(Method_Name,arguments);      // get the method 
--------------------------------------------------------------------------------------------------------------------------------- 
3475  Class arguments[]=new Class[] { };               // the method has no argument 
3476  java.lang.reflect.Method objMethod=cl.getMethod(Method_Name,arguments);      // get the method 
======================================================================================================= 
486: warning: [unchecked] unchecked call to getConstructor(java.lang.Class<?>...) as a member of the raw type java.lang.Class 
       Constructor ctor=cls.getConstructor(new Class[]{String.class}); 
--------------------------------------------------------------------------------------------------------------------------------- 
485    Class cls=Class.forName(loader); 
486    Constructor ctor=cls.getConstructor(new Class[]{String.class}); 
======================================================================================================= 
476: warning: [unchecked] unchecked method invocation: <T>doPrivileged(java.security.PrivilegedAction<T>) in java.security.AccessController is applied to (<anonymous java.security.PrivilegedAction>) 
    JarClassLoader bootLoader=(JarClassLoader)AccessController.doPrivileged(
--------------------------------------------------------------------------------------------------------------------------------- 
476 JarClassLoader bootLoader=(JarClassLoader)AccessController.doPrivileged(
======================================================================================================= 
224: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T>) in java.util.Collections is applied to (java.util.List<Sorted_List_Model.SortedListEntry>) 
     Collections.sort(sortedModel); 
--------------------------------------------------------------------------------------------------------------------------------- 
24 private List<SortedListEntry> sortedModel; 
105 sortedModel=new ArrayList<SortedListEntry>(size); 
224  Collections.sort(sortedModel); 
======================================================================================================= 
325: warning: [unchecked] unchecked conversion 
found : java.util.List 
required: java.util.List<? extends java.lang.Comparable<? super Sorted_List_Model.SortedListEntry>> 
     insertionPoint=Collections.binarySearch((List)sortedModel,entry); 
--------------------------------------------------------------------------------------------------------------------------------- 
320 private int findInsertionPoint(SortedListEntry entry) 
321 { 
322 int insertionPoint=sortedModel.size(); 
323 if (sortOrder != SortOrder.UNORDERED) 
324 { 
325  insertionPoint=Collections.binarySearch((List)sortedModel,entry); 
======================================================================================================= 
361: warning: [unchecked] unchecked call to compare(T,T) as a member of the raw type java.util.Comparator 
     int comparison=comparator.compare(thisElement, thatElement); 
--------------------------------------------------------------------------------------------------------------------------------- 
348 public int compareTo(Object o) 
349 { 
350  // retrieve the element that this entry points to in the original model 
351  Object thisElement=unsortedModel.getElementAt(index); 
352  SortedListEntry thatEntry=(SortedListEntry)o; 
353  // retrieve the element that thatEntry points to in the original model 
354  Object thatElement=unsortedModel.getElementAt(thatEntry.getIndex()); 
355  if (comparator instanceof Collator) 
356  { 
357  thisElement=thisElement.toString(); 
358  thatElement=thatElement.toString(); 
359  } 
360  // compare the base model's elements using the provided comparator 
361  int comparison=comparator.compare(thisElement, thatElement); 
======================================================================================================= 
3201: warning: [unchecked] unchecked method invocation: <T>sort(T[],java.util.Comparator<? super T>) in java.util.Arrays is applied to (java.io.File[],<anonymous java.util.Comparator>) 
    Arrays.sort(files,new Comparator() { public int compare(final Object o1,final Object o2) { return By_Ascending_Date?new Long(((File)o1).lastModified()).compareTo(new Long(((File) o2).lastModified())):new Long(((File)o2).lastModified()).compareTo(new Long(((File) o1).lastModified())); } }); 
--------------------------------------------------------------------------------------------------------------------------------- 
3197 public static File[] Get_File_List_From_Dir_By_Date(String Dir,final boolean By_Ascending_Date) 
3198 { 
3199 if (!new File(Dir).isDirectory()) return null; 
3200 File files[]=new File(Dir).listFiles(); 
3201 Arrays.sort(files,new Comparator() { public int compare(final Object o1,final Object o2) { return By_Ascending_Date?new Long(((File)o1).lastModified()).compareTo(new Long(((File) o2).lastModified())):new Long(((File)o2).lastModified()).compareTo(new Long(((File) o1).lastModified())); } }); 
+4

+1不使用SuppressWarnings! – erickson 2009-04-23 20:13:39

+1

不要使用Vector和數組。 – erickson 2009-04-23 20:24:53

回答

2

我認爲這是不可避免的。我喜歡將未經檢查的強制轉換警告本地化爲例程中唯一的行,然後用@SuppressWarnings(「unchecked」)來壓制此答案。這樣,警告抑制不會在我的方法的其他地方隱藏任何無意的此類事件。這在泛型中很常見,尤其是混合原始集合(例如List)和類型集合(例如List)。如果您使用的是使用原始集合的庫,那麼完全避免這些警告真的很難。

+0

或者讓它在初始化局部變量時發生未經檢查的強制轉換,然後在該行禁止該警告。 – 2009-04-23 19:57:06

0

Java的驗證系統非常纖細,它甚至可能偶爾會提醒您,如果沒有相當嚴重的重新實現某個部分,無法修復這些問題。

但是,如果您真的有興趣,您應該谷歌「Java靜態代碼分析」,有一些驚人的工具可以檢查您的代碼,並可以給你一個更深入的問題分析。

我相信你會用靜態分析工具取得更多進展,而不是嘗試清理java警告。

1

仿製藥肯定是最好的解決方案,例如:

Class<? extends SomeClass> cl = inst.getClass();