1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2008 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
19 // TypeReferenceTest.java
\r
20 // Since: 2008/10/29 8:06:17
\r
24 //--------------------------------------
\r
25 package org.xerial.util.bean;
\r
27 import static org.junit.Assert.assertEquals;
\r
28 import static org.junit.Assert.assertNotNull;
\r
30 import java.lang.reflect.Type;
\r
31 import java.util.List;
\r
32 import java.util.Map;
\r
34 import org.junit.Test;
\r
37 public class TypeReferenceTest
\r
40 public void reference()
\r
42 Type c = new TypeReference<List<String>>() {}.getElementType()[0];
\r
43 assertEquals(String.class, c);
\r
45 Type k = new TypeReference<Map<String, Integer>>() {}.getElementType()[0];
\r
46 Type v = new TypeReference<Map<String, Integer>>() {}.getElementType()[1];
\r
47 assertEquals(String.class, k);
\r
48 assertEquals(Integer.class, v);
\r
52 public void elementType()
\r
54 Type c = new TypeReference<String>() {}.getType();
\r
55 assertEquals(String.class, c);
\r
58 class GenericReference
\r
62 public GenericReference(Class< ? > c)
\r
67 Object newInstance()
\r
71 return TypeInfo.createInstance(c);
\r
73 catch (BeanException e)
\r
82 public void genericTypeReference()
\r
84 GenericReference gref = new GenericReference(String.class);
\r
86 String str = (String) gref.newInstance();
\r
89 assertEquals(String.class, str.getClass());
\r