View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.annotations;
15  
16  import java.lang.reflect.Array;
17  import java.util.List;
18  
19  import org.eclipse.jetty.util.Loader;
20  import org.objectweb.asm.Type;
21  
22  /**
23   * Util
24   *
25   *
26   */
27  public class Util
28  { 
29      private static Class[] __envEntryTypes = 
30          new Class[] {String.class, Character.class, Integer.class, Boolean.class, Double.class, Byte.class, Short.class, Long.class, Float.class};
31  
32      /**
33       * Check if the presented method belongs to a class that is one
34       * of the classes with which a servlet container should be concerned.
35       * @param m
36       * @return
37       */
38      public static boolean isServletType (Class c)
39      {    
40          boolean isServlet = false;
41          if (javax.servlet.Servlet.class.isAssignableFrom(c) ||
42                  javax.servlet.Filter.class.isAssignableFrom(c) || 
43                  javax.servlet.ServletContextListener.class.isAssignableFrom(c) ||
44                  javax.servlet.ServletContextAttributeListener.class.isAssignableFrom(c) ||
45                  javax.servlet.ServletRequestListener.class.isAssignableFrom(c) ||
46                  javax.servlet.ServletRequestAttributeListener.class.isAssignableFrom(c) ||
47                  javax.servlet.http.HttpSessionListener.class.isAssignableFrom(c) ||
48                  javax.servlet.http.HttpSessionAttributeListener.class.isAssignableFrom(c))
49  
50                  isServlet=true;
51          
52          return isServlet;  
53      }
54  
55      public static boolean isEnvEntryType (Class type)
56      {
57          boolean result = false;
58          for (int i=0;i<__envEntryTypes.length && !result;i++)
59          {
60              result = (type.equals(__envEntryTypes[i]));
61          }
62          return result;
63      }
64      
65      public static String normalizePattern(String p)
66      {
67          if (p!=null && p.length()>0 && !p.startsWith("/") && !p.startsWith("*"))
68              return "/"+p;
69          return p;
70      }
71      
72  
73      
74      public static Class[] convertTypes (String params)
75      throws Exception
76      {
77          return convertTypes(Type.getArgumentTypes(params));
78      }
79      
80      
81      public static Class[] convertTypes (Type[] types)
82      throws Exception
83      {
84          if (types==null)
85              return new Class[0];
86          
87          Class[] classArray = new Class[types.length];
88          
89          for (int i=0; i<types.length; i++)
90          {
91              classArray[i] = convertType(types[i]);
92          }
93          return classArray;
94      }
95  
96      public static Class convertType (Type t)
97      throws Exception
98      {
99          if (t == null)
100             return (Class)null;
101         
102         switch (t.getSort())
103         {
104             case Type.BOOLEAN:
105             {
106                 return Boolean.TYPE;
107             }
108             case Type.ARRAY:
109             {
110                 Class clazz = convertType(t.getElementType());
111                 return Array.newInstance(clazz, 0).getClass();
112             }
113             case Type.BYTE:
114             {
115                 return Byte.TYPE;
116             }
117             case Type.CHAR:
118             {
119                 return Character.TYPE;
120             }
121             case Type.DOUBLE:
122             {
123                 return Double.TYPE;
124             }
125             case Type.FLOAT:
126             {
127                 return Float.TYPE;
128             }
129             case Type.INT:
130             {
131                 return Integer.TYPE;
132             }
133             case Type.LONG:
134             {
135                 return Long.TYPE;
136             }
137             case Type.OBJECT:
138             {
139                 return (Loader.loadClass(null, t.getClassName()));
140             }
141             case Type.SHORT:
142             {
143                 return Short.TYPE;
144             }
145             case Type.VOID:
146             {
147                 return null;
148             }
149             default:
150                 return null;
151         }
152         
153     }
154   
155 }