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.common.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 JsonMap.getInt(...)} on a non-numeric <c>String</c>. 028 * 029 * <h5 class='section'>See Also:</h5><ul> 030 031 * </ul> 032 * 033 * @serial exclude 034 */ 035public final class InvalidDataConversionException extends BasicRuntimeException { 036 037 private static final long serialVersionUID = 1L; 038 039 /** 040 * Constructor. 041 * 042 * @param cause The cause of this exception. 043 * @param message The {@link MessageFormat}-style message. 044 * @param args Optional {@link MessageFormat}-style arguments. 045 */ 046 public InvalidDataConversionException(Throwable cause, String message, Object... args) { 047 super(cause, message, args); 048 } 049 050 /** 051 * @param toType Attempting to convert to this class type. 052 * @param cause The cause. 053 * @param value The value being converted. 054 */ 055 public InvalidDataConversionException(Object value, Class<?> toType, Exception cause) { 056 this(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''. Value={2}.", name(value), name(toType), value(value)); 057 } 058 059 /** 060 * @param toType Attempting to convert to this class type. 061 * @param cause The cause. 062 * @param value The value being converted. 063 */ 064 public InvalidDataConversionException(Object value, ClassMeta<?> toType, Exception cause) { 065 this(cause, "Invalid data conversion from type ''{0}'' to type ''{1}''. Value={2}.", name(value), stringify(toType), value(value)); 066 } 067 068 private static String value(Object o) { 069 if (o instanceof Class) 070 return "'" + name(o) + "'"; 071 return Json5Serializer.DEFAULT == null ? "'" + o.toString() + "'" : Json5Serializer.DEFAULT.toString(o); 072 } 073 074 private static String name(Class<?> c) { 075 return ClassInfo.of(c).getFullName(); 076 } 077 078 private static String name(Object o) { 079 return ClassInfo.of(o).getFullName(); 080 } 081}