001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.util;
019:
020: import java.io.File;
021: import java.io.IOException;
022:
023: import org.eclipse.jdt.core.IJavaElement;
024: import org.eclipse.jdt.core.compiler.CharOperation;
025: import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
026: import org.eclipse.jdt.internal.compiler.util.Util;
027:
028: /**
029: * A basic implementation of <code>ICompilationUnit</code> for use in the
030: * <code>SourceMapper</code>.
031: *
032: * @see ICompilationUnit
033: */
034: public class BasicCompilationUnit implements ICompilationUnit {
035: protected char[] contents;
036:
037: // Note that if this compiler ICompilationUnit's content is known in
038: // advance, the fileName is not used to retrieve this content.
039: // Instead it is used to keep enough information to recreate the
040: // IJavaElement corresponding to this compiler ICompilationUnit.
041: // Thus the fileName can be a path to a .class file, or even a path in a
042: // .jar to a .class file.
043: // (e.g. /P/lib/mylib.jar|org/eclipse/test/X.class)
044: protected char[] fileName;
045:
046: protected char[][] packageName;
047:
048: protected char[] mainTypeName;
049:
050: protected String encoding;
051:
052: public BasicCompilationUnit(char[] contents, char[][] packageName,
053: String fileName) {
054: this .contents = contents;
055: this .fileName = fileName.toCharArray();
056: this .packageName = packageName;
057: }
058:
059: public BasicCompilationUnit(char[] contents, char[][] packageName,
060: String fileName, String encoding) {
061: this (contents, packageName, fileName);
062: this .encoding = encoding;
063: }
064:
065: public BasicCompilationUnit(char[] contents, char[][] packageName,
066: String fileName, IJavaElement javaElement) {
067: this (contents, packageName, fileName);
068: }
069:
070: public char[] getContents() {
071: if (this .contents != null)
072: return this .contents; // answer the cached source
073:
074: // otherwise retrieve it
075: try {
076: return Util.getFileCharContent(new File(new String(
077: this .fileName)), this .encoding);
078: } catch (IOException e) {
079: // could not read file: returns an empty array
080: }
081: return CharOperation.NO_CHAR;
082: }
083:
084: /**
085: * @see org.eclipse.jdt.internal.compiler.env.IDependent#getFileName()
086: */
087: public char[] getFileName() {
088: return this .fileName;
089: }
090:
091: public char[] getMainTypeName() {
092: if (this .mainTypeName == null) {
093: int start = CharOperation.lastIndexOf('/', this .fileName) + 1;
094: if (start == 0
095: || start < CharOperation.lastIndexOf('\\',
096: this .fileName))
097: start = CharOperation.lastIndexOf('\\', this .fileName) + 1;
098: int separator = CharOperation.indexOf('|', this .fileName) + 1;
099: if (separator > start) // case of a .class file in a default
100: // package in a jar
101: start = separator;
102:
103: int end = CharOperation.lastIndexOf('$', this .fileName);
104: if (end == -1 || !Util.isClassFileName(this .fileName)) {
105: end = CharOperation.lastIndexOf('.', this .fileName);
106: if (end == -1)
107: end = this .fileName.length;
108: }
109:
110: this .mainTypeName = CharOperation.subarray(this .fileName,
111: start, end);
112: }
113: return this .mainTypeName;
114: }
115:
116: public char[][] getPackageName() {
117: return this .packageName;
118: }
119:
120: public String toString() {
121: return "CompilationUnit: " + new String(this .fileName); //$NON-NLS-1$
122: }
123: }
|