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