View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
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 net.sf.oness.common.all;
17  
18  import java.lang.reflect.Field;
19  import java.text.DateFormat;
20  import java.util.Calendar;
21  
22  import org.apache.commons.lang.builder.ToStringStyle;
23  
24  /***
25   * Customized ReflectionToStringBuilder
26   * 
27   * @see org.apache.commons.lang.builder.ReflectionToStringBuilder
28   * 
29   * @author Carlos Sanchez
30   * @version $Revision: 1.1 $
31   */
32  public class ReflectionToStringBuilder extends
33          org.apache.commons.lang.builder.ReflectionToStringBuilder {
34  
35      private static DateFormat formatter = DateFormat.getDateTimeInstance();
36  
37      public ReflectionToStringBuilder(Object object) {
38          super(object, ToStringStyle.MULTI_LINE_STYLE);
39      }
40  
41      /***
42       * Calendar fields are formatted with DateFormat.getDateTimeInstance()
43       * instead of using Calendar.toString().
44       * 
45       * @see org.apache.commons.lang.builder.ReflectionToStringBuilder#getValue(java.lang.reflect.Field)
46       */
47      protected Object getValue(Field f) throws IllegalArgumentException,
48              IllegalAccessException {
49          Object value = super.getValue(f);
50  
51          if (Calendar.class.isInstance(value)) {
52              Calendar c = (Calendar) value;
53              return formatter.format(c.getTime());
54          } else
55              return value;
56      }
57  
58  }