Kevin Meredith
Kevin Meredith

Reputation: 41909

No Warning with Raw Type

I defined a parameterized interface:

import com.google.common.base.Optional;

public interface AbstractResource<S extends Parent> {

    Optional<S> getOption();
    Optional<S> getAbsent();
    Optional<S> getNull();
}

Then, I implemented it as a raw type. Observe that I'm breaking the interface by returning the Optional types Child, Object and Integer for the respective methods.

public class FooResource implements AbstractResource { // Did not add type parameter

    @Override
    public Optional<Child> getOption() {   
        Child child = new Child("John");
        return Optional.of(child);
    }

    @Override
    public Optional<Object> getAbsent() { 
        return Optional.absent();
    }

    @Override
    public Optional<Integer> getNull() {
        return null;
    }
}

When compiling with the -Xlint:unchecked option, why doesn't the compiler show a warning that FooResource fails to add type parameters? In fact, it compiles successfully.

Upvotes: 2

Views: 916

Answers (2)

JamesB
JamesB

Reputation: 7894

If you want to see the warning, you should be using

-Xlint:rawtypes

instead of

-Xlint:unchecked

For Maven builds, refer to this: http://maven.apache.org/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId> 
   <version>3.1</version>
   <configuration>
        <compilerArgument>-Xlint:rawtypes</compilerArgument> 
   </configuration> 
</plugin>

Upvotes: 2

Seelenvirtuose
Seelenvirtuose

Reputation: 20608

-Xlint:unchecked is for unchecked conversion (aka casts). It does nothing have to do with using raw types. Compile your classes with the option -Xlint. You then will get the expected output:

FooResource.java:3: warning: [rawtypes] found raw type: AbstractResource
                AbstractResource
                ^
  missing type arguments for generic class AbstractResource<S>
  where S is a type-variable:
    S extends Parent declared in interface AbstractResource
1 warning

I would suggest using an IDE. Eclipse - for example - shows such warnings out of the box.


I just found out, that Java 7 supports more "Xlint" options (than Java 6, e.g.). So the option "-Xlint:rawtypes" will indeed help here.

Upvotes: 2

Related Questions