b****u 发帖数: 1130 | 1 For example
public class SpecialArray {
T[] createSpecialArray()
{
return (T[])Array.newInstance(???, 10);
}
}
如何获得 T 的type at runtime by Reflection?
I am not allowed to change API or method argument).
So I can not use T[] createSpecialArray(Class clazz). |
c*********e 发帖数: 16335 | 2 为什么不用arraylist ?
【在 b****u 的大作中提到】 : For example : public class SpecialArray { : T[] createSpecialArray() : { : return (T[])Array.newInstance(???, 10); : } : } : 如何获得 T 的type at runtime by Reflection? : I am not allowed to change API or method argument). : So I can not use T[] createSpecialArray(Class clazz).
|
b****u 发帖数: 1130 | 3 List is much better.
But I am not allowed to change API for compatibility reason. |
g*****g 发帖数: 34805 | 4 If you can't change it, you can wrap it.
【在 b****u 的大作中提到】 : List is much better. : But I am not allowed to change API for compatibility reason.
|
f*******n 发帖数: 12623 | 5 You can't. T doesn't exist at runtime. It's an illusion. Just remove T and
ask yourself how to do it. If you cannot do it without T, then you cannot do
it with T either:
public class SpecialArray {
Object[] createSpecialArray()
{
return (Object[])Array.newInstance(???, 10);
// How to get T?
// You do not pass anything into this method
}
}
Your problem is that arrays in Java know their component type at runtime. If
you do new String[5], the object knows it's an array of String at runtime.
However, generics does not exist at runtime. If you do new ArrayList
, it's just ArrayList at runtime. It does not know anything about String.
To create an array with a certain runtime type, you must know the component
type at runtime. Otherwise, the only thing your method can return is null:
T[] createSpecialArray()
{
return null;
}
What are you trying to do? It doesn't make sense for someone to ask you to
write a method like this.
If you are trying to create an array to use internally in your class, i.e.
public class SpecialArray {
private T[] elements = ???
}
then you have two options:
1. Create a new Object[], and make your internal variable type Object[].
That way, it is safe, but you need to cast to type T every time you access
an element.
public class SpecialArray {
private Object[] elements = new Object[5];
public T get(int i) { return (T)elements[i]; }
}
2. Create a new Object[], and then cast to T[], and have your internal
variable be type T[]. This way, you don't need to cast when you get elements
out. However, it is not safe, because the object's actual runtime type is
still Object[]. You need to make sure never to expose this internal variable
to the outside as type T[].
public class SpecialArray {
private T[] elements = (T[])new Object[5];
public T get(int i) { return elements[i]; }
// Do not do the following, will crash:
public T[] getElements() { return elements; }
}
【在 b****u 的大作中提到】 : For example : public class SpecialArray { : T[] createSpecialArray() : { : return (T[])Array.newInstance(???, 10); : } : } : 如何获得 T 的type at runtime by Reflection? : I am not allowed to change API or method argument). : So I can not use T[] createSpecialArray(Class clazz).
|
s******e 发帖数: 493 | 6 (Class)((ParameterizedType)getClass() .
getGenericSuperclass()).getActualTypeArguments()[0];
but there is some limit on this approach. check with hibernate generic dao
implementation |
W***o 发帖数: 6519 | 7 我记得java不支持generic array啊
【在 b****u 的大作中提到】 : For example : public class SpecialArray { : T[] createSpecialArray() : { : return (T[])Array.newInstance(???, 10); : } : } : 如何获得 T 的type at runtime by Reflection? : I am not allowed to change API or method argument). : So I can not use T[] createSpecialArray(Class clazz).
|
c*********w 发帖数: 65 | 8 You can do it using option 1, option 2 is cleaner but requires changing of
your method api:
http://docs.oracle.com/javase/tutorial/extra/generics/literals.
【在 b****u 的大作中提到】 : For example : public class SpecialArray { : T[] createSpecialArray() : { : return (T[])Array.newInstance(???, 10); : } : } : 如何获得 T 的type at runtime by Reflection? : I am not allowed to change API or method argument). : So I can not use T[] createSpecialArray(Class clazz).
|