Skip to content

Commit

Permalink
[FIXED JENKINS-37997] FIX NPE when descriptor is not in DescriptorList (
Browse files Browse the repository at this point in the history
#2536)

For example when Descriptor was defined as field and without @extension .

Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com>
  • Loading branch information
KostyaSha authored and oleg-nenashev committed Sep 9, 2016
1 parent d4169af commit 5d3b23d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core/src/main/java/hudson/model/Descriptor.java
Expand Up @@ -638,7 +638,13 @@ public Object instantiate(Class actualType, JSONObject json) {
if (isApplicable(actualType, json)) {
LOGGER.log(Level.FINE, "switching to newInstance {0} {1}", new Object[] {actualType.getName(), json});
try {
return Jenkins.getActiveInstance().getDescriptor(actualType).newInstance(Stapler.getCurrentRequest(), json);
final Descriptor descriptor = Jenkins.getActiveInstance().getDescriptor(actualType);
if (descriptor != null) {
return descriptor.newInstance(Stapler.getCurrentRequest(), json);
} else {
LOGGER.log(Level.WARNING, "Descriptor not found. Falling back to default instantiation "
+ actualType.getName() + " " + json);
}
} catch (Exception x) {
LOGGER.log(Level.WARNING, "falling back to default instantiation " + actualType.getName() + " " + json, x);
// If nested objects are not using newInstance, bindJSON will wind up throwing the same exception anyway,
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -1414,6 +1414,7 @@ public Descriptor getDescriptorByName(String id) {
* If you have an instance of {@code type} and call {@link Describable#getDescriptor()},
* you'll get the same instance that this method returns.
*/
@CheckForNull
public Descriptor getDescriptor(Class<? extends Describable> type) {
for( Descriptor d : getExtensionList(Descriptor.class) )
if(d.clazz==type)
Expand Down

0 comments on commit 5d3b23d

Please sign in to comment.