Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GenericProperty |
|
| 7.0;7 |
1 | /** | |
2 | * Copyright (c) 2008-2011, http://www.snakeyaml.org | |
3 | * | |
4 | * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 | |
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 | ||
17 | package org.yaml.snakeyaml.introspector; | |
18 | ||
19 | import java.lang.reflect.Array; | |
20 | import java.lang.reflect.GenericArrayType; | |
21 | import java.lang.reflect.ParameterizedType; | |
22 | import java.lang.reflect.Type; | |
23 | ||
24 | abstract public class GenericProperty extends Property { | |
25 | ||
26 | private Type genType; | |
27 | ||
28 | public GenericProperty(String name, Class<?> aClass, Type aType) { | |
29 | 58452 | super(name, aClass); |
30 | 58452 | genType = aType; |
31 | 58452 | actualClassesChecked = aType == null; |
32 | 58452 | } |
33 | ||
34 | private boolean actualClassesChecked; | |
35 | private Class<?>[] actualClasses; | |
36 | ||
37 | public Class<?>[] getActualTypeArguments() { // should we synchronize here ? | |
38 | 38523 | if (!actualClassesChecked) { |
39 | 9385 | if (genType instanceof ParameterizedType) { |
40 | 3232 | ParameterizedType parameterizedType = (ParameterizedType) genType; |
41 | 3232 | Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); |
42 | 3232 | if (actualTypeArguments.length > 0) { |
43 | 3232 | actualClasses = new Class<?>[actualTypeArguments.length]; |
44 | 6502 | for (int i = 0; i < actualTypeArguments.length; i++) { |
45 | 3280 | if (actualTypeArguments[i] instanceof Class<?>) { |
46 | 3259 | actualClasses[i] = (Class<?>) actualTypeArguments[i]; |
47 | 21 | } else if (actualTypeArguments[i] instanceof ParameterizedType) { |
48 | 3 | actualClasses[i] = (Class<?>) ((ParameterizedType) actualTypeArguments[i]) |
49 | .getRawType(); | |
50 | 18 | } else if (actualTypeArguments[i] instanceof GenericArrayType) { |
51 | 8 | Type componentType = ((GenericArrayType) actualTypeArguments[i]) |
52 | .getGenericComponentType(); | |
53 | 8 | if (componentType instanceof Class<?>) { |
54 | 8 | actualClasses[i] = Array.newInstance((Class<?>) componentType, 0) |
55 | .getClass(); | |
56 | } else { | |
57 | 0 | actualClasses = null; |
58 | 0 | break; |
59 | } | |
60 | 8 | } else { |
61 | 10 | actualClasses = null; |
62 | 10 | break; |
63 | } | |
64 | } | |
65 | } | |
66 | 3232 | } else if (genType instanceof GenericArrayType) { |
67 | 2 | Type componentType = ((GenericArrayType) genType).getGenericComponentType(); |
68 | 2 | if (componentType instanceof Class<?>) { |
69 | 0 | actualClasses = new Class<?>[] { (Class<?>) componentType }; |
70 | } | |
71 | 2 | } else if (genType instanceof Class<?>) {// XXX this check is only |
72 | // required for IcedTea6 | |
73 | 6151 | Class<?> classType = (Class<?>) genType; |
74 | 6151 | if (classType.isArray()) { |
75 | 12 | actualClasses = new Class<?>[1]; |
76 | 12 | actualClasses[0] = getType().getComponentType(); |
77 | } | |
78 | } | |
79 | 9385 | actualClassesChecked = true; |
80 | } | |
81 | 38523 | return actualClasses; |
82 | } | |
83 | } |