1 package spoon.support.util; 2 3 import java.io.File ; 4 import java.io.IOException ; 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 17 public class BasicCompilationUnit implements ICompilationUnit { 18 protected char[] contents; 19 20 protected char[] fileName; 28 29 protected char[][] packageName; 30 31 protected char[] mainTypeName; 32 33 protected String encoding; 34 35 public BasicCompilationUnit(char[] contents, char[][] packageName, 36 String 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 fileName, String encoding) { 44 this(contents, packageName, fileName); 45 this.encoding = encoding; 46 } 47 48 public BasicCompilationUnit(char[] contents, char[][] packageName, 49 String fileName, IJavaElement javaElement) { 50 this(contents, packageName, fileName); 51 } 52 53 public char[] getContents() { 54 if (this.contents != null) 55 return this.contents; 57 try { 59 return Util.getFileCharContent(new File (new String (this.fileName)), 60 this.encoding); 61 } catch (IOException e) { 62 } 64 return CharOperation.NO_CHAR; 65 } 66 67 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) 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 toString() { 103 return "CompilationUnit: " + new String (this.fileName); } 105 } 106 | Popular Tags |