View Javadoc
1   /*
2    * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  /*
27   * This source code is provided to illustrate the usage of a given feature
28   * or technique and has been deliberately simplified. Additional steps
29   * required for a production-quality application, such as security checks,
30   * input validation and proper error handling, might not be present in
31   * this sample code.
32   */
33  
34  
35  package com.sun.tools.example.debug.bdi;
36  
37  import com.sun.jdi.*;
38  import java.util.StringTokenizer;
39  
40  class PatternReferenceTypeSpec implements ReferenceTypeSpec {
41      final boolean isWild;
42      final String classId;
43  
44      PatternReferenceTypeSpec(String classId)
45  //                             throws ClassNotFoundException
46      {
47  //        checkClassName(classId);
48          isWild = classId.startsWith("*.");
49          if (isWild) {
50              this.classId = classId.substring(1);
51          } else {
52              this.classId = classId;
53          }
54      }
55  
56      /**
57       * Does the specified ReferenceType match this spec.
58       */
59      @Override
60      public boolean matches(ReferenceType refType) {
61          if (isWild) {
62              return refType.name().endsWith(classId);
63          } else {
64              return refType.name().equals(classId);
65          }
66      }
67  
68      @Override
69      public int hashCode() {
70          return classId.hashCode();
71      }
72  
73      @Override
74      public boolean equals(Object obj) {
75          if (obj instanceof PatternReferenceTypeSpec) {
76              PatternReferenceTypeSpec spec = (PatternReferenceTypeSpec)obj;
77  
78              return classId.equals(spec.classId) && (isWild == spec.isWild);
79          } else {
80              return false;
81          }
82      }
83  
84      private void checkClassName(String className) throws ClassNotFoundException {
85          // Do stricter checking of class name validity on deferred
86          //  because if the name is invalid, it will
87          // never match a future loaded class, and we'll be silent
88          // about it.
89          StringTokenizer tokenizer = new StringTokenizer(className, ".");
90          boolean first = true;
91          while (tokenizer.hasMoreTokens()) {
92              String token = tokenizer.nextToken();
93              // Each dot-separated piece must be a valid identifier
94              // and the first token can also be "*". (Note that
95              // numeric class ids are not permitted. They must
96              // match a loaded class.)
97              if (!Utils.isJavaIdentifier(token) && !(first && token.equals("*"))) {
98                  throw new ClassNotFoundException();
99              }
100             first = false;
101         }
102     }
103 
104     @Override
105     public String toString() {
106         return isWild? "*" + classId : classId;
107     }
108 }