File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\spring\ClosureScript.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.util.spring;
25
26 import groovy.lang.Binding;
27 import groovy.lang.Closure;
28 import groovy.lang.GroovyObject;
29 import groovy.lang.MissingMethodException;
30 import groovy.lang.MissingPropertyException;
31 import groovy.lang.Script;
32
33 /**
34 * {@link Script} that performs method invocations and property access like {@link Closure} does.
35 *
36 * <p>
37 * For example, when the script is:
38 *
39 * <pre>
40 * a = 1;
41 * b(2);
42 * <pre>
43 *
44 * <p>
45 * Using {@link ClosureScript} as the base class would run it as:
46 *
47 * <pre>
48 * delegate.a = 1;
49 * delegate.b(2);
50 * </pre>
51 *
52 * ... whereas in plain {@link Script}, this will be run as:
53 *
54 * <pre>
55 * binding.setProperty("a",1);
56 * ((Closure)binding.getProperty("b")).call(2);
57 * </pre>
58 *
59 * @author Kohsuke Kawaguchi
60 */
61 // TODO: moved to stapler
62 public abstract class ClosureScript extends Script {
63 private GroovyObject delegate;
64
65 protected ClosureScript() {
66 super();
67 }
68
69 protected ClosureScript(Binding binding) {
70 super(binding);
71 }
72
73 /**
74 * Sets the delegation target.
75 */
76 public void setDelegate(GroovyObject delegate) {
77 this.delegate = delegate;
78 }
79
80 @Override
81 public Object invokeMethod(String name, Object args) {
82 try {
83 return delegate.invokeMethod(name,args);
84 } catch (MissingMethodException mme) {
85 return super.invokeMethod(name, args);
86 }
87 }
88
89 @Override
90 public Object getProperty(String property) {
91 try {
92 return delegate.getProperty(property);
93 } catch (MissingPropertyException e) {
94 return super.getProperty(property);
95 }
96 }
97
98 @Override
99 public void setProperty(String property, Object newValue) {
100 try {
101 delegate.setProperty(property,newValue);
102 } catch (MissingPropertyException e) {
103 super.setProperty(property,newValue);
104 }
105 }
106 }
107