View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.reflection;
17  
18  import static com.google.common.base.Preconditions.checkArgument;
19  import static com.google.common.base.Preconditions.checkState;
20  
21  import java.lang.reflect.Field;
22  import java.lang.reflect.ParameterizedType;
23  import java.lang.reflect.Type;
24  import java.lang.reflect.TypeVariable;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.commons.lang3.builder.Builder;
29  import org.junit.Assert;
30  import org.junit.Test;
31  import org.kuali.common.util.ReflectionUtils;
32  import org.kuali.common.util.log.LoggerUtils;
33  import org.slf4j.Logger;
34  
35  public class ReflectionUtilsTest {
36  
37  	private static final Logger logger = LoggerUtils.make();
38  
39  	/**
40  	 * <p>
41  	 * Given a class that implements a parameterized interface, return the actual type of the first parameterized argument for the interface
42  	 * </p>
43  	 * 
44  	 * Given:
45  	 * 
46  	 * <pre>
47  	 * interface Foo&lt;T&gt; 
48  	 * 
49  	 * class Bar implements Foo&lt;String&gt;
50  	 * </pre>
51  	 * 
52  	 * <p>
53  	 * This method returns {@code String.class}
54  	 * </p>
55  	 */
56  	public static Class<?> getFirstParameterizedTypeArgumentAsClass(Class<?> type, Class<?> parameterizedInterface) {
57  		checkArgument(parameterizedInterface.isInterface(), "[%s] is not an interface", parameterizedInterface.getCanonicalName());
58  		TypeVariable<?>[] params = parameterizedInterface.getTypeParameters();
59  		checkArgument(params.length > 0, "[%s] has no type parameters", parameterizedInterface.getCanonicalName());
60  		Map<Class<?>, ParameterizedType> interfaces = ReflectionUtils.getAllParameterizedInterfaces(type);
61  		ParameterizedType parameterizedType = interfaces.get(parameterizedInterface);
62  		checkState(parameterizedType != null, "[%s] does not implement [%s]", type.getCanonicalName(), parameterizedInterface.getCanonicalName());
63  		Type[] args = parameterizedType.getActualTypeArguments();
64  		checkState(args.length > 0, "[%s] has no actual type arguments", parameterizedInterface.getCanonicalName());
65  		Type firstTypeArgument = args[0];
66  		checkState(firstTypeArgument instanceof Class<?>, "First actual type argument of [%s] is not a class [%s]", parameterizedInterface.getCanonicalName(), firstTypeArgument);
67  		return (Class<?>) firstTypeArgument;
68  	}
69  
70  	@Test
71  	public void extractBuilderType() {
72  		try {
73  			Class<?> type = Foo.Builder2.class;
74  			Class<?> builderType = getFirstParameterizedTypeArgumentAsClass(type, Builder.class);
75  			System.out.println(builderType.getCanonicalName());
76  		} catch (Exception e) {
77  			e.printStackTrace();
78  		}
79  	}
80  
81  	@Test
82  	public void testHasMatchingParameterizedArgTypes1() throws Exception {
83  		Field field = C.class.getDeclaredField("foo");
84  		boolean condition = ReflectionUtils.hasMatchingParameterizedArgTypes(field, String.class);
85  		Assert.assertTrue("'foo' must be an optional string", condition);
86  
87  	}
88  
89  	@Test
90  	public void testHasMatchingParameterizedArgTypes2() throws Exception {
91  		Field field = C.class.getDeclaredField("bar");
92  		boolean condition = ReflectionUtils.hasMatchingParameterizedArgTypes(field, Integer.class);
93  		Assert.assertTrue("'foo' must be an optional string", condition);
94  
95  	}
96  
97  	@Test
98  	public void testHasMatchingParameterizedArgTypes3() throws Exception {
99  		Field field = C.class.getDeclaredField("bar");
100 		boolean condition = ReflectionUtils.hasMatchingParameterizedArgTypes(field, String.class);
101 		Assert.assertFalse("'foo' must be an optional string", condition);
102 
103 	}
104 
105 	@Test
106 	public void testDetectOptionalString() throws Exception {
107 		Field field = C.class.getDeclaredField("foo");
108 		boolean condition = ReflectionUtils.isOptionalString(field);
109 		Assert.assertTrue("'foo' must be an optional string", condition);
110 	}
111 
112 	@Test
113 	public void testOptionalThatIsntAString() throws Exception {
114 		Field field = C.class.getDeclaredField("bar");
115 		boolean condition = ReflectionUtils.isOptionalString(field);
116 		Assert.assertFalse("'bar' is not an optional string", condition);
117 	}
118 
119 	@Test
120 	public void testNormalString() throws Exception {
121 		Field field = C.class.getDeclaredField("baz");
122 		boolean condition = ReflectionUtils.isOptionalString(field);
123 		Assert.assertFalse("'baz' is not an optional", condition);
124 	}
125 
126 	@Test
127 	public void test() {
128 		Set<Field> fields = ReflectionUtils.getAllFields(B.class);
129 		logger.info("fields.size()={}", fields.size());
130 		Assert.assertEquals(2, fields.size());
131 	}
132 }