开发者

Creating parameterized type object using anonymous class

开发者 https://www.devze.com 2023-01-02 08:06 出处:网络
This might b开发者_运维技巧e a stupid question, but I just saw a question asking how to create a Type variable for a generic type. The consensus seemed to be that you should have a dummy method return

This might b开发者_运维技巧e a stupid question, but I just saw a question asking how to create a Type variable for a generic type. The consensus seemed to be that you should have a dummy method returning that type, and then use reflection to get it (in this case he wanted Map<String, String>). Something like this :

public Map<String, String> dummy() { throw new Error(); }

Type mapStringString = Class.forName("ThisClass").getMethod("dummy").getGenericReturnType();

My question is, not having used reflection that much, couldn't you just do something like:

Type mapStringString = new ParameterizedType() {
    public Type getRawType() {
        return Map.class;
    }

    public Type getOwnerType() {
        return null;
    }

    public Type[] getActualTypeArguments() {
        return new Type[] { String.class, String.class };
    }
};

Would this work? If not, why not? And what are some of the dangers/problems if it does (besides being able to return some Type like Integer<String> which is obviously not possible.


Sure you could, and for most applications it would probably be sufficient.

However, using the first method, you get a more refined object. Let's say for instance that you create an object type1 using the first method, and type2 using the second method. Then type1.equals(type2) would return true (since the first method returns an object that properly implements the equals-method) but type2.equals(type1) would return false (since in the second method, you haven't overridden the equals-method, and are using the implementation from Object).

Same reasoning applies to other (sometimes useful methods) such as toString, hashCode etc. The second method does not provide useful implementations of these.


If you include Google's Guava library in your project (you should; it's great), use its TypeToken to get a type. Google's Gson library (for interacting with JSON) has a similar version. Both are used like this (to get a Type representing List<String>:

Type t = new TypeToken<List<String>>(){}.getType();

If you don't want to rely on any third-party libraries, you can still use anonymous types to get a generic concrete type with one line of code (this technique will not work with interfaces and could be more trouble than it's worth for Abstract Types). To get a Type representing HashMap<String, String>, do this:

Type t = new HashMap<String, String>(){}.getClass().getGenericSuperclass();

I have verified that resulting Type instance .equals() the Type instances created by Gson's TypeToken, though have not verified the same for Guava's version of TypeToken, which I do not have access to at the moment. (Guava is a more general-purpose library that is so handy for all sorts of things, you should probably be using it anyways.)


In addition to the mentioned libs from Google there is also a lib from Apache that does the job.

import org.apache.commons.lang3.reflect.TypeUtils;
...
ParameterizedType type = TypeUtils.parameterize(List.class, Double.class);
...

Finde code on GitHub here and Maven artifacts here.


Actually, I think the simplest way (== least code) to do this would be a dummy interface extending the type your interested in, and then getGenericInterfaces()[0] from its class (use getGenericSuperclass() if you're interested in a class):

private interface MapStringString extends Map<String, String> {}
private static ParameterizedType mapStringString(){
    return (ParameterizedType) MapStringString.class.getGenericInterfaces()[0];
}

It doen't scale well, though, as you have to create a new class for every ParameterizedType you want to represent. I don't see why your implementation wouldn't do (unless there are narrowing casts somewhere), and it does have the appealing benefit that you can make it reusable.

0

精彩评论

暂无评论...
验证码 换一张
取 消