001    /*
002     * Copyright © 2008, 2009 Pedro Agulló Soliveres.
003     * 
004     * This file is part of DirectJNgine.
005     *
006     * DirectJNgine is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU Lesser General Public License as published by
008     * the Free Software Foundation, either version 3 of the License.
009     *
010     * Commercial use is permitted to the extent that the code/component(s)
011     * do NOT become part of another Open Source or Commercially developed
012     * licensed development library or toolkit without explicit permission.
013     *
014     * DirectJNgine is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public License
020     * along with DirectJNgine.  If not, see <http://www.gnu.org/licenses/>.
021     * 
022     * This software uses the ExtJs library (http://extjs.com), which is 
023     * distributed under the GPL v3 license (see http://extjs.com/license).
024     */
025    
026    package com.softwarementors.extjs.djn;
027    
028    
029    public final class ClassUtils {
030      private ClassUtils() {
031        // Disable class instantiation
032      }
033      
034      public static boolean isNumericType( Class<?> cls ) {
035        assert cls != null;
036      
037        if( cls== float.class || cls == double.class || cls == byte.class || cls == short.class || cls == int.class || cls == long.class) {
038          return true;
039        }
040        
041        Class<?> currentClass = cls;
042        while( currentClass != null ) {
043          if( currentClass.equals(Number.class) )
044            return true;
045          currentClass = currentClass.getSuperclass();        
046        }
047        return false;
048      }
049    
050      // JDK 1.4 does not implement Class.getSimpleName()
051      public static String getSimpleName( Class<?> cls ) {
052        assert cls != null;
053        
054        String result = cls.getName();
055        
056        // Special case: inner classes!
057        int simpleNameStart = result.lastIndexOf("$");
058        if( simpleNameStart >= 0 ) {
059          simpleNameStart++; // Skip the "$"
060          // Need to skip the digits immediately after the "$"
061          for( ; simpleNameStart < result.length(); simpleNameStart++ ) {
062            if( !Character.isDigit(result.charAt(simpleNameStart)) ) {
063              return result.substring(simpleNameStart);
064            }
065          }
066        }
067        else {
068          simpleNameStart = result.lastIndexOf( ".");
069          if( simpleNameStart >= 0) {
070            return result.substring(simpleNameStart+1);
071          }
072        }
073        return result;
074      }
075    
076      // Note: would be nice to make recursive, so that we can handle array of array of array of ...,
077      //       but in reality pretty-printing just one-dimensional arrays is ok
078      public static String getNicePrintableName( Class<?> cls ) {
079        assert cls != null;
080        
081        Class<?> type = cls;
082        if( cls.isArray() ) {
083          type = cls.getComponentType();
084        }
085        
086        String typeName = type.getName();
087        if( typeName.startsWith( "java.lang") ) {
088          typeName = getSimpleName(type);
089        }
090        
091        if( cls.isArray() ) {
092          typeName += "[]";
093        }
094        return typeName;
095      }
096      
097    }