All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taobao.middleware.cli.converters.ConstructorBasedConverter Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
/*
 *  Copyright (c) 2011-2015 The original author or authors
 *  ------------------------------------------------------
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  and Apache License v2.0 which accompanies this distribution.
 *
 *       The Eclipse Public License is available at
 *       http://www.eclipse.org/legal/epl-v10.html
 *
 *       The Apache License v2.0 is available at
 *       http://www.opensource.org/licenses/apache2.0.php
 *
 *  You may elect to redistribute this code under either of these licenses.
 */
package com.taobao.middleware.cli.converters;

import java.lang.reflect.Constructor;

/**
 * This 'default' converter tries to create objects using a constructor taking a single String argument.
 * Be aware that implementation must also handle the case where the input is {@literal null}.
 *
 * @author Clement Escoffier 
 */
public final class ConstructorBasedConverter implements Converter {

    private final Constructor constructor;

    private ConstructorBasedConverter(Constructor constructor) {
        this.constructor = constructor;
    }

    /**
     * Checks whether the given class can be used by the {@link ConstructorBasedConverter} (i.e. has a constructor
     * taking a single String as argument). If so, creates a new instance of converter for this type.
     *
     * @param clazz the class
     * @return a {@link ConstructorBasedConverter} if the given class is eligible,
     * {@literal null} otherwise.
     */
    public static  ConstructorBasedConverter getIfEligible(Class clazz) {
        try {
            final Constructor constructor = clazz.getConstructor(String.class);
            if (!constructor.isAccessible()) {
                constructor.setAccessible(true);
            }
            return new ConstructorBasedConverter(constructor);
        } catch (NoSuchMethodException e) {
            // The class does not have the right constructor, return null.
            return null;
        }

    }

    /**
     * Converts the given input to an object by using the constructor approach. Notice that the constructor must
     * expect receiving a {@literal null} value.
     *
     * @param input the input, can be {@literal null}
     * @return the instance of T
     * @throws IllegalArgumentException if the instance of T cannot be created from the input.
     */
    @Override
    public T fromString(String input) throws IllegalArgumentException {
        try {
            return constructor.newInstance(input);
        } catch (Throwable e) {
            if (e.getCause() != null) {
                throw new IllegalArgumentException(e.getCause());
            } else {
                throw new IllegalArgumentException(e);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy