Java – Type Parameter is not within its bound

genericsjavamaventype-parameter

I am getting an error when running maven compile on this class but it seems to work just fine when executing. I have looked a quite a few other posts on the same topic but was unable to get this working for myself.

EDIT – Removed original code snippets and replaced

EDIT – Providing MCVE – very stripped down

By the way this is being run on Java 6

public class MainTester {

    @SuppressWarnings("rawtypes")
    public static void main(String[] args) {
        ProtocolHandler<MappingRule> mappingProtocolHandler = new MappingProtocolHandler();

        Map<String, List<MappingRule>> protocols = mappingProtocolHandler.getProtocols();

        System.out.println(protocols);
    }

}

ProtocolHandler and Child class:

public abstract class ProtocolHandler<E extends Rule<?, ?>> {
    public Map<String, List<E>> getProtocols() {                
            return getProtocolsForRequest();
    }

    protected abstract Map<String, List<E>> getProtocolsForRequest();
}

@SuppressWarnings("rawtypes")
public class MappingProtocolHandler extends ProtocolHandler<MappingRule>{
    @Override
    protected Map<String, List<MappingRule>> getProtocolsForRequest() {

        return new HashMap<String, List<MappingRule>>();
    }
}

Rule Classes:

public interface Rule<F, T> {
    public void execute(F object, T object2);
}

public abstract class BaseRule<F, T> implements Rule<F, T>, Comparable<BaseRule<F, T>> {

}

public abstract class MappingRule<F,T> extends BaseRule<F, T> implements CustomAttributes{

}

public abstract class InputRule extends MappingRule<Object, Map<String,Object>> {

}

public abstract class OutputRule extends MappingRule<Map<String, Object>, Object> {

}

public interface CustomAttributes {

}

Handler Classes:

public abstract class ProtocolHandler<E extends Rule<?, ?>> {
    public Map<String, List<E>> getProtocols() {                
            return getProtocolsForRequest();
    }

    protected abstract Map<String, List<E>> getProtocolsForRequest();
}

@SuppressWarnings("rawtypes")
public class MappingProtocolHandler extends ProtocolHandler<MappingRule>{
    @Override
    protected Map<String, List<MappingRule>> getProtocolsForRequest() {

        return new HashMap<String, List<MappingRule>>();
    }
}

[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile
(default-compile) on project Test: Compilation failure: Compilation
failure:
[ERROR]…\src\main\java\common\main\MainTester.java:[14,18]
type parameter common.mapping.rule.MappingRule is not within its bound
[ERROR]…\src\main\java\common\handler\MappingProtocolHandler.java:[10,60]
type parameter common.mapping.rule.MappingRule is not within its bound

Best Answer

I think you should not use the raw type of MappingRule

private ProtocolHandler<MappingRule> mappingProtocolHandler;

public void execute(){
 Map<String, List<MappingRule>> protocols =  mappingProtocolHandler.getProtocols(protocolExecutionRequest);
}

but

private ProtocolHandler<MappingRule<String, String>> mappingProtocolHandler;

public void execute(){
    Map<String, List<MappingRule<String, String>>> protocols = mappingProtocolHandler.getProtocols(/**/);
}

EDIT: if only maven gives you an ERROR, maybe it is configured to treat warnings as errors? see Javac: Treat warnings as errors

EDIT2: I always get errors when running this code. How do you compile this code? Maybe the eclipse compiler(?) allows you to run this code anyway?

EDIT3: According to the author of the question, the code runs only in eclipse. Sometimes the eclipse compiler seems to behave differently, but in that case the you should treat the JDK as imperative (since your build is not done in eclipse, but in maven with javac). I am not sure about the reason (one could certainly search the JLS for that), but why mixing type bounds and raw types which were only introduced for backwards compability with Java versions before 1.5?

There seem to be other differences in generics between eclipse and javac: Maven Compiler vs Eclipse Compiler Generics Difference?