我编译程序后得到一堆警告声明,我知道我可以关闭它们,但我想让它们正确,但我不知道正确的解决方法,对于其中一些它会以任何一种方式抱怨,就像这样:
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())); } });
答案 0 :(得分:8)
你应该阅读泛型。一个很好的资源是Angelika Langer's site。
答案 1 :(得分:2)
我认为其中一定数量是不可避免的。我喜欢将未经检查的强制警告本地化到例程中唯一的行,然后使用@SuppressWarnings(“unchecked”)来抑制该答案。这样,警告抑制不会隐藏我方法中其他地方的任何无意的发生。这在泛型中很常见,特别是将原始集合(例如List)与类型集合(例如List)混合。如果您使用的是使用原始集合的库,那么很难完全避免这些警告。
答案 2 :(得分:1)
泛型绝对是优选的解决方案,例如:
Class<? extends SomeClass> cl = inst.getClass();
答案 3 :(得分:0)
Java的验证系统非常类似于它,甚至可能偶尔会警告你,如果没有相当严重的部分重新实现,就无法解决这些问题。
如果您真的对此感兴趣,那么您应该谷歌“Java静态代码分析”,有一些很棒的工具可以检查您的代码,并可以为您提供更深入的问题分析。
我相信使用静态分析工具会比尝试清除java警告更进一步。