Return-Path: Delivered-To: apmail-commons-issues-archive@locus.apache.org Received: (qmail 43323 invoked from network); 23 Jul 2008 20:14:00 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Jul 2008 20:14:00 -0000 Received: (qmail 6917 invoked by uid 500); 23 Jul 2008 20:13:52 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 6839 invoked by uid 500); 23 Jul 2008 20:13:52 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 6740 invoked by uid 99); 23 Jul 2008 20:13:52 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Jul 2008 13:13:52 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Jul 2008 20:13:06 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id B233C234C179 for ; Wed, 23 Jul 2008 13:13:31 -0700 (PDT) Message-ID: <1073182109.1216844011728.JavaMail.jira@brutus> Date: Wed, 23 Jul 2008 13:13:31 -0700 (PDT) From: "Oliver Heger (JIRA)" To: issues@commons.apache.org Subject: [jira] Resolved: (CLI-144) adding a FileValidator results in ClassCastException in parser.parseAndHelp(args) In-Reply-To: <31904202.1187364510801.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/CLI-144?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Oliver Heger resolved CLI-144. ------------------------------ Resolution: Fixed Applied the proposed fix. The validate() method of an option that is part of a group is now called only once. Thus, the validators are also invoked only once and do not try converting a value multiple times - which caused the ClassCastException. > adding a FileValidator results in ClassCastException in parser.parseAndHelp(args) > --------------------------------------------------------------------------------- > > Key: CLI-144 > URL: https://issues.apache.org/jira/browse/CLI-144 > Project: Commons CLI > Issue Type: Bug > Components: CLI-2.x > Environment: Windows XP > Reporter: David Biesack > Fix For: 2.0 > > Attachments: BugCLI144Test.java, BugCLI144Test_oh.java, CLI-144.patch, CLI-144_oh.patch > > > When I add a FileValidator.getExistingFileInstance() to an Argument, I get a ClassCastException when I parse args. > Below is a testcase invoke with > java org.apache.commons.cli2.issues.CLI2Sample -classpath commons-cli-2.0-SNAPSHOT.jar --file-name path-to-an-existing-file > Run it and you get: > Exception in thread "main" java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String > at org.apache.commons.cli2.validation.FileValidator.validate(FileValidator.java:122) > at org.apache.commons.cli2.option.ArgumentImpl.validate(ArgumentImpl.java:250) > at org.apache.commons.cli2.option.ParentImpl.validate(ParentImpl.java:123) > at org.apache.commons.cli2.option.DefaultOption.validate(DefaultOption.java:175) > at org.apache.commons.cli2.option.GroupImpl.validate(GroupImpl.java:264) > at org.apache.commons.cli2.commandline.Parser.parse(Parser.java:105) > at org.apache.commons.cli2.commandline.Parser.parseAndHelp(Parser.java:125) > at org.apache.commons.cli2.issues.CLI2Sample.main(CLI2Sample.java:38) > Comment out the withValidator call and it runs with no exception. > I also get a similar ClassCastException if I add a > .withValidator(NumberValidator.getIntegerInstance()) > to another option/argument. > Here is the source > package org.apache.commons.cli2.issues; > import java.io.File; > import org.apache.commons.cli2.CommandLine; > import org.apache.commons.cli2.Group; > import org.apache.commons.cli2.builder.ArgumentBuilder; > import org.apache.commons.cli2.builder.DefaultOptionBuilder; > import org.apache.commons.cli2.builder.GroupBuilder; > import org.apache.commons.cli2.commandline.Parser; > import org.apache.commons.cli2.option.DefaultOption; > import org.apache.commons.cli2.validation.FileValidator; > public class CLI2Sample > { > public static void main(String[] args) > { > final DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); > final ArgumentBuilder abuilder = new ArgumentBuilder(); > final GroupBuilder gbuilder = new GroupBuilder(); > DefaultOption fileNameOption = obuilder > .withShortName("f") > .withLongName("file-name") > .withRequired(true) > .withDescription("name of an existing file") > .withArgument(abuilder > .withName("file-name") > .withValidator(FileValidator.getExistingFileInstance()) > .create()) > .create(); > Group options = gbuilder > .withName("options") > .withOption(fileNameOption) > .create(); > Parser parser = new Parser(); > parser.setHelpTrigger("--help"); > parser.setGroup(options); > CommandLine cl = parser.parseAndHelp(args); > } > } -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.