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