private OpenDeclarationDescriptor classFileNavigation(IClassFile classFile, IJavaElement element) throws JavaModelException { OpenDeclarationDescriptor dto = DtoFactory.getInstance().createDto(OpenDeclarationDescriptor.class); dto.setPath(classFile.getType().getFullyQualifiedName()); dto.setLibId(classFile.getAncestor(IPackageFragmentRoot.PACKAGE_FRAGMENT_ROOT).hashCode()); dto.setBinary(true); if (classFile.getSourceRange() != null) { if (element instanceof ISourceReference) { ISourceRange nameRange = ((ISourceReference) element).getNameRange(); dto.setOffset(nameRange.getOffset()); dto.setLength(nameRange.getLength()); } } return dto; }
/** * Creates a location for a given java element. * Element can be a {@link ICompilationUnit} or {@link IClassFile} * * @param element * @return location or null * @throws JavaModelException */ public static Location toLocation(IJavaElement element) throws JavaModelException{ ICompilationUnit unit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (unit == null && cf == null) { return null; } if (element instanceof ISourceReference) { ISourceRange nameRange = getNameRange(element); if (SourceRange.isAvailable(nameRange)) { if (cf == null) { return toLocation(unit, nameRange.getOffset(), nameRange.getLength()); } else { return toLocation(cf, nameRange.getOffset(), nameRange.getLength()); } } } return null; }
private static ISourceRange getNameRange(IJavaElement element) throws JavaModelException { ISourceRange nameRange = null; if (element instanceof IMember) { IMember member = (IMember) element; nameRange = member.getNameRange(); if ( (!SourceRange.isAvailable(nameRange))) { nameRange = member.getSourceRange(); } } else if (element instanceof ITypeParameter || element instanceof ILocalVariable) { nameRange = ((ISourceReference) element).getNameRange(); } else if (element instanceof ISourceReference) { nameRange = ((ISourceReference) element).getSourceRange(); } if (!SourceRange.isAvailable(nameRange) && element.getParent() != null) { nameRange = getNameRange(element.getParent()); } return nameRange; }
private ArrayList<IJavaElement> getSortedChildren(IType parent) throws JavaModelException { IJavaElement[] children = parent.getChildren(); ArrayList<IJavaElement> sortedChildren = new ArrayList<IJavaElement>(Arrays.asList(children)); Collections.sort( sortedChildren, new Comparator<IJavaElement>() { public int compare(IJavaElement e1, IJavaElement e2) { if (!(e1 instanceof ISourceReference)) return 0; if (!(e2 instanceof ISourceReference)) return 0; try { ISourceRange sr1 = ((ISourceReference) e1).getSourceRange(); ISourceRange sr2 = ((ISourceReference) e2).getSourceRange(); if (sr1 == null || sr2 == null) return 0; return sr1.getOffset() - sr2.getOffset(); } catch (JavaModelException e) { return 0; } } }); return sortedChildren; }
/** * Evaluates the indentation used by a Java element. (in tabulators) */ private int getUsedIndentation(IJavaElement elem) throws JavaModelException { if (elem instanceof ISourceReference) { ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { IBuffer buf = cu.getBuffer(); int offset = ((ISourceReference) elem).getSourceRange().getOffset(); int i = offset; // find beginning of line while (i > 0 && !isLineDelimiterChar(buf.getChar(i - 1))) { i--; } return computeIndent(buf.getText(i, offset - i), getTabWidth()); } } return 0; }
private ArrayList<IJavaElement> getSortedChildren(IType parent) throws JavaModelException { IJavaElement[] children= parent.getChildren(); ArrayList<IJavaElement> sortedChildren= new ArrayList<IJavaElement>(Arrays.asList(children)); Collections.sort(sortedChildren, new Comparator<IJavaElement>() { public int compare(IJavaElement e1, IJavaElement e2) { if (!(e1 instanceof ISourceReference)) return 0; if (!(e2 instanceof ISourceReference)) return 0; try { ISourceRange sr1= ((ISourceReference)e1).getSourceRange(); ISourceRange sr2= ((ISourceReference)e2).getSourceRange(); if (sr1 == null || sr2 == null) return 0; return sr1.getOffset() - sr2.getOffset(); } catch (JavaModelException e) { return 0; } } }); return sortedChildren; }
static RefactoringStatusContext createRefactoringStatusContext(IJavaElement element) { if (element instanceof IMember) { return JavaStatusContext.create((IMember) element); } if (element instanceof ISourceReference) { IOpenable openable= element.getOpenable(); try { if (openable instanceof ICompilationUnit) { return JavaStatusContext.create((ICompilationUnit) openable, ((ISourceReference) element).getSourceRange()); } else if (openable instanceof IClassFile) { return JavaStatusContext.create((IClassFile) openable, ((ISourceReference) element).getSourceRange()); } } catch (JavaModelException e) { // ignore } } return null; }
/** * Computes and returns the offset in the unclipped document based on the given text selection * from the clipped document. * * @param textSelection the text selection * @return the offset in the unclipped document or <code>-1</code> if the offset cannot be * computed */ private int getOffsetInUnclippedDocument(ITextSelection textSelection) { IDocument unclippedDocument= null; try { unclippedDocument= new Document(((ISourceReference)fCurrentViewInput).getSource()); } catch (JavaModelException e) { return -1; } IDocument clippedDoc= (IDocument)fViewer.getInput(); try { IRegion unclippedLineInfo= unclippedDocument.getLineInformation(fCommentLineCount + textSelection.getStartLine()); IRegion clippedLineInfo= clippedDoc.getLineInformation(textSelection.getStartLine()); int removedIndentation= unclippedLineInfo.getLength() - clippedLineInfo.getLength(); int relativeLineOffset= textSelection.getOffset() - clippedLineInfo.getOffset(); return unclippedLineInfo.getOffset() + removedIndentation + relativeLineOffset ; } catch (BadLocationException ex) { return -1; } }
public void setSelection(IJavaElement element) { if (element == null || element instanceof ICompilationUnit || element instanceof IClassFile) { /* * If the element is an ICompilationUnit this unit is either the input * of this editor or not being displayed. In both cases, nothing should * happened. (http://dev.eclipse.org/bugs/show_bug.cgi?id=5128) */ return; } IJavaElement corresponding= getCorrespondingElement(element); if (corresponding instanceof ISourceReference) { ISourceReference reference= (ISourceReference) corresponding; // set highlight range setSelection(reference, true); // set outliner selection if (fOutlinePage != null) fOutlinePage.select(reference); } }
/** * Makes the breadcrumb visible. Creates its content * if this is the first time it is made visible. * * @since 3.4 */ private void showBreadcrumb() { if (fBreadcrumb == null) return; if (fBreadcrumbComposite.getChildren().length == 0) { fBreadcrumb.createContent(fBreadcrumbComposite); } ((GridData) fBreadcrumbComposite.getLayoutData()).exclude= false; fBreadcrumbComposite.setVisible(true); ISourceReference selection= computeHighlightRangeSourceReference(); if (selection == null) selection= getInputJavaElement(); setBreadcrumbInput(selection); fBreadcrumbComposite.getParent().layout(true, true); }
@Override ISourceRange internalGetNewSelectionRange(ISourceRange oldSourceRange, ISourceReference sr, SelectionAnalyzer selAnalyzer) throws JavaModelException{ if (oldSourceRange.getLength() == 0 && selAnalyzer.getLastCoveringNode() != null) { ASTNode previousNode= NextNodeAnalyzer.perform(oldSourceRange.getOffset(), selAnalyzer.getLastCoveringNode()); if (previousNode != null) return getSelectedNodeSourceRange(sr, previousNode); } ASTNode first= selAnalyzer.getFirstSelectedNode(); if (first == null) return getLastCoveringNodeRange(oldSourceRange, sr, selAnalyzer); ASTNode parent= first.getParent(); if (parent == null) return getLastCoveringNodeRange(oldSourceRange, sr, selAnalyzer); ASTNode lastSelectedNode= selAnalyzer.getSelectedNodes()[selAnalyzer.getSelectedNodes().length - 1]; ASTNode nextNode= getNextNode(parent, lastSelectedNode); if (nextNode == parent) return getSelectedNodeSourceRange(sr, first.getParent()); int offset= oldSourceRange.getOffset(); int end= Math.min(sr.getSourceRange().getLength(), nextNode.getStartPosition() + nextNode.getLength() - 1); return StructureSelectionAction.createSourceRange(offset, end); }
protected boolean isEnabled(ISelection selection) { if (selection instanceof IStructuredSelection) { Object[] sel= ((IStructuredSelection) selection).toArray(); if (sel.length == 2) { for (int i= 0; i < 2; i++) { Object o= sel[i]; if (!(o instanceof ISourceReference)) return false; } fLeft= (ISourceReference) sel[0]; fRight= (ISourceReference) sel[1]; return true; } } return false; }
private Object[] getPackageContents(IPackageFragment fragment) throws JavaModelException { ISourceReference[] sourceRefs; if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) { sourceRefs= fragment.getCompilationUnits(); } else { IClassFile[] classFiles= fragment.getClassFiles(); List<IClassFile> topLevelClassFile= new ArrayList<IClassFile>(); for (int i= 0; i < classFiles.length; i++) { IType type= classFiles[i].getType(); if (type != null && type.getDeclaringType() == null && !type.isAnonymous() && !type.isLocal()) topLevelClassFile.add(classFiles[i]); } sourceRefs= topLevelClassFile.toArray(new ISourceReference[topLevelClassFile.size()]); } Object[] result= new Object[0]; for (int i= 0; i < sourceRefs.length; i++) result= concatenate(result, removeImportAndPackageDeclarations(getChildren(sourceRefs[i]))); return concatenate(result, fragment.getNonJavaResources()); }
/** * @see MultiOperation */ protected void verify(IJavaElement element) throws JavaModelException { if (element == null || !element.exists()) error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element); if (element.isReadOnly()) error(IJavaModelStatusConstants.READ_ONLY, element); if (!(element instanceof ISourceReference)) error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element); int elementType = element.getElementType(); if (elementType < IJavaElement.TYPE || elementType == IJavaElement.INITIALIZER) error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element); verifyRenaming(element); }
private int getErrorTicksFromAnnotationModel(IAnnotationModel model, ISourceReference sourceElement) throws CoreException { int info= 0; Iterator<Annotation> iter= model.getAnnotationIterator(); while ((info != ERRORTICK_ERROR) && iter.hasNext()) { Annotation annot= iter.next(); IMarker marker= isAnnotationInRange(model, annot, sourceElement); if (marker != null) { int priority= marker.getAttribute(IMarker.SEVERITY, -1); if (priority == IMarker.SEVERITY_WARNING) { info= ERRORTICK_WARNING; } else if (priority == IMarker.SEVERITY_ERROR) { info= ERRORTICK_ERROR; } } } return info; }
private ICodeLens getCodeLens(String type, IJavaElement element, ITypeRoot unit) throws JavaModelException { ISourceRange r = ((ISourceReference) element).getNameRange(); final Range range = JDTUtils.toRange(unit, r.getOffset(), r.getLength()); ICodeLens lens = new JavaCodeLens(range, type); // String uri = ResourceUtils.toClientUri(JDTUtils.getFileURI(unit)); // lens.setData(Arrays.asList(uri, range.getStart(), type)); return lens; }
private boolean hasSource(IJavaElement element) { if (element instanceof ISourceReference) { try { return ((ISourceReference) element).getSourceRange() != null; } catch (JavaModelException ex) { // we ignore this, the resource seems to have problems } } return false; }
@Override protected ISourceReference getSourceReference(IPackageFragment pkg, String sourcename) throws CoreException { int idx = sourcename.lastIndexOf('.'); if (idx != -1) { sourcename = sourcename.substring(0, idx); } return pkg.getClassFile(sourcename + ".class"); //$NON-NLS-1$ }
static boolean isSourceAvailable(ISourceReference sourceReference) { try { return SourceRange.isAvailable(sourceReference.getSourceRange()); } catch (JavaModelException e) { return false; } }
private CodeLens getCodeLens(String type, IJavaElement element, ITypeRoot typeRoot) throws JavaModelException { CodeLens lens = new CodeLens(); ISourceRange r = ((ISourceReference) element).getNameRange(); final Range range = JDTUtils.toRange(typeRoot, r.getOffset(), r.getLength()); lens.setRange(range); String uri = ResourceUtils.toClientUri(JDTUtils.toUri(typeRoot)); lens.setData(Arrays.asList(uri, range.getStart(), type)); return lens; }
private OpenDeclarationDescriptor compilationUnitNavigation( ICompilationUnit unit, IJavaElement element) throws JavaModelException { OpenDeclarationDescriptor dto = DtoFactory.getInstance().createDto(OpenDeclarationDescriptor.class); String absolutePath = unit.getPath().toOSString(); dto.setPath(absolutePath); dto.setBinary(false); if (element instanceof ISourceReference) { ISourceRange nameRange = ((ISourceReference) element).getNameRange(); dto.setOffset(nameRange.getOffset()); dto.setLength(nameRange.getLength()); } return dto; }
public Object[] getChildren(Object element) { if (!exists(element)) return NO_CHILDREN; try { if (element instanceof IJavaModel) return getJavaProjects((IJavaModel) element); if (element instanceof IJavaProject) return getPackageFragmentRoots((IJavaProject) element); if (element instanceof IPackageFragmentRoot) return getPackageFragmentRootContent((IPackageFragmentRoot) element); if (element instanceof IPackageFragment) return getPackageContent((IPackageFragment) element); if (element instanceof IFolder) return getFolderContent((IFolder) element); if (element instanceof IJarEntryResource) { return ((IJarEntryResource) element).getChildren(); } if (getProvideMembers() && element instanceof ISourceReference && element instanceof IParent) { return ((IParent) element).getChildren(); } } catch (CoreException e) { return NO_CHILDREN; } return NO_CHILDREN; }
/** * Evaluates the indentation used by a Java element. (in tabulators) * * @param elem the element to get the indent of * @return return the indent unit * @throws JavaModelException thrown if the element could not be accessed */ public static int getIndentUsed(IJavaElement elem) throws JavaModelException { IOpenable openable = elem.getOpenable(); if (openable instanceof ITypeRoot) { IBuffer buf = openable.getBuffer(); if (buf != null) { int offset = ((ISourceReference) elem).getSourceRange().getOffset(); return getIndentUsed(buf, offset, elem.getJavaProject()); } } return 0; }
private static ISourceRange getOldSourceRange(SearchMatch newMatch) { // cannot transfom offset in preview to offset in original -> just show enclosing method IJavaElement newMatchElement = (IJavaElement) newMatch.getElement(); IJavaElement primaryElement = newMatchElement.getPrimaryElement(); ISourceRange range = null; if (primaryElement.exists() && primaryElement instanceof ISourceReference) { try { range = ((ISourceReference) primaryElement).getSourceRange(); } catch (JavaModelException e) { // can live without source range } } return range; }
private static String getUnindentedSource(ISourceReference sourceReference) throws JavaModelException { String[] lines = Strings.convertIntoLines(sourceReference.getSource()); final IJavaProject project = ((IJavaElement) sourceReference).getJavaProject(); Strings.trimIndentation(lines, project, false); return Strings.concatenate( lines, StubUtility.getLineDelimiterUsed((IJavaElement) sourceReference)); }
public static boolean isSourceAvailable(ISourceReference sourceReference) { try { return SourceRange.isAvailable(sourceReference.getSourceRange()); } catch (JavaModelException e) { return false; } }
public static IJavaElement resolveEnclosingElement(IJavaElement input, ITextSelection selection) throws JavaModelException { IJavaElement atOffset = null; if (input instanceof ICompilationUnit) { ICompilationUnit cunit = (ICompilationUnit) input; JavaModelUtil.reconcile(cunit); atOffset = cunit.getElementAt(selection.getOffset()); } else if (input instanceof IClassFile) { IClassFile cfile = (IClassFile) input; atOffset = cfile.getElementAt(selection.getOffset()); } else { return null; } if (atOffset == null) { return input; } else { int selectionEnd = selection.getOffset() + selection.getLength(); IJavaElement result = atOffset; if (atOffset instanceof ISourceReference) { ISourceRange range = ((ISourceReference) atOffset).getSourceRange(); while (range.getOffset() + range.getLength() < selectionEnd) { result = result.getParent(); if (!(result instanceof ISourceReference)) { result = input; break; } range = ((ISourceReference) result).getSourceRange(); } } return result; } }
private static ISourceRange getOldSourceRange(SearchMatch newMatch) { // cannot transfom offset in preview to offset in original -> just show enclosing method IJavaElement newMatchElement= (IJavaElement) newMatch.getElement(); IJavaElement primaryElement= newMatchElement.getPrimaryElement(); ISourceRange range= null; if (primaryElement.exists() && primaryElement instanceof ISourceReference) { try { range= ((ISourceReference) primaryElement).getSourceRange(); } catch (JavaModelException e) { // can live without source range } } return range; }
public static ICompilationUnit getCompilationUnit(ISourceReference o){ Assert.isTrue(! (o instanceof IClassFile)); if (o instanceof ICompilationUnit) return (ICompilationUnit)o; if (o instanceof IJavaElement) return (ICompilationUnit) ((IJavaElement)o).getAncestor(IJavaElement.COMPILATION_UNIT); return null; }
private static boolean hasParentInSet(IJavaElement elem, Set<ISourceReference> set){ IJavaElement parent= elem.getParent(); while (parent != null) { if (set.contains(parent)) return true; parent= parent.getParent(); } return false; }
public static ISourceReference[] removeAllWithParentsSelected(ISourceReference[] elems){ Set<ISourceReference> set= new HashSet<ISourceReference>(Arrays.asList(elems)); List<ISourceReference> result= new ArrayList<ISourceReference>(elems.length); for (int i= 0; i < elems.length; i++) { ISourceReference elem= elems[i]; if (! (elem instanceof IJavaElement)) result.add(elem); else{ if (! hasParentInSet(((IJavaElement)elem), set)) result.add(elem); } } return result.toArray(new ISourceReference[result.size()]); }
public static ISourceReference[] sortByOffset(ISourceReference[] methods){ Arrays.sort(methods, new Comparator<ISourceReference>(){ public int compare(ISourceReference o1, ISourceReference o2){ try{ return o2.getSourceRange().getOffset() - o1.getSourceRange().getOffset(); } catch (JavaModelException e){ return o2.hashCode() - o1.hashCode(); } } }); return methods; }
private static String getUnindentedSource(ISourceReference sourceReference) throws JavaModelException { String[] lines= Strings.convertIntoLines(sourceReference.getSource()); final IJavaProject project= ((IJavaElement) sourceReference).getJavaProject(); Strings.trimIndentation(lines, project, false); return Strings.concatenate(lines, StubUtility.getLineDelimiterUsed((IJavaElement) sourceReference)); }
private static String getSourceOfDeclararationNode(IJavaElement elem, ICompilationUnit cu) throws JavaModelException, CoreException { Assert.isTrue(elem.getElementType() != IJavaElement.IMPORT_CONTAINER); if (elem instanceof ISourceReference) { ISourceReference reference= (ISourceReference) elem; String source= reference.getSource(); if (source != null) return Strings.trimIndentation(source, cu.getJavaProject(), false); } return ""; //$NON-NLS-1$ }