Unchecked or Unsafe Operations Message

If you’ve started writing your own JUnit test cases for your problems in ClassCube you may have come across the following message on a dialog box.

Dialog with unsafe or unchecked operations message

It’s a bit confusing for students because they see that their tests are successful, but that there was also some extra output. And that output looks like an error message.

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details. 

What Java is telling you is that there might be an error, but it’s not bad enough to stop compiling.

What causes it?

When Java compiles it looks through your code and tries to find any errors during the compile. Sometimes you’ll do things like forget a semicolon or have extra parenthesis and Java will fail to build. Sometimes Java finds something that might be an error, but isn’t bad enough to warrant failing the compilation.

Let’s look at an example.

ArrayList list = new ArrayList();

In Java 7 and up this will trigger the unchecked or unsafe operations message. The “right” way to code this would be to specify the data types inside the ArrayList.

ArrayList<String> list = new ArrayList<>(); 

How to fix it?

First way is to no do something that triggers the message like the example above. Normally, this will probably come from your test case file. But sometimes it comes from your student code, which you can’t do all that much about.

Or, you can ignore it.

If you’re compiling the code directly you can add -Xlint:unchecked to the javac command line and the messages won’t pop up.

If you’re using a tool, like ClassCube, where you don’t have direct control over the command that’s used to compile code you can’t just add a command line flag. So what you can do is put a flag in your code that does the same thing.

class ChickenTest {
  // Whatever unit test code you need

The line just above the class signature will tell JavaC to not display any unchecked warnings from within the class. You also can use the same @SuppressWarnings("unchecked") above method signatures if you only want to ignore the check in specific methods.

Why do we have to?

Admittedly, this seems like something that something we could take care of for you. And, we considered adding -Xlint:unchecked to the command that compiles your students’ code.

We decided against doing it though.

First, this should only show up when you build full JUnit test files. If you’re doing simple tests cases this is already added to the test cases that are built.

We left turning on the suppress flag to you in this case because we assumed there may be cases where you want your students to know about these messages. It might be something you want to teach your students how to not have the error message. That way you can actively turn it off. If we did it automatically there would be no way for you to turn it back on.

You might also see this pop up when using output matching. In that case though, it’s your student’s code that’s triggering the message. In this case it’s a good teaching moment between you and your student to explain why the messages are coming up.

Does it matter?

Not really.

If you’re using unit tests and this message pops up, it doesn’t affect the grading. If you look at the screenshot on the top of this post you’ll see that there’s still the message that all tests passed. It will still show up as completely passed even though that message shows up.

In most cases you can just ignore it.