J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2003 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.tools.example.debug.bdi; |
| 27 | |
| 28 | import java.util.*; |
| 29 | |
| 30 | import com.sun.jdi.*; |
| 31 | import com.sun.jdi.request.EventRequest; |
| 32 | |
| 33 | abstract public class EventRequestSpec { |
| 34 | |
| 35 | static final int STATUS_UNRESOLVED = 1; |
| 36 | static final int STATUS_RESOLVED = 2; |
| 37 | static final int STATUS_ERROR = 3; |
| 38 | |
| 39 | static final Object specPropertyKey = "spec"; |
| 40 | |
| 41 | final EventRequestSpecList specs; |
| 42 | final ReferenceTypeSpec refSpec; |
| 43 | EventRequest request = null; |
| 44 | |
| 45 | int status = STATUS_UNRESOLVED; |
| 46 | |
| 47 | EventRequestSpec(EventRequestSpecList specs, ReferenceTypeSpec refSpec) { |
| 48 | this.specs = specs; |
| 49 | this.refSpec = refSpec; |
| 50 | } |
| 51 | |
| 52 | void setRequest(EventRequest request) { |
| 53 | this.request = request; |
| 54 | request.putProperty(specPropertyKey, this); |
| 55 | request.enable(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * The 'refType' is known to match. |
| 60 | */ |
| 61 | abstract void resolve(ReferenceType refType) throws Exception; |
| 62 | |
| 63 | abstract void notifySet(SpecListener listener, SpecEvent evt); |
| 64 | abstract void notifyDeferred(SpecListener listener, SpecEvent evt); |
| 65 | abstract void notifyResolved(SpecListener listener, SpecEvent evt); |
| 66 | abstract void notifyDeleted(SpecListener listener, SpecEvent evt); |
| 67 | abstract void notifyError(SpecListener listener, SpecErrorEvent evt); |
| 68 | |
| 69 | /** |
| 70 | * The 'refType' is known to match. |
| 71 | */ |
| 72 | void resolveNotify(ReferenceType refType) { |
| 73 | try { |
| 74 | resolve(refType); |
| 75 | status = STATUS_RESOLVED; |
| 76 | specs.notifyResolved(this); |
| 77 | } catch(Exception exc) { |
| 78 | status = STATUS_ERROR; |
| 79 | specs.notifyError(this, exc); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * See if 'refType' matches and resolve. |
| 85 | */ |
| 86 | void attemptResolve(ReferenceType refType) { |
| 87 | if (!isResolved() && refSpec.matches(refType)) { |
| 88 | resolveNotify(refType); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void attemptImmediateResolve(VirtualMachine vm) { |
| 93 | // try to resolve immediately |
| 94 | Iterator iter = vm.allClasses().iterator(); |
| 95 | while (iter.hasNext()) { |
| 96 | ReferenceType refType = (ReferenceType)iter.next(); |
| 97 | if (refSpec.matches(refType)) { |
| 98 | try { |
| 99 | resolve(refType); |
| 100 | status = STATUS_RESOLVED; |
| 101 | specs.notifySet(this); |
| 102 | } catch(Exception exc) { |
| 103 | status = STATUS_ERROR; |
| 104 | specs.notifyError(this, exc); |
| 105 | } |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | specs.notifyDeferred(this); |
| 110 | } |
| 111 | |
| 112 | public EventRequest getEventRequest() { |
| 113 | return request; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @return true if this spec has been resolved. |
| 118 | */ |
| 119 | public boolean isResolved() { |
| 120 | return status == STATUS_RESOLVED; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @return true if this spec has not yet been resolved. |
| 125 | */ |
| 126 | public boolean isUnresolved() { |
| 127 | return status == STATUS_UNRESOLVED; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @return true if this spec is unresolvable due to error. |
| 132 | */ |
| 133 | public boolean isErroneous() { |
| 134 | return status == STATUS_ERROR; |
| 135 | } |
| 136 | |
| 137 | public String getStatusString() { |
| 138 | switch (status) { |
| 139 | case STATUS_RESOLVED: |
| 140 | return "resolved"; |
| 141 | case STATUS_UNRESOLVED: |
| 142 | return "deferred"; |
| 143 | case STATUS_ERROR: |
| 144 | return "erroneous"; |
| 145 | } |
| 146 | return "unknown"; |
| 147 | } |
| 148 | |
| 149 | boolean isJavaIdentifier(String s) { |
| 150 | return Utils.isJavaIdentifier(s); |
| 151 | } |
| 152 | |
| 153 | public String errorMessageFor(Exception e) { |
| 154 | if (e instanceof IllegalArgumentException) { |
| 155 | return ("Invalid command syntax"); |
| 156 | } else if (e instanceof RuntimeException) { |
| 157 | // A runtime exception that we were not expecting |
| 158 | throw (RuntimeException)e; |
| 159 | } else { |
| 160 | return ("Internal error; unable to set" + this); |
| 161 | } |
| 162 | } |
| 163 | } |