1

I want check that field type is List or not, but that is giving an error:

Incompatible conditional operand types Class<capture#5-of ?> and 
     List
- Incompatible conditional operand types Class<capture#6-of ?> and 
     List

How can I solve this?

private void convert(Class<?> load) {

        Field[] fields = load.getDeclaredFields();
        int i = 0;
        for (Field field : fields) {
            Class<?> type = field.getType();
            if (type instanceof java.util.List) {
            }
        }
    }

2 Answers 2

6
private void convert(Class<?> load) {

    Field[] fields = load.getDeclaredFields();
    int i = 0;
    for (Field field : fields) {
        Class<?> type = field.getType();
        if (java.util.List.class.isAssignableFrom(type)) {
        }
    }
}

instanceof operator is used only for instances!

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

2 Comments

actually type is child of List class. i want exact java.util.List class instances here list children can be assign
public class B implements List{ } the above loaded Field type is B so it is giving true because it can be assignable . i want only List type i dont want B type.
1

If you want the declared type of the field to match java.util.List exactly, you can use the expression field.getType()==java.util.List.class to test it.

Still, the value of that field is either null or an instance of a concrete class which implements the list interface.

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.