Search code examples
javagenericscompiler-warnings

unsafe operation, unchecked conversion


When I compile the following class (available here, the main method has been added to reproduce the problem in a single class), I get the following compiler warning in IntelliJ or from maven command line:

java: /Users/luigi/var/src/owner/src/test/java/org/aeonbits/owner/multithread/ThreadBase.java uses unchecked or unsafe operations.
java: Recompile with -Xlint:unchecked for details.

Then I added the -Xlint:unchecked to maven to see the details and I got this:

[WARNING] /Users/luigi/var/src/owner/src/test/java/org/aeonbits/owner/multithread/ThreadBase.java:[74,45] unchecked conversion
  required: java.util.List<java.lang.Throwable>
  found:    java.util.List

The class source follows (the main has been added to reproduce the problem in a single class):

package org.aeonbits.owner.multithread;

import org.aeonbits.owner.Config;
import org.aeonbits.owner.UtilTest.MyCloneable;

import java.util.ArrayList;
import java.util.List;

import static org.aeonbits.owner.UtilTest.debug;

abstract class ThreadBase<T extends Config> extends Thread implements MyCloneable {
    private static long counter = 0;
    final long uniqueThreadId = ++counter;
    final T cfg;
    final Object lock;
    final int loops;
    final List<Throwable> errors = new ArrayList<Throwable>();

    ThreadBase(T cfg, Object lock, int loops) {
        this.cfg = cfg;
        this.lock = lock;
        this.loops = loops;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public void run() {
        synchronized (lock) {
            try {
                lock.wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }
        }
        for (int i = 0; i < loops; i++) {
            debug("%s[%d] started loop #%d.\n", getClass().getName(), uniqueThreadId, i);
            try {
                execute();
            } catch (Throwable throwable) {
                debug("%s[%d] thrown an error in loop #%d.\n", getClass().getName(), uniqueThreadId, i);
                errors.add(throwable);
            }
            yield();
            debug("%s[%d] completed loop #%d.\n", getClass().getName(), uniqueThreadId, i);
        }
    }

    abstract void execute() throws Throwable;

    public List<Throwable> getErrors() {
        return errors;
    }

    public static void main(String[] args) {
        ThreadBase t = new ThreadBase(null, new Object(), 10) {
            @Override
            void execute() throws Throwable {

            }
        };
        List<Throwable> errors = t.getErrors();
        //                                  ^ compiler reports the warning here!
    }
}

Additional details:

$ mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)
Maven home: /Users/luigi/opt/apache-maven-3.0.4
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.4", arch: "x86_64", family: "mac"

In pom.xml I added compiler.source and compiler.target 1.5 since my library is targetted to jdk 1.5 or better:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                    <compilerArgument>-Xlint:unchecked</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

Can anybody explain me what is wrong here?

For me, it does not make any sense as t.getErrors() returns a List:

List<Throwable> errors = t.getErrors();

What's wrong here?!?


Solution

  • Your t is a raw type of ThreadBase.

    ThreadBase t = new ThreadBase(null, new Object(), 10) {
    

    That means that all generics are removed, even unrelated ones such as your method getErrors, which becomes

    public List getErrors() {
    

    Then you can't assign an List to a List<Throwable> without that warning.

    You must not have a raw instance of ThreadBase if you want to eliminate that warning. Supply a type parameter when initializing it, e.g.:

    ThreadBase<Config> t = new ThreadBase<Config>(null, new Object(), 10) {
    

    Then you should be able to call getErrors successfully with your existing code:

    List<Throwable> errors = t.getErrors();