From cac3efd90a099dfc19bf37507d2903e456c5f504 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Wed, 21 Nov 2012 14:59:24 -0500 Subject: [PATCH] =?UTF-8?q?Produce=20a=20helpful=20error=20message=20if=20?= =?UTF-8?q?optionalProperty#field=20is=20bogus.=20Otherwise=20get=20a=20us?= =?UTF-8?q?eless=20=E2=80=9Cnull=20is=20missing=20its=20descriptor?= =?UTF-8?q?=E2=80=9D=20from=20Jenkins.getDescriptorOrDie.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/hudson/model/Descriptor.java | 21 +++++++++++++++++-- .../resources/lib/form/optionalProperty.jelly | 4 ++-- .../main/resources/lib/form/property.jelly | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/hudson/model/Descriptor.java b/core/src/main/java/hudson/model/Descriptor.java index b4add73200..955eaf7f2d 100644 --- a/core/src/main/java/hudson/model/Descriptor.java +++ b/core/src/main/java/hudson/model/Descriptor.java @@ -31,7 +31,6 @@ import hudson.BulkChange; import hudson.Util; import hudson.model.listeners.SaveableListener; import hudson.util.FormApply; -import hudson.util.QuotedStringTokenizer; import hudson.util.ReflectionUtils; import hudson.util.ReflectionUtils.Parameter; import hudson.views.ListViewColumn; @@ -72,6 +71,8 @@ import java.lang.reflect.Type; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.beans.Introspector; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; /** * Metadata about a configurable instance. @@ -470,11 +471,27 @@ public abstract class Descriptor> implements Saveable { /** * Used by Jelly to abstract away the handlign of global.jelly vs config.jelly databinding difference. */ - public PropertyType getPropertyType(Object instance, String field) { + public @CheckForNull PropertyType getPropertyType(@Nonnull Object instance, @Nonnull String field) { // in global.jelly, instance==descriptor return instance==this ? getGlobalPropertyType(field) : getPropertyType(field); } + /** + * Akin to {@link #getPropertyType(Object,String) but never returns null. + * @throws AssertionError in case the field cannot be found + * @since 1.492 + */ + public @Nonnull PropertyType getPropertyTypeOrDie(@Nonnull Object instance, @Nonnull String field) { + PropertyType propertyType = getPropertyType(instance, field); + if (propertyType != null) { + return propertyType; + } else if (instance == this) { + throw new AssertionError(getClass().getName() + " has no property " + field); + } else { + throw new AssertionError(clazz.getName() + " has no property " + field); + } + } + /** * Obtains the property type of the given field of {@link #clazz} */ diff --git a/core/src/main/resources/lib/form/optionalProperty.jelly b/core/src/main/resources/lib/form/optionalProperty.jelly index 7b2827448a..cccf94e70c 100644 --- a/core/src/main/resources/lib/form/optionalProperty.jelly +++ b/core/src/main/resources/lib/form/optionalProperty.jelly @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + Renders inline an optional single-value nested data-bound property of the current instance, @@ -39,7 +39,7 @@ THE SOFTWARE. so override @checked manually. --> - + diff --git a/core/src/main/resources/lib/form/property.jelly b/core/src/main/resources/lib/form/property.jelly index 522452cc6e..a0cb378225 100644 --- a/core/src/main/resources/lib/form/property.jelly +++ b/core/src/main/resources/lib/form/property.jelly @@ -35,7 +35,7 @@ THE SOFTWARE. - + -- GitLab