如何使用-Xlint重新编译? Jgrasp

时间:2014-04-05 00:12:26

标签: java compiler-construction compiler-errors

我的程序似乎符合我的喜好。但是,当我编译它时,我收到此消息:

Note: Program.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

如何使用-Xlint识别不安全的操作,或者程序中的内容导致此消息?我认为它与我的Node类有关。?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;

/**
 * An application that reads from a file, enters/deletes in queue and writes output to the file
 */
public class Program {

/**
 * Driver code to test class
 * 
 * @param arguments
 *            Commandline arguments. 1st argument is input file and 2nd argument is output file
 * @throws IOException 
 */
   public static void main(String[] arguments) throws IOException {


    //Queue Object
      MyQueue<String> queue= (new MyQueue<String>());

      String name;
      //reading file
      read(queue,arguments[0]);

      String[] array = { "Offer Person", "Poll Person", "Peek person","Display Queue", "Exit Program"};
      int choice = 0;

        // display loop   
      while (choice != array.length-1) {
         choice = JOptionPane.showOptionDialog(null, // put in center of screen
                  "Press a Button", // message to user
                  "Queue(Line) of People", // title of window
                  JOptionPane.YES_NO_CANCEL_OPTION, // type of option
                  JOptionPane.QUESTION_MESSAGE, // type of message
                  null, // icon
                  array, // array of strings
                  array[array.length - 1]); // default choice (last one)


         if(choice==0)
         {
            //inserting the new name in queue
            name=JOptionPane.showInputDialog(null,"Enter Person's name","Input");
            queue.offer(name);

         }
         else if(choice==1){

                //Display and remove the name which is at front of line
            JOptionPane.showMessageDialog(null, queue.poll() + " is next in line");

         }

         else if(choice==2){

                //Display name which is at front of line
            JOptionPane.showMessageDialog(null, queue.peek() + " is front of the line");

         }

         else if(choice==3){
                //Dispay all the list
            JOptionPane.showMessageDialog(null, queue.toString());


         }
               //JOptionPane.showMessageDialog(null, "Your pressed button #" + choice);
      }
    //calling writing function
      write(queue, arguments[1]);


   }// end of main()

   /**
     * Reads a file
     * @param queue 
     * @param file_name name of file
     */
   public static void read(QueueInterface<String> queue, String file_name) throws IOException{

      try
      {
         String name;
            //creating a buffer reader to read
         BufferedReader br= new BufferedReader(new FileReader(file_name));
         while((name=br.readLine()) != null){
              //putting in the queue
            queue.offer(name);
         }
              //closing buffer reader
         br.close();
      }
      catch(Exception ex)
      {
         System.err.println(ex.getMessage());
      }

   }

   /**
    * Writes the contents of LinkedQueue to the output file at the ned of program
    * @param queue QueueInterface methods
    * @param file_name name of file
    */
   public static void write(QueueInterface<String> queue, String file_name) throws IOException{
      try
      {
         String name;
      //creating a buffer writer to write
         BufferedWriter bw= new BufferedWriter(new FileWriter(file_name));
         while((name=queue.poll()) != null){
          //writin in file
            bw.write(name);
            bw.newLine();

         }
          //closing buffer
         bw.close();
      }
      catch(Exception ex)
      {
         System.err.println(ex.getMessage());
      }
   }



}// end of class

/**
* Interface to be implemented by LinkedQueue
*/
interface QueueInterface<String> 
{
   public boolean empty();
   public boolean offer(String element);
   public String poll();
   public String peek();
}

class Node<String>
{
   private String data;
   private Node nextNode;
   public Node(String dataObject, Node nextNodeObject)
   {
      this.data=dataObject;
      this.nextNode=nextNodeObject;
   }

    /**
    * Gets the next node
    * @return next node
    */
   public Node getNext()
   {
      return nextNode;
   }

    /**
    * Sets the next node of the current node
    * @param nextNodeObject next node to be set as next to the current node
    */
   public void setNext(Node nextNodeObject)
   {
      nextNode=nextNodeObject;
   }

    /**
    * Sets data of the current node
    * @param dataObject data to be inserted in new node
    */
   public void setData(String dataObject)
   {
      this.data=dataObject;
   }

    /**
    * Gets data of the current node
    * @return data of the node
    */
   public String getData()
   {
      return this.data;
   }
}

class LinkedQueue implements QueueInterface<String>
{
   protected Node<String> lastNode=null;

   LinkedQueue() {
   }

    /**
    * Checks if the queue is empty
    * @return true if empty, false if not empty
    */
   public boolean empty() {
      if(lastNode==null)
      {
         return true;
      }
      else
         return false;
   }

    /**
    * Inserts new node in the queue
    * @param element data to be inserted in new node
    * @return true on success
    */
   public boolean offer(String element) 
   {
      Node<String> newLastNode = new Node<String>(element,null);

        //If the LinkedQueue is empty, add the node to the last and point next to itself
      if(empty())
      {
         newLastNode.setNext(newLastNode);
      }
      else
      {
            // Adding to the front of queue and updating next of the last node
         newLastNode.setNext(lastNode.getNext());
         lastNode.setNext(newLastNode);
      }
      lastNode=newLastNode;
      return true;
   }

    /**
    * Removes the first node and returns it
    * @return data at first node
    */
   public String poll() 
   {
        // If queue is empty then return null
      if(empty())
         return null;
      else
      {
         Node<String> frontNode = lastNode.getNext();

            //Check if there will be no node left after polling this one
         if (frontNode == lastNode) 
         {
            lastNode = null;
         }
         else //Remove the first node and update next of the last node
         {
            lastNode.setNext(frontNode.getNext());
         }
         return frontNode.getData();
      }
   }

    /**
    * Returns data of the first node without removing it from the queue
    * @return data at first node
    */
   public String peek() 
   {
      if (empty())
      {
         return null;
      }
      else
      {
         Node<String> frontNode = lastNode.getNext();
         return frontNode.getData();
      }
   }
}

class MyQueue<String> extends LinkedQueue{

   /**
    * Constructor
    * 
    */
   public MyQueue() 
   {
      super();      
   }

   /**
    * Returns a string representation of the object
    * 
    * @return a name on different lines
    */
   public java.lang.String toString() 
   {
        // create a variable to return
      java.lang.String toReturn = "";

        // Traversing the list
      Node<String> frontNode = lastNode.getNext();

      if(empty())    //If queue is  empty
         return "";
      else if(frontNode==lastNode)    //If only one elemtn
      {
         return frontNode.getData().toString();
      }
      else    //More than one element in the queue
      {
         while(frontNode != lastNode)
         {
            toReturn=toReturn+frontNode.getData()+"\n";
            frontNode=frontNode.getNext();
         }
         toReturn= toReturn+frontNode.getData()+"\n";    //Appending data of last node because it will be missed in the loop
      }
      return toReturn;
   }
}

4 个答案:

答案 0 :(得分:2)

如果您要在命令行上进行编译(例如javac Program.java),则只需添加-Xlint:unchecked参数即可为其打印出警告:

javac Program.java -Xlint:unchecked

那应该指出问题所在。但是,正如@DavidWallace在评论中提到的那样,您应该考虑修改您对泛型的使用,以便更清楚 - 即使不使用-Xlint参数,也可能会向您显示您的问题。

如果您的课程真的只涉及String,那么您根本不需要包含类型参数(现在,在您的代码中,<String>是一个类型参数,表示您在使用课程时传递的类型 - 它并不表示必须是java.lang.String - 这就是@DavidWallace建议您使用T代替的原因)。 Here's一个很好的教程,如果你想了解如何使用泛型。

答案 1 :(得分:1)

假设使用&#34; String&#34;因为类型参数不是导致实际消息的原因(或者即使它确实如此),我快速浏览一下:

Node<String> frontNode = lastNode.getNext();

但是getNext()返回Node而不是Node&lt; String&gt; (一旦修复了类型参数名称,应该是Node&lt; T&gt;)。

在jGRASP中,您可以使用&#34;设置&#34;添加各种命令的标记。 &GT; &#34;编译器设置&#34; &GT; &#34;工作区&#34; (或&#34;项目......&#34;或&#34;文件&#34;)。只需添加&#34; -Xlint&#34; for&#34; Flags or Args&#34;对于&#34;编译&#34;用于Java语言的命令。

答案 2 :(得分:0)

在jGrasp中,该选项位于“设置”菜单项下。转到设置 - &gt;文件/工作区/项目(选择适用于您的那个),然后在编译器标签上,选择标志/参数选项卡,然后单击编译旁边的框并输入-Xlint:取消选中文本框。单击“应用”,在下一个版本中,您将收到有关未选中内容的消息。

正如其他人所指出的那样,引发此警告是因为您使用的是泛型类的原始形式,而不是为其提供类型参数。 -Xlint:unchecked将显示您在代码中的位置。

答案 3 :(得分:0)

在build.gradle中添加

allprojects {
gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    }
}

}