View Javadoc
1   /*
2    * Copyright 2016 Andrew Rucker Jones.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.opencsv.exceptions;
17  
18  import com.opencsv.ICSVParser;
19  
20  import java.lang.reflect.Field;
21  import java.util.Locale;
22  import java.util.ResourceBundle;
23  
24  /**
25   * This exception is to be thrown when anything goes bad during introspection of
26   * beans given to opencsv.
27   * It encapsulates exceptions such as {@link java.lang.NoSuchMethodException},
28   * {@link java.lang.IllegalAccessException} and
29   * {@link java.lang.reflect.InvocationTargetException}. Some might notice that
30   * this effectively converts checked exceptions into unchecked exceptions.
31   * Introspection exceptions are coding errors that should be fixed during
32   * development, and should not have to be handled in production code.
33   * 
34   * @author Andrew Rucker Jones
35   * @since 3.9
36   */
37  public class CsvBeanIntrospectionException extends CsvRuntimeException {
38      private static final long serialVersionUID = 1L;
39      
40      /** The bean that was acted upon. */
41      private transient final Object bean;
42      
43      /** The field that was supposed to be manipulated in the bean. */
44      private transient final Field field;
45      
46      /**
47       * Nullary constructor.
48       */
49      public CsvBeanIntrospectionException() {
50          super();
51          bean = null;
52          field = null;
53      }
54      
55      /**
56       * Constructor with a human-readable error message.
57       * @param message Error message
58       */
59      public CsvBeanIntrospectionException(String message) {
60          super(message);
61          bean = null;
62          field = null;
63      }
64      
65      /**
66       * Constructor to specify the bean and field whose manipulation caused this
67       * exception.
68       * @param bean The bean that was to be manipulated
69       * @param field The field in the bean
70       */
71      public CsvBeanIntrospectionException(Object bean, Field field) {
72          super();
73          this.bean = bean;
74          this.field = field;
75      }
76      
77      /**
78       * Constructor to provide all information connected to the error raised.
79       * @param bean The bean that was to be manipulated
80       * @param field The field in the bean
81       * @param message Error message
82       */
83      public CsvBeanIntrospectionException(Object bean, Field field, String message) {
84          super(message);
85          this.bean = bean;
86          this.field = field;
87      }
88      
89      /**
90       * Gets a human-readable error message.
91       * @return The error message, or if none is found, but {@link #bean} and
92       * {@link #field} have been set, returns a default error message
93       * incorporating the names of {@link #bean} and {@link #field}
94       */
95      @Override
96      public String getMessage() {
97          return getMessageFromLocale(Locale.US);
98      }
99      
100     @Override
101     public String getLocalizedMessage() {
102         return getMessageFromLocale(Locale.getDefault());
103     }
104     
105     private String getMessageFromLocale(Locale locale) {
106         String supermessage = super.getMessage();
107         if(supermessage == null && getBean() != null && getField() != null) {
108             return String.format(ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, locale).getString("error.introspecting.field"),
109                     getField().getName(),
110                     getBean().getClass().getCanonicalName());
111         }
112         return supermessage;
113     }
114 
115     /**
116      * Gets the bean that was to be introspected.
117      * {@code bean} is marked {@code transient}, because
118      * {@link java.lang.Object} is not {@link java.io.Serializable}. If
119      * for any reason this exception is serialized and deserialized, this method
120      * will subsequently return {@code null}.
121      * 
122      * @return The bean that caused this exception
123      */
124     public Object getBean() {
125         return bean;
126     }
127 
128     /**
129      * Gets the field from the Reflection API that was involved in the error.
130      * {@code field} is marked {@code transient}, because
131      * {@link java.lang.reflect.Field} is not {@link java.io.Serializable}. If
132      * for any reason this exception is serialized and deserialized, this method
133      * will subsequently return {@code null}.
134      * 
135      * @return The field in the bean that caused this exception
136      */
137     public Field getField() {
138         return field;
139     }
140 }