jdk/src/share/classes/com/sun/tools/example/debug/tty/EventRequestSpec.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 10292 ed7db6a12c2a
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    26
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    27
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    28
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    29
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    30
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    31
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    32
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    33
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
package com.sun.tools.example.debug.tty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.request.EventRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jdi.request.ExceptionRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jdi.request.ClassPrepareRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.jdi.event.ClassPrepareEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
abstract class EventRequestSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    final ReferenceTypeSpec refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    int suspendPolicy = EventRequest.SUSPEND_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    EventRequest resolved = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    ClassPrepareRequest prepareRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    EventRequestSpec(ReferenceTypeSpec refSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.refSpec = refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The 'refType' is known to match, return the EventRequest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    abstract EventRequest resolveEventRequest(ReferenceType refType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                           throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @return If this EventRequestSpec matches the 'refType'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * return the cooresponding EventRequest.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    synchronized EventRequest resolve(ClassPrepareEvent event) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if ((resolved == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            (prepareRequest != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            prepareRequest.equals(event.request())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            resolved = resolveEventRequest(event.referenceType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            prepareRequest.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            prepareRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (refSpec instanceof PatternReferenceTypeSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                if (! prs.isUnique()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                     * Class pattern event requests are never
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                     * considered "resolved", since future class loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                     * might also match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                     * Create and enable a new ClassPrepareRequest to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                     * keep trying to resolve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    resolved = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    prepareRequest = refSpec.createPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    prepareRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return resolved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    synchronized void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (isResolved()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            Env.vm().eventRequestManager().deleteEventRequest(resolved());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (refSpec instanceof PatternReferenceTypeSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (! prs.isUnique()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                 * This is a class pattern.  Track down and delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                 * all EventRequests matching this spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                 * Note: Class patterns apply only to ExceptionRequests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                 * so that is all we need to examine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                ArrayList<ExceptionRequest> deleteList = new ArrayList<ExceptionRequest>();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   111
                for (ExceptionRequest er :
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   112
                         Env.vm().eventRequestManager().exceptionRequests()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    if (prs.matches(er.exception())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        deleteList.add (er);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                Env.vm().eventRequestManager().deleteEventRequests(deleteList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private EventRequest resolveAgainstPreparedClasses() throws Exception {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   123
        for (ReferenceType refType : Env.vm().allClasses()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (refType.isPrepared() && refSpec.matches(refType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                resolved = resolveEventRequest(refType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return resolved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    synchronized EventRequest resolveEagerly() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (resolved == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                 * Not resolved.  Schedule a prepare request so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                 * can resolve later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                prepareRequest = refSpec.createPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                prepareRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                // Try to resolve in case the class is already loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                resolveAgainstPreparedClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (resolved != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    prepareRequest.disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    Env.vm().eventRequestManager().deleteEventRequest(prepareRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    prepareRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (refSpec instanceof PatternReferenceTypeSpec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                PatternReferenceTypeSpec prs = (PatternReferenceTypeSpec)refSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (! prs.isUnique()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                     * Class pattern event requests are never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                     * considered "resolved", since future class loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                     * might also match.  Create a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                     * ClassPrepareRequest if necessary and keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                     * trying to resolve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    resolved = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    if (prepareRequest == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        prepareRequest = refSpec.createPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        prepareRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } catch (VMNotConnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // Do nothing. Another resolve will be attempted when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            // VM is started.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return resolved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return the eventRequest this spec has been resolved to,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * null if so far unresolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    EventRequest resolved() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return resolved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return true if this spec has been resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    boolean isResolved() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return resolved != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    protected boolean isJavaIdentifier(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (s.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int cp = s.codePointAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (! Character.isJavaIdentifierStart(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        for (int i = Character.charCount(cp); i < s.length(); i += Character.charCount(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            cp = s.codePointAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (! Character.isJavaIdentifierPart(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    String errorMessageFor(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (e instanceof IllegalArgumentException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return (MessageOutput.format("Invalid command syntax"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else if (e instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            // A runtime exception that we were not expecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw (RuntimeException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return (MessageOutput.format("Internal error; unable to set",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                         this.refSpec.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
}