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