FileDocCategorySizeDatePackage
ClassPath.javaAPI DocJava SE 6 API11543Tue Jun 10 00:22:24 BST 2008com.sun.org.apache.bcel.internal.util

ClassPath

public class ClassPath extends Object implements Serializable
Responsible for loading (class) files from the CLASSPATH. Inspired by sun.tools.ClassPath.
version
$Id: ClassPath.java,v 1.2 2005/08/16 19:34:42 jeffsuttor Exp $
author
M. Dahm

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


           
     
    this.class_path = class_path;

    ArrayList vec = new ArrayList();

    for(StringTokenizer tok=new StringTokenizer(class_path,
						System.getProperty("path.separator"));
	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.

deprecated
Use SYSTEM_CLASS_PATH constant

    // this(getClassPath());
    this("");
  
Methods Summary
public booleanequals(java.lang.Object o)

    if(o instanceof ClassPath) {
      return class_path.equals(((ClassPath)o).class_path);
    }

    return false;
  
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 byte[]getBytes(java.lang.String name)

return
byte array for class

    return getBytes(name, ".class");
  
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");
  
public static final java.lang.StringgetClassPath()
Checks for class path components in the following properties: "java.class.path", "sun.boot.class.path", "java.ext.dirs"

return
class path as used by default by BCEL

      
    String class_path, boot_path, ext_path;
    
    try {
      class_path = System.getProperty("java.class.path");
      boot_path  = System.getProperty("sun.boot.class.path");
      ext_path   = System.getProperty("java.ext.dirs");
    }
    catch (SecurityException e) {
        return "";
    }
    
    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().intern();
  
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)
Return stream for class or resource on CLASSPATH.

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

    InputStream is = null;

    try {
      is = getClass().getClassLoader().getResourceAsStream(name + suffix);
    } catch(Exception e) { }

    if(is != null)
      return is;
    
    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);
      }
    }
  
public inthashCode()

    return class_path.hashCode();
  
public java.lang.StringtoString()

return
used class path string

    return class_path;