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.eclipse.jetty.util.TypeUtil;
21  import org.objectweb.asm.Type;
22  
23  /**
24   * Util
25   *
26   *
27   */
28  public class Util
29  { 
30      private static Class[] __envEntryClassTypes = 
31          new Class[] {String.class, Character.class, Integer.class, Boolean.class, Double.class, Byte.class, Short.class, Long.class, Float.class};
32      
33  
34      private static String[] __envEntryTypes = 
35          new String[] { Type.getDescriptor(String.class), Type.getDescriptor(Character.class), Type.getDescriptor(Integer.class), Type.getDescriptor(Boolean.class),
36                         Type.getDescriptor(Double.class), Type.getDescriptor(Byte.class), Type.getDescriptor(Short.class), Type.getDescriptor(Long.class), Type.getDescriptor(Float.class)};
37      
38      /**
39       * Check if the presented method belongs to a class that is one
40       * of the classes with which a servlet container should be concerned.
41       * @param m
42       * @return
43       */
44      public static boolean isServletType (Class c)
45      {    
46          boolean isServlet = false;
47          if (javax.servlet.Servlet.class.isAssignableFrom(c) ||
48                  javax.servlet.Filter.class.isAssignableFrom(c) || 
49                  javax.servlet.ServletContextListener.class.isAssignableFrom(c) ||
50                  javax.servlet.ServletContextAttributeListener.class.isAssignableFrom(c) ||
51                  javax.servlet.ServletRequestListener.class.isAssignableFrom(c) ||
52                  javax.servlet.ServletRequestAttributeListener.class.isAssignableFrom(c) ||
53                  javax.servlet.http.HttpSessionListener.class.isAssignableFrom(c) ||
54                  javax.servlet.http.HttpSessionAttributeListener.class.isAssignableFrom(c))
55  
56                  isServlet=true;
57          
58          return isServlet;  
59      }
60  
61      public static boolean isEnvEntryType (Class type)
62      {
63          boolean result = false;
64          for (int i=0;i<__envEntryClassTypes.length && !result;i++)
65          {
66              result = (type.equals(__envEntryClassTypes[i]));
67          }
68          return result;
69      }
70      
71      public static boolean isEnvEntryType (String desc)
72      {
73          boolean result = false;
74          for (int i=0;i<__envEntryTypes.length && !result;i++)
75          {
76              result = (desc.equals(__envEntryTypes[i]));
77          }
78          return result;
79      }
80      
81      public static String normalizePattern(String p)
82      {
83          if (p!=null && p.length()>0 && !p.startsWith("/") && !p.startsWith("*"))
84              return "/"+p;
85          return p;
86      }
87      
88  
89      
90      public static Class[] convertTypes (String params)
91      throws Exception
92      {
93          return convertTypes(Type.getArgumentTypes(params));
94      }
95      
96      
97      public static Class[] convertTypes (Type[] types)
98      throws Exception
99      {
100         if (types==null)
101             return new Class[0];
102         
103         Class[] classArray = new Class[types.length];
104         
105         for (int i=0; i<types.length; i++)
106         {
107             classArray[i] = convertType(types[i]);
108         }
109         return classArray;
110     }
111 
112     public static Class convertType (Type t)
113     throws Exception
114     {
115         if (t == null)
116             return (Class)null;
117         
118         switch (t.getSort())
119         {
120             case Type.BOOLEAN:
121             {
122                 return Boolean.TYPE;
123             }
124             case Type.ARRAY:
125             {
126                 Class clazz = convertType(t.getElementType());
127                 return Array.newInstance(clazz, 0).getClass();
128             }
129             case Type.BYTE:
130             {
131                 return Byte.TYPE;
132             }
133             case Type.CHAR:
134             {
135                 return Character.TYPE;
136             }
137             case Type.DOUBLE:
138             {
139                 return Double.TYPE;
140             }
141             case Type.FLOAT:
142             {
143                 return Float.TYPE;
144             }
145             case Type.INT:
146             {
147                 return Integer.TYPE;
148             }
149             case Type.LONG:
150             {
151                 return Long.TYPE;
152             }
153             case Type.OBJECT:
154             {
155                 return (Loader.loadClass(null, t.getClassName()));
156             }
157             case Type.SHORT:
158             {
159                 return Short.TYPE;
160             }
161             case Type.VOID:
162             {
163                 return null;
164             }
165             default:
166                 return null;
167         }
168         
169     }
170   
171     public static String asCanonicalName (Type t)
172     {
173         if (t == null)
174             return null;
175         
176         switch (t.getSort())
177         {
178             case Type.BOOLEAN:
179             {
180                 return TypeUtil.toName(Boolean.TYPE);
181             }
182             case Type.ARRAY:
183             {
184                 return  t.getElementType().getClassName();
185             }
186             case Type.BYTE:
187             {
188                 return TypeUtil.toName(Byte.TYPE);
189             }
190             case Type.CHAR:
191             {
192                 return TypeUtil.toName(Character.TYPE);
193             }
194             case Type.DOUBLE:
195             {
196                 return TypeUtil.toName(Double.TYPE);
197             }
198             case Type.FLOAT:
199             {
200                  return TypeUtil.toName(Float.TYPE);
201             }
202             case Type.INT:
203             {
204                 return TypeUtil.toName(Integer.TYPE);
205             }
206             case Type.LONG:
207             {
208                 return TypeUtil.toName(Long.TYPE);
209             }
210             case Type.OBJECT:
211             {
212                 return t.getClassName();
213             }
214             case Type.SHORT:
215             {
216                 return TypeUtil.toName(Short.TYPE);
217             }
218             case Type.VOID:
219             {
220                 return null;
221             }
222             default:
223                 return null;
224         }
225     }
226 }