FileDocCategorySizeDatePackage
ClassPath.javaAPI DocJava SE 5 API10095Fri Aug 26 14:55:24 BST 2005com.sun.org.apache.bcel.internal.util

ClassPath

public class ClassPath extends Object
Responsible for loading (class) files from the CLASSPATH. Inspired by sun.tools.ClassPath.
version
$Id: ClassPath.java,v 1.1 2003/12/12 09:01:32 rameshm Exp $
author
M. Dahm

Fields Summary
private PathEntry[]
paths
Constructors Summary
public ClassPath(String class_path)
Search for classes in given path.

    ArrayList vec = new ArrayList();

    for(StringTokenizer tok=new StringTokenizer(class_path,
						java.io.File.pathSeparator);
	tok.hasMoreTokens();)
    {
      String path = tok.nextToken();
      
      if(!path.equals("")) {
	File file = new File(path);

	try {
	  if(file.exists()) {
	    if(file.isDirectory())
	      vec.add(new Dir(path));
	    else
	      vec.add(new Zip(new ZipFile(file)));
	  }
	} catch(IOException e) {
	  System.err.println("CLASSPATH component " + file + ": " + e);
	}
      }
    }

    paths = new PathEntry[vec.size()];
    vec.toArray(paths);
  
public ClassPath()
Search for classes in CLASSPATH.

    this(getClassPath());
  
Methods Summary
public byte[]getBytes(java.lang.String name)

return
byte array for class

    return getBytes(name, ".class");
  
public byte[]getBytes(java.lang.String name, java.lang.String suffix)

param
name fully qualified file name, e.g. java/lang/String
param
suffix file name ends with suffix, e.g. .java
return
byte array for file on class path

    InputStream is = getInputStream(name, suffix);
    
    if(is == null)
      throw new IOException("Couldn't find: " + name + suffix);

    DataInputStream dis   = new DataInputStream(is);
    byte[]          bytes = new byte[is.available()];
    dis.readFully(bytes);
    dis.close(); is.close();

    return bytes;
  
public com.sun.org.apache.bcel.internal.util.ClassPath$ClassFilegetClassFile(java.lang.String name, java.lang.String suffix)

param
name fully qualified file name, e.g. java/lang/String
param
suffix file name ends with suff, e.g. .java
return
class file for the java class

    for(int i=0; i < paths.length; i++) {
      ClassFile cf;

      if((cf = paths[i].getClassFile(name, suffix)) != null)
	return cf;
    }

    throw new IOException("Couldn't find: " + name + suffix);
  
public com.sun.org.apache.bcel.internal.util.ClassPath$ClassFilegetClassFile(java.lang.String name)

param
name fully qualified class name, e.g. java.lang.String
return
input stream for class

    return getClassFile(name, ".class");
  
private static final java.lang.StringgetClassPath()

    String class_path = System.getProperty("java.class.path");
    String boot_path  = System.getProperty("sun.boot.class.path");
    String ext_path   = System.getProperty("java.ext.dirs");

    ArrayList list = new ArrayList();

    getPathComponents(class_path, list);
    getPathComponents(boot_path, list);

    ArrayList dirs = new ArrayList();
    getPathComponents(ext_path, dirs);

    for(Iterator e = dirs.iterator(); e.hasNext(); ) {
      File     ext_dir    = new File((String)e.next());
      String[] extensions = ext_dir.list(new FilenameFilter() {
	public boolean accept(File dir, String name) {
	  name = name.toLowerCase();
	  return name.endsWith(".zip") || name.endsWith(".jar");
	}
      });

      if(extensions != null)
	for(int i=0; i < extensions.length; i++)
	  list.add(ext_path + File.separatorChar + extensions[i]);
    }

    StringBuffer buf = new StringBuffer();

    for(Iterator e = list.iterator(); e.hasNext(); ) {
      buf.append((String)e.next());

      if(e.hasNext())
	buf.append(File.pathSeparatorChar);
    }

    return buf.toString();
  
public java.io.InputStreamgetInputStream(java.lang.String name)

param
name fully qualified class name, e.g. java.lang.String
return
input stream for class

    return getInputStream(name, ".class");
  
public java.io.InputStreamgetInputStream(java.lang.String name, java.lang.String suffix)

param
name fully qualified file name, e.g. java/lang/String
param
suffix file name ends with suff, e.g. .java
return
input stream for file on class path

    return getClassFile(name, suffix).getInputStream();
  
public java.lang.StringgetPath(java.lang.String name)

param
name name of file to search for, e.g. java/lang/String.java
return
full (canonical) path for file

    int    index  = name.lastIndexOf('.");
    String suffix = "";

    if(index > 0) {
      suffix = name.substring(index);
      name   = name.substring(0, index);
    }
    
    return getPath(name, suffix);
  
public java.lang.StringgetPath(java.lang.String name, java.lang.String suffix)

param
name name of file to search for, e.g. java/lang/String
param
suffix file name suffix, e.g. .java
return
full (canonical) path for file, if it exists

    return getClassFile(name, suffix).getPath();
  
private static final voidgetPathComponents(java.lang.String path, java.util.ArrayList list)

    if(path != null) {
      StringTokenizer tok = new StringTokenizer(path, File.pathSeparator);

      while(tok.hasMoreTokens()) {
        String name = tok.nextToken();
        File   file = new File(name);

	if(file.exists())
	  list.add(name);
      }
    }