KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > util > ClassNameMatcher


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.util;
22
23 import proguard.classfile.ClassConstants;
24
25 /**
26  * This StringMatcher tests whether internal class names match a
27  * given regular expression.
28  * Supported wildcards are
29  * '?' for a single Java identifier character,
30  * '*' for any number of regular Java identifier characters, and
31  * '**' for any number of regular Java identifier characters or package separator
32  * characters.
33  * '%' for a single internal primitive type character (Z, B, C, S, I, F, J, or D),
34  *
35  * @author Eric Lafortune
36  */

37 public class ClassNameMatcher extends BasicMatcher
38 {
39     private static final char[] CLASS_NAME_CHARACTERS = new char[]
40     {
41         ClassConstants.SPECIAL_CLASS_CHARACTER
42     };
43
44     private static final char[] EXTENDED_CLASS_NAME_CHARACTERS = new char[]
45     {
46         ClassConstants.INTERNAL_PACKAGE_SEPARATOR
47     };
48
49     private static final char[] SPECIAL_PRIMITIVE_CHARACTERS = new char[]
50     {
51         ClassConstants.INTERNAL_TYPE_BOOLEAN,
52         ClassConstants.INTERNAL_TYPE_BYTE,
53         ClassConstants.INTERNAL_TYPE_CHAR,
54         ClassConstants.INTERNAL_TYPE_SHORT,
55         ClassConstants.INTERNAL_TYPE_INT,
56         ClassConstants.INTERNAL_TYPE_FLOAT,
57         ClassConstants.INTERNAL_TYPE_LONG,
58         ClassConstants.INTERNAL_TYPE_DOUBLE
59     };
60
61
62     /**
63      * Creates a new ClassNameMatcher.
64      * @param regularExpression the regular expression against which strings
65      * will be matched.
66      */

67     public ClassNameMatcher(String JavaDoc regularExpression)
68     {
69         super(regularExpression,
70               CLASS_NAME_CHARACTERS,
71               EXTENDED_CLASS_NAME_CHARACTERS,
72               SPECIAL_PRIMITIVE_CHARACTERS);
73     }
74
75
76     /**
77      * A main method for testing class name matching.
78      */

79     public static void main(String JavaDoc[] args)
80     {
81         try
82         {
83             System.out.println("Regular expression ["+args[0]+"]");
84             ClassNameMatcher matcher = new ClassNameMatcher(args[0]);
85             for (int index = 1; index < args.length; index++)
86             {
87                 String JavaDoc string = args[index];
88                 System.out.print("String ["+string+"]");
89                 System.out.println(" -> match = "+matcher.matches(args[index]));
90             }
91         }
92         catch (Exception JavaDoc ex)
93         {
94             ex.printStackTrace();
95         }
96     }
97 }
98
Popular Tags