Skip to main content
summaryrefslogtreecommitdiffstats
blob: d2e9a4fc8390dbabf37360b4947806c09032a493 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Stephan Herrmann - Contribution for
 *								Bug 458577 - IClassFile.getWorkingCopy() may lead to NPE in BecomeWorkingCopyOperation
 *								Bug 440477 - [null] Infrastructure for feeding external annotations into compilation
 *								Bug 462768 - [null] NPE when using linked folder for external annotations
 *******************************************************************************/
package org.eclipse.jdt.internal.core;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.*;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
import org.eclipse.jdt.internal.core.util.Util;

/**
 * Common parts of ClassFile (containing a BinaryType) and ModularClassFile (containing a BinaryModule).
 * Prior to Java 9, most of this content was directly in ClassFile.
 */
public abstract class AbstractClassFile extends Openable implements IClassFile, SuffixConstants {

	protected String name;

	protected AbstractClassFile(PackageFragment parent, String nameWithoutExtension) {
		super(parent);
		this.name = nameWithoutExtension;
	}

	/*
	 * @see IClassFile#becomeWorkingCopy(IProblemRequestor, WorkingCopyOwner, IProgressMonitor)
	 */
	public ICompilationUnit becomeWorkingCopy(IProblemRequestor problemRequestor, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException {
		JavaModelManager manager = JavaModelManager.getJavaModelManager();
		CompilationUnit workingCopy = new ClassFileWorkingCopy(this, owner == null ? DefaultWorkingCopyOwner.PRIMARY : owner);
		JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = manager.getPerWorkingCopyInfo(workingCopy, false/*don't create*/, true /*record usage*/, null/*no problem requestor needed*/);
		if (perWorkingCopyInfo == null) {
			// close cu and its children
			close();
	
			BecomeWorkingCopyOperation operation = new BecomeWorkingCopyOperation(workingCopy, problemRequestor);
			operation.runOperation(monitor);
	
			return workingCopy;
		}
		return perWorkingCopyInfo.workingCopy;
	}

	/**
	 * @see ICodeAssist#codeComplete(int, ICompletionRequestor)
	 * @deprecated
	 */
	@Deprecated
	public void codeComplete(int offset, ICompletionRequestor requestor) throws JavaModelException {
		codeComplete(offset, requestor, DefaultWorkingCopyOwner.PRIMARY);
	}
	/**
	 * @see ICodeAssist#codeComplete(int, ICompletionRequestor, WorkingCopyOwner)
	 * @deprecated
	 */
	@Deprecated
	public void codeComplete(int offset, ICompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException {
		if (requestor == null) {
			throw new IllegalArgumentException("Completion requestor cannot be null"); //$NON-NLS-1$
		}
		codeComplete(offset, new org.eclipse.jdt.internal.codeassist.CompletionRequestorWrapper(requestor), owner);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jdt.core.ICodeAssist#codeComplete(int, org.eclipse.jdt.core.CompletionRequestor)
	 */
	public void codeComplete(int offset, CompletionRequestor requestor) throws JavaModelException {
		codeComplete(offset, requestor, DefaultWorkingCopyOwner.PRIMARY);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.jdt.core.ICodeAssist#codeComplete(int, org.eclipse.jdt.core.CompletionRequestor, org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void codeComplete(int offset, CompletionRequestor requestor, IProgressMonitor monitor) throws JavaModelException {
		codeComplete(offset, requestor, DefaultWorkingCopyOwner.PRIMARY, monitor);
	}
	/* (non-Javadoc)
	 * @see org.eclipse.jdt.core.ICodeAssist#codeComplete(int, org.eclipse.jdt.core.CompletionRequestor, org.eclipse.jdt.core.WorkingCopyOwner)
	 */
	public void codeComplete(int offset, CompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException {
		codeComplete(offset, requestor, owner, null);
	}
	public abstract void codeComplete(int offset, CompletionRequestor requestor, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException;
	
	/**
	 * @see ICodeAssist#codeSelect(int, int)
	 */
	public IJavaElement[] codeSelect(int offset, int length) throws JavaModelException {
		return codeSelect(offset, length, DefaultWorkingCopyOwner.PRIMARY);
	}
	public abstract IJavaElement[] codeSelect(int offset, int length, WorkingCopyOwner owner) throws JavaModelException;
	
	/**
	 * Returns a new element info for this element.
	 */
	@Override
	protected Object createElementInfo() {
		return new ClassFileInfo();
	}
	@Override
	public boolean equals(Object o) {
		if (!(o instanceof AbstractClassFile)) return false;
		AbstractClassFile other = (AbstractClassFile) o;
		return this.name.equals(other.name) && this.parent.equals(other.parent);
	}
	
	/**
	 * Finds the deepest <code>IJavaElement</code> in the hierarchy of
	 * <code>elt</elt>'s children (including <code>elt</code> itself)
	 * which has a source range that encloses <code>position</code>
	 * according to <code>mapper</code>.
	 */
	protected IJavaElement findElement(IJavaElement elt, int position, SourceMapper mapper) {
		SourceRange range = mapper.getSourceRange(elt);
		if (range == null || position < range.getOffset() || range.getOffset() + range.getLength() - 1 < position) {
			return null;
		}
		if (elt instanceof IParent) {
			try {
				IJavaElement[] children = ((IParent) elt).getChildren();
				for (int i = 0; i < children.length; i++) {
					IJavaElement match = findElement(children[i], position, mapper);
					if (match != null) {
						return match;
					}
				}
			} catch (JavaModelException npe) {
				// elt doesn't exist: return the element
			}
		}
		return elt;
	}
	
	public byte[] getBytes() throws JavaModelException {
		JavaElement pkg = (JavaElement) getParent();
		if (pkg instanceof JarPackageFragment) {
			JarPackageFragmentRoot root = (JarPackageFragmentRoot) pkg.getParent();
			try {
				String entryName = Util.concatWith(((PackageFragment) pkg).names, getElementName(), '/');
				entryName = root.getClassFilePath(entryName);
				return getClassFileContent(root, entryName);
				// Java 9 - The below exception is not thrown in new scheme of things. Could cause issues?
	//			throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this));
			} catch (IOException ioe) {
				throw new JavaModelException(ioe, IJavaModelStatusConstants.IO_EXCEPTION);
			} catch (CoreException e) {
				if (e instanceof JavaModelException) {
					throw (JavaModelException)e;
				} else {
					throw new JavaModelException(e);
				}
			}
		} else {
			IFile file = (IFile) resource();
			return Util.getResourceContentsAsByteArray(file);
		}
	}
	protected byte[] getClassFileContent(JarPackageFragmentRoot root, String className) throws CoreException, IOException {
		byte[] contents = null;
		String rootPath = root.getPath().toOSString();
		if (org.eclipse.jdt.internal.compiler.util.Util.isJrt(rootPath)) {
				try {
					contents = org.eclipse.jdt.internal.compiler.util.JRTUtil.getClassfileContent(
							new File(rootPath),
							className,
							root.getElementName());
				} catch (ClassFormatException e) {
					e.printStackTrace();
				}
		} else {
			ZipFile zip = root.getJar();
			try {
				ZipEntry ze = zip.getEntry(className);
				if (ze != null) {
					contents = org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
				}
			} finally {
				JavaModelManager.getJavaModelManager().closeZipFile(zip);
			}
		}
		return contents;
	}
	
	@Override
	public IBuffer getBuffer() throws JavaModelException {
		IStatus status = validateClassFile();
		if (status.isOK()) {
			return super.getBuffer();
		} else {
			switch (status.getCode()) {
			case IJavaModelStatusConstants.ELEMENT_NOT_ON_CLASSPATH: // don't throw a JavaModelException to be able to open .class file outside the classpath (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=138507 )
			case IJavaModelStatusConstants.INVALID_ELEMENT_TYPES: // don't throw a JavaModelException to be able to open .class file in proj==src case without source (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=221904 )
				return null;
			default:
				throw new JavaModelException(status);
			}
		}
	}
	/**
	 * @see IMember#getTypeRoot()
	 */
	public ITypeRoot getTypeRoot() {
		return this;
	}
	
	/**
	 * A class file has a corresponding resource unless it is contained
	 * in a jar.
	 *
	 * @see IJavaElement
	 */
	@Override
	public IResource getCorrespondingResource() throws JavaModelException {
		IPackageFragmentRoot root= (IPackageFragmentRoot)getParent().getParent();
		if (root.isArchive()) {
			return null;
		} else {
			return getUnderlyingResource();
		}
	}
	public IJavaElement getElementAtConsideringSibling(int position) throws JavaModelException {
		IPackageFragment fragment = (IPackageFragment)getParent();
		PackageFragmentRoot root = (PackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
		SourceMapper mapper = root.getSourceMapper();
		if (mapper == null) {
			return null;
		} else {
			int index = this.name.indexOf('$');
			int prefixLength = index < 0 ? this.name.length() : index;
	
			IType type = null;
			int start = -1;
			int end = Integer.MAX_VALUE;
			IJavaElement[] children = fragment.getChildren();
			for (int i = 0; i < children.length; i++) {
				if (children[i] instanceof IOrdinaryClassFile) {
					IOrdinaryClassFile classFile = (IOrdinaryClassFile) children[i];
					String childName = classFile.getElementName();
		
					int childIndex = childName.indexOf('$');
					int childPrefixLength = childIndex < 0 ? childName.indexOf('.') : childIndex;
					if (prefixLength == childPrefixLength && this.name.regionMatches(0, childName, 0, prefixLength)) {
		
						// ensure this class file's buffer is open so that source ranges are computed
						classFile.getBuffer();
		
						SourceRange range = mapper.getSourceRange(classFile.getType());
						if (range == SourceMapper.UNKNOWN_RANGE) continue;
						int newStart = range.getOffset();
						int newEnd = newStart + range.getLength() - 1;
						if(newStart > start && newEnd < end
								&& newStart <= position && newEnd >= position) {
							type = classFile.getType();
							start = newStart;
							end = newEnd;
						}
					}
				}
			}
			if(type != null) {
				return findElement(type, position, mapper);
			}
			return null;
		}
	}
	@Override
	public String getElementName() {
		return this.name + SuffixConstants.SUFFIX_STRING_class;
	}
	/**
	 * @see IJavaElement
	 */
	public int getElementType() {
		return CLASS_FILE;
	}

	/*
	 * @see IJavaElement
	 */
	public IPath getPath() {
		PackageFragmentRoot root = getPackageFragmentRoot();
		if (root.isArchive()) {
			return root.getPath();
		} else {
			return getParent().getPath().append(getElementName());
		}
	}
	
	/*
	 * @see IJavaElement
	 */
	@Override
	public IResource resource(PackageFragmentRoot root) {
		return ((IContainer) ((Openable) this.parent).resource(root)).getFile(new Path(getElementName()));
	}
	/**
	 * @see ISourceReference
	 */
	public String getSource() throws JavaModelException {
		IBuffer buffer = getBuffer();
		if (buffer == null) {
			return null;
		}
		return buffer.getContents();
	}
	/**
	 * @see ISourceReference
	 */
	public ISourceRange getSourceRange() throws JavaModelException {
		IBuffer buffer = getBuffer();
		if (buffer != null) {
			String contents = buffer.getContents();
			if (contents == null) return null;
			return new SourceRange(0, contents.length());
		} else {
			return null;
		}
	}
	/**
	 * @see IClassFile
	 * @deprecated
	 */
	@Deprecated
	public IJavaElement getWorkingCopy(IProgressMonitor monitor, org.eclipse.jdt.core.IBufferFactory factory) throws JavaModelException {
		return getWorkingCopy(BufferFactoryWrapper.create(factory), monitor);
	}
	/**
	 * @see Openable
	 */
	@Override
	protected boolean hasBuffer() {
		return true;
	}
	@Override
	public int hashCode() {
		return Util.combineHashCodes(this.name.hashCode(), this.parent.hashCode());
	}
	/**
	 * Returns true - class files are always read only.
	 */
	@Override
	public boolean isReadOnly() {
		return true;
	}
	private IStatus validateClassFile() {
		IPackageFragmentRoot root = getPackageFragmentRoot();
		try {
			if (root.getKind() != IPackageFragmentRoot.K_BINARY)
				return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, root);
		} catch (JavaModelException e) {
			return e.getJavaModelStatus();
		}
		IJavaProject project = getJavaProject();
		return JavaConventions.validateClassFileName(getElementName(), project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true));
	}
	

	/**
	 * @see ICodeAssist#codeComplete(int, ICodeCompletionRequestor)
	 * @deprecated - should use codeComplete(int, ICompletionRequestor) instead
	 */
	@Deprecated
	public void codeComplete(int offset, final org.eclipse.jdt.core.ICodeCompletionRequestor requestor) throws JavaModelException {

		if (requestor == null){
			codeComplete(offset, (ICompletionRequestor)null);
			return;
		}
		codeComplete(
			offset,
			new ICompletionRequestor(){
				public void acceptAnonymousType(char[] superTypePackageName,char[] superTypeName, char[][] parameterPackageNames,char[][] parameterTypeNames,char[][] parameterNames,char[] completionName,int modifiers,int completionStart,int completionEnd, int relevance) {
					// ignore
				}
				public void acceptClass(char[] packageName, char[] className, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
					requestor.acceptClass(packageName, className, completionName, modifiers, completionStart, completionEnd);
				}
				public void acceptError(IProblem error) {
					// was disabled in 1.0
				}
				public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] fieldName, char[] typePackageName, char[] typeName, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
					requestor.acceptField(declaringTypePackageName, declaringTypeName, fieldName, typePackageName, typeName, completionName, modifiers, completionStart, completionEnd);
				}
				public void acceptInterface(char[] packageName,char[] interfaceName,char[] completionName,int modifiers,int completionStart,int completionEnd, int relevance) {
					requestor.acceptInterface(packageName, interfaceName, completionName, modifiers, completionStart, completionEnd);
				}
				public void acceptKeyword(char[] keywordName,int completionStart,int completionEnd, int relevance){
					requestor.acceptKeyword(keywordName, completionStart, completionEnd);
				}
				public void acceptLabel(char[] labelName,int completionStart,int completionEnd, int relevance){
					requestor.acceptLabel(labelName, completionStart, completionEnd);
				}
				public void acceptLocalVariable(char[] localVarName,char[] typePackageName,char[] typeName,int modifiers,int completionStart,int completionEnd, int relevance){
					// ignore
				}
				public void acceptMethod(char[] declaringTypePackageName,char[] declaringTypeName,char[] selector,char[][] parameterPackageNames,char[][] parameterTypeNames,char[][] parameterNames,char[] returnTypePackageName,char[] returnTypeName,char[] completionName,int modifiers,int completionStart,int completionEnd, int relevance){
					// skip parameter names
					requestor.acceptMethod(declaringTypePackageName, declaringTypeName, selector, parameterPackageNames, parameterTypeNames, returnTypePackageName, returnTypeName, completionName, modifiers, completionStart, completionEnd);
				}
				public void acceptMethodDeclaration(char[] declaringTypePackageName,char[] declaringTypeName,char[] selector,char[][] parameterPackageNames,char[][] parameterTypeNames,char[][] parameterNames,char[] returnTypePackageName,char[] returnTypeName,char[] completionName,int modifiers,int completionStart,int completionEnd, int relevance){
					// ignore
				}
				public void acceptModifier(char[] modifierName,int completionStart,int completionEnd, int relevance){
					requestor.acceptModifier(modifierName, completionStart, completionEnd);
				}
				public void acceptPackage(char[] packageName,char[] completionName,int completionStart,int completionEnd, int relevance){
					requestor.acceptPackage(packageName, completionName, completionStart, completionEnd);
				}
				public void acceptType(char[] packageName,char[] typeName,char[] completionName,int completionStart,int completionEnd, int relevance){
					requestor.acceptType(packageName, typeName, completionName, completionStart, completionEnd);
				}
				public void acceptVariableName(char[] typePackageName,char[] typeName,char[] varName,char[] completionName,int completionStart,int completionEnd, int relevance){
					// ignore
				}
			});
	}
	
	@Override
	protected IStatus validateExistence(IResource underlyingResource) {
		// check whether the class file can be opened
		IStatus status = validateClassFile();
		if (!status.isOK())
			return status;
		if (underlyingResource != null) {
			if (!underlyingResource.isAccessible())
				return newDoesNotExistStatus();
			PackageFragmentRoot root;
			if ((underlyingResource instanceof IFolder) && (root = getPackageFragmentRoot()).isArchive()) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=204652
				return root.newDoesNotExistStatus();
			}
		}
		return JavaModelStatus.VERIFIED_OK;
	}
	
	public ISourceRange getNameRange() {
		return null;
	}
}

Back to the top