0N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/Apackage com.sun.tools.example.debug.gui;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport com.sun.jdi.*;
0N/A
0N/Aimport com.sun.tools.example.debug.event.*;
0N/A
0N/A/**
0N/A * Manage the list of source files.
0N/A * Origin of SourceListener events.
0N/A */
0N/Apublic class SourceManager {
0N/A
0N/A //### TODO: The source cache should be aged, and some cap
0N/A //### put on memory consumption by source files loaded into core.
0N/A
0N/A private List<SourceModel> sourceList;
0N/A private SearchPath sourcePath;
0N/A
4123N/A private ArrayList<SourceListener> sourceListeners = new ArrayList<SourceListener>();
0N/A
0N/A private Map<ReferenceType, SourceModel> classToSource = new HashMap<ReferenceType, SourceModel>();
0N/A
0N/A private Environment env;
0N/A
0N/A /**
0N/A * Hold on to it so it can be removed.
0N/A */
0N/A private SMClassListener classListener = new SMClassListener();
0N/A
0N/A public SourceManager(Environment env) {
0N/A this(env, new SearchPath(""));
0N/A }
0N/A
0N/A public SourceManager(Environment env, SearchPath sourcePath) {
0N/A this.env = env;
0N/A this.sourceList = new LinkedList<SourceModel>();
0N/A this.sourcePath = sourcePath;
0N/A env.getExecutionManager().addJDIListener(classListener);
0N/A }
0N/A
0N/A /**
0N/A * Set path for access to source code.
0N/A */
0N/A public void setSourcePath(SearchPath sp) {
0N/A sourcePath = sp;
0N/A // Old cached sources are now invalid.
0N/A sourceList = new LinkedList<SourceModel>();
0N/A notifySourcepathChanged();
0N/A classToSource = new HashMap<ReferenceType, SourceModel>();
0N/A }
0N/A
0N/A public void addSourceListener(SourceListener l) {
4123N/A sourceListeners.add(l);
0N/A }
0N/A
0N/A public void removeSourceListener(SourceListener l) {
4123N/A sourceListeners.remove(l);
0N/A }
0N/A
0N/A private void notifySourcepathChanged() {
4123N/A ArrayList<SourceListener> l = new ArrayList<SourceListener>(sourceListeners);
0N/A SourcepathChangedEvent evt = new SourcepathChangedEvent(this);
0N/A for (int i = 0; i < l.size(); i++) {
4123N/A l.get(i).sourcepathChanged(evt);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get path for access to source code.
0N/A */
0N/A public SearchPath getSourcePath() {
0N/A return sourcePath;
0N/A }
0N/A
0N/A /**
0N/A * Get source object associated with a Location.
0N/A */
0N/A public SourceModel sourceForLocation(Location loc) {
0N/A return sourceForClass(loc.declaringType());
0N/A }
0N/A
0N/A /**
0N/A * Get source object associated with a class or interface.
0N/A * Returns null if not available.
0N/A */
0N/A public SourceModel sourceForClass(ReferenceType refType) {
28N/A SourceModel sm = classToSource.get(refType);
0N/A if (sm != null) {
0N/A return sm;
0N/A }
0N/A try {
0N/A String filename = refType.sourceName();
0N/A String refName = refType.name();
0N/A int iDot = refName.lastIndexOf('.');
0N/A String pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : "";
0N/A String full = pkgName.replace('.', File.separatorChar) + filename;
0N/A File path = sourcePath.resolve(full);
0N/A if (path != null) {
0N/A sm = sourceForFile(path);
0N/A classToSource.put(refType, sm);
0N/A return sm;
0N/A }
0N/A return null;
0N/A } catch (AbsentInformationException e) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get source object associated with an absolute file path.
0N/A */
0N/A //### Use hash table for this?
0N/A public SourceModel sourceForFile(File path) {
28N/A Iterator<SourceModel> iter = sourceList.iterator();
0N/A SourceModel sm = null;
0N/A while (iter.hasNext()) {
28N/A SourceModel candidate = iter.next();
0N/A if (candidate.fileName().equals(path)) {
0N/A sm = candidate;
0N/A iter.remove(); // Will move to start of list.
0N/A break;
0N/A }
0N/A }
0N/A if (sm == null && path.exists()) {
0N/A sm = new SourceModel(env, path);
0N/A }
0N/A if (sm != null) {
0N/A // At start of list for faster access
0N/A sourceList.add(0, sm);
0N/A }
0N/A return sm;
0N/A }
0N/A
0N/A private class SMClassListener extends JDIAdapter
0N/A implements JDIListener {
0N/A
4123N/A @Override
0N/A public void classPrepare(ClassPrepareEventSet e) {
0N/A ReferenceType refType = e.getReferenceType();
0N/A SourceModel sm = sourceForClass(refType);
0N/A if (sm != null) {
0N/A sm.addClass(refType);
0N/A }
0N/A }
0N/A
4123N/A @Override
0N/A public void classUnload(ClassUnloadEventSet e) {
0N/A //### iterate through looking for (e.getTypeName()).
0N/A //### then remove it.
0N/A }
0N/A }
0N/A}