2323 */
2424package org .jenkinsci .plugins .tokenmacro ;
2525
26+ import static java .lang .annotation .ElementType .*;
27+ import static java .lang .annotation .RetentionPolicy .*;
28+
2629import com .google .common .collect .ListMultimap ;
2730import hudson .FilePath ;
2831import hudson .model .AbstractBuild ;
2932import hudson .model .Run ;
3033import hudson .model .TaskListener ;
31- import org .apache .commons .beanutils .ConvertUtils ;
32-
3334import java .beans .Introspector ;
3435import java .io .IOException ;
3536import java .lang .annotation .Retention ;
4142import java .util .Map ;
4243import java .util .Map .Entry ;
4344import 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 ;
4846import org .apache .commons .lang .StringEscapeUtils ;
4947import org .apache .commons .lang .StringUtils ;
5048
6462public abstract class DataBoundTokenMacro extends TokenMacro {
6563
6664 @ Retention (RUNTIME )
67- @ Target ({FIELD ,METHOD })
65+ @ Target ({FIELD , METHOD })
6866 public @interface Parameter {
6967 boolean required () default false ;
7068
71- String alias () default "" ;
69+ String alias () default "" ;
7270 }
7371
7472 private interface Setter {
@@ -85,7 +83,8 @@ private interface Setter {
8583 boolean required ();
8684 }
8785
88- private Map <String ,Setter > setters ;
86+ private Map <String , Setter > setters ;
87+
8988 @ Parameter
9089 public boolean escapeHtml = false ;
9190
@@ -97,27 +96,27 @@ public DataBoundTokenMacro() {
9796 * Builds up the {@link #setters} map that encapsulates where/how to set the value.
9897 */
9998 private void buildMap () {
100- if (setters != null ) return ;
99+ if (setters != null ) return ;
101100
102101 setters = new ConcurrentHashMap <>();
103102 for (final Field f : getClass ().getFields ()) {
104103 final Parameter p = f .getAnnotation (Parameter .class );
105- if (p !=null ) {
104+ if (p != null ) {
106105 String name = f .getName ();
107106 if (StringUtils .isNotEmpty (p .alias ())) {
108107 name = p .alias ();
109108 }
110109
111- setters .put (name ,new Setter () {
110+ setters .put (name , new Setter () {
112111 public Class <?> getType () {
113112 return f .getType ();
114113 }
115114
116115 public void set (Object target , Object value ) {
117116 try {
118- f .set (target ,value );
117+ f .set (target , value );
119118 } catch (IllegalAccessException e ) {
120- throw (IllegalAccessError )new IllegalAccessError (e .getMessage ()).initCause (e );
119+ throw (IllegalAccessError ) new IllegalAccessError (e .getMessage ()).initCause (e );
121120 }
122121 }
123122
@@ -130,10 +129,11 @@ public boolean required() {
130129
131130 for (final Method m : getClass ().getMethods ()) {
132131 final Parameter p = m .getAnnotation (Parameter .class );
133- if (p !=null ) {
132+ if (p != null ) {
134133 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" );
137137
138138 String name = m .getName ();
139139 if (name .startsWith ("set" )) {
@@ -144,16 +144,16 @@ public boolean required() {
144144 name = p .alias ();
145145 }
146146
147- setters .put (name ,new Setter () {
147+ setters .put (name , new Setter () {
148148 public Class <?> getType () {
149149 return pt [0 ];
150150 }
151151
152152 public void set (Object target , Object value ) {
153153 try {
154- m .invoke (target ,value );
154+ m .invoke (target , value );
155155 } catch (IllegalAccessException e ) {
156- throw (IllegalAccessError )new IllegalAccessError (e .getMessage ()).initCause (e );
156+ throw (IllegalAccessError ) new IllegalAccessError (e .getMessage ()).initCause (e );
157157 } catch (InvocationTargetException e ) {
158158 throw new Error (e );
159159 }
@@ -167,28 +167,30 @@ public boolean required() {
167167 }
168168 }
169169
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 {
171173 DataBoundTokenMacro copy ;
172174 try {
173175 copy = getClass ().newInstance ();
174176
175177 for (Entry <String , String > e : argumentMultimap .entries ()) {
176178 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 ));
179182
180183 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 ());
185186
186187 s .set (copy , v );
187188 }
188189
189190 for (Entry <String , Setter > e : setters .entrySet ()) {
190191 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 ));
192194 }
193195 } catch (InstantiationException e ) {
194196 throw new Error (e );
@@ -200,8 +202,14 @@ private DataBoundTokenMacro prepare(String macroName, Map<String, String> argume
200202 }
201203
202204 @ 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 );
205213 String res = copy .evaluate (build , listener , macroName );
206214 if (copy .escapeHtml && !copy .handlesHtmlEscapeInternally ()) {
207215 res = StringEscapeUtils .escapeHtml (res );
@@ -210,8 +218,15 @@ public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String
210218 }
211219
212220 @ 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 );
215230 String res = copy .evaluate (run , workspace , listener , macroName );
216231 if (copy .escapeHtml && !copy .handlesHtmlEscapeInternally ()) {
217232 res = StringEscapeUtils .escapeHtml (res );
@@ -234,9 +249,11 @@ public boolean handlesHtmlEscapeInternally() {
234249 return false ;
235250 }
236251
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 ;
238254
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 {
240257 return macroName + " is not supported in this context" ;
241258 }
242259
0 commit comments