tags:

views:

2238

answers:

3

I get a bunch of warning statements after I compiled my programs, I know I can switch them off, but I want to get them right, and yet I don't know the correct ways to fix them, for some of them it complains either way, like this one :

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

Any help please ? Here are some of them :

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())); } });
+8  A: 

You should read up on generics. An excellent resource is Angelika Langer's site.

Kees de Kooter
+1  A: 

I think a certain amount of this is unavoidable. I like to localize the unchecked cast warning to where it's the only line in the routine, then suppress that answer with @SuppressWarnings("unchecked"). That way, the warning suppression doesn't hide any inadvertent such occurrences elsewhere in my method. This is common with generics, particularly mixing raw collections (eg List) with typed collections (eg List). If you're using libraries that use raw collections, it's really hard to avoid these warnings altogether.

Carl Manaster
Or make it so that the unchecked cast occurs in initializing a local variable, then just suppress the warning on that line.
Michael Myers
It makes me feel better on one hand, because it's not all my fault, but on the other hand it also makes me feel annoyed, because Java can't make sure it understands your intention. Thanks !
Frank
A: 

Java's validation system is quite lint-like, it may even occasionally warn you about things that can't be fixed without a pretty serious re-implementation of a section.

If you are really interested, however, you should google "Java static code analysis", there are some amazing tools out there that can examine your code and can give you a much more in-depth analysis of the problems.

I believe you'll make a lot more progress with a static analysis tool than trying to clean java warnings.

Bill K
Thanks for the advice, I use Netbeans, I wonder if it has such functions, I definite wish it does !
Frank
Try Findbugs (netbeans plugin here https://sqe.dev.java.net/updatecenters/nbheaven-updatecenter.xml). It give you a comprehensive (most of the time ;)) description of the problem, and advise to fix it.
Olivier
Yes, Findbugs is supposed to be excellent. I haven't used it and couldn't remember the name or I would have posted it.
Bill K