Showing the changes to java/pkg/com/sun/tools/example/debug/gui/SourceManager.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,180 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1998, 2008, 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 | package com.sun.tools.example.debug.gui; | ||
| 27 | |||
| 28 | import java.io.*; | ||
| 29 | import java.util.*; | ||
| 30 | |||
| 31 | import com.sun.jdi.*; | ||
| 32 | |||
| 33 | import com.sun.tools.example.debug.event.*; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Manage the list of source files. | ||
| 37 | * Origin of SourceListener events. | ||
| 38 | */ | ||
| 39 | public class SourceManager { | ||
| 40 | |||
| 41 | //### TODO: The source cache should be aged, and some cap | ||
| 42 | //### put on memory consumption by source files loaded into core. | ||
| 43 | |||
| 44 | private List<SourceModel> sourceList; | ||
| 45 | private SearchPath sourcePath; | ||
| 46 | |||
| 47 | private ArrayList<SourceListener> sourceListeners = new ArrayList<SourceListener>(); | ||
| 48 | |||
| 49 | private Map<ReferenceType, SourceModel> classToSource = new HashMap<ReferenceType, SourceModel>(); | ||
| 50 | |||
| 51 | private Environment env; | ||
| 52 | |||
| 53 | /** | ||
| 54 | * Hold on to it so it can be removed. | ||
| 55 | */ | ||
| 56 | private SMClassListener classListener = new SMClassListener(); | ||
| 57 | |||
| 58 | public SourceManager(Environment env) { | ||
| 59 | this(env, new SearchPath("")); | ||
| 60 | } | ||
| 61 | |||
| 62 | public SourceManager(Environment env, SearchPath sourcePath) { | ||
| 63 | this.env = env; | ||
| 64 | this.sourceList = new LinkedList<SourceModel>(); | ||
| 65 | this.sourcePath = sourcePath; | ||
| 66 | env.getExecutionManager().addJDIListener(classListener); | ||
| 67 | } | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Set path for access to source code. | ||
| 71 | */ | ||
| 72 | public void setSourcePath(SearchPath sp) { | ||
| 73 | sourcePath = sp; | ||
| 74 | // Old cached sources are now invalid. | ||
| 75 | sourceList = new LinkedList<SourceModel>(); | ||
| 76 | notifySourcepathChanged(); | ||
| 77 | classToSource = new HashMap<ReferenceType, SourceModel>(); | ||
| 78 | } | ||
| 79 | |||
| 80 | public void addSourceListener(SourceListener l) { | ||
| 81 | sourceListeners.add(l); | ||
| 82 | } | ||
| 83 | |||
| 84 | public void removeSourceListener(SourceListener l) { | ||
| 85 | sourceListeners.remove(l); | ||
| 86 | } | ||
| 87 | |||
| 88 | private void notifySourcepathChanged() { | ||
| 89 | ArrayList<SourceListener> l = new ArrayList<SourceListener>(sourceListeners); | ||
| 90 | SourcepathChangedEvent evt = new SourcepathChangedEvent(this); | ||
| 91 | for (int i = 0; i < l.size(); i++) { | ||
| 92 | l.get(i).sourcepathChanged(evt); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | /** | ||
| 97 | * Get path for access to source code. | ||
| 98 | */ | ||
| 99 | public SearchPath getSourcePath() { | ||
| 100 | return sourcePath; | ||
| 101 | } | ||
| 102 | |||
| 103 | /** | ||
| 104 | * Get source object associated with a Location. | ||
| 105 | */ | ||
| 106 | public SourceModel sourceForLocation(Location loc) { | ||
| 107 | return sourceForClass(loc.declaringType()); | ||
| 108 | } | ||
| 109 | |||
| 110 | /** | ||
| 111 | * Get source object associated with a class or interface. | ||
| 112 | * Returns null if not available. | ||
| 113 | */ | ||
| 114 | public SourceModel sourceForClass(ReferenceType refType) { | ||
| 115 | SourceModel sm = classToSource.get(refType); | ||
| 116 | if (sm != null) { | ||
| 117 | return sm; | ||
| 118 | } | ||
| 119 | try { | ||
| 120 | String filename = refType.sourceName(); | ||
| 121 | String refName = refType.name(); | ||
| 122 | int iDot = refName.lastIndexOf('.'); | ||
| 123 | String pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : ""; | ||
| 124 | String full = pkgName.replace('.', File.separatorChar) + filename; | ||
| 125 | File path = sourcePath.resolve(full); | ||
| 126 | if (path != null) { | ||
| 127 | sm = sourceForFile(path); | ||
| 128 | classToSource.put(refType, sm); | ||
| 129 | return sm; | ||
| 130 | } | ||
| 131 | return null; | ||
| 132 | } catch (AbsentInformationException e) { | ||
| 133 | return null; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Get source object associated with an absolute file path. | ||
| 139 | */ | ||
| 140 | //### Use hash table for this? | ||
| 141 | public SourceModel sourceForFile(File path) { | ||
| 142 | Iterator<SourceModel> iter = sourceList.iterator(); | ||
| 143 | SourceModel sm = null; | ||
| 144 | while (iter.hasNext()) { | ||
| 145 | SourceModel candidate = iter.next(); | ||
| 146 | if (candidate.fileName().equals(path)) { | ||
| 147 | sm = candidate; | ||
| 148 | iter.remove(); // Will move to start of list. | ||
| 149 | break; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | if (sm == null && path.exists()) { | ||
| 153 | sm = new SourceModel(env, path); | ||
| 154 | } | ||
| 155 | if (sm != null) { | ||
| 156 | // At start of list for faster access | ||
| 157 | sourceList.add(0, sm); | ||
| 158 | } | ||
| 159 | return sm; | ||
| 160 | } | ||
| 161 | |||
| 162 | private class SMClassListener extends JDIAdapter | ||
| 163 | implements JDIListener { | ||
| 164 | |||
| 165 | @Override | ||
| 166 | public void classPrepare(ClassPrepareEventSet e) { | ||
| 167 | ReferenceType refType = e.getReferenceType(); | ||
| 168 | SourceModel sm = sourceForClass(refType); | ||
| 169 | if (sm != null) { | ||
| 170 | sm.addClass(refType); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | @Override | ||
| 175 | public void classUnload(ClassUnloadEventSet e) { | ||
| 176 | //### iterate through looking for (e.getTypeName()). | ||
| 177 | //### then remove it. | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||