File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\JobPropertyDescriptor.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.model;
25
26 import jenkins.model.Jenkins;
27 import org.kohsuke.stapler.StaplerRequest;
28 import org.jvnet.tiger_types.Types;
29
30 import java.util.List;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.lang.reflect.Type;
34 import java.lang.reflect.ParameterizedType;
35
36 import net.sf.json.JSONObject;
37
38 /**
39 * {@link Descriptor} for {@link JobProperty}.
40 *
41 * @author Kohsuke Kawaguchi
42 * @since 1.72
43 */
44 public abstract class JobPropertyDescriptor extends Descriptor<JobProperty<?>> {
45 protected JobPropertyDescriptor(Class<? extends JobProperty<?>> clazz) {
46 super(clazz);
47 }
48
49 /**
50 * Infers the type of the corresponding {@link JobProperty} from the outer class.
51 * This version works when you follow the common convention, where a descriptor
52 * is written as the static nested class of the describable class.
53 *
54 * @since 1.278
55 */
56 protected JobPropertyDescriptor() {
57 }
58
59 /**
60 * {@inheritDoc}
61 *
62 * @return
63 * null to avoid setting an instance of {@link JobProperty} to the target project.
64 */
65 @Override
66 public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) throws FormException {
67 // JobPropertyDescriptors are bit different in that we allow them even without any user-visible configuration parameter,
68 // so replace the lack of form data by an empty one.
69 if(formData.isNullObject()) formData=new JSONObject();
70
71 return super.newInstance(req, formData);
72 }
73
74 /**
75 * Returns true if this {@link JobProperty} type is applicable to the
76 * given job type.
77 *
78 * <p>
79 * The default implementation of this method checks if the given job type is assignable to 'J' of
80 * {@link JobProperty}<tt>&lt;J></tt>, but subtypes can extend this to change this behavior.
81 *
82 * @return
83 * true to indicate applicable, in which case the property will be
84 * displayed in the configuration screen of this job.
85 */
86 public boolean isApplicable(Class<? extends Job> jobType) {
87 Type parameterization = Types.getBaseClass(clazz, JobProperty.class);
88 if (parameterization instanceof ParameterizedType) {
89 ParameterizedType pt = (ParameterizedType) parameterization;
90 Class applicable = Types.erasure(Types.getTypeArgument(pt, 0));
91 return applicable.isAssignableFrom(jobType);
92 } else {
93 throw new AssertionError(clazz+" doesn't properly parameterize JobProperty. The isApplicable() method must be overriden.");
94 }
95 }
96
97 /**
98 * Gets the {@link JobPropertyDescriptor}s applicable for a given job type.
99 */
100 public static List<JobPropertyDescriptor> getPropertyDescriptors(Class<? extends Job> clazz) {
101 List<JobPropertyDescriptor> r = new ArrayList<JobPropertyDescriptor>();
102 for (JobPropertyDescriptor p : all())
103 if(p.isApplicable(clazz))
104 r.add(p);
105 return r;
106 }
107
108 public static Collection<JobPropertyDescriptor> all() {
109 return (Collection) Jenkins.getInstance().getDescriptorList(JobProperty.class);
110 }
111 }
112