2 * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
27 package sun.tools.javap;
31 import java.util.jar.*;
35 * Strores flag values according to command line options
36 * and sets path where to find classes.
38 * @author Sucheta Dambalkar
40 public class JavapEnvironment {
43 public static final int PRIVATE = 0;
44 public static final int PROTECTED = 1;
45 public static final int PACKAGE = 2;
46 public static final int PUBLIC = 3;
49 private static final int start = 0;
50 private static final int cmdboot= 1;
51 private static final int sunboot = 2;
52 private static final int javaclass= 3;
53 private static final int cmdextdir= 4;
54 private static final int javaext= 5;
55 private static final int cmdclasspath= 6;
56 private static final int envclasspath= 7;
57 private static final int javaclasspath= 8;
58 private static final int currentdir = 9;
61 // JavapEnvironment flag settings
62 boolean showLineAndLocal = false;
63 int showAccess = PACKAGE;
64 boolean showDisassembled = false;
65 boolean showVerbose = false;
66 boolean showInternalSigs = false;
67 String classPathString = null;
68 String bootClassPathString = null;
69 String extDirsString = null;
70 boolean extDirflag = false;
71 boolean nothingToDo = true;
72 boolean showallAttr = false;
73 String classpath = null;
74 int searchpath = start;
77 * According to which flags are set,
78 * returns file input stream for classfile to disassemble.
81 public InputStream getFileInputStream(String Name){
82 InputStream fileInStream = null;
85 if(searchpath == cmdboot){
86 if(bootClassPathString != null){
87 //search in specified bootclasspath.
88 classpath = bootClassPathString;
89 if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
90 //no classes found in search path.
91 else searchpath = cmdextdir;
93 else searchpath = sunboot;
96 if(searchpath == sunboot){
97 if(System.getProperty("sun.boot.class.path") != null){
98 //search in sun.boot.class.path
99 classpath = System.getProperty("sun.boot.class.path");
100 if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
101 //no classes found in search path
102 else searchpath = cmdextdir;
104 else searchpath = javaclass;
107 if(searchpath == javaclass){
108 if(System.getProperty("java.class.path") != null){
109 //search in java.class.path
110 classpath =System.getProperty("java.class.path");
111 if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
112 //no classes found in search path
113 else searchpath = cmdextdir;
115 else searchpath = cmdextdir;
118 if(searchpath == cmdextdir){
119 if(extDirsString != null){
120 //search in specified extdir.
121 classpath = extDirsString;
123 if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
124 //no classes found in search path
126 searchpath = cmdclasspath;
130 else searchpath = javaext;
133 if(searchpath == javaext){
134 if(System.getProperty("java.ext.dirs") != null){
135 //search in java.ext.dirs
136 classpath = System.getProperty("java.ext.dirs");
138 if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
139 //no classes found in search path
141 searchpath = cmdclasspath;
145 else searchpath = cmdclasspath;
147 if(searchpath == cmdclasspath){
148 if(classPathString != null){
149 //search in specified classpath.
150 classpath = classPathString;
151 if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
152 //no classes found in search path
155 else searchpath = envclasspath;
158 if(searchpath == envclasspath){
159 if(System.getProperty("env.class.path")!= null){
160 //search in env.class.path
161 classpath = System.getProperty("env.class.path");
162 if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
163 //no classes found in search path.
164 else searchpath = javaclasspath;
166 else searchpath = javaclasspath;
169 if(searchpath == javaclasspath){
170 if(("application.home") == null){
171 //search in java.class.path
172 classpath = System.getProperty("java.class.path");
173 if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
174 //no classes found in search path.
175 else searchpath = currentdir;
177 else searchpath = currentdir;
180 if(searchpath == currentdir){
182 //search in current dir.
183 if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
185 //no classes found in search path.
186 error("Could not find "+ Name);
190 }catch(SecurityException excsec){
191 excsec.printStackTrace();
192 error("fatal exception");
193 }catch(NullPointerException excnull){
194 excnull.printStackTrace();
195 error("fatal exception");
196 }catch(IllegalArgumentException excill){
197 excill.printStackTrace();
198 error("fatal exception");
205 public void error(String msg) {
206 System.err.println("ERROR:" +msg);
210 * Resolves file name for classfile to disassemble.
212 public InputStream resolvefilename(String name){
213 String classname = name.replace('.', '/') + ".class";
215 InputStream instream = extDirflag
216 ? resolveExdirFilename(classname)
217 : resolveclasspath(classname);
218 if (instream != null)
220 int lastindex = classname.lastIndexOf('/');
221 if (lastindex == -1) return null;
222 classname = classname.substring(0, lastindex) + "$" +
223 classname.substring(lastindex + 1);
228 * Resolves file name for classfile to disassemble if flag exdir is set.
230 public InputStream resolveExdirFilename(String classname){
231 if(classpath.indexOf(File.pathSeparator) != -1){
233 StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
234 while(st.hasMoreTokens()){
235 String path = st.nextToken();
236 InputStream in = resolveExdirFilenamehelper(path, classname);
240 }else return (resolveExdirFilenamehelper(classpath, classname));
246 * Resolves file name for classfile to disassemble.
248 public InputStream resolveclasspath(String classname){
249 if(classpath.indexOf(File.pathSeparator) != -1){
250 StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
252 while(st.hasMoreTokens()){
253 String path = (st.nextToken()).trim();
254 InputStream in = resolveclasspathhelper(path, classname);
255 if(in != null) return in;
260 else return (resolveclasspathhelper(classpath, classname));
265 * Returns file input stream for classfile to disassemble if exdir is set.
267 public InputStream resolveExdirFilenamehelper(String path, String classname){
268 File fileobj = new File(path);
269 if(fileobj.isDirectory()){
270 // gets list of files in that directory.
271 File[] filelist = fileobj.listFiles();
272 for(int i = 0; i < filelist.length; i++){
274 //file is a jar file.
275 if(filelist[i].toString().endsWith(".jar")){
276 JarFile jfile = new JarFile(filelist[i]);
277 if((jfile.getEntry(classname)) != null){
279 InputStream filein = jfile.getInputStream(jfile.getEntry(classname));
280 int bytearraysize = filein.available();
281 byte []b = new byte[bytearraysize];
283 while(totalread < bytearraysize){
284 totalread += filein.read(b, totalread, bytearraysize-totalread);
286 InputStream inbyte = new ByteArrayInputStream(b);
292 String filename = path+"/"+ classname;
293 File file = new File(filename);
295 return (new FileInputStream(file));
298 }catch(FileNotFoundException fnexce){
299 fnexce.printStackTrace();
300 error("cant read file");
301 error("fatal exception");
302 }catch(IOException ioexc){
303 ioexc.printStackTrace();
304 error("fatal exception");
314 * Returns file input stream for classfile to disassemble.
316 public InputStream resolveclasspathhelper(String path, String classname){
317 File fileobj = new File(path);
319 if(fileobj.isDirectory()){
321 String filename = path+"/"+ classname;
322 File file = new File(filename);
324 return (new FileInputStream(file));
327 }else if(fileobj.isFile()){
328 if(fileobj.toString().endsWith(".jar")){
330 JarFile jfile = new JarFile(fileobj);
331 if((jfile.getEntry(classname)) != null){
332 InputStream filein = jfile.getInputStream(jfile.getEntry(classname));
333 int bytearraysize = filein.available();
334 byte []b = new byte[bytearraysize];
336 while(totalread < bytearraysize){
337 totalread += filein.read(b, totalread, bytearraysize-totalread);
339 InputStream inbyte = new ByteArrayInputStream(b);
345 }catch(FileNotFoundException fnexce){
346 fnexce.printStackTrace();
347 error("cant read file");
348 error("fatal exception");
349 }catch(IOException ioexce){
350 ioexce.printStackTrace();
351 error("fatal exception");