/** * Checks whether the given Java element has accessible source. * * @param je the Java element to test * @return <code>true</code> if the element has source * @since 3.2 */ private static boolean hasSource(ITypeRoot je) { if (je == null || !je.exists()) return false; try { return je.getBuffer() != null; } catch (JavaModelException ex) { IStatus status= new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.OK, "Error in JDT Core during AST creation", ex); //$NON-NLS-1$ JavaPlugin.getDefault().getLog().log(status); } return false; }
/** * Checks whether the given Java element has accessible source. * * @param je the Java element to test * @return <code>true</code> if the element has source */ private static boolean hasSource(ITypeRoot je) { if (je == null || !je.exists()) return false; try { return je.getBuffer() != null; } catch (JavaModelException ex) { IStatus status= new Status(IStatus.ERROR, JavaManipulation.ID_PLUGIN, IStatus.OK, "Error in JDT Core during AST creation", ex); //$NON-NLS-1$ JavaManipulationPlugin.getDefault().getLog().log(status); } return false; }
/** * Checks whether the given Java element has accessible source. * * @param je the Java element to test * @return <code>true</code> if the element has source * @since 3.2 */ private boolean hasSource(IJavaElement je) { if (je == null || !je.exists()) return false; try { return je instanceof ITypeRoot && ((ITypeRoot)je).getBuffer() != null; } catch (JavaModelException ex) { IStatus status= new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.OK, "Error in JDT Core during AST creation", ex); //$NON-NLS-1$ JavaPlugin.getDefault().getLog().log(status); } return false; }
fFlags= 0; IBuffer buffer= element.getBuffer(); if (buffer == null) { throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, Messages.format( SearchMessages.JavaElementLine_error_nobuffer, BasicElementLabels.getFileName(element))));
private int getOffset(TextDocumentPositionParams param, ITypeRoot typeRoot) { int offset = 0; try { IDocument document = JsonRpcHelpers.toDocument(typeRoot.getBuffer()); offset = document.getLineOffset(param.getPosition().getLine()) + param.getPosition().getCharacter(); } catch (JavaModelException | BadLocationException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } return offset; }
private List<DocumentHighlight> computeOccurrences(ITypeRoot unit, int line, int column, IProgressMonitor monitor) { if (unit != null) { try { int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column); OccurrencesFinder finder = new OccurrencesFinder(); CompilationUnit ast = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor); if (ast != null) { String error = finder.initialize(ast, offset, 0); if (error == null){ List<DocumentHighlight> result = new ArrayList<>(); OccurrenceLocation[] occurrences = finder.getOccurrences(); if (occurrences != null) { for (OccurrenceLocation loc : occurrences) { if (monitor.isCanceled()) { return Collections.emptyList(); } result.add(convertToHighlight(unit, loc)); } } return result; } } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem with compute occurrences for" + unit.getElementName(), e); } } return Collections.emptyList(); }
/** * Creates a TokenScanner * @param typeRoot The type root to scan on * @throws CoreException thrown if the buffer cannot be accessed */ public TokenScanner(ITypeRoot typeRoot) throws CoreException { IJavaProject project= typeRoot.getJavaProject(); IBuffer buffer= typeRoot.getBuffer(); if (buffer == null) { throw new CoreException(createError(DOCUMENT_ERROR, "Element has no source", null)); //$NON-NLS-1$ } String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScanner= ToolFactory.createScanner(true, false, true, sourceLevel, complianceLevel); // line info required fScanner.setSource(buffer.getCharacters()); fDocument= null; // use scanner for line information fEndPosition= fScanner.getSource().length - 1; }
public void initialize() throws JavaModelException { fDocument= fSource == null ? new Document(fTypeRoot.getBuffer().getContents()) : fSource; fAnalyzer.initialize(); if (hasReturnValue()) { ASTNode last= getLastStatement(); if (last != null) { ReturnAnalyzer analyzer= new ReturnAnalyzer(); last.accept(analyzer); } } }
public void initialize() throws JavaModelException { fDocument= fSource == null ? new Document(fTypeRoot.getBuffer().getContents()) : fSource; fAnalyzer.initialize(); if (hasReturnValue()) { ASTNode last= getLastStatement(); if (last != null) { ReturnAnalyzer analyzer= new ReturnAnalyzer(); last.accept(analyzer); } } }
public void initialize() throws JavaModelException { fDocument= fSource == null ? new Document(fTypeRoot.getBuffer().getContents()) : fSource; fAnalyzer.initialize(); if (hasReturnValue()) { ASTNode last= getLastStatement(); if (last != null) { ReturnAnalyzer analyzer= new ReturnAnalyzer(); last.accept(analyzer); } } }
static CompilationUnit getCompilationUnitNode(IMember member, boolean resolveBindings) { ITypeRoot typeRoot= member.getTypeRoot(); try { if (typeRoot.exists() && typeRoot.getBuffer() != null) { ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); parser.setSource(typeRoot); parser.setResolveBindings(resolveBindings); return (CompilationUnit) parser.createAST(null); } } catch (JavaModelException e) { JavaPlugin.log(e); } return null; }
/** * Creates a TokenScanner * @param typeRoot The type root to scan on * @throws CoreException thrown if the buffer cannot be accessed */ public TokenScanner(ITypeRoot typeRoot) throws CoreException { IJavaProject project= typeRoot.getJavaProject(); IBuffer buffer= typeRoot.getBuffer(); if (buffer == null) { throw new CoreException(createError(DOCUMENT_ERROR, "Element has no source", null)); //$NON-NLS-1$ } String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScanner= ToolFactory.createScanner(true, false, true, sourceLevel, complianceLevel); // line info required fScanner.setSource(buffer.getCharacters()); fDocument= null; // use scanner for line information fEndPosition= fScanner.getSource().length - 1; }
static CompilationUnit getCompilationUnitNode(IMember member, boolean resolveBindings) { ITypeRoot typeRoot= member.getTypeRoot(); try { if (typeRoot.exists() && typeRoot.getBuffer() != null) { ASTParser parser= ASTParser.newParser(AST.JLS3); parser.setSource(typeRoot); parser.setResolveBindings(resolveBindings); return (CompilationUnit) parser.createAST(null); } } catch (JavaModelException e) { JavaPlugin.log(e); } return null; } }
static CompilationUnit getCompilationUnitNode(IMember member, boolean resolveBindings) { ITypeRoot typeRoot= member.getTypeRoot(); try { if (typeRoot.exists() && typeRoot.getBuffer() != null) { ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); parser.setSource(typeRoot); parser.setResolveBindings(resolveBindings); return (CompilationUnit) parser.createAST(null); } } catch (JavaModelException e) { JavaPlugin.log(e); } return null; }
ITypeRoot typeRoot= astRoot.getTypeRoot(); try { if (typeRoot != null && typeRoot.getBuffer() != null) { IBuffer buffer= typeRoot.getBuffer(); int offset= extendedRange ? astRoot.getExtendedStartPosition(node) : node.getStartPosition(); int length= extendedRange ? astRoot.getExtendedLength(node) : node.getLength();
ITypeRoot typeRoot= astRoot.getTypeRoot(); try { if (typeRoot != null && typeRoot.getBuffer() != null) { IBuffer buffer= typeRoot.getBuffer(); int offset= extendedRange ? astRoot.getExtendedStartPosition(node) : node.getStartPosition(); int length= extendedRange ? astRoot.getExtendedLength(node) : node.getLength();
@Override public String initialize(CompilationUnit root, ASTNode node) { ASTNode controlNode= getBreakOrContinueNode(node); if (controlNode != null) { fASTRoot= root; try { if (root.getTypeRoot() == null || root.getTypeRoot().getBuffer() == null) return SearchMessages.BreakContinueTargetFinder_cannot_highlight; } catch (JavaModelException e) { return SearchMessages.BreakContinueTargetFinder_cannot_highlight; } fSelected= controlNode; fIsBreak= fSelected instanceof BreakStatement; fLabel= getLabel(); fDescription= Messages.format(SearchMessages.BreakContinueTargetFinder_occurrence_description, BasicElementLabels.getJavaElementName(ASTNodes.asString(fSelected))); return null; } else { return SearchMessages.BreakContinueTargetFinder_no_break_or_continue_selected; } }
@Override public String initialize(CompilationUnit root, ASTNode node) { ASTNode controlNode= getBreakOrContinueNode(node); if (controlNode != null) { fASTRoot= root; try { if (root.getTypeRoot() == null || root.getTypeRoot().getBuffer() == null) return SearchMessages.BreakContinueTargetFinder_cannot_highlight; } catch (JavaModelException e) { return SearchMessages.BreakContinueTargetFinder_cannot_highlight; } fSelected= controlNode; fIsBreak= fSelected instanceof BreakStatement; fLabel= getLabel(); fDescription= Messages.format(SearchMessages.BreakContinueTargetFinder_occurrence_description, BasicElementLabels.getJavaElementName(ASTNodes.asString(fSelected))); return null; } else { return SearchMessages.BreakContinueTargetFinder_no_break_or_continue_selected; } }
private DocumentHighlight convertToHighlight(ITypeRoot unit, OccurrenceLocation occurrence) throws JavaModelException { DocumentHighlight h = new DocumentHighlight(); if ((occurrence.getFlags() | IOccurrencesFinder.F_WRITE_OCCURRENCE) == IOccurrencesFinder.F_WRITE_OCCURRENCE) { h.setKind(DocumentHighlightKind.Write); } else if ((occurrence.getFlags() | IOccurrencesFinder.F_READ_OCCURRENCE) == IOccurrencesFinder.F_READ_OCCURRENCE) { h.setKind(DocumentHighlightKind.Read); } int[] loc = JsonRpcHelpers.toLine(unit.getBuffer(), occurrence.getOffset()); int[] endLoc = JsonRpcHelpers.toLine(unit.getBuffer(), occurrence.getOffset() + occurrence.getLength()); h.setRange(new Range( new Position(loc[0], loc[1]), new Position(endLoc[0],endLoc[1]) )); return h; }
IBuffer buffer = compilationUnit.getTypeRoot().getBuffer(); int length = buffer.getLength(); while (surroundingEnd < length && Character.isWhitespace(buffer.getChar(surroundingEnd))) {