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