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