KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > javaguard > Tools


1 /**
2  * JavaGuard -- an obfuscation package for Java classfiles.
3  *
4  * Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
5  * Copyright (c) 2002 Thorsten Heit (theit@gmx.de)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * The author may be contacted at theit@gmx.de.
22  *
23  *
24  * $Id: Tools.java,v 1.1 2002/05/11 18:52:09 glurk Exp $
25  */

26 package net.sf.javaguard;
27
28
29 import java.util.Vector JavaDoc;
30
31
32 /** The Tools class contains generally useful, miscellaneous static methods.
33  *
34  * @author <a HREF="mailto:theit@gmx.de">Thorsten Heit</a>
35  * @author <a HREF="mailto:markw@retrologic.com">Mark Welsh</a>
36  */

37 public class Tools {
38   /** Checks whether a string is contained in a given array.
39    * @param s the string to check
40    * @param list the string array
41    * @return true if the string is contained in the array; false else
42    */

43   public static boolean isInArray(String JavaDoc s, String JavaDoc[] list) {
44     for (int i = 0; i < list.length; i++) {
45       if (s.equals(list[i])) return true;
46     }
47     return false;
48   }
49   
50   
51   /** Checks whether a string is contained in a given array. Capitalization is
52    * ignored when comparing strings.
53    * @param s the string to check
54    * @param list the string array
55    * @return true if the string is contained in the array; false else
56    */

57   public static boolean isInArrayIgnoreCase(String JavaDoc s, String JavaDoc[] list) {
58     for (int i = 0; i < list.length; i++) {
59       if (s.equalsIgnoreCase(list[i])) return true;
60     }
61     return false;
62   }
63   
64   
65   
66   
67   /** Parse a method or field descriptor into a list of parameter names (for
68    * methods) and a return type. The format is the same as the Class.forName()
69    * method returns.
70    * @param descriptor the method or field descriptor to parse
71    * @return array with parameter names and a return type
72    * @see #parseDescriptor(String, boolean)
73    * @throws IllegalStateException if an error occurs
74    */

75   public static String JavaDoc[] parseDescriptor(String JavaDoc descriptor)
76   throws IllegalStateException JavaDoc {
77     return parseDescriptor(descriptor, false);
78   }
79   
80   
81   /** Parse a method or field descriptor into a list of parameter names (for
82    * methods) and a return type. The format is the same as the Class.forName()
83    * method returns.
84    * @param descriptor the method or field descriptor to parse
85    * @param isDisplay true if the pretty display form for array types should be
86    * used; false if slashes in array type names should be replaced by the
87    * dot character
88    * @return array with parameter names and a return type
89    * @see #parseDescriptor(String)
90    * @throws IllegalStateException if an error occurs
91    */

92   public static String JavaDoc[] parseDescriptor(String JavaDoc descriptor, boolean isDisplay)
93   throws IllegalStateException JavaDoc {
94     // Check for field descriptor
95
String JavaDoc[] names = null;
96     if (descriptor.charAt(0) != '(') {
97       names = new String JavaDoc[1];
98       names[0] = descriptor;
99     } else {
100       // Method descriptor
101
Vector JavaDoc namesVec = new Vector JavaDoc();
102       descriptor = descriptor.substring(1);
103       String JavaDoc type = "";
104       while (descriptor.length() > 0) {
105         switch (descriptor.charAt(0)) {
106           case '[':
107             type = type + "[";
108             descriptor = descriptor.substring(1);
109             break;
110             
111           case 'B':
112           case 'C':
113           case 'D':
114           case 'F':
115           case 'I':
116           case 'J':
117           case 'S':
118           case 'Z':
119           case 'V':
120             namesVec.addElement(type + descriptor.substring(0, 1));
121             descriptor = descriptor.substring(1);
122             type = "";
123             break;
124             
125           case ')':
126             descriptor = descriptor.substring(1);
127             break;
128             
129           case 'L': {
130             int pos = descriptor.indexOf(';') + 1;
131             namesVec.addElement(type + descriptor.substring(0, pos));
132             descriptor = descriptor.substring(pos);
133             type = "";
134           }
135           break;
136           
137           default:
138             throw new IllegalStateException JavaDoc("Illegal field or method descriptor: " + descriptor);
139         }
140       }
141       names = new String JavaDoc[namesVec.size()];
142       for (int i = 0; i < names.length; i++) {
143         names[i] = (String JavaDoc) namesVec.elementAt(i);
144       }
145     }
146     
147     // Translate the names from JVM to Class.forName() format.
148
String JavaDoc[] translatedNames = new String JavaDoc[names.length];
149     for (int i = 0; i < names.length; i++) {
150       translatedNames[i] = translateType(names[i], isDisplay);
151     }
152     return translatedNames;
153   }
154   
155   
156   
157   
158   /** Translates a type specifier from the internal JVM convention to the
159    * Class.forName() one.
160    * @param inName the type specifier to translate
161    * @param isDisplay true if the pretty display form for array types should be
162    * returned; false if slashes in array type names should be replaced by the
163    * dot charactoer
164    * @return translated type
165    * @throws IllegalStateException if an error occurs
166    */

167   public static String JavaDoc translateType(String JavaDoc inName, boolean isDisplay)
168   throws IllegalStateException JavaDoc {
169     String JavaDoc outName = null;
170     switch (inName.charAt(0)) {
171       case '[':
172         // For array types, Class.forName() inconsistently uses the internal
173
// type name but with '/' --> '.'
174
if (!isDisplay) {
175           // return the Class.forName() form
176
outName = translate(inName);
177         } else {
178           // return the pretty display form
179
outName = translateType(inName.substring(1), true) + "[]";
180         }
181         break;
182         
183       case 'B':
184         outName = Byte.TYPE.getName();
185         break;
186         
187       case 'C':
188         outName = Character.TYPE.getName();
189         break;
190         
191       case 'D':
192         outName = Double.TYPE.getName();
193         break;
194         
195       case 'F':
196         outName = Float.TYPE.getName();
197         break;
198         
199       case 'I':
200         outName = Integer.TYPE.getName();
201         break;
202         
203       case 'J':
204         outName = Long.TYPE.getName();
205         break;
206         
207       case 'S':
208         outName = Short.TYPE.getName();
209         break;
210         
211       case 'Z':
212         outName = Boolean.TYPE.getName();
213         break;
214         
215       case 'V':
216         outName = Void.TYPE.getName();
217         break;
218         
219       case 'L': {
220         int pos = inName.indexOf(';');
221         outName = translate(inName.substring(1, inName.indexOf(';')));
222       }
223       break;
224       
225       default:
226         throw new IllegalStateException JavaDoc("Illegal type: " + inName);
227     }
228     return outName;
229   }
230   
231   
232   
233   
234   /** Translate a class name from the internal '/' convention to the regular
235    * '.' one.
236    * @param name the class name to translate
237    * @return translated class name
238    */

239   public static String JavaDoc translate(String JavaDoc name) {
240     return name.replace('/', '.');
241   }
242 }
243
Popular Tags