/** * @see IClassFile */ public IJavaElement getElementAt(int position) throws JavaModelException { IJavaElement parentElement = getParent(); while (parentElement.getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT) { parentElement = parentElement.getParent(); } PackageFragmentRoot root = (PackageFragmentRoot) parentElement; SourceMapper mapper = root.getSourceMapper(); if (mapper == null) { return null; } else { // ensure this class file's buffer is open so that source ranges are computed getBuffer(); IType type = getType(); return findElement(type, position, mapper); } }
/** * Compute the children of this package fragment. Children of jar package fragments * can only be IClassFile (representing .class files). */ private IJavaElement[] computeChildren(ArrayList namesWithoutExtension) { int size = namesWithoutExtension.size(); if (size == 0) return NO_ELEMENTS; IJavaElement[] children = new IJavaElement[size]; for (int i = 0; i < size; i++) { String nameWithoutExtension = (String) namesWithoutExtension.get(i); children[i] = new ClassFile(this, nameWithoutExtension); } return children; } /**
public void codeComplete(int offset, CompletionRequestor requestor, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { String source = getSource(); if (source != null) { BinaryType type = (BinaryType) getType(); BasicCompilationUnit cu = new BasicCompilationUnit( getSource().toCharArray(), null, type.sourceFileName((IBinaryType) type.getElementInfo()), getJavaProject()); // use project to retrieve corresponding .java IFile codeComplete(cu, cu, offset, requestor, owner, null/*extended context isn't computed*/, monitor); } }
public IBuffer getBuffer() throws JavaModelException { IStatus status = validateClassFile(); if (status.isOK()) { return super.getBuffer(); } else { // .class file not on classpath, create a new buffer to be nice (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=41444) Object info = ((ClassFile) getClassFile()).getBinaryTypeInfo((IFile) resource()); IBuffer buffer = openBuffer(null, info); if (buffer != null && !(buffer instanceof NullBuffer)) return buffer; 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((IJavaModelStatus) status); } } } /**
public boolean existsUsingJarTypeCache() { if (getPackageFragmentRoot().isArchive()) { JavaModelManager manager = JavaModelManager.getJavaModelManager(); IType type = getType(); Object info = manager.getInfo(type); if (info == JavaModelCache.NON_EXISTING_JAR_TYPE_INFO) return true; JavaElementInfo parentInfo = (JavaElementInfo) manager.getInfo(getParent()); if (parentInfo != null) { info = getJarBinaryTypeInfo((PackageFragment) getParent(), true/*fully initialize so as to not keep a reference to the byte array*/); } catch (CoreException e) { return info != null; } else return exists();
/** * Opens and returns buffer on the source code associated with this class file. * Maps the source code to the children elements of this class file. * If no source code is associated with this class file, * <code>null</code> is returned. * * @see Openable */ protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException { // Check the cache for the top-level type first IType outerMostEnclosingType = getOuterMostEnclosingType(); IBuffer buffer = getBufferManager().getBuffer(outerMostEnclosingType.getClassFile()); if (buffer == null) { SourceMapper mapper = getSourceMapper(); IBinaryType typeInfo = info instanceof IBinaryType ? (IBinaryType) info : null; if (mapper != null) { buffer = mapSource(mapper, typeInfo, outerMostEnclosingType.getClassFile()); } } return buffer; } /** Loads the buffer via SourceMapper, and maps it in SourceMapper */
ClassFile classFile = new ClassFile((PackageFragment) pkg, name); if (classFile.existsUsingJarTypeCache()) { IType type = classFile.getType(); if (acceptType(type, acceptFlags, false/*not a source type*/)) { requestor.acceptType(type); IType type = ((ClassFile) classFile).getType(); String typeName = type.getElementName(); if (typeName.length() > 0 && !Character.isDigit(typeName.charAt(0))) { //not an anonymous type
char[] contents = mapper.findSource(getType(), info); if (contents != null) { BufferManager bufManager = getBufferManager(); bufManager.addBuffer(buffer); mapper.mapSource((NamedMember) getOuterMostEnclosingType(), contents, info); BufferManager bufManager = getBufferManager(); bufManager.addBuffer(buffer);
/** * Creates the children elements for this class file adding the resulting * new handles and info objects to the newElements table. Returns true * if successful, or false if an error is encountered parsing the class file. * * @see Openable * @see Signature */ @Override protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException { IBinaryType typeInfo = getBinaryTypeInfo(); if (typeInfo == null) { // The structure of a class file is unknown if a class file format errors occurred //during the creation of the diet class file representative of this ClassFile. info.setChildren(JavaElement.NO_ELEMENTS); return false; } // Make the type IType type = getType(); info.setChildren(new IJavaElement[] {type}); newElements.put(type, typeInfo); // Read children ((ClassFileInfo) info).readBinaryChildren(this, (HashMap) newElements, typeInfo); return true; }
/** * @see IClassFile */ @Override public boolean isClass() throws JavaModelException { return getType().isClass(); } /**
/** * @see ISourceReference */ public String getSource() throws JavaModelException { IBuffer buffer = getBuffer(); if (buffer == null) { return null; } return buffer.getContents(); } /**
/** * @see ICodeAssist#codeComplete(int, ICompletionRequestor) * @deprecated */ public void codeComplete(int offset, ICompletionRequestor requestor) throws JavaModelException { codeComplete(offset, requestor, DefaultWorkingCopyOwner.PRIMARY); } /**
public IJavaElement getElementAtConsideringSibling(int position) throws JavaModelException { IPackageFragment fragment = (IPackageFragment)getParent(); PackageFragmentRoot root = (PackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); SourceMapper mapper = root.getSourceMapper(); return findElement(type, position, mapper);
protected IJavaElement findLocalElement(int pos) { IJavaElement res = null; if(this.openable instanceof ICompilationUnit) { ICompilationUnit cu = (ICompilationUnit) this.openable; try { res = cu.getElementAt(pos); } catch (JavaModelException e) { // do nothing } } else if (this.openable instanceof ClassFile) { ClassFile cf = (ClassFile) this.openable; try { res = cf.getElementAtConsideringSibling(pos); } catch (JavaModelException e) { // do nothing } } return res; } /*
ClassFileReader reader = (ClassFileReader) classFile.getBinaryTypeInfo((IFile) classFile.resource(), false/*don't fully initialize so as to keep constant pool (used below)*/); CompilationResult result = new CompilationResult(reader.getFileName(), 1, 1, this.compilerOptions.maxProblemsPerUnit); parsedUnit = new CompilationUnitDeclaration(this.parser.problemReporter(), result, 0);
/** * Returns the <code>ClassFileReader</code>specific for this IClassFile, based * on its underlying resource, or <code>null</code> if unable to create * the diet class file. * There are two cases to consider:<ul> * <li>a class file corresponding to an IFile resource</li> * <li>a class file corresponding to a zip entry in a JAR</li> * </ul> * * @exception JavaModelException when the IFile resource or JAR is not available * or when this class file is not present in the JAR */ public IBinaryType getBinaryTypeInfo(IFile file) throws JavaModelException { return getBinaryTypeInfo(file, true/*fully initialize so as to not keep a reference to the byte array*/); } public IBinaryType getBinaryTypeInfo(IFile file, boolean fullyInitialize) throws JavaModelException {
/** * 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 boolean existsUsingJarTypeCache() { if (getPackageFragmentRoot().isArchive()) { JavaModelManager manager = JavaModelManager.getJavaModelManager(); IType type = getType(); Object info = manager.getInfo(type); if (info == JavaModelCache.NON_EXISTING_JAR_TYPE_INFO) return true; JavaElementInfo parentInfo = (JavaElementInfo) manager.getInfo(getParent()); if (parentInfo != null) { info = getJarBinaryTypeInfo((PackageFragment) getParent(), true/*fully initialize so as to not keep a reference to the byte array*/); } catch (CoreException e) { return info != null; } else return exists();
/** * Opens and returns buffer on the source code associated with this class file. * Maps the source code to the children elements of this class file. * If no source code is associated with this class file, * <code>null</code> is returned. * * @see Openable */ protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException { // Check the cache for the top-level type first IType outerMostEnclosingType = getOuterMostEnclosingType(); IBuffer buffer = getBufferManager().getBuffer(outerMostEnclosingType.getClassFile()); if (buffer == null) { SourceMapper mapper = getSourceMapper(); IBinaryType typeInfo = info instanceof IBinaryType ? (IBinaryType) info : null; if (mapper != null) { buffer = mapSource(mapper, typeInfo, outerMostEnclosingType.getClassFile()); } } return buffer; } /** Loads the buffer via SourceMapper, and maps it in SourceMapper */
ClassFile classFile = new ClassFile((PackageFragment) pkg, name); if (classFile.existsUsingJarTypeCache()) { IType type = classFile.getType(); if (acceptType(type, acceptFlags, false/*not a source type*/)) { requestor.acceptType(type); IType type = ((ClassFile) classFile).getType(); String typeName = type.getElementName(); if (typeName.length() > 0 && !Character.isDigit(typeName.charAt(0))) { //not an anonymous type