KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > util > BasicCompilationUnit


1 package spoon.support.util;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 import org.eclipse.jdt.core.IJavaElement;
7 import org.eclipse.jdt.core.compiler.CharOperation;
8 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
9 import org.eclipse.jdt.internal.compiler.util.Util;
10
11 /**
12  * A basic implementation of <code>ICompilationUnit</code> for use in the
13  * <code>SourceMapper</code>.
14  *
15  * @see ICompilationUnit
16  */

17 public class BasicCompilationUnit implements ICompilationUnit {
18     protected char[] contents;
19
20     // Note that if this compiler ICompilationUnit's content is known in
21
// advance, the fileName is not used to retrieve this content.
22
// Instead it is used to keep enough information to recreate the
23
// IJavaElement corresponding to this compiler ICompilationUnit.
24
// Thus the fileName can be a path to a .class file, or even a path in a
25
// .jar to a .class file.
26
// (e.g. /P/lib/mylib.jar|org/eclipse/test/X.class)
27
protected char[] fileName;
28
29     protected char[][] packageName;
30
31     protected char[] mainTypeName;
32
33     protected String JavaDoc encoding;
34
35     public BasicCompilationUnit(char[] contents, char[][] packageName,
36             String JavaDoc fileName) {
37         this.contents = contents;
38         this.fileName = fileName.toCharArray();
39         this.packageName = packageName;
40     }
41
42     public BasicCompilationUnit(char[] contents, char[][] packageName,
43             String JavaDoc fileName, String JavaDoc encoding) {
44         this(contents, packageName, fileName);
45         this.encoding = encoding;
46     }
47
48     public BasicCompilationUnit(char[] contents, char[][] packageName,
49             String JavaDoc fileName, IJavaElement javaElement) {
50         this(contents, packageName, fileName);
51     }
52
53     public char[] getContents() {
54         if (this.contents != null)
55             return this.contents; // answer the cached source
56

57         // otherwise retrieve it
58
try {
59             return Util.getFileCharContent(new File JavaDoc(new String JavaDoc(this.fileName)),
60                     this.encoding);
61         } catch (IOException JavaDoc e) {
62             // could not read file: returns an empty array
63
}
64         return CharOperation.NO_CHAR;
65     }
66
67     /**
68      * @see org.eclipse.jdt.internal.compiler.env.IDependent#getFileName()
69      */

70     public char[] getFileName() {
71         return this.fileName;
72     }
73
74     public char[] getMainTypeName() {
75         if (this.mainTypeName == null) {
76             int start = CharOperation.lastIndexOf('/', this.fileName) + 1;
77             if (start == 0
78                     || start < CharOperation.lastIndexOf('\\', this.fileName))
79                 start = CharOperation.lastIndexOf('\\', this.fileName) + 1;
80             int separator = CharOperation.indexOf('|', this.fileName) + 1;
81             if (separator > start) // case of a .class file in a default
82
// package in a jar
83
start = separator;
84
85             int end = CharOperation.lastIndexOf('$', this.fileName);
86             if (end == -1 || !Util.isClassFileName(this.fileName)) {
87                 end = CharOperation.lastIndexOf('.', this.fileName);
88                 if (end == -1)
89                     end = this.fileName.length;
90             }
91
92             this.mainTypeName = CharOperation.subarray(this.fileName, start,
93                     end);
94         }
95         return this.mainTypeName;
96     }
97
98     public char[][] getPackageName() {
99         return this.packageName;
100     }
101
102     public String JavaDoc toString() {
103         return "CompilationUnit: " + new String JavaDoc(this.fileName); //$NON-NLS-1$
104
}
105 }
106
Popular Tags