001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.messaging.amf.io.convert;
022
023import java.lang.reflect.Type;
024
025/**
026 * @author Franck WOLFF
027 */
028public class IllegalConverterArgumentException extends RuntimeException {
029
030    private static final long serialVersionUID = 1L;
031
032    private final Converter converter;
033    private final Object value;
034    private final Type targetType;
035
036    public IllegalConverterArgumentException(Converter converter, Object value, Type targetType) {
037        this(converter, value, targetType, buildDefaultMessage(converter, value, targetType), null);
038    }
039
040    public IllegalConverterArgumentException(Converter converter, Object value, Type targetType, String message) {
041        this(converter, value, targetType, message, null);
042    }
043
044    public IllegalConverterArgumentException(Converter converter, Object value, Type targetType, Throwable cause) {
045        this(converter, value, targetType, buildDefaultMessage(converter, value, targetType), cause);
046    }
047
048    public IllegalConverterArgumentException(Converter converter, Object value, Type targetType, String message, Throwable cause) {
049        super(message, cause);
050        this.converter = converter;
051        this.value = value;
052        this.targetType = targetType;
053    }
054
055    public Converter getConverter() {
056        return converter;
057    }
058
059    public Object getValue() {
060        return value;
061    }
062
063    public Type getTargetType() {
064        return targetType;
065    }
066
067    private static String buildDefaultMessage(Converter converter, Object value, Type targetType) {
068        try {
069            return "Illegal argument for converter: " + converter + " from: " + value + " to: " + targetType;
070        } catch (Exception e) {
071            return "Illegal argument for converter. Additionally, an error occured when trying to build default error message: " + e.toString();
072        }
073    }
074}