001    package railo.transformer.bytecode.util;
002    
003    import java.io.BufferedReader;
004    import java.io.Reader;
005    import java.util.Iterator;
006    import java.util.Map;
007    
008    import javax.servlet.jsp.JspWriter;
009    import javax.servlet.jsp.tagext.BodyContent;
010    import javax.servlet.jsp.tagext.Tag;
011    
012    import org.objectweb.asm.Type;
013    
014    import railo.commons.io.IOUtil;
015    import railo.commons.io.res.Resource;
016    import railo.commons.lang.ClassException;
017    import railo.commons.lang.ClassUtil;
018    import railo.commons.lang.StringUtil;
019    import railo.runtime.InterfacePage;
020    import railo.runtime.Page;
021    import railo.runtime.PageContext;
022    import railo.runtime.PageContextImpl;
023    import railo.runtime.PagePlus;
024    import railo.runtime.PageSource;
025    import railo.runtime.component.ImportDefintion;
026    import railo.runtime.component.Member;
027    import railo.runtime.config.Config;
028    import railo.runtime.config.ConfigWeb;
029    import railo.runtime.exp.Abort;
030    import railo.runtime.exp.ExceptionHandler;
031    import railo.runtime.exp.PageException;
032    import railo.runtime.interpreter.VariableInterpreter;
033    import railo.runtime.op.Caster;
034    import railo.runtime.op.Operator;
035    import railo.runtime.security.SecurityManager;
036    import railo.runtime.type.Collection;
037    import railo.runtime.type.FunctionValue;
038    import railo.runtime.type.Iteratorable;
039    import railo.runtime.type.List;
040    import railo.runtime.type.Scope;
041    import railo.runtime.type.UDF;
042    import railo.runtime.type.UDFImpl;
043    import railo.runtime.type.UDFProperties;
044    import railo.runtime.type.ref.Reference;
045    import railo.runtime.type.ref.VariableReference;
046    import railo.runtime.type.scope.Undefined;
047    import railo.runtime.type.scope.Variables;
048    import railo.runtime.type.util.ArrayUtil;
049    import railo.runtime.util.NumberRange;
050    import railo.runtime.writer.BodyContentUtil;
051    import railo.transformer.bytecode.BytecodeException;
052    
053    public final class Types {
054    
055    
056            // TODO muss wohl alle Prim typen sein plus Object
057        public static final int _BOOLEAN=1;
058        public static final int _DOUBLE=2;
059        private static final int _SHORT=7;
060        
061        public static final int _OBJECT=0;
062        public static final int _STRING=3;
063        
064    
065        private static final int _CHAR=     _DOUBLE;
066        private static final int _FLOAT=    _DOUBLE;
067        private static final int _LONG=     _DOUBLE;
068        private static final int _INT=      _DOUBLE;
069        private static final int _BYTE=     _DOUBLE;
070        
071        //public static final int SIZE_INT_TYPES=10;
072        
073    
074        
075    
076        public static final Type ABORT=Type.getType(Abort.class);
077        public static final Type ARRAY=Type.getType(railo.runtime.type.Array.class);
078    
079        public static final Type BYTE=Type.getType(Byte.class);
080        public static final Type BYTE_VALUE=Type.getType(byte.class);
081        public static final Type BYTE_ARRAY=Type.getType(Byte[].class);
082        public static final Type BYTE_VALUE_ARRAY=Type.getType(byte[].class);
083    
084        public static final Type BOOLEAN = Type.getType(Boolean.class);
085        public static final Type BOOLEAN_VALUE = Type.getType(boolean.class);
086    
087        public static final Type CHAR=Type.getType(char.class);
088        public static final Type CHARACTER=Type.getType(Character.class);
089    
090        public static final Type DOUBLE = Type.getType(Double.class);
091        public static final Type DOUBLE_VALUE = Type.getType(double.class);
092    
093        public static final Type FLOAT = Type.getType(Float.class);
094        public static final Type FLOAT_VALUE = Type.getType(float.class);
095    
096        public static final Type INTEGER = Type.getType(Integer.class);
097        public static final Type INT_VALUE = Type.getType(int.class);
098    
099        public static final Type LONG = Type.getType(Long.class);
100        public static final Type LONG_VALUE = Type.getType(long.class);
101    
102        public static final Type SHORT = Type.getType(Short.class);
103        public static final Type SHORT_VALUE = Type.getType(short.class);
104    
105        public static final Type COMPONENT=Type.getType(railo.runtime.Component.class);
106    
107        public final static Type PAGE=Type.getType(Page.class);
108        public final static Type PAGE_PLUS=Type.getType(PagePlus.class);
109        public final static Type PAGE_SOURCE=Type.getType(PageSource.class);
110        public static final Type COMPONENT_PAGE=Type.getType(railo.runtime.ComponentPage.class);
111            public static final Type INTERFACE_PAGE = Type.getType(InterfacePage.class);
112    
113        public static final Type COMPONENT_IMPL=Type.getType(railo.runtime.ComponentImpl.class);
114        public static final Type INTERFACE_IMPL=Type.getType(railo.runtime.InterfaceImpl.class);
115        
116        public static final Type DATE_TIME=Type.getType(railo.runtime.type.dt.DateTime.class);
117        
118        public static final Type DATE=Type.getType(java.util.Date.class);
119        
120        public static final Type FILE=Type.getType(java.io.File.class);
121        
122        public static final Type RESOURCE=Type.getType(Resource.class);
123        
124            public static final Type FUNCTION_VALUE = Type.getType(FunctionValue.class);
125    
126        public static final Type ITERATOR=Type.getType(Iterator.class);
127        public static final Type ITERATORABLE=Type.getType(Iteratorable.class);
128    
129        public static final Type NODE=Type.getType(org.w3c.dom.Node.class);
130    
131        public static final Type OBJECT=Type.getType(Object.class);
132    
133        public static final Type OBJECT_ARRAY=Type.getType(Object[].class);
134    
135        public static final Type PAGE_CONTEXT=Type.getType(PageContext.class);
136        public static final Type PAGE_CONTEXT_IMPL=Type.getType(PageContextImpl.class);
137    
138    
139        public final static Type QUERY=Type.getType(railo.runtime.type.Query.class);
140        //public final static Type QUERY_IMPL=Type.getType(railo.runtime.type.QueryImpl.class);
141        
142        public final static Type PAGE_EXCEPTION=Type.getType(PageException.class);
143    
144        public final static Type REFERENCE=Type.getType(Reference.class);
145    
146        public static final Type CASTER = Type.getType(Caster.class);
147    
148        public static final Type COLLECTION = Type.getType(Collection.class);
149        
150        public static final Type STRING = Type.getType(String.class);
151        public static final Type STRING_ARRAY = Type.getType(String[].class);
152        
153        public static final Type STRUCT = Type.getType(railo.runtime.type.Struct.class);
154        
155        public static final Type OPERATOR = Type.getType(Operator.class);
156    
157        public static final Type CONFIG = Type.getType(Config.class);
158        public static final Type CONFIG_WEB = Type.getType(ConfigWeb.class);
159    
160        public static final Type SCOPE = Type.getType(Scope.class);
161        public static final Type VARIABLES = Type.getType(Variables.class);
162    
163        public static final Type TIMESPAN = Type.getType(railo.runtime.type.dt.TimeSpan.class);
164    
165            public static final Type THROWABLE = Type.getType(Throwable.class);
166            public static final Type EXCEPTION = Type.getType(Exception.class);
167            
168            public static final Type VOID = Type.VOID_TYPE;
169            
170            public static final Type LIST = Type.getType(List.class);
171            public static final Type VARIABLE_INTERPRETER = Type.getType(VariableInterpreter.class);
172            public static final Type VARIABLE_REFERENCE = Type.getType(VariableReference.class);
173            public static final Type JSP_WRITER = Type.getType(JspWriter.class);
174            public static final Type TAG = Type.getType(Tag.class);
175            public static final Type NUMBER_RANGE = Type.getType(NumberRange.class);
176            public static final Type SECURITY_MANAGER = Type.getType(SecurityManager.class);
177            public static final Type READER = Type.getType(Reader.class);
178            public static final Type BUFFERED_READER = Type.getType(BufferedReader.class);
179            public static final Type ARRAY_UTIL = Type.getType(ArrayUtil.class);
180            public static final Type EXCEPTION_HANDLER = Type.getType(ExceptionHandler.class);
181            //public static final Type RETURN_ EXCEPTION = Type.getType(ReturnException.class);
182            public static final Type TIMEZONE = Type.getType(java.util.TimeZone.class);
183            public static final Type STRING_BUFFER = Type.getType(StringBuffer.class);
184            public static final Type MEMBER = Type.getType(Member.class);
185            public static final Type UDF = Type.getType(UDF.class);
186            public static final Type UDF_PROPERTIES = Type.getType(UDFProperties.class);
187            public static final Type UDF_IMPL = Type.getType(UDFImpl.class);
188            public static final Type UDF_PROPERTIES_ARRAY = Type.getType(UDFProperties[].class);
189            public static final Type UDF_IMPL_ARRAY = Type.getType(UDFImpl[].class);
190            public static final Type COLLECTION_KEY = Type.getType(Collection.Key.class);
191            public static final Type COLLECTION_KEY_ARRAY = Type.getType(Collection.Key[].class);
192            public static final Type UNDEFINED = Type.getType(Undefined.class);
193            public static final Type MAP = Type.getType(Map.class);
194            public static final Type CHAR_ARRAY = Type.getType(char[].class);
195            public static final Type IOUTIL = Type.getType(IOUtil.class);
196            public static final Type BODY_CONTENT = Type.getType(BodyContent.class);
197            public static final Type BODY_CONTENT_UTIL = Type.getType(BodyContentUtil.class);
198            public static final Type IMPORT_DEFINITIONS = Type.getType(ImportDefintion.class);
199            public static final Type IMPORT_DEFINITIONS_ARRAY = Type.getType(ImportDefintion[].class);
200             
201    
202            /**
203             * translate sString classname to a real type
204             * @param type
205             * @return
206             * @throws railo.runtime.exp.TemplateExceptionption 
207             */
208            public static Type toType(String type) throws BytecodeException {
209                    if(type==null) return OBJECT;
210                    type=type.trim();
211                    String lcType=StringUtil.toLowerCase(type);
212                    char first=lcType.charAt(0);
213                            
214            switch(first) {
215            case 'a':
216                if("any".equals(lcType))                                                            return OBJECT;
217                if("array".equals(lcType))                                                          return ARRAY;
218            break;
219            case 'b':
220                if("base64".equals(lcType))                                                         return STRING;
221                if("binary".equals(lcType))                                                         return BYTE_VALUE_ARRAY;
222                if("bool".equals(lcType) || "boolean".equals(type))         return BOOLEAN_VALUE;
223                if("boolean".equals(lcType))                                                        return BOOLEAN;
224                if("byte".equals(type))                                                                     return BYTE_VALUE;
225                if("byte".equals(lcType))                                                           return BYTE;
226            break;
227            case 'c':
228                if("char".equals(lcType))                                                           return CHAR;
229                if("character".equals(lcType))                                                      return CHARACTER;
230                if("collection".equals(lcType))                                                     return BYTE_VALUE_ARRAY;
231                if("component".equals(lcType))                                                      return COMPONENT;
232            break;
233            case 'd':
234                if("date".equals(lcType) || "datetime".equals(lcType))      return DATE_TIME;
235                if("decimal".equals(lcType))                                                        return STRING;
236                if("double".equals(type))                                                           return DOUBLE_VALUE;
237                if("double".equals(lcType))                                                         return DOUBLE;
238            break;
239            case 'f':
240                if("file".equals(lcType))                                                           return FILE;
241                if("float".equals(type))                                                            return FLOAT_VALUE;
242                if("float".equals(lcType))                                                          return FLOAT;
243            break;
244            case 'i':
245                if("int".equals(lcType))                                                            return INT_VALUE;
246                else if("integer".equals(lcType))                                           return INTEGER;
247            break;
248            case 'j':
249                if("java.lang.boolean".equals(lcType))                                      return BOOLEAN;
250                if("java.lang.byte".equals(lcType))                                         return BYTE;
251                if("java.lang.character".equals(lcType))                            return CHARACTER;
252                if("java.lang.short".equals(lcType))                                        return SHORT;
253                if("java.lang.integer".equals(lcType))                                      return INTEGER;
254                if("java.lang.long".equals(lcType))                                         return LONG;
255                if("java.lang.float".equals(lcType))                                        return FLOAT;
256                if("java.lang.double".equals(lcType))                                       return DOUBLE;
257                if("java.io.file".equals(lcType))                                           return FILE;
258                if("java.lang.string".equals(lcType))                                       return STRING;
259                if("java.lang.string[]".equals(lcType))                                     return STRING_ARRAY;
260                if("java.util.date".equals(lcType))                                         return DATE; 
261                if("java.lang.object".equals(lcType))                                       return OBJECT; 
262            break;
263            case 'l':
264                if("long".equals(type))                                                                     return LONG_VALUE;
265                if("long".equals(lcType))                                                           return LONG;
266                if("long".equals(lcType))                                                           return LONG;
267            break;
268            case 'n':
269                if("node".equals(lcType))                                                           return NODE;
270                if("number".equals(lcType))                                                         return DOUBLE_VALUE;
271                if("numeric".equals(lcType))                                                        return DOUBLE_VALUE;
272            break;
273            case 'o':
274                if("object".equals(lcType))                                                         return OBJECT;
275            break;
276            case 's':
277                if("string".equals(lcType))                                                         return STRING;
278                if("struct".equals(lcType))                                                         return STRUCT;
279                if("short".equals(type))                                                            return SHORT_VALUE;
280                if("short".equals(lcType))                                                          return SHORT;
281            break;
282            case 'v':
283                if("void".equals(lcType))                                                           return VOID;
284                if("variablestring".equals(lcType))                                         return STRING;
285                if("variable_string".equals(lcType))                                        return STRING;
286            break;
287            case 'x':
288                if("xml".equals(lcType))                                                            return NODE;
289            break;
290            case '[':
291                if("[Ljava.lang.String;".equals(lcType))                            return STRING_ARRAY;
292            break;
293            
294            
295            
296            default:
297                if("query".equals(lcType))                                                          return QUERY;
298                if("timespan".equals(lcType))                                                       return TIMESPAN;
299            }
300            
301            // TODO Array als Lbyte und auch byte[]
302            
303                    try {
304                            return Type.getType(ClassUtil.loadClass(type));
305                    } catch (ClassException e) {
306                            throw new BytecodeException(e.getMessage(),-1);
307                    }
308            }
309    
310            /**
311             * returns if given type is a "primitve" type or in other words a value type (no reference type, no object)
312             * @param type
313             * @return
314             */
315            public static boolean isPrimitiveType(int type) {
316                    return type!=_OBJECT && type!=_STRING;
317            }
318            
319            /**
320             * returns if given type is a "primitve" type or in other words a value type (no reference type, no object)
321             * @param type
322             * @return
323             */
324            public static boolean isPrimitiveType(Type type) {
325                    String className=type.getClassName();
326                    if(className.indexOf('.')!=-1) return false;
327    
328                    if("boolean".equals(className)) return true;
329                    if("short".equals(className)) return true;
330                    if("float".equals(className)) return true;
331                    if("long".equals(className)) return true;
332                    if("double".equals(className)) return true;
333                    if("char".equals(className)) return true;
334                    if("int".equals(className)) return true;
335                    if("byte".equals(className)) return true;
336                    
337                    return false;
338            }
339            public static int getType(Type type) {
340                    String className=type.getClassName();
341                    if(className.indexOf('.')!=-1) {
342                            if("java.lang.String".equalsIgnoreCase(className)) return _STRING;
343                            return _OBJECT;
344                    }
345    
346                    if("boolean".equals(className)) return _BOOLEAN;
347                    if("short".equals(className)) return _SHORT;
348                    if("float".equals(className)) return _FLOAT;
349                    if("long".equals(className)) return _LONG;
350                    if("double".equals(className)) return _DOUBLE;
351                    if("char".equals(className)) return _CHAR;
352                    if("int".equals(className)) return _INT;
353                    if("byte".equals(className)) return _BYTE; 
354                    
355                    return _OBJECT;
356            }
357    
358            public static Type toRefType(Type type) {
359                    String className=type.getClassName();
360                    if(className.indexOf('.')!=-1) return type;
361    
362                    if("boolean".equals(className)) return BOOLEAN;
363                    if("short".equals(className)) return SHORT;
364                    if("float".equals(className)) return FLOAT;
365                    if("long".equals(className)) return LONG;
366                    if("double".equals(className)) return DOUBLE;
367                    if("char".equals(className)) return CHARACTER;
368                    if("int".equals(className)) return INT_VALUE;
369                    if("byte".equals(className)) return BYTE;
370                    return type;
371            }
372    
373            
374    
375    }