Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright (C) 2011 Tatsuhiro Tsujikawa
  * Permission is hereby granted, free of charge, to any person
  * obtaining a copy of this software and associated documentation
  * files (the "Software"), to deal in the Software without
  * restriction, including without limitation the rights to use, copy,
  * modify, merge, publish, distribute, sublicense, and/or sell copies
  * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
package net.sourceforge.argparse4j.impl.type;

This implementation converts String value into given type using given type's constructor.

The constructor must have 1 String argument. If error occurred inside the constructor, net.sourceforge.argparse4j.inf.ArgumentParserException will be thrown. If error occurred in other locations, subclass of java.lang.RuntimeException will be thrown.

public class ConstructorArgumentType<T> implements ArgumentType<T> {
    private Class<T> type_;


Creates ConstructorArgumentType object with given type.

The constructor of type must have 1 String argument.

type The type String value should be converted to.
    public ConstructorArgumentType(Class<T> type) {
         = type;
    public T convert(ArgumentParser parserArgument argString value)
            throws ArgumentParserException {
        T obj = null;
        try {
            obj = .getConstructor(String.class).newInstance(value);
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
            throw new ArgumentParserException(String.format(
                    "could not convert '%s' to %s (%s)"value,
                    .getSimpleName(), e.getCause().getMessage()),
                    e.getCause(), parserarg);
        } catch (NoSuchMethodException e) {
        return obj;
    private void handleInstatiationError(Exception e) {
        throw new IllegalArgumentException("Failed to instantiate object"e);
New to GrepCode? Check out our FAQ X