1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/javap/src/main/java/sun/tools/javap/JavapEnvironment.java Fri Nov 09 21:33:22 2012 +0100
1.3 @@ -0,0 +1,355 @@
1.4 +/*
1.5 + * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +
1.30 +package sun.tools.javap;
1.31 +
1.32 +import java.util.*;
1.33 +import java.io.*;
1.34 +import java.util.jar.*;
1.35 +
1.36 +
1.37 +/**
1.38 + * Strores flag values according to command line options
1.39 + * and sets path where to find classes.
1.40 + *
1.41 + * @author Sucheta Dambalkar
1.42 + */
1.43 +public class JavapEnvironment {
1.44 +
1.45 + //Access flags
1.46 + public static final int PRIVATE = 0;
1.47 + public static final int PROTECTED = 1;
1.48 + public static final int PACKAGE = 2;
1.49 + public static final int PUBLIC = 3;
1.50 +
1.51 + //search path flags.
1.52 + private static final int start = 0;
1.53 + private static final int cmdboot= 1;
1.54 + private static final int sunboot = 2;
1.55 + private static final int javaclass= 3;
1.56 + private static final int cmdextdir= 4;
1.57 + private static final int javaext= 5;
1.58 + private static final int cmdclasspath= 6;
1.59 + private static final int envclasspath= 7;
1.60 + private static final int javaclasspath= 8;
1.61 + private static final int currentdir = 9;
1.62 +
1.63 +
1.64 + // JavapEnvironment flag settings
1.65 + boolean showLineAndLocal = false;
1.66 + int showAccess = PACKAGE;
1.67 + boolean showDisassembled = false;
1.68 + boolean showVerbose = false;
1.69 + boolean showInternalSigs = false;
1.70 + String classPathString = null;
1.71 + String bootClassPathString = null;
1.72 + String extDirsString = null;
1.73 + boolean extDirflag = false;
1.74 + boolean nothingToDo = true;
1.75 + boolean showallAttr = false;
1.76 + String classpath = null;
1.77 + int searchpath = start;
1.78 +
1.79 + /**
1.80 + * According to which flags are set,
1.81 + * returns file input stream for classfile to disassemble.
1.82 + */
1.83 +
1.84 + public InputStream getFileInputStream(String Name){
1.85 + InputStream fileInStream = null;
1.86 + searchpath = cmdboot;
1.87 + try{
1.88 + if(searchpath == cmdboot){
1.89 + if(bootClassPathString != null){
1.90 + //search in specified bootclasspath.
1.91 + classpath = bootClassPathString;
1.92 + if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
1.93 + //no classes found in search path.
1.94 + else searchpath = cmdextdir;
1.95 + }
1.96 + else searchpath = sunboot;
1.97 + }
1.98 +
1.99 + if(searchpath == sunboot){
1.100 + if(System.getProperty("sun.boot.class.path") != null){
1.101 + //search in sun.boot.class.path
1.102 + classpath = System.getProperty("sun.boot.class.path");
1.103 + if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
1.104 + //no classes found in search path
1.105 + else searchpath = cmdextdir;
1.106 + }
1.107 + else searchpath = javaclass;
1.108 + }
1.109 +
1.110 + if(searchpath == javaclass){
1.111 + if(System.getProperty("java.class.path") != null){
1.112 + //search in java.class.path
1.113 + classpath =System.getProperty("java.class.path");
1.114 + if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
1.115 + //no classes found in search path
1.116 + else searchpath = cmdextdir;
1.117 + }
1.118 + else searchpath = cmdextdir;
1.119 + }
1.120 +
1.121 + if(searchpath == cmdextdir){
1.122 + if(extDirsString != null){
1.123 + //search in specified extdir.
1.124 + classpath = extDirsString;
1.125 + extDirflag = true;
1.126 + if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
1.127 + //no classes found in search path
1.128 + else {
1.129 + searchpath = cmdclasspath;
1.130 + extDirflag = false;
1.131 + }
1.132 + }
1.133 + else searchpath = javaext;
1.134 + }
1.135 +
1.136 + if(searchpath == javaext){
1.137 + if(System.getProperty("java.ext.dirs") != null){
1.138 + //search in java.ext.dirs
1.139 + classpath = System.getProperty("java.ext.dirs");
1.140 + extDirflag = true;
1.141 + if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
1.142 + //no classes found in search path
1.143 + else {
1.144 + searchpath = cmdclasspath;
1.145 + extDirflag = false;
1.146 + }
1.147 + }
1.148 + else searchpath = cmdclasspath;
1.149 + }
1.150 + if(searchpath == cmdclasspath){
1.151 + if(classPathString != null){
1.152 + //search in specified classpath.
1.153 + classpath = classPathString;
1.154 + if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
1.155 + //no classes found in search path
1.156 + else searchpath = 8;
1.157 + }
1.158 + else searchpath = envclasspath;
1.159 + }
1.160 +
1.161 + if(searchpath == envclasspath){
1.162 + if(System.getProperty("env.class.path")!= null){
1.163 + //search in env.class.path
1.164 + classpath = System.getProperty("env.class.path");
1.165 + if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
1.166 + //no classes found in search path.
1.167 + else searchpath = javaclasspath;
1.168 + }
1.169 + else searchpath = javaclasspath;
1.170 + }
1.171 +
1.172 + if(searchpath == javaclasspath){
1.173 + if(("application.home") == null){
1.174 + //search in java.class.path
1.175 + classpath = System.getProperty("java.class.path");
1.176 + if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
1.177 + //no classes found in search path.
1.178 + else searchpath = currentdir;
1.179 + }
1.180 + else searchpath = currentdir;
1.181 + }
1.182 +
1.183 + if(searchpath == currentdir){
1.184 + classpath = ".";
1.185 + //search in current dir.
1.186 + if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
1.187 + else {
1.188 + //no classes found in search path.
1.189 + error("Could not find "+ Name);
1.190 + System.exit(1);
1.191 + }
1.192 + }
1.193 + }catch(SecurityException excsec){
1.194 + excsec.printStackTrace();
1.195 + error("fatal exception");
1.196 + }catch(NullPointerException excnull){
1.197 + excnull.printStackTrace();
1.198 + error("fatal exception");
1.199 + }catch(IllegalArgumentException excill){
1.200 + excill.printStackTrace();
1.201 + error("fatal exception");
1.202 + }
1.203 +
1.204 + return null;
1.205 + }
1.206 +
1.207 +
1.208 + public void error(String msg) {
1.209 + System.err.println("ERROR:" +msg);
1.210 + }
1.211 +
1.212 + /**
1.213 + * Resolves file name for classfile to disassemble.
1.214 + */
1.215 + public InputStream resolvefilename(String name){
1.216 + String classname = name.replace('.', '/') + ".class";
1.217 + while (true) {
1.218 + InputStream instream = extDirflag
1.219 + ? resolveExdirFilename(classname)
1.220 + : resolveclasspath(classname);
1.221 + if (instream != null)
1.222 + return instream;
1.223 + int lastindex = classname.lastIndexOf('/');
1.224 + if (lastindex == -1) return null;
1.225 + classname = classname.substring(0, lastindex) + "$" +
1.226 + classname.substring(lastindex + 1);
1.227 + }
1.228 + }
1.229 +
1.230 + /**
1.231 + * Resolves file name for classfile to disassemble if flag exdir is set.
1.232 + */
1.233 + public InputStream resolveExdirFilename(String classname){
1.234 + if(classpath.indexOf(File.pathSeparator) != -1){
1.235 + //separates path
1.236 + StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
1.237 + while(st.hasMoreTokens()){
1.238 + String path = st.nextToken();
1.239 + InputStream in = resolveExdirFilenamehelper(path, classname);
1.240 + if (in != null)
1.241 + return in;
1.242 + }
1.243 + }else return (resolveExdirFilenamehelper(classpath, classname));
1.244 +
1.245 + return null;
1.246 + }
1.247 +
1.248 + /**
1.249 + * Resolves file name for classfile to disassemble.
1.250 + */
1.251 + public InputStream resolveclasspath(String classname){
1.252 + if(classpath.indexOf(File.pathSeparator) != -1){
1.253 + StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
1.254 + //separates path.
1.255 + while(st.hasMoreTokens()){
1.256 + String path = (st.nextToken()).trim();
1.257 + InputStream in = resolveclasspathhelper(path, classname);
1.258 + if(in != null) return in;
1.259 +
1.260 + }
1.261 + return null;
1.262 + }
1.263 + else return (resolveclasspathhelper(classpath, classname));
1.264 + }
1.265 +
1.266 +
1.267 + /**
1.268 + * Returns file input stream for classfile to disassemble if exdir is set.
1.269 + */
1.270 + public InputStream resolveExdirFilenamehelper(String path, String classname){
1.271 + File fileobj = new File(path);
1.272 + if(fileobj.isDirectory()){
1.273 + // gets list of files in that directory.
1.274 + File[] filelist = fileobj.listFiles();
1.275 + for(int i = 0; i < filelist.length; i++){
1.276 + try{
1.277 + //file is a jar file.
1.278 + if(filelist[i].toString().endsWith(".jar")){
1.279 + JarFile jfile = new JarFile(filelist[i]);
1.280 + if((jfile.getEntry(classname)) != null){
1.281 +
1.282 + InputStream filein = jfile.getInputStream(jfile.getEntry(classname));
1.283 + int bytearraysize = filein.available();
1.284 + byte []b = new byte[bytearraysize];
1.285 + int totalread = 0;
1.286 + while(totalread < bytearraysize){
1.287 + totalread += filein.read(b, totalread, bytearraysize-totalread);
1.288 + }
1.289 + InputStream inbyte = new ByteArrayInputStream(b);
1.290 + filein.close();
1.291 + return inbyte;
1.292 + }
1.293 + } else {
1.294 + //not a jar file.
1.295 + String filename = path+"/"+ classname;
1.296 + File file = new File(filename);
1.297 + if(file.isFile()){
1.298 + return (new FileInputStream(file));
1.299 + }
1.300 + }
1.301 + }catch(FileNotFoundException fnexce){
1.302 + fnexce.printStackTrace();
1.303 + error("cant read file");
1.304 + error("fatal exception");
1.305 + }catch(IOException ioexc){
1.306 + ioexc.printStackTrace();
1.307 + error("fatal exception");
1.308 + }
1.309 + }
1.310 + }
1.311 +
1.312 + return null;
1.313 + }
1.314 +
1.315 +
1.316 + /**
1.317 + * Returns file input stream for classfile to disassemble.
1.318 + */
1.319 + public InputStream resolveclasspathhelper(String path, String classname){
1.320 + File fileobj = new File(path);
1.321 + try{
1.322 + if(fileobj.isDirectory()){
1.323 + //is a directory.
1.324 + String filename = path+"/"+ classname;
1.325 + File file = new File(filename);
1.326 + if(file.isFile()){
1.327 + return (new FileInputStream(file));
1.328 + }
1.329 +
1.330 + }else if(fileobj.isFile()){
1.331 + if(fileobj.toString().endsWith(".jar")){
1.332 + //is a jar file.
1.333 + JarFile jfile = new JarFile(fileobj);
1.334 + if((jfile.getEntry(classname)) != null){
1.335 + InputStream filein = jfile.getInputStream(jfile.getEntry(classname));
1.336 + int bytearraysize = filein.available();
1.337 + byte []b = new byte[bytearraysize];
1.338 + int totalread = 0;
1.339 + while(totalread < bytearraysize){
1.340 + totalread += filein.read(b, totalread, bytearraysize-totalread);
1.341 + }
1.342 + InputStream inbyte = new ByteArrayInputStream(b);
1.343 + filein.close();
1.344 + return inbyte;
1.345 + }
1.346 + }
1.347 + }
1.348 + }catch(FileNotFoundException fnexce){
1.349 + fnexce.printStackTrace();
1.350 + error("cant read file");
1.351 + error("fatal exception");
1.352 + }catch(IOException ioexce){
1.353 + ioexce.printStackTrace();
1.354 + error("fatal exception");
1.355 + }
1.356 + return null;
1.357 + }
1.358 +}