简体   繁体   中英

How do I recompile with -Xlint? Jgrasp

My program appears to work to my liking. However, when I compile it I get this message:

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

What can I do to identify the unsafe operations with -Xlint, or what in program is causing this message? I'm thinking it has something to do with my Node class..?

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

If you're compiling on the command line (ie javac Program.java ), you just have to add the -Xlint:unchecked parameter to have it print out the warnings for you:

javac Program.java -Xlint:unchecked

That should point out the problem spots to you. But, as @DavidWallace mentioned in a comment, you should consider revising your use of generics to be a bit more clear -- that might reveal your issue to you even without using the -Xlint parameter.

If your classes should really only deal with String s, then you don't need to include a type parameter at all (right now, in your code, <String> is a type parameter, representing the type you pass in when you use the class -- it doesn't signify that it has to be java.lang.String -- that's why @DavidWallace suggested you use T instead). Here's a good tutorial if you want to brush up on how to use Generics.

Assuming that using "String" as the type parameter is not what causes the actual message (or even if it does), with a quick glance I see:

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

but getNext() returns Node instead of Node<String> (should be Node<T> once you fix the type parameter name).

In jGRASP you can add flags for various commands using "Settings" > "Compiler Settings" > "Workspace" (or "Project..." or "File"). Just add "-Xlint" for "Flags or Args" for the "Compile" command for the Java language.

in jGrasp, the option is under the Settings menu item. Go to Settings-> File/Workspace/Project (pick whichever is applicable to you), and then on the Compiler tag, pick the Flags/Args tab, and then click the box next to Compile and enter -Xlint:unchecked into the text box. Click apply, and on your next build, you will get the messages on what was unchecked.

As others have pointed out, this warning is raised because you are using a raw form of a generic class rather than providing a type parameter to it. -Xlint:unchecked will show you where in your code you did that.

In the build.gradle add

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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