Coverage Report - net.sf.ezmorph.object.ClassMorpher
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassMorpher
100%
19/19
100%
10/10
2.286
 
 1  
 /*
 2  
  * Copyright 2006-2007 the original author or authors.
 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  
 
 17  
 package net.sf.ezmorph.object;
 18  
 
 19  
 import net.sf.ezmorph.MorphException;
 20  
 import net.sf.ezmorph.ObjectMorpher;
 21  
 
 22  
 /**
 23  
  * Morphs to a Class.<br>
 24  
  * This morpher is a singleton.
 25  
  *
 26  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 27  
  */
 28  3
 public final class ClassMorpher implements ObjectMorpher
 29  
 {
 30  4
    private static final ClassMorpher INSTANCE = new ClassMorpher();
 31  
 
 32  
    /**
 33  
     * Returns the singleton instance
 34  
     */
 35  
    public static ClassMorpher getInstance()
 36  
    {
 37  1102
       return INSTANCE;
 38  
    }
 39  
 
 40  3
    private ClassMorpher()
 41  1
    {
 42  4
    }
 43  
 
 44  
    public boolean equals( Object obj )
 45  
    {
 46  8
       return INSTANCE == obj;
 47  
    }
 48  
 
 49  
    public int hashCode()
 50  
    {
 51  12
       return 42 + getClass().hashCode();
 52  
    }
 53  
 
 54  
    public Object morph( Object value )
 55  
    {
 56  60
       if( value == null ){
 57  8
          return null;
 58  
       }
 59  
 
 60  52
       if( value instanceof Class ){
 61  16
          return (Class) value;
 62  
       }
 63  
 
 64  36
       if( "null".equals( value ) ){
 65  4
          return null;
 66  
       }
 67  
 
 68  
       try{
 69  32
          return Class.forName( value.toString() );
 70  
       }
 71  8
       catch( Exception e ){
 72  8
          throw new MorphException( e );
 73  
       }
 74  
    }
 75  
 
 76  
    public Class morphsTo()
 77  
    {
 78  2606
       return Class.class;
 79  
    }
 80  
 
 81  
    public boolean supports( Class clazz )
 82  
    {
 83  32
       return true;
 84  
    }
 85  
 }