繁体   English   中英

如何使Java语法正确?

[英]How to get the Java syntax correct?

我编译程序后得到一堆警告声明,我知道我可以关闭它们,但我想让它们正确,但我不知道正确的方法来解决它们,因为它们中的一些抱怨方式,像这样:

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())); } });

你应该阅读泛型。 Angelika Langer的网站是一个很好的资源。

我认为这一定数量是不可避免的。 我喜欢将未经检查的强制警告本地化到例程中唯一的行,然后使用@SuppressWarnings(“unchecked”)来抑制该答案。 这样,警告抑制不会隐藏我方法中其他地方的任何无意的发生。 这在泛型中很常见,特别是将原始集合(例如List)与类型集合(例如List)混合。 如果您使用的是使用原始集合的库,则很难完全避免这些警告。

泛型绝对是首选的解决方案,例如:

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

Java的验证系统非常类似于它,甚至可能偶尔会警告你,如果没有相当严重的部分重新实现,就无法解决这些问题。

但是,如果您真的对此感兴趣,那么您应该谷歌“Java静态代码分析”,有一些很棒的工具可以检查您的代码,并可以让您对问题进行更深入的分析。

我相信你会使用静态分析工具比尝试清理java警告更进一步。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM