git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: File type mapping in external task


On 2018-04-03, Gilles Querret wrote:

> If I have an attribute mapped to a File type (such as setSrcDir(File file)
> { this.srcDir = file; } ), then passing an empty string (as in <MyTask
> srcDir="" ) leads to srcDir set to baseDir (or current directory, not sure).

baseDir :-)

> Is there a way to trap the empty string in the setter ? Or is it required
> to change the setter to accept a String parameter, and create the File
> object from the string ?

You will need the String-arg setter for that. The File (or Path) -arg
versions simply go through Project#resolveFile.

If you are using typed attribute setters you are all in on Ant's
behavior without any chance to tweak it.

An alternative may be a type of your own with a Project and String-arg
constructor, something like

public class MyFileWrapper {
    private final File file;

    public MyFileWrapper(Project p, String fileName) {
        if ("".equals(fileName)) {
            throw new BuildException("file name must not be empty");
        }
        this.file = p.resolveFile(fileName);
    }

    public File getFile() {
        return file;
    }
}

public void setSrcDir(MyFileWrapper w) {
    this.srcDir = w.getFile();
}

which may be slightly better.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@xxxxxxxxxxxxxx
For additional commands, e-mail: dev-help@xxxxxxxxxxxxxx