I have trouble getting optionalBlock to work. Test code, full compiling 
files which can be directly to default "hello world" plugin created by mvn 
hpi:create:

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" 
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"><f:block>
    <table>
        <f:optionalBlock name="enableText" title="Enable optional text" 
checked="${instance.enableText}">
            <f:entry title="Optional text" field="text">
                <f:textbox />
            </f:entry>
        </f:optionalBlock>
    </table></f:block>

and

package foo.hyde.jenkins.plugins;
public class OptionalBlockSampleBuilder extends hudson.tasks.Builder {

    public final String text;
    public final boolean enableText;

    @org.kohsuke.stapler.DataBoundConstructor
    public OptionalBlockSampleBuilder(String text, Boolean enableText) {
        this.text = text;
        this.enableText = (enableText != null) && enableText;
    }

    @Override
    public boolean perform(hudson.model.AbstractBuild build, hudson.Launcher 
launcher, hudson.model.BuildListener listener) {
        listener.getLogger().println("OptionalBlockSampleBuilder " + enableText 
+ "/" + text);
        return true;
    }

    @hudson.Extension
    public static final class DescriptorImpl extends 
hudson.tasks.BuildStepDescriptor<hudson.tasks.Builder> {
        public boolean isApplicable(Class<? extends 
hudson.model.AbstractProject> aClass) {
            return true;
        }
        public String getDisplayName() {
            return "Optional Block Sample";
        }
    }}

Everything works as expected if I don't check the checkbox, and build 
console output in perform shows expected "false/null". But if I check the 
checkbox and enter text in the field, and then save/apply the job's 
configuration. I immediately get exception:

java.lang.RuntimeException:
  Failed to instantiate class
    foo.hyde.jenkins.plugins.OptionalBlockSampleBuilder 
  from
    {"enableText":{"text":"xx"},
    "kind":"foo.hyde.jenkins.plugins.OptionalBlockSampleBuilder",
    "stapler-class":"foo.hyde.jenkins.plugins.OptionalBlockSampleBuilder"}

This probably simple to fix, but how? I've tried quite a few variations, 
didn't find a working one. What is right way to use optionalBlock jelly tag?

Reply via email to