Java Code Examples for org.eclipse.jdt.core.ITypeRoot#getBuffer()
The following examples show how to use
org.eclipse.jdt.core.ITypeRoot#getBuffer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ASTNodes.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Returns the source of the given node from the location where it was parsed. * @param node the node to get the source from * @param extendedRange if set, the extended ranges of the nodes should ne used * @param removeIndent if set, the indentation is removed. * @return return the source for the given node or null if accessing the source failed. */ public static String getNodeSource(ASTNode node, boolean extendedRange, boolean removeIndent) { ASTNode root= node.getRoot(); if (root instanceof CompilationUnit) { CompilationUnit astRoot= (CompilationUnit) root; 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(); String str= buffer.getText(offset, length); if (removeIndent) { IJavaProject project= typeRoot.getJavaProject(); int indent= StubUtility.getIndentUsed(buffer, node.getStartPosition(), project); str= Strings.changeIndent(str, indent, project, new String(), typeRoot.findRecommendedLineSeparator()); } return str; } } catch (JavaModelException e) { // ignore } } return null; }
Example 2
Source File: NodeFinder.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * A visitor that maps a selection to a given ASTNode. The result node is * determined as follows: * <ul> * <li>first the visitor tries to find a node that is covered by <code>start</code> and * <code>length</code> where either <code>start</code> and <code>length</code> exactly * matches the node or where the text covered before and after the node only consists * of white spaces or comments.</li> * <li>if no such node exists than the node that encloses the range defined by * start and end is returned.</li> * <li>if the length is zero than also nodes are considered where the node's * start or end position matches <code>start</code>.</li> * <li>otherwise <code>null</code> is returned.</li> * </ul> * * @param root the root node from which the search starts * @param start the start offset * @param length the length * @param source the source of the compilation unit * * @return the result node * @throws JavaModelException if an error occurs in the Java model * * @since 3.0 */ public static ASTNode perform(ASTNode root, int start, int length, ITypeRoot source) throws JavaModelException { NodeFinder finder= new NodeFinder(start, length); root.accept(finder); ASTNode result= finder.getCoveredNode(); if (result == null) return null; Selection selection= Selection.createFromStartLength(start, length); if (selection.covers(result)) { IBuffer buffer= source.getBuffer(); if (buffer != null) { IScanner scanner= ToolFactory.createScanner(false, false, false, false); scanner.setSource(buffer.getText(start, length).toCharArray()); try { int token= scanner.getNextToken(); if (token != ITerminalSymbols.TokenNameEOF) { int tStart= scanner.getCurrentTokenStartPosition(); if (tStart == result.getStartPosition() - start) { scanner.resetTo(tStart + result.getLength(), length - 1); token= scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameEOF) return result; } } } catch (InvalidInputException e) { } } } return finder.getCoveringNode(); }
Example 3
Source File: CallHierarchy.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
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; }
Example 4
Source File: TokenScanner.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * 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; }
Example 5
Source File: JavaElementLine.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * @param element either an ICompilationUnit or an IClassFile * @param lineNumber the line number, starting at 0 * @param lineStartOffset the start offset of the line * @throws CoreException thrown when accessing of the buffer failed */ public JavaElementLine(ITypeRoot element, int lineNumber, int lineStartOffset) throws CoreException { fElement= element; 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)))); } int length= buffer.getLength(); int i= lineStartOffset; char ch= buffer.getChar(i); while (lineStartOffset < length && IndentManipulation.isIndentChar(ch)) { ch= buffer.getChar(++i); } fLineStartOffset= i; StringBuffer buf= new StringBuffer(); while (i < length && !IndentManipulation.isLineDelimiterChar(ch)) { if (Character.isISOControl(ch)) { buf.append(' '); } else { buf.append(ch); } i++; if (i < length) ch= buffer.getChar(i); } fLineContents= buf.toString(); fLineNumber= lineNumber; }
Example 6
Source File: ASTProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * 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; }
Example 7
Source File: ASTRewrite.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Converts all modifications recorded by this rewriter into an object representing the the corresponding text * edits to the source of a {@link ITypeRoot} from which the AST was created from. * The type root's source itself is not modified by this method call. * <p> * Important: This API can only be used if the modified AST has been created from a * {@link ITypeRoot} with source. That means {@link ASTParser#setSource(ICompilationUnit)}, * {@link ASTParser#setSource(IClassFile)} or {@link ASTParser#setSource(ITypeRoot)} * has been used when initializing the {@link ASTParser}. A {@link IllegalArgumentException} is thrown * otherwise. An {@link IllegalArgumentException} is also thrown when the type roots buffer does not correspond * anymore to the AST. Use {@link #rewriteAST(IDocument, Map)} for all ASTs created from other content. * </p> * <p> * For nodes in the original that are being replaced or deleted, * this rewriter computes the adjusted source ranges * by calling {@link TargetSourceRangeComputer#computeSourceRange(ASTNode) getExtendedSourceRangeComputer().computeSourceRange(node)}. * </p> * <p> * Calling this methods does not discard the modifications * on record. Subsequence modifications are added to the ones * already on record. If this method is called again later, * the resulting text edit object will accurately reflect * the net cumulative effect of all those changes. * </p> * * @return text edit object describing the changes to the * document corresponding to the changes recorded by this rewriter * @throws JavaModelException A {@link JavaModelException} is thrown when * the underlying compilation units buffer could not be accessed. * @throws IllegalArgumentException An {@link IllegalArgumentException} * is thrown if the document passed does not correspond to the AST that is rewritten. * * @since 3.2 */ public TextEdit rewriteAST() throws JavaModelException, IllegalArgumentException { ASTNode rootNode= getRootNode(); if (rootNode == null) { return new MultiTextEdit(); // no changes } ASTNode root= rootNode.getRoot(); if (!(root instanceof CompilationUnit)) { throw new IllegalArgumentException("This API can only be used if the AST is created from a compilation unit or class file"); //$NON-NLS-1$ } CompilationUnit astRoot= (CompilationUnit) root; ITypeRoot typeRoot = astRoot.getTypeRoot(); if (typeRoot == null || typeRoot.getBuffer() == null) { throw new IllegalArgumentException("This API can only be used if the AST is created from a compilation unit or class file"); //$NON-NLS-1$ } char[] content= typeRoot.getBuffer().getCharacters(); LineInformation lineInfo= LineInformation.create(astRoot); String lineDelim= typeRoot.findRecommendedLineSeparator(); Map options= typeRoot.getJavaProject().getOptions(true); return internalRewriteAST(content, lineInfo, lineDelim, astRoot.getCommentList(), options, rootNode, (RecoveryScannerData)astRoot.getStatementsRecoveryData()); }