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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
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.cli.declarative;
25
26 import hudson.cli.CLICommand;
27 import hudson.util.ReflectionUtils;
28 import hudson.util.ReflectionUtils.Parameter;
29 import org.kohsuke.args4j.Argument;
30 import org.kohsuke.args4j.CmdLineException;
31 import org.kohsuke.args4j.CmdLineParser;
32 import org.kohsuke.args4j.Option;
33 import org.kohsuke.args4j.spi.Setter;
34 import org.kohsuke.args4j.spi.OptionHandler;
35
36 import java.lang.annotation.Annotation;
37 import java.lang.reflect.InvocationTargetException;
38 import java.lang.reflect.Method;
39 import java.util.List;
40
41 /**
42 * Binds method parameters to CLI arguments and parameters via args4j.
43 * Once the parser fills in the instance state, {@link #call(Object)}
44 * can be used to invoke a method.
45 *
46 * @author Kohsuke Kawaguchi
47 */
48 class MethodBinder {
49 private final CLICommand command;
50 private final Method method;
51 private final Object[] arguments;
52
53 /**
54 * @param method
55 */
56 public MethodBinder(Method method, CLICommand command, CmdLineParser parser) {
57 this.method = method;
58 this.command = command;
59
60 List<Parameter> params = ReflectionUtils.getParameters(method);
61 arguments = new Object[params.size()];
62
63 // to work in cooperation with earlier arguments, add bias to all the ones that this one defines.
64 final int bias = parser.getArguments().size();
65
66 for (final Parameter p : params) {
67 final int index = p.index();
68
69 // TODO: collection and map support
70 Setter setter = new Setter() {
71 public void addValue(Object value) throws CmdLineException {
72 arguments[index] = value;
73 }
74
75 public Class getType() {
76 return p.type();
77 }
78
79 public boolean isMultiValued() {
80 return false;
81 }
82 };
83 Option option = p.annotation(Option.class);
84 if (option!=null) {
85 parser.addOption(setter,option);
86 }
87 Argument arg = p.annotation(Argument.class);
88 if (arg!=null) {
89 if (bias>0) arg = new ArgumentImpl(arg,bias);
90 parser.addArgument(setter,arg);
91 }
92 if (p.type()==CLICommand.class)
93 arguments[index] = command;
94
95 if (p.type().isPrimitive())
96 arguments[index] = ReflectionUtils.getVmDefaultValueForPrimitiveType(p.type());
97 }
98 }
99
100 public Object call(Object instance) throws Exception {
101 try {
102 return method.invoke(instance,arguments);
103 } catch (InvocationTargetException e) {
104 Throwable t = e.getTargetException();
105 if (t instanceof Exception)
106 throw (Exception) t;
107 throw e;
108 }
109 }
110
111 /**
112 * {@link Argument} implementation that adds a bias to {@link #index()}.
113 */
114 @SuppressWarnings({"ClassExplicitlyAnnotation"})
115 private static final class ArgumentImpl implements Argument {
116 private final Argument base;
117 private final int bias;
118
119 private ArgumentImpl(Argument base, int bias) {
120 this.base = base;
121 this.bias = bias;
122 }
123
124 public String usage() {
125 return base.usage();
126 }
127
128 public String metaVar() {
129 return base.metaVar();
130 }
131
132 public boolean required() {
133 return base.required();
134 }
135
136 public Class<? extends OptionHandler> handler() {
137 return base.handler();
138 }
139
140 public int index() {
141 return base.index()+bias;
142 }
143
144 public boolean multiValued() {
145 return base.multiValued();
146 }
147
148 public Class<? extends Annotation> annotationType() {
149 return base.annotationType();
150 }
151 }
152 }
153