summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/util/ClassPath.scala
blob: c72bf3beb4891284118617578d1b750e421d6bc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2006, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |                                         **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$

package scala.tools.nsc.util;

import scala.tools.util.AbstractFile;
import scala.collection.mutable.ArrayBuffer;
import java.io.FileNotFoundException;
import java.io.File;
import java.util.StringTokenizer;

/** Richer classpath abstraction than files.
 *  Roughly based on Eclipse's classpath abstractions.
 *
 *  @author Sean McDirmid
 */
class ClassPath(onlyPresentation : Boolean) {

  class Source(val location : AbstractFile, val compile : Boolean) {
    // assert(location           != null, "cannot find source location");
    // assert(location.getFile() != null, "cannot find source location " + " " + location + " " + location.getClass());
    override def toString() : String = "" + location + " " + compile;
  }

  abstract class Entry(val location: AbstractFile) {
    // assert(location           != null, "cannot find classpath location");
    // assert(location.getFile() != null, "cannot find classpath location " + " " + location + " " + location.getClass());
    def source   : Source;
  }


  class Output(location0 : AbstractFile, val sourceFile : AbstractFile) extends Entry(location0) {
    def source = if (sourceFile != null) new Source(sourceFile, true); else null;
  }

  class Library(location0: AbstractFile) extends Entry(location0) {
    def doc : AbstractFile = null;
    def sourceFile : AbstractFile = null;
    def source = if (sourceFile == null) null else new Source(sourceFile, false);
  }


  class Context(val entries : List[Entry]) {
    def find(name : String, isDir : boolean) : Context = if (isPackage) {
      def find0(entries : List[Entry]) : Context = {
      	if (entries.isEmpty) new Context(Nil);
      	else {
      	  val ret = find0(entries.tail);
      	  val head = entries.head;
      	  val clazz = if (head.location == null) null;
      	              else head.location.lookupPath(name + (if (!isDir) ".class" else ""), isDir);


      	  val source0 = if (head.source == null) null; else {
      	    val source1 = head.source.location.lookupPath(name + (if (isDir) "" else ".scala"), isDir);
      	      if (source1 == null && !isDir && clazz != null) head.source.location;
      	      else source1;
      	  }
      	  if (clazz == null && source0 == null) ret;
      	  else {
      	    object entry extends Entry(clazz) {
      	      override def source =
            		if (source0 == null) null;
            		else new Source(source0, head.source.compile);
      	    };
      	    new Context(entry :: ret.entries);
      	  }
      	}
      }
      val ret = find0(entries);
      if (false && this.toString().indexOf("scala") != -1)
        System.err.println("FIND " + name + " in " + this + " => " + ret);

      if (ret.entries.isEmpty) {
        System.err.println("BAD_FILE: " + name + " in " + this);
        null;
      } else ret;
    } else null;


    def isPackage = {
    	if (entries.isEmpty) false;
    	else if (entries.head.location != null) entries.head.location.isDirectory();
    	else entries.head.source.location.isDirectory();
    }

    def name = {
    	if (entries.isEmpty) "<none>";
    	else {
    		val head = entries.head;

	      val name = if (head.location != null) head.location.getName() else head.source.location.getName();
	      if (isPackage) name;
	      else name.substring(0, name.length() - (".class").length());
    	}
    }

    override def toString(): String = toString(entries);

    def toString(entry : Entry): String =
    	((if (entry.location == null) "<none>";
    	  else entry.location.toString()) +
    	 (if (entry.source == null) ""; else " with_source=" + entry.source.location.toString()));

    def toString(entries0: List[Entry]): String =
      if (entries0.isEmpty) "";
      else toString(entries0.head) + ":::" + toString(entries0.tail);

    def isSourceFile = {

      def head = entries.head;
      def clazz = head.location;
      def source = if (head.source == null) null else head.source.location;
      def isPredef = source.getName().equals("Predef.scala");

      if (entries.isEmpty || entries.isEmpty || source == null) false;
      else if (!onlyPresentation && !head.source.compile) false;
      else if (source.isDirectory()) false;
      else if (clazz == null) true;
      else if (onlyPresentation && !isPredef) true;
      else if (source.lastModified() > clazz.lastModified()) true;
      else false;
    }

    def sourceFile = if (entries.head.source != null && !entries.head.source.location.isDirectory())
    	entries.head.source.location else null;

    def  classFile = if (!isSourceFile) entries.head       .location else null;

    def sourcePath = if (!isSourceFile && entries.head.source != null) entries.head.source.location else null;

    def validPackage(name: String): Boolean =
      if (name.equals("META-INF")) false;
      else if (name.startsWith(".")) false;
      else true;
  }
  class Build {
    val entries = new ArrayBuffer[Entry];


    def root = new Context(entries.toList);


    def this(classpath : String, source : String, output : String, boot : String, extdirs : String) = {
      this();
      //System.err.println("BOOT: " + boot);
      //System.err.println("CLAS: " + classpath);

      addFilesInPath(boot);

      addArchivesInExtDirPath(extdirs);
      val clazzes = AbstractFile.getDirectory(output);
      if (clazzes          == null) throw new FileNotFoundException("output location \"" + output + "\" not found");

      val strtok = new StringTokenizer(source, File.pathSeparator);
      if (!strtok.hasMoreTokens()) {
	val output0 = (new Output(clazzes, null));
  	entries += output0;
      } else while (strtok.hasMoreTokens()) {
	val sources = AbstractFile.getDirectory(strtok.nextToken());

	val output0 = (new Output(clazzes, sources));
	//System.err.println("OUTPUT: " + output);
  	entries += output0;
      }
      addFilesInPath(classpath);
      //System.err.println("CLASSPATH: " + root);
    }
    def library(classes : String, sources : String) = {
      assert(classes != null);
      val location = AbstractFile.getDirectory(classes);
      val sourceFile0 = (if (sources != null) AbstractFile.getDirectory(sources) else null);
      class Library0 extends Library(location) {
				override def sourceFile = sourceFile0;
      }
      entries += new Library0()
    }

    private def addFilesInPath(path: String) = {
      val strtok = new StringTokenizer(path, File.pathSeparator);
      while (strtok.hasMoreTokens()) {
				val file = AbstractFile.getDirectory(strtok.nextToken());
				if (file != null) entries += (new Library(file));
      }
    }

    private def addArchivesInExtDirPath(path: String) = {
      val strtok = new StringTokenizer(path, File.pathSeparator);
      while (strtok.hasMoreTokens()) {
        val file = AbstractFile.getDirectory(strtok.nextToken());
        val files = (if (file != null) file.list() else null);
        if (files != null) while(files.hasNext()) {
          val file0 = files.next().asInstanceOf[AbstractFile];
          val name = file0.getName();
          if (name.endsWith(".jar") || name.endsWith(".zip")) {
            val archive = AbstractFile.getDirectory(new File(file.getFile(), name));
            if (archive != null) entries += (new Library(archive));
          }
        }
      }
    }
  } // class Build

}