6

I've the following classes

KeyValue.java

package test;

public class KeyValue<T> {
    private String key;

    private T value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

}

Reader.java

package test;

public interface Reader<T> {
    <S extends T> S read(Class<S> clazz);
}

Test.java

package test;

import java.util.List;

public class Test {

    public static void main(String[] args) {
        List<KeyValue<Object>> list = find(KeyValue.class, new Reader<KeyValue<Object>>() {

            @Override
            public <S extends KeyValue<Object>> S read(Class<S> clazz) {
                return null;
            }
        });
    }

    public static <T> List<T> find(Class<T> targetClass, Reader<T> reader) {
        return null;
    }

}

Here the method call find(......) is failing at compile time with error message

The method find(Class, Reader) in the type Test is not applicable for the arguments (Class, new Reader>(){}).

This method has to return object of type List<KeyValue<Object>>.

What is wrong with this design and how to fix this.

Thank you.

3 Answers 3

2

finddefines T and T (in first and second arg) to be of same type - your call to find uses the type Object in the first arg and the Type KeyValue<Object>in the second arg.

Either use two different type identifiers (e.g. T and X, i.e. public static <T, X extends T> List<T> find(Class<T> targetClass, List<X> reader) ) in your find declaration or repair your call to find.

Sign up to request clarification or add additional context in comments.

4 Comments

I've changed it, it was a mistake from my part. Now both are of type KeyValue.
No, still it has the error The method find(Class<T>, Reader<T>) in the type Test is not applicable for the arguments (Class<KeyValue>, new Reader<KeyValue<Object>>(){})
As you see from the Error message. java takes the first T as <KeyValue> and the second T as <KeyValue<Object> - So they are not identical -- Perhaps change your signature to find(Class<X extends T> targetClass, Reader<T> reader) or to find(Class<T> targetClass, Reader<X super T> reader) depending on what you want to express (I assume the first).
Do KeyValue<Object> and KeyValue.class share the inheritance hierarchy that you've implied above?
1

you want to get a list of your class KeyValue from your method find but u defined it as List note that it is a list of T not KeyValue change ur method declaration to be as follows

private static <T> List<KeyValue<T> > find(Class<KeyValue> aClass, Reader<KeyValue<T> > reader) {
    throw new UnsupportedOperationException("Not yet implemented");
}

i think this is what u want

Comments

-1

Try to declare Test as

public class Test<T> {.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.