|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DocumentDebugAction.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.model.debug.jpda; | |
| 38 | ||
| 39 | import com.sun.jdi.*; | |
| 40 | import com.sun.jdi.request.*; | |
| 41 | ||
| 42 | import java.util.Vector; | |
| 43 | import java.util.List; | |
| 44 | import java.io.File; | |
| 45 | ||
| 46 | import edu.rice.cs.drjava.model.*; | |
| 47 | import edu.rice.cs.drjava.model.definitions.ClassNameNotFoundException; | |
| 48 | import edu.rice.cs.drjava.model.debug.DebugException; | |
| 49 | import edu.rice.cs.drjava.model.debug.LineNotExecutableException; | |
| 50 | import javax.swing.text.BadLocationException; | |
| 51 | ||
| 52 | /** Superclasses all DebugActions that are associated with specific OpenDefinitionsDocuments. | |
| 53 | * @version $Id: DocumentDebugAction.java 5175 2010-01-20 08:46:32Z mgricken $ | |
| 54 | */ | |
| 55 | public abstract class DocumentDebugAction<T extends EventRequest> extends DebugAction<T> { | |
| 56 | ||
| 57 | protected volatile String _className; | |
| 58 | protected volatile String _exactClassName; | |
| 59 | protected volatile File _file; | |
| 60 | protected volatile OpenDefinitionsDocument _doc; | |
| 61 | protected int _offset; | |
| 62 | ||
| 63 | public final int SHORT_DOC_MAX_LENGTH = 20000; | |
| 64 | ||
| 65 | ||
| 66 | /** Creates a new DocumentDebugAction. Automatically tries to create the EventRequest if a ReferenceType can be | |
| 67 | * found, or else adds this object to the PendingRequestManager. Any subclass should automatically call | |
| 68 | * _initializeRequest in its constructor. | |
| 69 | * @param manager JPDADebugger in charge | |
| 70 | * @param doc Document this action corresponds to | |
| 71 | * @param offset Offset into the document that the action affects | |
| 72 | */ | |
| 73 | 0 | public DocumentDebugAction (JPDADebugger manager, OpenDefinitionsDocument doc, int offset) throws DebugException { |
| 74 | 0 | super(manager); |
| 75 | 0 | _exactClassName = null; |
| 76 | 0 | try { |
| 77 | 0 | if (offset >= 0) { |
| 78 | 0 | if (doc.getLength() < SHORT_DOC_MAX_LENGTH) { |
| 79 | // only do this on short files | |
| 80 | // in long files, getEnclosingClassName might take too long | |
| 81 | 0 | _exactClassName = doc.getEnclosingClassName(offset, true); |
| 82 | } | |
| 83 | } | |
| 84 | } | |
| 85 | catch(ClassNameNotFoundException cnnfe) { /* ignore, we don't need the exact class name */ } | |
| 86 | catch(BadLocationException ble) { /* ignore, we don't need the exact class name */ } | |
| 87 | 0 | try { |
| 88 | 0 | if (offset >= 0) { |
| 89 | 0 | _className = doc.getQualifiedClassName(offset); |
| 90 | } | |
| 91 | } | |
| 92 | catch (ClassNameNotFoundException cnnfe) { | |
| 93 | // Still couldn't find a class name, use "" | |
| 94 | 0 | _className = ""; |
| 95 | } | |
| 96 | // System.out.println("Breakpoint added: " + _className + ", exact=" + _exactClassName); | |
| 97 | ||
| 98 | 0 | try { |
| 99 | 0 | _file = doc.getFile(); |
| 100 | 0 | if (_file == null) throw new DebugException("This document has no source file."); |
| 101 | } | |
| 102 | catch (FileMovedException fme) { | |
| 103 | 0 | throw new DebugException("This document's file no longer exists: " + fme.getMessage()); |
| 104 | } | |
| 105 | 0 | _doc = doc; |
| 106 | 0 | _offset = offset; |
| 107 | } | |
| 108 | ||
| 109 | /** Returns the class name this DebugAction occurs in. */ | |
| 110 | 0 | public String getClassName() { return _className; } |
| 111 | ||
| 112 | /** Returns the file this DebugAction occurs in. */ | |
| 113 | 0 | public File getFile() { return _file; } |
| 114 | ||
| 115 | /** Returns the document this DebugAction occurs in. */ | |
| 116 | 0 | public OpenDefinitionsDocument getDocument() { return _doc; } |
| 117 | ||
| 118 | /** @return offset of this debug action. */ | |
| 119 | 0 | public int getOffset() { return _offset; } |
| 120 | ||
| 121 | /** @return exact class name, or null if not available. */ | |
| 122 | 0 | public String getExactClassName() { return _exactClassName; } |
| 123 | ||
| 124 | /** Creates EventRequests corresponding to this DebugAction, using the given ReferenceTypes. This is called either | |
| 125 | * from the DebugAction constructor or the PendingRequestManager, depending on when the ReferenceTypes become | |
| 126 | * available. (There may be multiple reference types for the same class if a custom class loader is used.) | |
| 127 | * @return true if the EventRequest is successfully created | |
| 128 | */ | |
| 129 | 0 | public boolean createRequests(Vector<ReferenceType> refTypes) throws DebugException { |
| 130 | 0 | _createRequests(refTypes); |
| 131 | 0 | if (_requests.size() > 0) { |
| 132 | 0 | _prepareRequests(_requests); |
| 133 | 0 | return true; |
| 134 | } | |
| 135 | 0 | else return false; |
| 136 | } | |
| 137 | ||
| 138 | /** This should always be called from the constructor of the subclass. Attempts to create EventRequests on the | |
| 139 | * given ReferenceTypes, and also adds this action to the pending request manager (so identical classes loaded | |
| 140 | * in the future will also have this action). | |
| 141 | */ | |
| 142 | 0 | protected void _initializeRequests(Vector<ReferenceType> refTypes) throws DebugException { |
| 143 | 0 | if (refTypes.size() > 0) createRequests(refTypes); |
| 144 | else { | |
| 145 | 0 | if (_exactClassName != null) { |
| 146 | 0 | List<ReferenceType> referenceTypes = _manager.getVM().classesByName(_exactClassName); |
| 147 | 0 | if (referenceTypes.size() > 0) { |
| 148 | // class has been loaded, but couldn't find this line number | |
| 149 | 0 | throw new LineNotExecutableException("Cannot set breakpoint, line " + getLineNumber() + |
| 150 | " is not an executable line.\nYou may have to recompile."); | |
| 151 | } | |
| 152 | } | |
| 153 | } | |
| 154 | //if (_request == null) { | |
| 155 | // couldn't create the request yet, add to the pending request manager | |
| 156 | ||
| 157 | // Experiment: always add to pending request, to deal with multpile class loads | |
| 158 | 0 | _manager.getPendingRequestManager().addPendingRequest(this); |
| 159 | //} | |
| 160 | } | |
| 161 | ||
| 162 | /** Creates appropriate EventRequests from the EventRequestManager and stores them in the _requests field. | |
| 163 | * @param refTypes All (identical) ReferenceTypes to which this action applies. (There may be multiple if a custom | |
| 164 | * class loader is in use.) | |
| 165 | * @throws DebugException if the requests could not be created. | |
| 166 | */ | |
| 167 | protected abstract void _createRequests(Vector<ReferenceType> refTypes) throws DebugException; | |
| 168 | ||
| 169 | /** Prepares this EventRequest with the current stored values. | |
| 170 | * @param request the EventRequest to prepare | |
| 171 | */ | |
| 172 | 0 | protected void _prepareRequest(T request) { |
| 173 | 0 | super._prepareRequest(request); |
| 174 | 0 | request.putProperty("document", _doc); |
| 175 | } | |
| 176 | } |
|
||||||||||