View Javadoc

1   /*
2    * Copyright 2004-2005, 2007-2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.transform.converters;
17  
18  import java.util.Locale;
19  
20  import net.sf.morph.transform.DecoratedConverter;
21  import net.sf.morph.transform.ExplicitTransformer;
22  import net.sf.morph.transform.transformers.BaseTransformer;
23  import net.sf.morph.util.ClassUtils;
24  
25  /**
26   * Converts an object into that object's class.
27   * 
28   * @author Matt Sgarlata
29   * @since Feb 10, 2006
30   */
31  public class ObjectToClassConverter extends BaseTransformer implements DecoratedConverter, ExplicitTransformer {
32  	private static final Class[] DEST = new Class[] { Class.class };
33  
34  	/**
35  	 * {@inheritDoc}
36  	 */
37  	protected Object convertImpl(Class destinationClass, Object source, Locale locale) throws Exception {
38  		return ClassUtils.getClass(source);
39  	}
40  
41  	/**
42  	 * {@inheritDoc}
43  	 */
44  	// this isn't technically needed but will be faster than the default
45  	// implementation in BaseTransformer
46  	protected boolean isTransformableImpl(Class destinationType, Class sourceType) throws Exception {
47  		return destinationType == Class.class;
48  	}
49  
50  	/**
51  	 * {@inheritDoc}
52  	 */
53  	protected boolean isWrappingRuntimeExceptions() {
54  	    return true;
55      }
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	protected Class[] getSourceClassesImpl() throws Exception {
61  		return new Class[] { Object.class, float.class, double.class,
62  				byte.class, short.class, int.class, long.class, boolean.class, char.class };
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	protected Class[] getDestinationClassesImpl() throws Exception {
69  		return DEST;
70  	}
71  
72  }