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  
61                  isServlet=true;
62          
63          return isServlet;  
64      }
65  
66      public static boolean isEnvEntryType (Class type)
67      {
68          boolean result = false;
69          for (int i=0;i<__envEntryClassTypes.length && !result;i++)
70          {
71              result = (type.equals(__envEntryClassTypes[i]));
72          }
73          return result;
74      }
75      
76      public static boolean isEnvEntryType (String desc)
77      {
78          boolean result = false;
79          for (int i=0;i<__envEntryTypes.length && !result;i++)
80          {
81              result = (desc.equals(__envEntryTypes[i]));
82          }
83          return result;
84      }
85      
86      public static String normalizePattern(String p)
87      {
88          if (p!=null && p.length()>0 && !p.startsWith("/") && !p.startsWith("*"))
89              return "/"+p;
90          return p;
91      }
92      
93  
94      
95      public static Class[] convertTypes (String params)
96      throws Exception
97      {
98          return convertTypes(Type.getArgumentTypes(params));
99      }
100     
101     
102     public static Class[] convertTypes (Type[] types)
103     throws Exception
104     {
105         if (types==null)
106             return new Class[0];
107         
108         Class[] classArray = new Class[types.length];
109         
110         for (int i=0; i<types.length; i++)
111         {
112             classArray[i] = convertType(types[i]);
113         }
114         return classArray;
115     }
116 
117     public static Class convertType (Type t)
118     throws Exception
119     {
120         if (t == null)
121             return (Class)null;
122         
123         switch (t.getSort())
124         {
125             case Type.BOOLEAN:
126             {
127                 return Boolean.TYPE;
128             }
129             case Type.ARRAY:
130             {
131                 Class clazz = convertType(t.getElementType());
132                 return Array.newInstance(clazz, 0).getClass();
133             }
134             case Type.BYTE:
135             {
136                 return Byte.TYPE;
137             }
138             case Type.CHAR:
139             {
140                 return Character.TYPE;
141             }
142             case Type.DOUBLE:
143             {
144                 return Double.TYPE;
145             }
146             case Type.FLOAT:
147             {
148                 return Float.TYPE;
149             }
150             case Type.INT:
151             {
152                 return Integer.TYPE;
153             }
154             case Type.LONG:
155             {
156                 return Long.TYPE;
157             }
158             case Type.OBJECT:
159             {
160                 return (Loader.loadClass(null, t.getClassName()));
161             }
162             case Type.SHORT:
163             {
164                 return Short.TYPE;
165             }
166             case Type.VOID:
167             {
168                 return null;
169             }
170             default:
171                 return null;
172         }
173         
174     }
175   
176     public static String asCanonicalName (Type t)
177     {
178         if (t == null)
179             return null;
180         
181         switch (t.getSort())
182         {
183             case Type.BOOLEAN:
184             {
185                 return TypeUtil.toName(Boolean.TYPE);
186             }
187             case Type.ARRAY:
188             {
189                 return  t.getElementType().getClassName();
190             }
191             case Type.BYTE:
192             {
193                 return TypeUtil.toName(Byte.TYPE);
194             }
195             case Type.CHAR:
196             {
197                 return TypeUtil.toName(Character.TYPE);
198             }
199             case Type.DOUBLE:
200             {
201                 return TypeUtil.toName(Double.TYPE);
202             }
203             case Type.FLOAT:
204             {
205                  return TypeUtil.toName(Float.TYPE);
206             }
207             case Type.INT:
208             {
209                 return TypeUtil.toName(Integer.TYPE);
210             }
211             case Type.LONG:
212             {
213                 return TypeUtil.toName(Long.TYPE);
214             }
215             case Type.OBJECT:
216             {
217                 return t.getClassName();
218             }
219             case Type.SHORT:
220             {
221                 return TypeUtil.toName(Short.TYPE);
222             }
223             case Type.VOID:
224             {
225                 return null;
226             }
227             default:
228                 return null;
229         }
230     }
231 }