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

io.guise.framework.converter.DefaultStringLiteralConverter Maven / Gradle / Ivy

There is a newer version: 0.5.3
Show newest version
/*
 * Copyright © 2005-2008 GlobalMentor, Inc. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.guise.framework.converter;

import static java.util.Objects.*;

import static com.globalmentor.java.Classes.*;
import static java.text.MessageFormat.format;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * A converter that converts any object to a string literal using its {@link Object#toString()} method. This converter converts from a string literal to an
 * object using a string constructor, or if one is not present, the first constructor with a single parameter that is type-compatible with {@link String}, such
 * as {@link CharSequence}. If there is no string-compatible constructor, a {@link ConversionException} is thrown.
 * @param  The value type this converter supports.
 * @author Garret Wilson
 */
public class DefaultStringLiteralConverter extends AbstractStringLiteralConverter {

	/** The class representing the type of value to convert. */
	private final Class valueClass;

	/** @return The class representing the type of value to convert. */
	public Class getValueClass() {
		return valueClass;
	}

	/**
	 * Constructs a default string literal converter indicating the type of value to convert.
	 * @param valueClass The class indicating the type of value to convert.
	 * @throws NullPointerException if the given value class is null.
	 */
	public DefaultStringLiteralConverter(final Class valueClass) {
		this.valueClass = requireNonNull(valueClass, "Value class cannot be null."); //store the value class
	}

	/**
	 * {@inheritDoc}
	 * 

* This implementation converts from a string literal to an object using a string constructor, or if one is not present, the first constructor with a single * parameter that is type-compatible with {@link String}, such as {@link CharSequence}. If there is no string-compatible constructor, a * {@link ConversionException} is thrown. *

*/ @Override public V convertLiteral(final String literal) throws ConversionException { if(literal == null) { //if the literal is null return null; //the value is null } else { //if the literal is not null final Class valueClass = getValueClass(); //get the value class final Constructor stringCompatibleConstructor = getCompatiblePublicConstructor(valueClass, String.class); //get the string-compatible constructor if(stringCompatibleConstructor != null) { //if there is a string-compatible constructor try { return stringCompatibleConstructor.newInstance(literal); //try to invoke the constructor and return a new object } catch(final InvocationTargetException invocationTargetException) { //if the constructor threw an error final Throwable cause = invocationTargetException.getCause(); //get the cause of the exception if(cause instanceof IllegalArgumentException) { //if there was something incorrect about the string literal argument throw new ConversionException(format(getSession().dereferenceString(getInvalidValueMessage()), literal), literal); //indicate that the value was invalid } else { //if there is some other constructor error throw new ConversionException(cause); //send it back; we don't know what it is, and we can't be sure it means the value is invalid } } catch(final IllegalArgumentException e) { throw new ConversionException(e); } catch(final InstantiationException e) { throw new ConversionException(e); } catch(final IllegalAccessException e) { throw new ConversionException(e); } } else { //if there are no string-compatible constructors throw new ConversionException("Class " + getValueClass() + " does not support literal to value conversions.", literal); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy