1 package org.apache.commons.beanutils2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.commons.beanutils2.Assertions.checkNoneIsNull;
23 import static org.apache.commons.beanutils2.Assertions.checkNotNull;
24
25 final class TypeUtils
26 {
27
28
29
30
31 private TypeUtils()
32 {
33
34 }
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public static boolean isAssignmentCompatible( Class<?> parameterType, Class<?> parameterization )
54 {
55 parameterType = checkNotNull( parameterType, "parameterType must not be null!" );
56 parameterization = checkNotNull( parameterization, "parameterization must not be null!" );
57
58 if ( parameterType.isAssignableFrom( parameterization ) )
59 {
60 return true;
61 }
62
63 if ( parameterType.isPrimitive() )
64 {
65
66
67 Class<?> parameterWrapperClazz = getPrimitiveWrapper( parameterType );
68 if ( parameterWrapperClazz != null )
69 {
70 return parameterWrapperClazz.equals( parameterization );
71 }
72 }
73
74 return false;
75 }
76
77
78
79
80
81
82
83
84 public static Class<?> getPrimitiveWrapper( Class<?> primitiveType )
85 {
86
87 if ( boolean.class.equals( primitiveType ) )
88 {
89 return Boolean.class;
90 }
91 else if ( float.class.equals( primitiveType ) )
92 {
93 return Float.class;
94 }
95 else if ( long.class.equals( primitiveType ) )
96 {
97 return Long.class;
98 }
99 else if ( int.class.equals( primitiveType ) )
100 {
101 return Integer.class;
102 }
103 else if ( short.class.equals( primitiveType ) )
104 {
105 return Short.class;
106 }
107 else if ( byte.class.equals( primitiveType ) )
108 {
109 return Byte.class;
110 }
111 else if ( double.class.equals( primitiveType ) )
112 {
113 return Double.class;
114 }
115 else if ( char.class.equals( primitiveType ) )
116 {
117 return Character.class;
118 }
119 return null;
120 }
121
122
123
124
125
126
127
128
129 public static Class<?> getPrimitiveType( Class<?> wrapperType )
130 {
131
132 if ( Boolean.class.equals( wrapperType ) )
133 {
134 return boolean.class;
135 }
136 else if ( Float.class.equals( wrapperType ) )
137 {
138 return float.class;
139 }
140 else if ( Long.class.equals( wrapperType ) )
141 {
142 return long.class;
143 }
144 else if ( Integer.class.equals( wrapperType ) )
145 {
146 return int.class;
147 }
148 else if ( Short.class.equals( wrapperType ) )
149 {
150 return short.class;
151 }
152 else if ( Byte.class.equals( wrapperType ) )
153 {
154 return byte.class;
155 }
156 else if ( Double.class.equals( wrapperType ) )
157 {
158 return double.class;
159 }
160 else if ( Character.class.equals( wrapperType ) )
161 {
162 return char.class;
163 }
164 return null;
165 }
166
167
168
169
170
171
172
173 public static Class<?> toNonPrimitiveClass( Class<?> clazz )
174 {
175 clazz = checkNotNull( clazz, "Parameter clazz must not be null!" );
176 if ( clazz.isPrimitive() )
177 {
178 Class<?> primitiveClazz = getPrimitiveWrapper( clazz );
179
180 if ( primitiveClazz != null )
181 {
182 return primitiveClazz;
183 }
184 return clazz;
185 }
186 return clazz;
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200 public static boolean checkTypesCompatible( Class<?>[] types, Class<?>[] otherTypes )
201 {
202 checkNotNull( types, "Input parameter 'types' must not be null!" );
203 checkNotNull( otherTypes, "Input parameter 'otherTypes' must not be null!" );
204 checkNoneIsNull( types );
205 checkNoneIsNull( otherTypes );
206 if ( types.length != otherTypes.length )
207 {
208 return false;
209 }
210 for ( int n = 0; n < types.length; n++ )
211 {
212 if ( !isAssignmentCompatible( types[n], otherTypes[n] ) )
213 {
214 return false;
215 }
216 }
217 return true;
218 }
219
220 }