langtools/src/share/classes/sun/tools/javap/JavapEnvironment.java
author lana
Tue, 10 Feb 2009 12:26:12 -0800 (2009-02-10)
changeset 1979 f47a542ee9b7
parent 10 06bc494ca11e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
package sun.tools.javap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.jar.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * Strores flag values according to command line options
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * and sets path where to find classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * @author  Sucheta Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
public class JavapEnvironment {
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    //Access flags
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    public static final int PRIVATE = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    public static final int PROTECTED  = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    public static final int PACKAGE = 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    public static final int PUBLIC  = 3;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    //search path flags.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    private static final int start = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    private static final int  cmdboot= 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    private static final int sunboot = 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    private static final int  javaclass= 3;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private static final int  cmdextdir= 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private static final int  javaext= 5;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private static final int  cmdclasspath= 6;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private static final int  envclasspath= 7;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    private static final int  javaclasspath= 8;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    private static final int  currentdir = 9;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    // JavapEnvironment flag settings
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    boolean showLineAndLocal = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    int showAccess = PACKAGE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    boolean showDisassembled = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    boolean showVerbose = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    boolean showInternalSigs = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    String classPathString = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    String bootClassPathString = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    String extDirsString = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    boolean extDirflag = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    boolean nothingToDo = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    boolean showallAttr = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    String classpath = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    int searchpath = start;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     *  According to which flags are set,
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     *  returns file input stream for classfile to disassemble.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public InputStream getFileInputStream(String Name){
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        InputStream fileInStream = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        searchpath = cmdboot;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        try{
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            if(searchpath == cmdboot){
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                if(bootClassPathString != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                    //search in specified bootclasspath.
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
                    classpath = bootClassPathString;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                    if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                    //no classes found in search path.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
                    else searchpath = cmdextdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
                else searchpath = sunboot;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            if(searchpath == sunboot){
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                if(System.getProperty("sun.boot.class.path") != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
                    //search in sun.boot.class.path
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                    classpath = System.getProperty("sun.boot.class.path");
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                    if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                    //no classes found in search path
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                    else searchpath = cmdextdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                else searchpath = javaclass;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            if(searchpath == javaclass){
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                if(System.getProperty("java.class.path") != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                    //search in java.class.path
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                    classpath =System.getProperty("java.class.path");
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                    if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                    //no classes found in search path
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                    else searchpath = cmdextdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                else searchpath = cmdextdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            if(searchpath == cmdextdir){
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                if(extDirsString != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                    //search in specified extdir.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                    classpath = extDirsString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                    extDirflag = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                    if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                    //no classes found in search path
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                    else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                        searchpath = cmdclasspath;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                        extDirflag = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                else searchpath = javaext;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            if(searchpath == javaext){
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                if(System.getProperty("java.ext.dirs") != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                    //search in java.ext.dirs
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                    classpath = System.getProperty("java.ext.dirs");
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                    extDirflag = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                    if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                    //no classes found in search path
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                    else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                        searchpath = cmdclasspath;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                        extDirflag = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                else searchpath = cmdclasspath;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            if(searchpath == cmdclasspath){
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                if(classPathString != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                    //search in specified classpath.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                    classpath = classPathString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                    if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                    //no classes found in search path
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                    else searchpath = 8;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                else searchpath = envclasspath;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            if(searchpath == envclasspath){
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                if(System.getProperty("env.class.path")!= null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                    //search in env.class.path
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                    classpath = System.getProperty("env.class.path");
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                    if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                    //no classes found in search path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                    else searchpath = javaclasspath;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                else searchpath = javaclasspath;
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            if(searchpath == javaclasspath){
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                if(("application.home") == null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                    //search in java.class.path
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                    classpath = System.getProperty("java.class.path");
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                    if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                    //no classes found in search path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                    else searchpath = currentdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                else searchpath = currentdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
            if(searchpath == currentdir){
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                classpath = ".";
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                //search in current dir.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                if((fileInStream = resolvefilename(Name)) != null) return fileInStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                    //no classes found in search path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                    error("Could not find "+ Name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                    System.exit(1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        }catch(SecurityException excsec){
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            excsec.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            error("fatal exception");
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        }catch(NullPointerException excnull){
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            excnull.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            error("fatal exception");
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        }catch(IllegalArgumentException excill){
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            excill.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            error("fatal exception");
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    public void error(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        System.err.println("ERROR:" +msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     * Resolves file name for classfile to disassemble.
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    public InputStream resolvefilename(String name){
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        String classname = name.replace('.', '/') + ".class";
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
            InputStream instream = extDirflag
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                ? resolveExdirFilename(classname)
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                : resolveclasspath(classname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
            if (instream != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                return instream;
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
            int lastindex = classname.lastIndexOf('/');
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            if (lastindex == -1) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            classname = classname.substring(0, lastindex) + "$" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                classname.substring(lastindex + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     * Resolves file name for classfile to disassemble if flag exdir is set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    public InputStream resolveExdirFilename(String classname){
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        if(classpath.indexOf(File.pathSeparator) != -1){
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            //separates path
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            while(st.hasMoreTokens()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                String path = st.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                InputStream in = resolveExdirFilenamehelper(path, classname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                if (in != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                    return in;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        }else return (resolveExdirFilenamehelper(classpath, classname));
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
     * Resolves file name for classfile to disassemble.
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    public InputStream resolveclasspath(String classname){
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        if(classpath.indexOf(File.pathSeparator) != -1){
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator);
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            //separates path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            while(st.hasMoreTokens()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                String path = (st.nextToken()).trim();
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                InputStream in = resolveclasspathhelper(path, classname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                if(in != null) return in;
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        else return (resolveclasspathhelper(classpath, classname));
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     * Returns file input stream for classfile to disassemble if exdir is set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    public InputStream resolveExdirFilenamehelper(String path, String classname){
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        File fileobj = new File(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        if(fileobj.isDirectory()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            // gets list of files in that directory.
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            File[] filelist = fileobj.listFiles();
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            for(int i = 0; i < filelist.length; i++){
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                try{
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                    //file is a jar file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    if(filelist[i].toString().endsWith(".jar")){
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                        JarFile jfile = new JarFile(filelist[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                        if((jfile.getEntry(classname)) != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                            InputStream filein = jfile.getInputStream(jfile.getEntry(classname));
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                            int bytearraysize = filein.available();
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                            byte []b =  new byte[bytearraysize];
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                            int totalread = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                            while(totalread < bytearraysize){
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                                totalread += filein.read(b, totalread, bytearraysize-totalread);
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                            InputStream inbyte = new ByteArrayInputStream(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                            filein.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                            return inbyte;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                        //not a jar file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                        String filename = path+"/"+ classname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                        File file = new File(filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                        if(file.isFile()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                            return (new FileInputStream(file));
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                }catch(FileNotFoundException fnexce){
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                    fnexce.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                    error("cant read file");
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                    error("fatal exception");
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                }catch(IOException ioexc){
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                    ioexc.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                    error("fatal exception");
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
     * Returns file input stream for classfile to disassemble.
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    public InputStream resolveclasspathhelper(String path, String classname){
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        File fileobj = new File(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        try{
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            if(fileobj.isDirectory()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                //is a directory.
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                String filename = path+"/"+ classname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                File file = new File(filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                if(file.isFile()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                    return (new FileInputStream(file));
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            }else if(fileobj.isFile()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                if(fileobj.toString().endsWith(".jar")){
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                    //is a jar file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
                    JarFile jfile = new JarFile(fileobj);
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
                    if((jfile.getEntry(classname)) != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
                        InputStream filein = jfile.getInputStream(jfile.getEntry(classname));
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                        int bytearraysize = filein.available();
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                        byte []b =  new byte[bytearraysize];
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                        int totalread = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                        while(totalread < bytearraysize){
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                                totalread += filein.read(b, totalread, bytearraysize-totalread);
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                        InputStream inbyte = new ByteArrayInputStream(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                        filein.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                         return inbyte;
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        }catch(FileNotFoundException fnexce){
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            fnexce.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
            error("cant read file");
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            error("fatal exception");
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        }catch(IOException ioexce){
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            ioexce.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
            error("fatal exception");
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
}