/** {@inheritDoc} */ @Override public <T extends Describable> T bind(JSONObject json) throws IOException, FormException { final String clazz = checkNotNull(json.optString("$class", null)); final Descriptor descriptor = getDescriptor(clazz); final Stapler stapler = getStapler(); final StaplerRequest request = getRequest(stapler, json); // We do this instead of 'request.bindJson' because this doesn't // require a DataBoundConstructor. // TODO(mattmoor): Should we do the rewrite of describable lists // here as well? return (T) descriptor.newInstance(request, json); }
/** * Gets the {@link Descriptor} of the {@link Describable} we are * looking to instantiate. */ @VisibleForTesting Descriptor getDescriptor(String clazzName) throws IOException { Class<? extends Describable<?>> implClass; try { implClass = (Class<? extends Describable<?>>) getClassLoader() .loadClass(clazzName); } catch (ClassNotFoundException ex) { throw new BadTypeException( Messages.DefaultBinder_CannotLoadClass(clazzName)); } final Descriptor descriptor = checkNotNull(Jenkins.getInstance()).getDescriptor(implClass); if (descriptor == null) { throw new BadTypeException( Messages.DefaultBinder_NoDescriptor(implClass.getName())); } return descriptor; }
private <T extends Describable<T>,D extends Descriptor<T>> void populate(List<Descriptor<?>> r, Class<T> c) { for (Descriptor<?> d : Jenkins.getActiveInstance().getDescriptorList(c)) { if (SimpleBuildStep.class.isAssignableFrom(d.clazz)) { r.add(d); } } }
@Override protected final Folder createMockItem() throws Exception { // mock the common item methods Folder folder = super.createMockItem(); // mock the property list DescribableList propertyList = createMock(DescribableList.class); expect(folder.getProperties()).andReturn(propertyList).anyTimes(); // mock adding properties folder.addProperty(anyObject(FolderProperty.class)); expectLastCall().andAnswer(new IAnswer<Void>() { public Void answer() throws Throwable { addProperty((FolderProperty)getCurrentArguments()[0]); return null; } }).anyTimes(); // mock getting properties expect(propertyList.get(anyObject(Class.class))).andAnswer(new IAnswer<Describable>() { public Describable answer() throws Throwable { return getProperty((Class)getCurrentArguments()[0]); } }).anyTimes(); replay(propertyList); return folder; }
private <T extends Describable<T>, D extends Descriptor<T>> List<Descriptor<?>> getClassList(Class<T> c) { ArrayList<Descriptor<?>> r = new ArrayList<Descriptor<?>>(); if (jenkins == null) { return new ArrayList<Descriptor<?>>(); } for (Descriptor<?> d : jenkins.getDescriptorList(c)) { if (SimpleBuildStep.class.isAssignableFrom(d.clazz)) { r.add(d); } } return r; }
@Override public void addHelpFileRedirect(String fieldName, Class<? extends Describable> owner, String fieldNameToRedirectTo) { initPython(); if (pexec.isImplemented(16)) { pexec.execPythonVoid("add_help_file_redirect", fieldName, owner, fieldNameToRedirectTo); } else { super.addHelpFileRedirect(fieldName, owner, fieldNameToRedirectTo); } }
@Override public void addHelpFileRedirect(String fieldName, Class<? extends Describable> owner, String fieldNameToRedirectTo) { initPython(); if (pexec.isImplemented(17)) { pexec.execPythonVoid("add_help_file_redirect", fieldName, owner, fieldNameToRedirectTo); } else { super.addHelpFileRedirect(fieldName, owner, fieldNameToRedirectTo); } }
@Override public void addHelpFileRedirect(String fieldName, Class<? extends Describable> owner, String fieldNameToRedirectTo) { initPython(); if (pexec.isImplemented(19)) { pexec.execPythonVoid("add_help_file_redirect", fieldName, owner, fieldNameToRedirectTo); } else { super.addHelpFileRedirect(fieldName, owner, fieldNameToRedirectTo); } }
protected static <R extends Describable<R>> DescribableList<R, Descriptor<R>> reduceDescribableByMerge( Deque<DescribableList<R, Descriptor<R>>> list) { if (list == null) { return new DescribableList<R, Descriptor<R>>(NOOP); } List<R> merge = new LinkedList<R>(); for (DescribableList<R, Descriptor<R>> sub : list) { for (R item : sub) { merge.add(item); } } return new DescribableList<R, Descriptor<R>>(NOOP, merge); }
protected static <R extends Describable<R>> DescribableList<R, Descriptor<R>> reduceDescribableByMergeWithoutDuplicates( Deque<DescribableList<R, Descriptor<R>>> list) { // set for keeping track of found classes Set<Class<?>> seen = new HashSet<Class<?>>(); if (list == null) { return new DescribableList<R, Descriptor<R>>(NOOP); } // result list List<R> merge = new LinkedList<R>(); // descending order for keeping the last buildwrapper that was set = lower in the inheritance tree Iterator<DescribableList<R, Descriptor<R>>> rIter = list.descendingIterator(); while (rIter.hasNext()) { DescribableList<R, Descriptor<R>> sub = rIter.next(); for (R item : sub) { Class<?> clazz = item.getClass(); if (!seen.contains(clazz)) { merge.add(item); seen.add(clazz); } } } return new DescribableList<R, Descriptor<R>>(NOOP, merge); }
@SuppressWarnings("unchecked") protected static <R extends Describable<R>> DescribableList<R, Descriptor<R>> castToDescribableList(Object o) { try { if (o instanceof DescribableList) { return ((DescribableList<R, Descriptor<R>>) o); } return null; } catch (ClassCastException ex) { return null; } }
@Nonnull <E extends Describable<E>, T extends Descriptor<E>> T getDescriptorForType(E describable);
@Nonnull @Override public <E extends Describable<E>, T extends Descriptor<E>> T getDescriptorForType(E describable) { //noinspection unchecked return (T) getJenkinsInstanceOrDie().getDescriptor(describable.getClass()); }
@Nonnull @Override public <E extends Describable<E>, T extends Descriptor<E>> T getDescriptorForType(E describable) { throw new UnsupportedOperationException(""); }
protected static String defaultName(BuildStep bs) { return bs instanceof Describable<?> ? ((Describable<?>) bs).getDescriptor().getDisplayName() : null; }
public void superAddHelpFileRedirect(String fieldName, Class<? extends Describable> owner, String fieldNameToRedirectTo) { super.addHelpFileRedirect(fieldName, owner, fieldNameToRedirectTo); }
private static String getDescriptorName(@CheckForNull Describable<?> d) { if (d == null) { return "(none)"; } return "`" + d.getClass().getName() + "`"; }
/** * Bind the {@link JSONObject} to the specified type. * * @param <T> The type to which we are binding the JSON. * @param json The serialized object we are instantiating * @return The object of the specified type, from the JSON. */ <T extends Describable> T bind(JSONObject json) throws IOException, FormException;