File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\ReflectionUtils.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Oracle Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import org.apache.commons.beanutils.PropertyUtils;
27 import org.kohsuke.stapler.ClassDescriptor;
28
29 import java.beans.PropertyDescriptor;
30 import java.lang.annotation.Annotation;
31 import java.lang.reflect.Field;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
34 import java.lang.reflect.Type;
35 import java.util.AbstractList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 /**
41 * Utility code for reflection.
42 *
43 * @author Kohsuke Kawaguchi
44 * @since 1.351
45 */
46 public class ReflectionUtils extends org.springframework.util.ReflectionUtils {
47 /**
48 * Finds a public method of the given name, regardless of its parameter definitions,
49 */
50 public static Method getPublicMethodNamed(Class c, String methodName) {
51 for( Method m : c.getMethods() )
52 if(m.getName().equals(methodName))
53 return m;
54 return null;
55 }
56
57 /**
58 * Returns an object-oriented view of parameters of each type.
59 */
60 public static List<Parameter> getParameters(Method m) {
61 return new MethodInfo(m);
62 }
63
64 public static Object getPublicProperty(Object o, String p) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
65 PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(o, p);
66 if(pd==null) {
67 // field?
68 try {
69 Field f = o.getClass().getField(p);
70 return f.get(o);
71 } catch (NoSuchFieldException e) {
72 throw new IllegalArgumentException("No such property "+p+" on "+o.getClass());
73 }
74 } else {
75 return PropertyUtils.getProperty(o, p);
76 }
77 }
78
79 /**
80 * Most reflection operations give us properties of parameters in a batch,
81 * so we use this object to store them, then {@link Parameter} will created
82 * more object-oriented per-parameter view.
83 */
84 private static final class MethodInfo extends AbstractList<Parameter> {
85 private final Method method;
86 private final Class<?>[] types;
87 private Type[] genericTypes;
88 private Annotation[][] annotations;
89 private String[] names;
90
91 private MethodInfo(Method method) {
92 this.method = method;
93 types = method.getParameterTypes();
94 }
95
96 @Override
97 public Parameter get(int index) {
98 return new Parameter(this,index);
99 }
100
101 @Override
102 public int size() {
103 return types.length;
104 }
105
106 public Type[] genericTypes() {
107 if (genericTypes==null)
108 genericTypes = method.getGenericParameterTypes();
109 return genericTypes;
110 }
111
112 public Annotation[][] annotations() {
113 if (annotations==null)
114 annotations = method.getParameterAnnotations();
115 return annotations;
116 }
117
118 public String[] names() {
119 if (names==null)
120 names = ClassDescriptor.loadParameterNames(method);
121 return names;
122 }
123 }
124
125 public static final class Parameter {
126 private final MethodInfo parent;
127 private final int index;
128
129 public Parameter(MethodInfo parent, int index) {
130 this.parent = parent;
131 this.index = index;
132 }
133
134 /**
135 * 0-origin index of this parameter.
136 */
137 public int index() {
138 return index;
139 }
140
141 /**
142 * Gets the type of this parameter.
143 */
144 public Class<?> type() {
145 return parent.types[index];
146 }
147
148 /**
149 * Gets the unerased generic type of this parameter.
150 */
151 public Type genericType() {
152 return parent.genericTypes()[index];
153 }
154
155 /**
156 * Gets all the annotations on this parameter.
157 */
158 public Annotation[] annotations() {
159 return parent.annotations()[index];
160 }
161
162 /**
163 * Gets the specified annotation on this parameter or null.
164 */
165 public <A extends Annotation> A annotation(Class<A> type) {
166 for (Annotation a : annotations())
167 if (a.annotationType()==type)
168 return type.cast(a);
169 return null;
170 }
171
172 /**
173 * Name of this parameter.
174 *
175 * If unknown, this method returns null.
176 */
177 public String name() {
178 String[] names = parent.names();
179 if (index<names.length)
180 return names[index];
181 return null;
182 }
183 }
184
185 /**
186 * Given the primitive type, returns the VM default value for that type in a boxed form.
187 */
188 public static Object getVmDefaultValueForPrimitiveType(Class<?> type) {
189 return defaultPrimitiveValue.get(type);
190 }
191
192 private static final Map<Class,Object> defaultPrimitiveValue = new HashMap<Class, Object>();
193 static {
194 defaultPrimitiveValue.put(boolean.class,false);
195 defaultPrimitiveValue.put(int.class,0);
196 defaultPrimitiveValue.put(long.class,0L);
197 }
198 }
199