1 /*
2 * Copyright 2012 Olivier Godineau
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 * License for the specific language governing permissions and limitations under
12 * the License.
13 */
14 package olg.csv.bean;
15
16 import java.beans.BeanInfo;
17 import java.beans.IntrospectionException;
18 import java.beans.Introspector;
19 import java.beans.PropertyDescriptor;
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Modifier;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27 * Util in loading objects from XML Documents and identifying getter and setter
28 * method.
29 *
30 * @author Olivier Godineau
31 *
32 */
33 public final class Util { // NOPMD by olivier on 28/01/12 14:39
34
35 /**
36 * the class logger.
37 */
38 private static final Logger LOGGER = LoggerFactory.getLogger(Util.class);
39
40 /**
41 * private constructor to prevent its instantiation.
42 */
43 private Util() {
44
45 }
46 /**
47 * Returns Class corresponding to the given field identify by its setter.
48 *
49 * @param clazz
50 * class which has the given field
51 * @param field
52 * field name
53 * @param <T>
54 * the type of the class
55 * @return declared class by the setter as the returned type
56 * @throws NoSuchMethodException
57 * if no setter found
58 */
59 public static <T> Class<?> identifySetType(Class<T> clazz, String field) throws NoSuchMethodException {
60
61 Method setter = identifySetter(clazz, field);
62
63 if (setter == null) {
64 throw new NoSuchMethodException(field + " has no setter");
65 }
66 return setter.getParameterTypes()[0];
67
68 }
69
70 /**
71 * Returns public setter corresponding to the given field.
72 *
73 * @param clazz
74 * class which has the given field
75 * @param field
76 * field name
77 * @param <T>
78 * the type of the class.
79 * @return <code>null</code> if no public setter found
80 */
81 public static <T> Method identifySetter(Class<T> clazz, String field) {
82 Method method = null;
83 if (clazz != null && field != null) {
84 try {
85 BeanInfo info = Introspector.getBeanInfo(clazz);
86 for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
87 if (field.equals(pd.getName())) {
88 method = pd.getWriteMethod();
89 break;
90 }
91 }
92 } catch (IntrospectionException e) {
93 LOGGER.error(String.format("%s.%s setter search : ", clazz.getSimpleName(), field), e);
94 }
95 }
96 return method;
97
98 }
99
100 /**
101 * Returns public getter corresponding to the given field.
102 *
103 * @param clazz
104 * class which has the given field
105 * @param field
106 * field name
107 * @param <T>
108 * the type of the class.
109 * @return <code>null</code> if no public getter found
110 */
111
112 public static <T> Method identifyGetter(Class<T> clazz, String field) {
113 Method method = null;
114 if (clazz != null && field != null) {
115 try {
116 BeanInfo info = Introspector.getBeanInfo(clazz);
117 for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
118 if (field.equals(pd.getName())) {
119 method = pd.getReadMethod();
120 break;
121 }
122 }
123 } catch (IntrospectionException e) {
124 LOGGER.error(String.format("%s.%s getter search : ", clazz.getSimpleName(), field), e);
125 }
126 }
127 return method;
128
129 }
130
131 /**
132 * Convert empty String to <code>null</code>.
133 *
134 * @param str
135 * String to convert
136 * @return the converted value.
137 */
138 public static String emptyToNull(String str) {
139 String retour = null;
140 if (!"".equals(str)) {
141 retour = str;
142 }
143 return retour;
144 }
145
146 /**
147 * Definie if a class is a subclass of an another class.
148 *
149 * @param clazzA
150 * the class.
151 * @param clazzB
152 * the subclass candidate.
153 * @return true if clazzB is a subclass of clazzA.
154 */
155 public static boolean asSubClass(Class<?> clazzA, Class<?> clazzB) {
156 try {
157 clazzB.asSubclass(clazzA);
158 return true;
159 } catch (ClassCastException ex) {
160 // Nothing
161 }
162 return false;
163 }
164
165 /**
166 * Returns if a class is concrete and not an enum class.
167 *
168 * @param clazz
169 * the class.
170 * @param <T>
171 * the type of the class.
172 * @return true if the class is concrete.
173 */
174 public static <T> boolean isConcrete(Class<T> clazz) {
175 return clazz.isPrimitive() || clazz.isEnum()
176 || (!clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()));
177 }
178
179 }