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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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.search;
25
26 import java.lang.reflect.Field;
27 import java.lang.reflect.Method;
28 import java.lang.reflect.InvocationTargetException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.HashMap;
33 import java.beans.Introspector;
34
35 /**
36 * Parsed {@link QuickSilver}s so that {@link SearchIndex} can be easily created.
37 * One instance per one class.
38 *
39 * @author Kohsuke Kawaguchi
40 */
41 final class ParsedQuickSilver {
42 private static final Map<Class,ParsedQuickSilver> TABLE = new HashMap<Class,ParsedQuickSilver>();
43
44 synchronized static ParsedQuickSilver get(Class<? extends SearchableModelObject> clazz) {
45 ParsedQuickSilver pqs = TABLE.get(clazz);
46 if(pqs==null)
47 TABLE.put(clazz,pqs = new ParsedQuickSilver(clazz));
48 return pqs;
49 }
50
51 private final List<Getter> getters = new ArrayList<Getter>();
52
53 private ParsedQuickSilver(Class<? extends SearchableModelObject> clazz) {
54 QuickSilver qs;
55
56 for (Method m : clazz.getMethods()) {
57 qs = m.getAnnotation(QuickSilver.class);
58 if(qs!=null) {
59 String url = stripGetPrefix(m);
60 if(qs.value().length==0)
61 getters.add(new MethodGetter(url,splitName(url),m));
62 else {
63 for (String name : qs.value())
64 getters.add(new MethodGetter(url,name,m));
65 }
66 }
67 }
68 for (Field f : clazz.getFields()) {
69 qs = f.getAnnotation(QuickSilver.class);
70 if(qs!=null) {
71 if(qs.value().length==0)
72 getters.add(new FieldGetter(f.getName(),splitName(f.getName()),f));
73 else {
74 for (String name : qs.value())
75 getters.add(new FieldGetter(f.getName(),name,f));
76 }
77 }
78 }
79 }
80
81 /**
82 * Convert names like "abcDefGhi" to "abc def ghi".
83 */
84 private String splitName(String url) {
85 StringBuilder buf = new StringBuilder(url.length()+5);
86 for(String token : url.split("(?<=[a-z])(?=[A-Z])")) {
87 if(buf.length()>0) buf.append(' ');
88 buf.append(Introspector.decapitalize(token));
89 }
90 return buf.toString();
91 }
92
93 private String stripGetPrefix(Method m) {
94 String n = m.getName();
95 if(n.startsWith("get"))
96 n = Introspector.decapitalize(n.substring(3));
97 return n;
98 }
99
100
101 static abstract class Getter {
102 final String url;
103 final String searchName;
104
105 protected Getter(String url, String searchName) {
106 this.url = url;
107 this.searchName = searchName;
108 }
109
110 abstract Object get(Object obj);
111 }
112
113 static final class MethodGetter extends Getter {
114 private final Method method;
115
116 public MethodGetter(String url, String searchName, Method method) {
117 super(url, searchName);
118 this.method = method;
119 }
120
121 Object get(Object obj) {
122 try {
123 return method.invoke(obj);
124 } catch (IllegalAccessException e) {
125 throw toError(e);
126 } catch (InvocationTargetException e) {
127 Throwable x = e.getTargetException();
128 if (x instanceof Error)
129 throw (Error) x;
130 if (x instanceof RuntimeException)
131 throw (RuntimeException) x;
132 throw new Error(e);
133 }
134 }
135 }
136
137 static final class FieldGetter extends Getter {
138 private final Field field;
139
140 public FieldGetter(String url, String searchName, Field field) {
141 super(url, searchName);
142 this.field = field;
143 }
144
145 Object get(Object obj) {
146 try {
147 return field.get(obj);
148 } catch (IllegalAccessException e) {
149 throw toError(e);
150 }
151 }
152 }
153
154 private static IllegalAccessError toError(IllegalAccessException e) {
155 IllegalAccessError iae = new IllegalAccessError();
156 iae.initCause(e);
157 return iae;
158 }
159
160 public void addTo(SearchIndexBuilder builder, final Object instance) {
161 for (final Getter getter : getters)
162 builder.add(new SearchItem() {
163 public String getSearchName() {
164 return getter.searchName;
165 }
166
167 public String getSearchUrl() {
168 return getter.url;
169 }
170
171 public SearchIndex getSearchIndex() {
172 Object child = getter.get(instance);
173 if(child==null) return SearchIndex.EMPTY;
174 return ((SearchableModelObject) child).getSearchIndex();
175 }
176 });
177 }
178 }
179