KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > tty > PatternReferenceTypeSpec


1 /*
2  * @(#)PatternReferenceTypeSpec.java 1.15 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.tty;
36
37 import com.sun.jdi.*;
38 import com.sun.jdi.request.ClassPrepareRequest;
39 import java.util.StringTokenizer JavaDoc;
40
41
42 class PatternReferenceTypeSpec implements ReferenceTypeSpec {
43     final String JavaDoc classId;
44     String JavaDoc stem;
45
46     PatternReferenceTypeSpec(String JavaDoc classId) throws ClassNotFoundException JavaDoc {
47         this.classId = classId;
48         stem = classId;
49         if (classId.startsWith("*")) {
50             stem = stem.substring(1);
51         } else if (classId.endsWith("*")) {
52             stem = stem.substring(0, classId.length() - 1);
53         }
54         checkClassName(stem);
55     }
56
57     /**
58      * Is this spec unique or is it a class pattern?
59      */

60     public boolean isUnique() {
61         return classId.equals(stem);
62     }
63
64     /**
65      * Does the specified ReferenceType match this spec.
66      */

67     public boolean matches(ReferenceType refType) {
68         if (classId.startsWith("*")) {
69             return refType.name().endsWith(stem);
70         } else if (classId.endsWith("*")) {
71             return refType.name().startsWith(stem);
72         } else {
73             return refType.name().equals(classId);
74         }
75     }
76
77     public ClassPrepareRequest createPrepareRequest() {
78         ClassPrepareRequest request =
79             Env.vm().eventRequestManager().createClassPrepareRequest();
80         request.addClassFilter(classId);
81         request.addCountFilter(1);
82         return request;
83     }
84
85     public int hashCode() {
86         return classId.hashCode();
87     }
88
89     public boolean equals(Object JavaDoc obj) {
90         if (obj instanceof PatternReferenceTypeSpec) {
91             PatternReferenceTypeSpec spec = (PatternReferenceTypeSpec)obj;
92
93             return classId.equals(spec.classId);
94         } else {
95             return false;
96         }
97     }
98
99     private void checkClassName(String JavaDoc className) throws ClassNotFoundException JavaDoc {
100         // Do stricter checking of class name validity on deferred
101
// because if the name is invalid, it will
102
// never match a future loaded class, and we'll be silent
103
// about it.
104
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(className, ".");
105         while (tokenizer.hasMoreTokens()) {
106             String JavaDoc token = tokenizer.nextToken();
107             // Each dot-separated piece must be a valid identifier
108
// and the first token can also be "*". (Note that
109
// numeric class ids are not permitted. They must
110
// match a loaded class.)
111
if (!isJavaIdentifier(token)) {
112                 throw new ClassNotFoundException JavaDoc();
113             }
114         }
115     }
116
117     private boolean isJavaIdentifier(String JavaDoc s) {
118         if (s.length() == 0) {
119             return false;
120         }
121
122         int cp = s.codePointAt(0);
123         if (! Character.isJavaIdentifierStart(cp)) {
124             return false;
125         }
126
127         for (int i = Character.charCount(cp); i < s.length(); i += Character.charCount(cp)) {
128             cp = s.codePointAt(i);
129             if (! Character.isJavaIdentifierPart(cp)) {
130                 return false;
131             }
132         }
133
134         return true;
135     }
136
137     public String JavaDoc toString() {
138         return classId;
139     }
140 }
141
142
143
Popular Tags