View Javadoc

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