001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.type.util;
020
021import lucee.runtime.Component;
022import lucee.runtime.reflection.Reflector;
023import lucee.runtime.text.xml.struct.XMLStruct;
024import lucee.runtime.type.Array;
025import lucee.runtime.type.Query;
026import lucee.runtime.type.Struct;
027import lucee.runtime.type.UDF;
028import lucee.runtime.type.dt.DateTime;
029import lucee.runtime.type.dt.TimeSpan;
030import lucee.runtime.type.scope.Scope;
031
032public final class Type {
033        
034    public static String getName(Object o) {
035        if(o == null) return "null";
036        if(o instanceof UDF) return "user defined function ("+(((UDF)o).getFunctionName())+")";
037        else if(o instanceof Boolean) return "Boolean";
038        else if(o instanceof Number) return "Number";
039        else if(o instanceof TimeSpan) return "TimeSpan";
040        else if(o instanceof Array) return "Array";
041        else if(o instanceof Component) return "Component "+((Component)o).getAbsName();
042        else if(o instanceof Scope) return ((Scope)o).getTypeAsString();
043        else if(o instanceof Struct) {
044                if(o instanceof XMLStruct)return "XML";
045                return "Struct";
046        }
047        else if(o instanceof Query) return "Query";
048        else if(o instanceof DateTime) return "DateTime";
049        else if(o instanceof byte[]) return "Binary";
050        else {
051            String className=o.getClass().getName();
052            if(className.startsWith("java.lang.")) {
053                return className.substring(10);
054            }
055            return className;
056        }
057        
058    }
059
060    public static String getName(Class clazz) {
061        if(clazz == null) return "null";
062        //String name=clazz.getName();
063        //if(Reflector.isInstaneOf(clazz,String.class))                   return "String";
064        if(Reflector.isInstaneOf(clazz,UDF.class)) return "user defined function";
065        //else if(Reflector.isInstaneOf(clazz,Boolean.class))             return "Boolean";
066        //else if(Reflector.isInstaneOf(clazz,Number.class))              return "Number";
067        else if(Reflector.isInstaneOf(clazz,Array.class))               return "Array";
068        else if(Reflector.isInstaneOf(clazz,Struct.class))              return "Struct";
069        else if(Reflector.isInstaneOf(clazz,Query.class))               return "Query";
070        else if(Reflector.isInstaneOf(clazz,DateTime.class))            return "DateTime";
071        else if(Reflector.isInstaneOf(clazz,Component.class))           return "Component";
072        else if(Reflector.isInstaneOf(clazz,byte[].class))              return "Binary";
073        else {
074            String className=clazz.getName();
075            if(className.startsWith("java.lang.")) {
076                return className.substring(10);
077            }
078            return className;
079        }
080        
081        
082    }
083
084}