jdk/src/share/classes/com/sun/tools/example/debug/tty/EventRequestSpec.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.example.debug.tty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jdi.request.EventRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jdi.request.ExceptionRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jdi.request.ClassPrepareRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.jdi.event.ClassPrepareEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
abstract class EventRequestSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    final ReferenceTypeSpec refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    int suspendPolicy = EventRequest.SUSPEND_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    EventRequest resolved = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    ClassPrepareRequest prepareRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    EventRequestSpec(ReferenceTypeSpec refSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.refSpec = refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * The 'refType' is known to match, return the EventRequest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    abstract EventRequest resolveEventRequest(ReferenceType refType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                           throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @return If this EventRequestSpec matches the 'refType'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * return the cooresponding EventRequest.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    synchronized EventRequest resolve(ClassPrepareEvent event) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if ((resolved == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            (prepareRequest != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            prepareRequest.equals(event.request())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            resolved = resolveEventRequest(event.referenceType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            prepareRequest.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            prepareRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (refSpec instanceof PatternReferenceTypeSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                if (! prs.isUnique()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                     * Class pattern event requests are never
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                     * considered "resolved", since future class loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                     * might also match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                     * Create and enable a new ClassPrepareRequest to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                     * keep trying to resolve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    resolved = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    prepareRequest = refSpec.createPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    prepareRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return resolved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    synchronized void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (isResolved()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            Env.vm().eventRequestManager().deleteEventRequest(resolved());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (refSpec instanceof PatternReferenceTypeSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (! prs.isUnique()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                 * This is a class pattern.  Track down and delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                 * all EventRequests matching this spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                 * Note: Class patterns apply only to ExceptionRequests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                 * so that is all we need to examine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                ArrayList<ExceptionRequest> deleteList = new ArrayList<ExceptionRequest>();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   104
                for (ExceptionRequest er :
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   105
                         Env.vm().eventRequestManager().exceptionRequests()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    if (prs.matches(er.exception())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        deleteList.add (er);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                Env.vm().eventRequestManager().deleteEventRequests(deleteList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private EventRequest resolveAgainstPreparedClasses() throws Exception {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   116
        for (ReferenceType refType : Env.vm().allClasses()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (refType.isPrepared() && refSpec.matches(refType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                resolved = resolveEventRequest(refType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return resolved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    synchronized EventRequest resolveEagerly() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (resolved == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                 * Not resolved.  Schedule a prepare request so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                 * can resolve later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                prepareRequest = refSpec.createPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                prepareRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // Try to resolve in case the class is already loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                resolveAgainstPreparedClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (resolved != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    prepareRequest.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    prepareRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (refSpec instanceof PatternReferenceTypeSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (! prs.isUnique()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                     * Class pattern event requests are never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                     * considered "resolved", since future class loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                     * might also match.  Create a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                     * ClassPrepareRequest if necessary and keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                     * trying to resolve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    resolved = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    if (prepareRequest == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        prepareRequest = refSpec.createPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        prepareRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } catch (VMNotConnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // Do nothing. Another resolve will be attempted when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // VM is started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return resolved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the eventRequest this spec has been resolved to,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * null if so far unresolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    EventRequest resolved() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return resolved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return true if this spec has been resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    boolean isResolved() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return resolved != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    protected boolean isJavaIdentifier(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (s.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int cp = s.codePointAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (! Character.isJavaIdentifierStart(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        for (int i = Character.charCount(cp); i < s.length(); i += Character.charCount(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            cp = s.codePointAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (! Character.isJavaIdentifierPart(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    String errorMessageFor(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (e instanceof IllegalArgumentException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return (MessageOutput.format("Invalid command syntax"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else if (e instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // A runtime exception that we were not expecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw (RuntimeException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return (MessageOutput.format("Internal error; unable to set",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                         this.refSpec.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}