001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau; 014 015import static org.apache.juneau.internal.StringUtils.*; 016 017import java.text.*; 018 019import org.apache.juneau.json.*; 020import org.apache.juneau.reflect.*; 021 022/** 023 * General invalid conversion exception. 024 * 025 * <p> 026 * Exception that gets thrown if you try to perform an invalid conversion, such as when calling 027 * {@code OMap.getInt(...)} on a non-numeric <c>String</c>. 028 */ 029public final class InvalidDataConversionException extends BasicRuntimeException { 030 031 private static final long serialVersionUID = 1L; 032 033 /** 034 * Constructor. 035 * 036 * @param cause The cause of this exception. 037 * @param message The {@link MessageFormat}-style message. 038 * @param args Optional {@link MessageFormat}-style arguments. 039 */ 040 public InvalidDataConversionException(Throwable cause, String message, Object... args) { 041 super(cause, getMessage(cause, message, null), args); 042 } 043 044 /** 045 * @param toType Attempting to convert to this class type. 046 * @param cause The cause. 047 * @param value The value being converted. 048 */ 049 public InvalidDataConversionException(Object value, Class<?> toType, Exception cause) { 050 this(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''. Value={2}.", name(value), name(toType), value(value)); 051 } 052 053 /** 054 * @param toType Attempting to convert to this class type. 055 * @param cause The cause. 056 * @param value The value being converted. 057 */ 058 public InvalidDataConversionException(Object value, ClassMeta<?> toType, Exception cause) { 059 this(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''. Value={2}.", name(value), stringify(toType), value(value)); 060 } 061 062 private static String value(Object o) { 063 if (o instanceof Class) 064 return "'" + name(o) + "'"; 065 return SimpleJsonSerializer.DEFAULT == null ? "'" + o.toString() + "'" : SimpleJsonSerializer.DEFAULT.toString(o); 066 } 067 068 private static String name(Class<?> c) { 069 return ClassInfo.of(c).getFullName(); 070 } 071 072 private static String name(Object o) { 073 return ClassInfo.of(o).getFullName(); 074 } 075}