Java5ã®åã·ã¹ãã ãç解ããã«ã¯ãªãã¬ã¯ã·ã§ã³APIã使ã£ã¦ã¿ãã®ãæçã®è¿éã«ãªã
Java5ã«ãããç·ç§°åï¼genericsï¼ã®å°å ¥ã«ä¼´ããJavaã®åã·ã¹ãã ã¯ä»¥åã¨æ¯ã¹ã¦é«æ©è½ã«ãªã£ãåé¢ãç解ããããã®ãã¼ãã«ãé«ããªã£ã¦ãã¾ãããã¡ãããJavaã®åã«ã¤ãã¦ãã¡ãã¨ç解ããããã«ã¯è¨èªä»æ§ãåå¼·ããã°ããã®ã§ãããæã£åãæ©ãç解ããããã®æ¹æ³ã¨ãã¦ãªãã¬ã¯ã·ã§ã³APIã使ã£ã¦ã¿ãã¨ããã®ãæå¹ã§ãããªãã¬ã¯ã·ã§ã³APIã®å ç¥ã¯Java1.xã®ããããåå¨ãã¦ããããã£ã¼ã«ããã¡ã½ãããã¯ã©ã¹ãªã©ã®æ å ±ãå®è¡æã«åå¾ããããã®ãã®ã§ãããç·ç§°åã«åããã¦Java5ããæ°ããAPIã追å ããã¦ãã¾ããããã§ã¯ãªãã¬ã¯ã·ã§ã³APIã使ããJava5ã®æ°ããåã·ã¹ãã ã«ã¤ãã¦ã¾ã¨ãã¦ã¿ããã¨æãã¾ãã
JDK1.4ã¾ã§ã®åã¯ãã¹ã¦Classã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã«ä¸å¯¾ä¸å¯¾å¿ãã
JDK1.4ã¾ã§ã«åå¨ãã¦ããåã¯ãã¿ã¼ã³ã«åããã¨ä»¥ä¸ã®3éãã«åé¡ã§ãã¾ãã
- åºæ¬åï¼intãdoubleãªã©ï¼
- åç §åï¼ã¯ã©ã¹ãã¤ã³ã¿ã¼ãã§ã¼ã¹ï¼
- é ååï¼byte[ ]ãString[ ]ãString[ ][ ]ãªã©ï¼
JDK1.4ã¾ã§ã®åã«é¢ãããªãã¬ã¯ã·ã§ã³APIã¯é常ã«åç´ã§ããããã®åæ å ±ã¯ãã¹ã¦java.lang.Classã«ãã£ã¦è¡¨ç¾ããã¾ãããClassã®ã¤ã³ã¹ã¿ã³ã¹ã¨ããããã®åã¨ãä¸å¯¾ä¸ã«å¯¾å¿ãã¦ããã®ã§ããåºæ¬åãé åã¯ã¯ã©ã¹ã§ãªãã®ã§ããããã®åãã¯ã©ã¹ã§è¡¨ç¾ãããã¨ããã¨ããã¯ã¡ãã£ã¨éåæãããããããã¾ããããå®éã
Class intClass = int.class; // intã®åºæ¬åãããããã¯ã©ã¹ãåå¾ Class longArrayClass = long[].class; // long[]ãããããã¯ã©ã¹ãåå¾ Class long2DArrayClass = long[][].class; // long[][]ãããããã¯ã©ã¹ãåå¾
ã®ããã«ã¯ã©ã¹ãªãã©ã«ã®æ¸å¼ãç¨ãã¦ããããåã«å¯¾å¿ããClassã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ã§ãã¾ãã
Java5以éã§ã¯å
·è±¡åå¯è½åã«éãClassã§è¡¨ç¾ã§ãã
åæ¶å»ï¼type erasureï¼ã¨å ·è±¡åå¯è½åï¼reifiable typeï¼ã«ã¤ãã¦ã¯ã普通の(業務)Javaアプリケーションでは配列をなるべく使用しない方がよい - 達人プログラマーを目指してã§ã説æãã¾ãããã
- åºæ¬åï¼intãªã©ï¼
- ç·ç§°åããã¦ããªãã¯ã©ã¹ãã¤ã³ã¿ã¼ãã§ã¼ã¹ï¼StringãNumberãRunnableãªã©ï¼
- ãã¹ã¦ã®ç·ç§°åå¤æ°ã«å¯¾ãã¦å¢çã®ãªãã¯ã¤ã«ãã«ã¼ããæã¤åï¼List<?>ãMap<?, ?>ãªã©ï¼
- ç·ç§°ãã©ã¡ã¼ã¿ã¼æªæå®ã®rawåï¼ListãMapãªã©ï¼
- å ·è±¡åå¯è½åã®è¦ç´ ãæã¤é åï¼int[ ]ãString[ ]ãList<?>[ ]ãint[ ][ ]ãªã©ï¼
ã®ãããªãã®ãããã¾ãããããã¯åæ¶å»ã«ããåã®æ
å ±ã失ãããªãåã§ãããJDK1.4ã¾ã§ã«åå¨ããåã¨äºæã®ããåã§ããåºæ¬çã«Javaã®ææ³ã¯éå»ã®ãã¼ã¸ã§ã³ã¨äºææ§ãä¿ã¤ã¨ãããã¨ãéè¦ãã¦ããããããã®åã®æ
å ±ã¯å¾æ¥ã©ããjava.lang.Class
class TypePatterns<T> { // JDK1.4ã¾ã§ã«åå¨ãã¦ããåã®ãã¿ã¼ã³ /** åºæ¬å */ public int primitiveField; /** åç §å */ public String objectField; /** é åå */ public long[] arrayField; ... }
次ã®ãã¹ãã³ã¼ãã示ãããã«JDK1.4ã¨äºææ§ã®ããAPIã®å¼ã³åºãã«ãããã£ã¼ã«ãã®æ å ±ãåå¾ãããã¨ãã§ãã¾ãã
/** * åºæ¬åãã£ã¼ã«ãã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void primitiveField() throws Exception { Field primitiveField = TypePatterns.class.getField("primitiveField"); Class<?> primitiveFieldType = primitiveField.getType(); assertEquals(int.class, primitiveFieldType); assertTrue(primitiveFieldType.isPrimitive()); //åºæ¬å } /** * åç §åãã£ã¼ã«ãã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void objectField() throws Exception { Field objectField = TypePatterns.class.getField("objectField"); Class<?> objectFieldType = objectField.getType(); assertEquals(String.class, objectFieldType); } /** * é ååãã£ã¼ã«ãã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void arrayField() throws Exception { Field arrayField = TypePatterns.class.getField("arrayField"); Class<?> arrayFieldType = arrayField.getType(); assertEquals(long[].class, arrayFieldType); assertTrue(arrayFieldType.isArray()); //é åå assertEquals(long.class, arrayFieldType.getComponentType()); //è¦ç´ åã®åå¾ }
å¾æ¥ã®APIã使ã£ãå ´åãå ·è±¡åå¯è½åã§ãªãåã¯åæ¶å»ãããå ·è±¡ååã¨ãã¦å¾ããã
ãã®ããã«å ·è±¡åå¯è½åã«ã¤ãã¦ã¯Java5ã§ã以åã®ãã¼ã¸ã§ã³ã¨ã¾ã£ããåæ§ã«Classã¯ã©ã¹ã¨åã対å¿ãã¦ãã¾ããããããJava5以éã§ã¯ã½ã¼ã¹ã³ã¼ãä¸ã§è¨è¿°ã§ããåã«ã¯ä»¥ä¸ã®ããã«å ·è±¡åå¯è½ã§ãªãåãåå¨ãã¾ãã
- ç·ç§°åå¤æ°ãã®ãã®ï¼Tãªã©ï¼
- å®åãã©ã¡ã¼ã¿ã¼ãæã¤ãã©ã¡ã¼ã¿ã¼ååï¼List
ãMap ãªã©ï¼ - å¢çä»ãã¯ã¤ã«ãã«ã¼ããæã¤ãã©ã¡ã¼ã¿ã¼ååï¼List<? extends Number>ãComparable <? super String>ãªã©ï¼
- å
·è±¡åå¯è½åã§ãªãè¦ç´ ãæã¤ç·ç§°é
åï¼T[ ]ãList
[ ]ãªã©ï¼
ãããã®åã¯ãã¤ãã³ã¼ãä¸ã§ã¯åæ¶å»ã«ããå¥ã®å ·è±¡ååã«å¤æããããã¨ã«ãããç´æ¥å¯¾å¿ããClassã®ã¤ã³ã¹ã¿ã³ã¹ãåå¨ãã¾ããã
class TypePatterns<T> { ... // Java5ã§ãµãã¼ããããããã«ãªã£ãåã®ãã¿ã¼ã³ /** åå¤æ° */ public T typeVariableField; /** ãã©ã¡ã¼ã¿ã¼åå ï¼åãã©ã¡ã¼ã¿ã¼ãæ®éã®ã¯ã©ã¹ã®å ´åï¼ */ public List<String> parameterizedField; /** ãã©ã¡ã¼ã¿ã¼åå ï¼åãã©ã¡ã¼ã¿ã¼ãåå¤æ°ã®å ´åï¼ */ public List<T> typeVariableParameterizedField; /** ãã©ã¡ã¼ã¿ã¼åå ï¼åãã©ã¡ã¼ã¿ã¼ãã¯ã¤ã«ãã«ã¼ãåã®å ´åï¼ */ public List<? extends Number> wildcardParameterizedField; /** ç·ç§°åé åï¼è¦ç´ ãåå¤æ°ã®å ´åï¼ */ public T[] typeVariableArrayField; /** ç·ç§°åé åï¼è¦ç´ ããã©ã¡ã¼ã¿ã¼ååã®å ´åï¼ */ public List<String>[] parameterizedComponentArrayField; }
å®éã以ä¸ã§å®£è¨ãããå¤æ°ã«å¯¾ãã¦åç¯ã¨åæ§ã«JDK1.4äºæã®APIãå¼ã³åºãã¦ã¿ãçµæã¯æ¬¡ã®ããã«ãªãã¾ãã
// ç·ç§°åã使ã£ã¦ããå ´å /** * åå¤æ°Tã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void typeVariableField() throws Exception { // JDK1.4ã¾ã§ã«åå¨ãã¦ããAPIã¯åæ¶å»å¾ã®æ å ±ã®ã¿ãè¿ãã Field typeVariableField = TypePatterns.class.getField("typeVariableField"); Class<?> typeVariableFieldType = typeVariableField.getType(); assertEquals(Object.class, typeVariableFieldType); // åæ¶å»å¾ã®Tã®å ·è±¡åå } /** * ãã©ã¡ã¼ã¿ã¼ååList<String>ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void parameterizedField() throws Exception { // JDK1.4ã¾ã§ã«åå¨ãã¦ããAPIã¯åæ¶å»å¾ã®æ å ±ã®ã¿ãè¿ãã Field parameterizedField = TypePatterns.class.getField("parameterizedField"); Class<?> parameterizedFieldType = parameterizedField.getType(); assertEquals(List.class, parameterizedFieldType); // åæ¶å»å¾ã®List<String>ã®å ·è±¡åå } /** * ãã©ã¡ã¼ã¿ã¼ååList<T>ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void typeVariableParameterizedField() throws Exception { // JDK1.4ã¾ã§ã«åå¨ãã¦ããAPIã¯åæ¶å»å¾ã®æ å ±ã®ã¿ãè¿ãã Field typeVariableParameterizedField = TypePatterns.class.getField("typeVariableParameterizedField"); Class<?> typeVariableParameterizedFieldType = typeVariableParameterizedField.getType(); assertEquals(List.class, typeVariableParameterizedFieldType); // åæ¶å»å¾ã®List<T>ã®å ·è±¡åå } /** * ãã©ã¡ã¼ã¿ã¼åå<? extends Number>ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void wildcardParameterizedField() throws Exception { // JDK1.4ã¾ã§ã«åå¨ãã¦ããAPIã¯åæ¶å»å¾ã®æ å ±ã®ã¿ãè¿ãã Field wildcardParameterizedField = TypePatterns.class.getField("wildcardParameterizedField"); Class<?> wildcardParameterizedFieldType = wildcardParameterizedField.getType(); assertEquals(List.class, wildcardParameterizedFieldType); // åæ¶å»å¾ã®List<? extends Number>ã®å ·è±¡åå } /** * ç·ç§°åé åT[]ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void typeVariableArrayField() throws Exception { // JDK1.4ã¾ã§ã«åå¨ãã¦ããAPIã¯åæ¶å»å¾ã®æ å ±ã®ã¿ãè¿ãã Field typeVariableArrayField = TypePatterns.class.getField("typeVariableArrayField"); Class<?> typeVariableArrayFieldType = typeVariableArrayField.getType(); assertEquals(Object[].class, typeVariableArrayFieldType); // åæ¶å»å¾ã®T[]ã®å ·è±¡åå assertTrue(typeVariableArrayFieldType.isArray()); assertEquals(Object.class, typeVariableArrayFieldType.getComponentType()); } /** * ç·ç§°åé åList<String>[]ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void parameterizedComponentArrayField() throws Exception { Field parameterizedComponentArrayField = TypePatterns.class.getField("parameterizedComponentArrayField"); Class<?> parameterizedComponentArrayFieldType = parameterizedComponentArrayField.getType(); assertEquals(List[].class, parameterizedComponentArrayFieldType); // åæ¶å»å¾ã®List<String>[]ã®å ·è±¡åå assertTrue(parameterizedComponentArrayFieldType.isArray()); assertEquals(List.class, parameterizedComponentArrayFieldType.getComponentType()); }
以ä¸ã®çµæã示ãããã«ããã¨ãã¨ã½ã¼ã¹ã³ã¼ãä¸ã§å®£è¨ããã¦ããéãã®ç·ç§°åã使ã£ãåã¯åå¾ã§ããã代ããã«åæ¶å»å¾ã®å ·è±¡ååãå¾ããããã¨ããããã¾ããåæ¶å»ã«ãããJava5ã§ãJDK1.4ã¾ã§ã¨åãçµæãå¾ããã¦ãããã¤ã¾ããäºææ§ãä¿ããã¦ããã¨ãããã¨ã§ãã
ç·ç§°åã«å¯¾å¿ããJava5ã®æ°ãããªãã¬ã¯ã·ã§ã³API
åæ¶å»ã«ããå®éã«å®è¡ããããã¤ãã³ã¼ãã®ä¸ã§ã¯å
·è±¡åå¯è½ãªåããæ®ãã¾ãããããªãã¬ã¯ã·ã§ã³ç¨ã«ã½ã¼ã¹ã³ã¼ãä¸ã®ç·ç§°åã®æ
å ±ãä¿åãããããã«ãªã£ã¦ãã¾ããããããç·ç§°åã®æ
å ±ã¯ãã¸ãã¯ããã¼ã¿æ§é ã«ã¯ä¸åå½±é¿ããªããã®ã®ãã¤ãã³ã¼ãä¸ã«åãè¾¼ã¾ããã³ã¡ã³ãæ
å ±ã®ãããªãã®ã¨èãããã¾ããJava5ã§ã¯å¾æ¥ã®ãªãã¬ã¯ã·ã§ã³APIã«å ãã¦getGenericType()ã¨ããååã®ã¡ã½ãããæä¾ããã¦ãããç·ç§°åã®æ
å ±ãå®è¡æã«åå¾ã§ãã¾ãããã¨ãã°Fieldã¯ã©ã¹ã®getType()ã¡ã½ããã¯Class<?>ãè¿ãã¾ããããã®ã¡ã½ããããã¯åç¯ã§èª¿ã¹ãããã«åæ¶å»å¾ã®åæ
å ±ããå¾ããã¾ããã代ããã«getGenericType()ãå¼ã³åºããã¨ã§ç·ç§°åã®æ
å ±ãå¾ããã¾ãããã®ã¡ã½ããã®æ»ãå¤ã¯Class<?>ã§ã¯ãªãã¦Typeã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ãªã£ã¦ãã¾ãããã®Typeã¤ã³ã¿ã¼ãã§ã¼ã¹ãéå
·è±¡åå¯è½ãªãã®ãå«ãã¦ããããåã表ç¾ããããã«Java5ããå°å
¥ããã¦ãã¾ãããå¾æ¥ã®Class<?>ã¨ã®é¢ä¿ãå«ãã¦ä»¥ä¸ã®ã¯ã©ã¹å³ã®ãããªé¢ä¿ã¨ãªã£ã¦ãã¾ãã
å¾æ¥ããããClass
åã®ã«ãã´ãª | 表ç¾ããããã®åï¼ã¡ã¿åï¼ | 説æ | ä¾ |
---|---|---|---|
å ·è±¡åå¯è½å | Class |
åºæ¬åãç·ç§°ã使ããªãåç
§åã é åãªã©JDK1.4以åããããåã |
intãlong[]ãStringãList |
åå¤æ° | TypeVariable | ã¯ã©ã¹ãã¡ã½ãããã³ã³ã¹ãã©ã¯ã¿ ãªã©ã§å®£è¨ãããåå¤æ°ã è¤æ°ã®ä¸éåãæã¤ãã¨ãã§ããã |
SomeClass ã§å®£è¨ãããEãT |
ã¯ã¤ã«ãã«ã¼ãå | WildcardType | ãã©ã¡ã¼ã¿ã¼ååã§ä½¿ç¨ãã ã¯ã¤ã«ãã«ã¼ããå«ãã å |
List<? extends Number> ãªã©ã®ã? extends Numberãã®é¨å |
ãã©ã¡ã¼ã¿ã¼åå | ParameterizedType | ãã©ã¡ã¼ã¿ã¼åãããå | List List<? extends Number>ãªã© |
ç·ç§°åé å | GenericArrayType | è¦ç´ ã«åå¤æ°ããã©ã¡ã¼ã¿ã¼å ãããåã使ã£ãé åå |
TãList |
Class
Oracle Technology Network for Java Developers | Oracle Technology Network | Oracle
ããã§ã¯ã以ä¸ã®æ¡å¼µãããAPIãå©ç¨ãã¦ãå®éã«ç·ç§°åã®æ
å ±ãåå¾ã§ãããã¨ã確èªãã¦ã¿ã¾ãã
/** * åå¤æ°Tã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void typeVariableField() throws Exception { // Java5ã®æ¡å¼µAPIã使ãGenericsåºæã®æ å ±ãåå¾ assertTrue(typeVariableField.getGenericType() instanceof TypeVariable); TypeVariable<?> typeVariable = (TypeVariable<?>)typeVariableField.getGenericType(); assertEquals("T", typeVariable.getName()); // åå¤æ°åãTã assertArrayEquals(new Type[]{Object.class}, typeVariable.getBounds()); // Tã®ä¸éï¼æå®ããã¦ããªãããObjectï¼ assertEquals(TypePatterns.class, typeVariable.getGenericDeclaration()); // Tã®å®£è¨ã¯TypePatternsã¯ã©ã¹ } /** * ãã©ã¡ã¼ã¿ã¼ååList<String>ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void parameterizedField() throws Exception { // Java5ã®æ¡å¼µAPIã使ãGenericsåºæã®æ å ±ãåå¾ assertTrue(parameterizedField.getGenericType() instanceof ParameterizedType); ParameterizedType parameterizedType = (ParameterizedType)parameterizedField.getGenericType(); assertArrayEquals(new Type[]{String.class}, parameterizedType.getActualTypeArguments()); // åå¤æ°ã«å¯¾ãããã©ã¡ã¼ã¿ã¼å¤ãStringã assertEquals(List.class, parameterizedType.getRawType()); } /** * ãã©ã¡ã¼ã¿ã¼ååList<T>ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void typeVariableParameterizedField() throws Exception { // Java5ã®æ¡å¼µAPIã使ãGenericsåºæã®æ å ±ãåå¾ assertTrue(typeVariableParameterizedField.getGenericType() instanceof ParameterizedType); ParameterizedType parameterizedType = (ParameterizedType)typeVariableParameterizedField.getGenericType(); assertEquals(1, parameterizedType.getActualTypeArguments().length); assertTrue(parameterizedType.getActualTypeArguments()[0] instanceof TypeVariable); TypeVariable<?> typeVariable = (TypeVariable<?>)parameterizedType.getActualTypeArguments()[0]; assertEquals("T", typeVariable.getName()); // åå¤æ°åãTã assertArrayEquals(new Type[]{Object.class}, typeVariable.getBounds()); // Tã®ä¸éï¼æå®ããã¦ããªãããObjectï¼ assertEquals(TypePatterns.class, typeVariable.getGenericDeclaration()); // Tã®å®£è¨ã¯TypePatternsã¯ã©ã¹ assertEquals(List.class, parameterizedType.getRawType()); } /** * ãã©ã¡ã¼ã¿ã¼åå<? extends Number>ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void wildcardParameterizedField() throws Exception { // Java5ã®æ¡å¼µAPIã使ãGenericsåºæã®æ å ±ãåå¾ assertTrue(wildcardParameterizedField.getGenericType() instanceof ParameterizedType); ParameterizedType parameterizedType = (ParameterizedType)wildcardParameterizedField.getGenericType(); assertEquals(1, parameterizedType.getActualTypeArguments().length); assertTrue(parameterizedType.getActualTypeArguments()[0] instanceof WildcardType); assertEquals(List.class, parameterizedType.getRawType()); WildcardType wildcardType = (WildcardType)parameterizedType.getActualTypeArguments()[0]; assertArrayEquals(new Type[] {Number.class}, wildcardType.getUpperBounds()); assertArrayEquals(new Type[0], wildcardType.getLowerBounds()); } /** * ç·ç§°åé åT[]ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void typeVariableArrayField() throws Exception { // Java5ã®æ¡å¼µAPIã使ãGenericsåºæã®æ å ±ãåå¾ assertTrue(typeVariableArrayField.getGenericType() instanceof GenericArrayType); GenericArrayType genericArrayType = (GenericArrayType)typeVariableArrayField.getGenericType(); assertTrue(genericArrayType.getGenericComponentType() instanceof TypeVariable); TypeVariable<?> typeVariable = (TypeVariable<?>)genericArrayType.getGenericComponentType(); assertEquals("T", typeVariable.getName()); // åå¤æ°åãTã assertArrayEquals(new Type[]{Object.class}, typeVariable.getBounds()); // Tã®ä¸éï¼æå®ããã¦ããªãããObjectï¼ assertEquals(TypePatterns.class, typeVariable.getGenericDeclaration()); // Tã®å®£è¨ã¯TypePatternsã¯ã©ã¹ } /** * ç·ç§°åé åList<String>[]ã«å¯¾ãããªãã¬ã¯ã·ã§ã³æ å ±ã®åå¾ */ @Test public void parameterizedComponentArrayField() throws Exception { // Java5ã®æ¡å¼µAPIã使ãGenericsåºæã®æ å ±ãåå¾ assertTrue(parameterizedComponentArrayField.getGenericType() instanceof GenericArrayType); GenericArrayType genericArrayType = (GenericArrayType)parameterizedComponentArrayField.getGenericType(); assertTrue(genericArrayType.getGenericComponentType() instanceof ParameterizedType); ParameterizedType parameterizedType = (ParameterizedType)genericArrayType.getGenericComponentType(); assertArrayEquals(new Type[]{String.class}, parameterizedType.getActualTypeArguments()); // åå¤æ°ã«å¯¾ãããã©ã¡ã¼ã¿ã¼å¤ãStringã assertEquals(List.class, parameterizedType.getRawType()); }
ãã®ããã«ãå ·è±¡åå¯è½ã§ãªãåã«ã¤ãã¦ãå®è¡æã«åæ å ±ãå¾ããã¨ãã§ãããã¨ãåããã¾ããå®éãHibernateãSpringãªã©ã®ææ°ã®ãã¼ã¸ã§ã³ã§ã¯ããããç·ç§°åã®æ å ±ãã¡ã¿æ å ±ã¨ãã¦æå¹ã«æ´»ç¨ãã¦ãã¾ãããªããç·ç§°åããã¦ããªãJDK1.4äºæã®åã®å ´åãgetGenericType()ã¡ã½ããã¯getType()ã¡ã½ããã¨åæ§ã«Class<?>ãè¿ãããã«ãªã£ã¦ãã¾ããClassã¯Typeãå®è£ ãã¦ãããããã¤ãã¤ã¾ãåã£ã¦ãã¾ããï¼ãµã³ãã«ããã°ã©ã ã¯gistã«ã¢ãããã¦ããã¾ããhttps://gist.github.com/888253ï¼
ã¾ã¨ã
以ä¸ã®è¦ç¹ãã¾ã¨ãã¾ãã
- JDK1.4以åã®åã¯Classã¯ã©ã¹ã¨ä¸å¯¾ä¸ã«å¯¾å¿ãã¦ãã
- Java5以éã§ãå ·è±¡åå¯è½åã¯Classã¯ã©ã¹ã¨å¯¾å¿ãã¦ãããäºææ§ãç¶æããã¦ããã
- åæ¶å»ã«ãã£ã¦å ·è±¡åå¯è½ã§ãªãåã®æ å ±ã¯æ¶å»ãããããã³ã¡ã³ãçãªã¡ã¿æ å ±ã¨ãã¦ãªãã¬ã¯ã·ã§ã³APIã使ã£ã¦åå¾ãããã¨ãã§ããã
- Java5以éã§ã¯ç·ç§°åã®æ å ±ãåå¾ããããTypeã¤ã³ã¿ã¼ãã§ã¼ã¹ãé ç¹ã¨ããã¡ã¿åã®é層ãæä¾ããã¦ããã
ãªãã¬ã¯ã·ã§ã³APIã¯ååã®ã¨ããJavaèªèº«ã®ç¶æ ãå®è¡æã«æ ãåºãããã®ãã®ã§ãããAPIã®ã¯ã©ã¹è¨è¨ã«Javaè¨èªã®ä»æ§ã表ç¾ããã¦ãã¾ããæ®æ®µãªãã¬ã¯ã·ã§ã³APIã使ããªãã¨ãã人ãã使ãæ¹ã調æ»ãã¦ã¿ãã¨ãJavaè¨èªã®ä»æ§ã«é¢ãã¦ç解ãæ·±ãããã¨ãã§ããã¨æãã¾ãã
*1:Java6ã§ã¯javax.lang.model.typeããã±ã¼ã¸é ä¸ã«ãåä¸åã®ã¯ã©ã¹ç¾¤ãæ ¼ç´ããã¦ãã¾ãããããã¯å®è¡æã®ãªãã¬ã¯ã·ã§ã³ã¨ã¯ç¡é¢ä¿ã§ã½ã¼ã¹ã³ã¼ãä¸ã®è§£æã«é¢ãããã®ã§ãã