23
23
*/
24
24
package org .jenkinsci .plugins .tokenmacro ;
25
25
26
+ import static java .lang .annotation .ElementType .*;
27
+ import static java .lang .annotation .RetentionPolicy .*;
28
+
26
29
import com .google .common .collect .ListMultimap ;
27
30
import hudson .FilePath ;
28
31
import hudson .model .AbstractBuild ;
29
32
import hudson .model .Run ;
30
33
import hudson .model .TaskListener ;
31
- import org .apache .commons .beanutils .ConvertUtils ;
32
-
33
34
import java .beans .Introspector ;
34
35
import java .io .IOException ;
35
36
import java .lang .annotation .Retention ;
41
42
import java .util .Map ;
42
43
import java .util .Map .Entry ;
43
44
import java .util .concurrent .ConcurrentHashMap ;
44
-
45
- import static java .lang .annotation .ElementType .*;
46
- import static java .lang .annotation .RetentionPolicy .*;
47
-
45
+ import org .apache .commons .beanutils .ConvertUtils ;
48
46
import org .apache .commons .lang .StringEscapeUtils ;
49
47
import org .apache .commons .lang .StringUtils ;
50
48
64
62
public abstract class DataBoundTokenMacro extends TokenMacro {
65
63
66
64
@ Retention (RUNTIME )
67
- @ Target ({FIELD ,METHOD })
65
+ @ Target ({FIELD , METHOD })
68
66
public @interface Parameter {
69
67
boolean required () default false ;
70
68
71
- String alias () default "" ;
69
+ String alias () default "" ;
72
70
}
73
71
74
72
private interface Setter {
@@ -85,7 +83,8 @@ private interface Setter {
85
83
boolean required ();
86
84
}
87
85
88
- private Map <String ,Setter > setters ;
86
+ private Map <String , Setter > setters ;
87
+
89
88
@ Parameter
90
89
public boolean escapeHtml = false ;
91
90
@@ -97,27 +96,27 @@ public DataBoundTokenMacro() {
97
96
* Builds up the {@link #setters} map that encapsulates where/how to set the value.
98
97
*/
99
98
private void buildMap () {
100
- if (setters != null ) return ;
99
+ if (setters != null ) return ;
101
100
102
101
setters = new ConcurrentHashMap <>();
103
102
for (final Field f : getClass ().getFields ()) {
104
103
final Parameter p = f .getAnnotation (Parameter .class );
105
- if (p !=null ) {
104
+ if (p != null ) {
106
105
String name = f .getName ();
107
106
if (StringUtils .isNotEmpty (p .alias ())) {
108
107
name = p .alias ();
109
108
}
110
109
111
- setters .put (name ,new Setter () {
110
+ setters .put (name , new Setter () {
112
111
public Class <?> getType () {
113
112
return f .getType ();
114
113
}
115
114
116
115
public void set (Object target , Object value ) {
117
116
try {
118
- f .set (target ,value );
117
+ f .set (target , value );
119
118
} catch (IllegalAccessException e ) {
120
- throw (IllegalAccessError )new IllegalAccessError (e .getMessage ()).initCause (e );
119
+ throw (IllegalAccessError ) new IllegalAccessError (e .getMessage ()).initCause (e );
121
120
}
122
121
}
123
122
@@ -130,10 +129,11 @@ public boolean required() {
130
129
131
130
for (final Method m : getClass ().getMethods ()) {
132
131
final Parameter p = m .getAnnotation (Parameter .class );
133
- if (p !=null ) {
132
+ if (p != null ) {
134
133
final Class <?>[] pt = m .getParameterTypes ();
135
- if (pt .length !=1 )
136
- throw new IllegalArgumentException ("Expecting one-arg method for @Parameter but found " +m +" instead" );
134
+ if (pt .length != 1 )
135
+ throw new IllegalArgumentException (
136
+ "Expecting one-arg method for @Parameter but found " + m + " instead" );
137
137
138
138
String name = m .getName ();
139
139
if (name .startsWith ("set" )) {
@@ -144,16 +144,16 @@ public boolean required() {
144
144
name = p .alias ();
145
145
}
146
146
147
- setters .put (name ,new Setter () {
147
+ setters .put (name , new Setter () {
148
148
public Class <?> getType () {
149
149
return pt [0 ];
150
150
}
151
151
152
152
public void set (Object target , Object value ) {
153
153
try {
154
- m .invoke (target ,value );
154
+ m .invoke (target , value );
155
155
} catch (IllegalAccessException e ) {
156
- throw (IllegalAccessError )new IllegalAccessError (e .getMessage ()).initCause (e );
156
+ throw (IllegalAccessError ) new IllegalAccessError (e .getMessage ()).initCause (e );
157
157
} catch (InvocationTargetException e ) {
158
158
throw new Error (e );
159
159
}
@@ -167,28 +167,30 @@ public boolean required() {
167
167
}
168
168
}
169
169
170
- private DataBoundTokenMacro prepare (String macroName , Map <String , String > arguments , ListMultimap <String , String > argumentMultimap ) throws MacroEvaluationException {
170
+ private DataBoundTokenMacro prepare (
171
+ String macroName , Map <String , String > arguments , ListMultimap <String , String > argumentMultimap )
172
+ throws MacroEvaluationException {
171
173
DataBoundTokenMacro copy ;
172
174
try {
173
175
copy = getClass ().newInstance ();
174
176
175
177
for (Entry <String , String > e : argumentMultimap .entries ()) {
176
178
Setter s = setters .get (e .getKey ());
177
- if (s ==null )
178
- throw new MacroEvaluationException (MessageFormat .format ("Undefined parameter {0} in token {1}" , e .getKey (),macroName ));
179
+ if (s == null )
180
+ throw new MacroEvaluationException (
181
+ MessageFormat .format ("Undefined parameter {0} in token {1}" , e .getKey (), macroName ));
179
182
180
183
Object v ;
181
- if (s .getType ()==boolean .class && e .getValue ()==null )
182
- v = true ;
183
- else
184
- v = ConvertUtils .convert (e .getValue (), s .getType ());
184
+ if (s .getType () == boolean .class && e .getValue () == null ) v = true ;
185
+ else v = ConvertUtils .convert (e .getValue (), s .getType ());
185
186
186
187
s .set (copy , v );
187
188
}
188
189
189
190
for (Entry <String , Setter > e : setters .entrySet ()) {
190
191
if (!arguments .containsKey (e .getKey ()) && e .getValue ().required ())
191
- throw new MacroEvaluationException (MessageFormat .format ("Parameter {0} in token {1} is required but was not specfified" , e .getKey (), macroName ));
192
+ throw new MacroEvaluationException (MessageFormat .format (
193
+ "Parameter {0} in token {1} is required but was not specfified" , e .getKey (), macroName ));
192
194
}
193
195
} catch (InstantiationException e ) {
194
196
throw new Error (e );
@@ -200,8 +202,14 @@ private DataBoundTokenMacro prepare(String macroName, Map<String, String> argume
200
202
}
201
203
202
204
@ Override
203
- public String evaluate (AbstractBuild <?, ?> build , TaskListener listener , String macroName , Map <String , String > arguments , ListMultimap <String , String > argumentMultimap ) throws MacroEvaluationException , IOException , InterruptedException {
204
- DataBoundTokenMacro copy = prepare (macroName ,arguments ,argumentMultimap );
205
+ public String evaluate (
206
+ AbstractBuild <?, ?> build ,
207
+ TaskListener listener ,
208
+ String macroName ,
209
+ Map <String , String > arguments ,
210
+ ListMultimap <String , String > argumentMultimap )
211
+ throws MacroEvaluationException , IOException , InterruptedException {
212
+ DataBoundTokenMacro copy = prepare (macroName , arguments , argumentMultimap );
205
213
String res = copy .evaluate (build , listener , macroName );
206
214
if (copy .escapeHtml && !copy .handlesHtmlEscapeInternally ()) {
207
215
res = StringEscapeUtils .escapeHtml (res );
@@ -210,8 +218,15 @@ public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String
210
218
}
211
219
212
220
@ Override
213
- public String evaluate (Run <?,?> run , FilePath workspace , TaskListener listener , String macroName , Map <String , String > arguments , ListMultimap <String , String > argumentMultimap ) throws MacroEvaluationException , IOException , InterruptedException {
214
- DataBoundTokenMacro copy = prepare (macroName ,arguments ,argumentMultimap );
221
+ public String evaluate (
222
+ Run <?, ?> run ,
223
+ FilePath workspace ,
224
+ TaskListener listener ,
225
+ String macroName ,
226
+ Map <String , String > arguments ,
227
+ ListMultimap <String , String > argumentMultimap )
228
+ throws MacroEvaluationException , IOException , InterruptedException {
229
+ DataBoundTokenMacro copy = prepare (macroName , arguments , argumentMultimap );
215
230
String res = copy .evaluate (run , workspace , listener , macroName );
216
231
if (copy .escapeHtml && !copy .handlesHtmlEscapeInternally ()) {
217
232
res = StringEscapeUtils .escapeHtml (res );
@@ -234,9 +249,11 @@ public boolean handlesHtmlEscapeInternally() {
234
249
return false ;
235
250
}
236
251
237
- public abstract String evaluate (AbstractBuild <?, ?> context , TaskListener listener , String macroName ) throws MacroEvaluationException , IOException , InterruptedException ;
252
+ public abstract String evaluate (AbstractBuild <?, ?> context , TaskListener listener , String macroName )
253
+ throws MacroEvaluationException , IOException , InterruptedException ;
238
254
239
- public String evaluate (Run <?,?> run , FilePath workspace , TaskListener listener , String macroName ) throws MacroEvaluationException , IOException , InterruptedException {
255
+ public String evaluate (Run <?, ?> run , FilePath workspace , TaskListener listener , String macroName )
256
+ throws MacroEvaluationException , IOException , InterruptedException {
240
257
return macroName + " is not supported in this context" ;
241
258
}
242
259
0 commit comments