Showing the changes to java/pkg/com/sun/tools/example/debug/tty/SourceMapper.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,138 @@ | |||
| 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.tty; | ||
| 27 | |||
| 28 | import com.sun.jdi.Location; | ||
| 29 | import com.sun.jdi.AbsentInformationException; | ||
| 30 | import java.util.List; | ||
| 31 | import java.util.ArrayList; | ||
| 32 | import java.util.StringTokenizer; | ||
| 33 | import java.io.*; | ||
| 34 | |||
| 35 | class SourceMapper { | ||
| 36 | |||
| 37 | private final String[] dirs; | ||
| 38 | |||
| 39 | SourceMapper(List<String> sourcepath) { | ||
| 40 | /* | ||
| 41 | * sourcepath can arrive from the debugee as a List. | ||
| 42 | * (via PathSearchingVirtualMachine.classPath()) | ||
| 43 | */ | ||
| 44 | List<String> dirList = new ArrayList<String>(); | ||
| 45 | for (String element : sourcepath) { | ||
| 46 | //XXX remove .jar and .zip files; we want only directories on | ||
| 47 | //the source path. (Bug ID 4186582) | ||
| 48 | if ( ! (element.endsWith(".jar") || | ||
| 49 | element.endsWith(".zip"))) { | ||
| 50 | dirList.add(element); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | dirs = dirList.toArray(new String[0]); | ||
| 54 | } | ||
| 55 | |||
| 56 | SourceMapper(String sourcepath) { | ||
| 57 | /* | ||
| 58 | * sourcepath can also arrive from the command line | ||
| 59 | * as a String. (via "-sourcepath") | ||
| 60 | * | ||
| 61 | * Using File.pathSeparator as delimiter below is OK | ||
| 62 | * because we are on the same machine as the command | ||
| 63 | * line originiated. | ||
| 64 | */ | ||
| 65 | StringTokenizer st = new StringTokenizer(sourcepath, | ||
| 66 | File.pathSeparator); | ||
| 67 | List<String> dirList = new ArrayList<String>(); | ||
| 68 | while (st.hasMoreTokens()) { | ||
| 69 | String s = st.nextToken(); | ||
| 70 | //XXX remove .jar and .zip files; we want only directories on | ||
| 71 | //the source path. (Bug ID 4186582) | ||
| 72 | if ( ! (s.endsWith(".jar") || | ||
| 73 | s.endsWith(".zip"))) { | ||
| 74 | dirList.add(s); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | dirs = dirList.toArray(new String[0]); | ||
| 78 | } | ||
| 79 | |||
| 80 | /* | ||
| 81 | * Return the current sourcePath as a String. | ||
| 82 | */ | ||
| 83 | String getSourcePath() { | ||
| 84 | int i = 0; | ||
| 85 | StringBuffer sp; | ||
| 86 | if (dirs.length < 1) { | ||
| 87 | return ""; //The source path is empty. | ||
| 88 | } else { | ||
| 89 | sp = new StringBuffer(dirs[i++]); | ||
| 90 | } | ||
| 91 | for (; i < dirs.length; i++) { | ||
| 92 | sp.append(File.pathSeparator); | ||
| 93 | sp.append(dirs[i]); | ||
| 94 | } | ||
| 95 | return sp.toString(); | ||
| 96 | } | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Return a File cooresponding to the source of this location. | ||
| 100 | * Return null if not available. | ||
| 101 | */ | ||
| 102 | File sourceFile(Location loc) { | ||
| 103 | try { | ||
| 104 | String filename = loc.sourceName(); | ||
| 105 | String refName = loc.declaringType().name(); | ||
| 106 | int iDot = refName.lastIndexOf('.'); | ||
| 107 | String pkgName = (iDot >= 0)? refName.substring(0, iDot+1) : ""; | ||
| 108 | String full = pkgName.replace('.', File.separatorChar) + filename; | ||
| 109 | for (int i= 0; i < dirs.length; ++i) { | ||
| 110 | File path = new File(dirs[i], full); | ||
| 111 | if (path.exists()) { | ||
| 112 | return path; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | return null; | ||
| 116 | } catch (AbsentInformationException e) { | ||
| 117 | return null; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | /** | ||
| 122 | * Return a BufferedReader cooresponding to the source | ||
| 123 | * of this location. | ||
| 124 | * Return null if not available. | ||
| 125 | * Note: returned reader must be closed. | ||
| 126 | */ | ||
| 127 | BufferedReader sourceReader(Location loc) { | ||
| 128 | File sourceFile = sourceFile(loc); | ||
| 129 | if (sourceFile == null) { | ||
| 130 | return null; | ||
| 131 | } | ||
| 132 | try { | ||
| 133 | return new BufferedReader(new FileReader(sourceFile)); | ||
| 134 | } catch(IOException exc) { | ||
| 135 | } | ||
| 136 | return null; | ||
| 137 | } | ||
| 138 | } | ||