3

Hello i have got hash map with value:

    private static final LinkedHashMap<Class<? extends Annotation> , Class<? extends AGenerator<?>>> annotationGeneratorMap = new LinkedHashMap<Class<? extends Annotation>, Class<? extends AGenerator<?>>>();

and my numeric Generator looks like :

public abstract class NumericGenerator<T extends Number> extends AGenerator<T>{


public NumericGenerator(Field field) {
    super(field);
    // TODO Auto-generated constructor stub
}

public T random() {
    // TODO Auto-generated method stub
    return null;
}

  }

And i have got problem when i need to put this class into hashmap :

annotationGeneratorMap.put(GenerateNumeric.class, NumericGenerator.class);

And in eclipse i have got error the method is not applicable for the argument ???

but :

        annotationGeneratorMap.put(GenerateNumeric.class, (Class<? extends AGenerator<?>>) NumericGenerator.class);

and @SuppressWarnings("unchecked") is good ..:/

Can i do this without casting ?? (Class<? extends AGenerator<?>>) NumericGenerator.class

3 Answers 3

2

use Class<? extends AGenerator> instead

LinkedHashMap<
    Class<? extends Annotation>, 
    Class<? extends AGenerator> > annotationGeneratorMap = new LinkedHashMap<>()

In Java, class can represent a raw type, but cannot represent a generic type.

There is a class for List, but there is no class for List<String>.

So you can declare Class<? extends List>, which is compatible with ArrayList.class etc. because raw type ArrayList is a subtype of List.

But Class<? extends List<?>> doesn't make much sense, because there is no class that is a subtype of List<?>.

And all thanks to erasure.

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

Comments

1

Compile time check won't allow you to do that unless you cast it of course.

Information about generic types is lost at runtime, not compile time.

Comments

1

Following compiles without error on version 1.7.0_02:

import java.util.*;
import java.lang.annotation.*;

interface AGenerator<T> {}

interface A extends Annotation {}
class X implements AGenerator<X> {}

class E7 {
    private static final LinkedHashMap<Class<? extends Annotation>, 
        Class<? extends AGenerator<?>>> annotationGeneratorMap 
        = new LinkedHashMap<Class<? extends Annotation>, 
                            Class<? extends AGenerator<?>>>();


    void foo() {
        annotationGeneratorMap.put(A.class, X.class);
    }

}

2 Comments

yes, X is a subtype of AGenerator<?>. but X is not generic, unlike OP's example.
@Miserable Variable but consider scenario like that that your X class user generics: class X<T extends Numver> implements AGenerator<T> this is not allowed :/

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.