Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IntrospectorUtils |
|
| 4.166666666666667;4.167 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.beanutils; | |
18 | ||
19 | import java.beans.IntrospectionException; | |
20 | import java.beans.PropertyDescriptor; | |
21 | import java.lang.reflect.Method; | |
22 | import java.lang.reflect.Modifier; | |
23 | ||
24 | /** | |
25 | * An internal class used for working with {@link java.beans.Introspector}. | |
26 | */ | |
27 | class IntrospectorUtils { | |
28 | ||
29 | 0 | private IntrospectorUtils() { |
30 | 0 | throw new UnsupportedOperationException("do not call"); |
31 | } | |
32 | ||
33 | /** | |
34 | * Property descriptors may contain synthetic methods for the read and write method | |
35 | * due to a bug in the java api. This is a workaround. | |
36 | * | |
37 | * <p>if beanClass or descriptors this method will just return</p> | |
38 | * | |
39 | * <p/> | |
40 | * See: | |
41 | * <p/> | |
42 | * http://bugs.sun.com/view_bug.do?bug_id=6528714 | |
43 | * https://issues.apache.org/jira/browse/BEANUTILS-340 | |
44 | * | |
45 | * @param descriptors the descriptors to set | |
46 | * @param beanClass the class the descriptors are associated with | |
47 | */ | |
48 | static void setNonSyntheticReadWriteMethods(Class beanClass, PropertyDescriptor[] descriptors) throws NoSuchMethodException, IntrospectionException { | |
49 | 263 | if (beanClass == null || descriptors == null) { |
50 | 0 | return; |
51 | } | |
52 | ||
53 | 4630 | for (int i = 0; i < descriptors.length; i++) { |
54 | 4367 | if (descriptors[i] != null) { |
55 | 4367 | final PropertyDescriptor descriptor = descriptors[i]; |
56 | 4367 | Method read = descriptor.getReadMethod(); |
57 | 4367 | Method write = descriptor.getWriteMethod(); |
58 | ||
59 | 4367 | descriptor.setReadMethod(null); |
60 | 4367 | descriptor.setWriteMethod(null); |
61 | ||
62 | 4367 | if (MethodUtils.isSynthetic(read) || MethodUtils.isBridge(read)) { |
63 | 1 | read = findReadMethodNonSynPreference(beanClass, read.getName()); |
64 | } | |
65 | ||
66 | 4367 | if (MethodUtils.isSynthetic(write) || MethodUtils.isBridge(write)) { |
67 | //use the read method's return type as the type for the write method. | |
68 | //The PropertyDescriptor classes forces them to be the same type so this is ok | |
69 | 1 | if (read != null) { |
70 | 1 | write = beanClass.getMethod(write.getName(), new Class[]{ read.getReturnType()}); |
71 | } else { | |
72 | //rare case where there is only a write - single arg overloading here would be non-deterministic | |
73 | 0 | final Method searchedWrite = findWriteMethodNonSynPreference(beanClass, write.getName()); |
74 | 0 | if (searchedWrite != null) { |
75 | 0 | write = searchedWrite; |
76 | } | |
77 | } | |
78 | } | |
79 | ||
80 | 4367 | descriptor.setReadMethod(read); |
81 | 4367 | descriptor.setWriteMethod(write); |
82 | } | |
83 | } | |
84 | 263 | } |
85 | ||
86 | /** | |
87 | * Searches all methods on a class looking for a write method that is not synthetic & visible. If found, that method | |
88 | * is returned. If a non-synthetic, visible method is NOT found then the a visible synthetic method may be returned. | |
89 | * @param beanClass the class to search | |
90 | * @param methodName the write method name | |
91 | * @return the method or null | |
92 | */ | |
93 | private static Method findWriteMethodNonSynPreference(Class beanClass, String methodName) { | |
94 | 0 | return findMethodNonSynPreference(beanClass, methodName, 1); |
95 | } | |
96 | ||
97 | /** | |
98 | * Searches all methods on a class looking for a read method that is not synthetic & visible. If found, that method | |
99 | * is returned. If a non-synthetic, visible method is NOT found then the a visible synthetic method may be returned. | |
100 | * @param beanClass the class to search | |
101 | * @param methodName the write method name | |
102 | * @return the method or null | |
103 | */ | |
104 | private static Method findReadMethodNonSynPreference(Class beanClass, String methodName) { | |
105 | 1 | return findMethodNonSynPreference(beanClass, methodName, 0); |
106 | } | |
107 | ||
108 | /** searches for the method with a given name and number of args. */ | |
109 | private static Method findMethodNonSynPreference(Class beanClass, String methodName, int argNum) { | |
110 | 1 | final Method[] methods = beanClass.getMethods(); |
111 | 1 | Method fallback = null; |
112 | 14 | for (int i = 0; i < methods.length; i ++) { |
113 | 13 | final Method m = methods[i]; |
114 | 13 | if (methodName.equals(m.getName()) && m.getParameterTypes().length == argNum && !isPrivate(m)) { |
115 | 1 | if (!MethodUtils.isSynthetic(m) && !MethodUtils.isBridge(m)) { |
116 | 0 | return m; |
117 | } | |
118 | 1 | fallback = m; |
119 | } | |
120 | } | |
121 | 1 | return fallback; |
122 | } | |
123 | ||
124 | /** is a method private? */ | |
125 | private static boolean isPrivate(Method m) { | |
126 | 1 | return (m.getModifiers() & Modifier.PRIVATE) != 0; |
127 | ||
128 | } | |
129 | } |