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 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 General Public License for more details. 018 * 019 * You should have received a copy of the GNU 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 public final class ClassUtils { 029 private ClassUtils() { 030 // Disable class instantiation 031 } 032 033 public static boolean isNumericType( Class<?> cls ) { 034 assert cls != null; 035 036 if( cls== float.class || cls == double.class || cls == byte.class || cls == short.class || cls == int.class || cls == long.class) { 037 return true; 038 } 039 040 Class<?> currentClass = cls; 041 while( currentClass != null ) { 042 if( currentClass == Number.class ) 043 return true; 044 currentClass = currentClass.getSuperclass(); 045 } 046 return false; 047 } 048 049 // JDK 1.4 does not implement Class.getSimpleName() 050 public static String getSimpleName( Class<?> cls ) { 051 assert cls != null; 052 053 String result = cls.getName(); 054 055 // Special case: inner classes! 056 int simpleNameStart = result.lastIndexOf("$"); 057 if( simpleNameStart >= 0 ) { 058 simpleNameStart++; // Skip the "$" 059 // Need to skip the digits immediately after the "$" 060 for( ; simpleNameStart < result.length(); simpleNameStart++ ) { 061 if( !Character.isDigit(result.charAt(simpleNameStart)) ) { 062 return result.substring(simpleNameStart); 063 } 064 } 065 } 066 else { 067 simpleNameStart = result.lastIndexOf( "."); 068 if( simpleNameStart >= 0) { 069 return result.substring(simpleNameStart+1); 070 } 071 } 072 return result; 073 } 074 075 // Note: would be nice to make recursive, so that we can handle array of array of array of ..., 076 // but in reality pretty-printing just one-dimensional arrays is ok 077 public static String getNicePrintableName( Class<?> cls ) { 078 assert cls != null; 079 080 Class<?> type = cls; 081 if( cls.isArray() ) { 082 type = cls.getComponentType(); 083 } 084 085 String typeName = type.getName(); 086 if( typeName.startsWith( "java.lang") ) { 087 typeName = getSimpleName(type); 088 } 089 090 if( cls.isArray() ) { 091 typeName += "[]"; 092 } 093 return typeName; 094 } 095 }