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

org.joda.convert.ReflectionStringConverter Maven / Gradle / Ivy

There is a newer version: 3.2.3
Show newest version
/*
 *  Copyright 2010-present Stephen Colebourne
 *
 *  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 org.joda.convert;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Conversion to and from a string using reflection.
 * 

* The toString method must meet the following signature:
* {@code String anyName()} on Class T. *

* ReflectionStringConverter is abstract, but all known implementations are thread-safe and immutable. * * @param the type of the converter */ abstract class ReflectionStringConverter implements TypedStringConverter { /** The converted class. */ private final Class cls; /** Conversion to a string. */ private final Method toString; /** * Creates an instance using two methods. * @param cls the class this converts for, not null * @param toString the toString method, not null * @throws RuntimeException (or subclass) if the method signatures are invalid */ ReflectionStringConverter(Class cls, Method toString) { if (toString.getParameterTypes().length != 0) { throw new IllegalStateException("ToString method must have no parameters: " + toString); } if (toString.getReturnType() != String.class) { throw new IllegalStateException("ToString method must return a String: " + toString); } this.cls = cls; this.toString = toString; } //----------------------------------------------------------------------- /** * Converts the object to a {@code String}. * @param object the object to convert, not null * @return the converted string, may be null but generally not */ @Override public String convertToString(T object) { try { return (String) toString.invoke(object); } catch (IllegalAccessException ex) { throw new IllegalStateException("Method is not accessible: " + toString); } catch (InvocationTargetException ex) { if (ex.getCause() instanceof RuntimeException) { throw (RuntimeException) ex.getCause(); } throw new RuntimeException(ex.getMessage(), ex.getCause()); } } //----------------------------------------------------------------------- @Override public String toString() { return "RefectionStringConverter[" + cls.getSimpleName() + "]"; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy