本文整理汇总了Java中com.sun.source.util.SourcePositions.getEndPosition方法的典型用法代码示例。如果您正苦于以下问题:Java SourcePositions.getEndPosition方法的具体用法?Java SourcePositions.getEndPosition怎么用?Java SourcePositions.getEndPosition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.util.SourcePositions
的用法示例。
在下文中一共展示了SourcePositions.getEndPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findIdentifierSpanImpl
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
private static Token<JavaTokenId> findIdentifierSpanImpl(CompilationInfo info, IdentifierTree tree, CompilationUnitTree cu, SourcePositions positions) {
int start = (int)positions.getStartPosition(cu, tree);
int endPosition = (int)positions.getEndPosition(cu, tree);
if (start == (-1) || endPosition == (-1))
return null;
TokenHierarchy<?> th = info.getTokenHierarchy();
TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
if (ts.move(start) == Integer.MAX_VALUE) {
return null;
}
if (ts.moveNext()) {
if (ts.offset() >= start) {
Token<JavaTokenId> t = ts.token();
return t;
}
}
return null;
}
示例2: visitClass
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
@Override
public Void visitClass(ClassTree node, Void p) {
final SourcePositions sourcePositions = wc.getTrees().getSourcePositions();
final TreeMaker make = wc.getTreeMaker();
List<Tree> members = new LinkedList<Tree>();
ClassTree classTree = node;
for (Tree member : node.getMembers()) {
int s = (int) sourcePositions.getStartPosition(wc.getCompilationUnit(), member);
int e = (int) sourcePositions.getEndPosition(wc.getCompilationUnit(), member);
if (s >= start && e <= end) {
classTree = make.removeClassMember(classTree, member);
members.add(member);
}
}
classTree = GeneratorUtils.insertClassMembers(wc, classTree, members, start);
wc.rewrite(node, classTree);
return super.visitClass(classTree, p);
}
示例3: initialize
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
public void initialize() {
int size = oldL.size();
matrix = new int[size+1][5];
matrix[size] = new int[] { -1, -1, -1, -1, -1 };
SourcePositions positions = diffContext.trees.getSourcePositions();
CompilationUnitTree compilationUnit = diffContext.origUnit;
int i = 0;
for (Tree item : oldL) {
int treeStart = (int) positions.getStartPosition(compilationUnit, item);
int treeEnd = (int) positions.getEndPosition(compilationUnit, item);
// stupid hack, we have to remove syntetic constructors --
// should be filtered before and shouldn't be part of this
// collection (oldL)
if (treeEnd < 0) continue;
seq.move(treeStart);
int startIndex = seq.index();
// go back to opening/closing curly, semicolon or other
// token java-compiler important token.
moveToSrcRelevant(seq, Direction.BACKWARD);
seq.moveNext();
int veryBeg = seq.index();
seq.move(treeEnd);
matrix[i++] = new int[] { veryBeg, veryBeg, veryBeg, startIndex, seq.index() };
if (i == size) {
seq.move(treeEnd);
matrix[i][2] = seq.index();
}
}
initialized = true;
}
示例4: insideElse
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
private boolean insideElse(CompilationController controller, IfTree ifTree, int caretPosition) {
if (ifTree.getElseStatement() == null) {
return false;
}
SourcePositions sp = controller.getTrees().getSourcePositions();
int end = (int) sp.getEndPosition(controller.getCompilationUnit(), ifTree.getThenStatement());
return end > 0 && caretPosition > end;
}
示例5: getForwardReferences
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
/**
* Returns all elements of the given scope that are declared after given position in a source.
* @param path to the given search scope
* @param pos position in the source
* @param sourcePositions
* @param trees
* @return collection of forward references
*
* @since 0.136
*/
public static Collection<? extends Element> getForwardReferences(TreePath path, int pos, SourcePositions sourcePositions, Trees trees) {
HashSet<Element> refs = new HashSet<>();
Element el;
while(path != null) {
switch(path.getLeaf().getKind()) {
case VARIABLE:
el = trees.getElement(path);
if (el != null) {
refs.add(el);
}
TreePath parent = path.getParentPath();
if (TreeUtilities.CLASS_TREE_KINDS.contains(parent.getLeaf().getKind())) {
boolean isStatic = ((VariableTree)path.getLeaf()).getModifiers().getFlags().contains(Modifier.STATIC);
for(Tree member : ((ClassTree)parent.getLeaf()).getMembers()) {
if (member.getKind() == Tree.Kind.VARIABLE && sourcePositions.getStartPosition(path.getCompilationUnit(), member) >= pos &&
(isStatic || !((VariableTree)member).getModifiers().getFlags().contains(Modifier.STATIC))) {
el = trees.getElement(new TreePath(parent, member));
if (el != null) {
refs.add(el);
}
}
}
}
break;
case ENHANCED_FOR_LOOP:
EnhancedForLoopTree efl = (EnhancedForLoopTree)path.getLeaf();
if (sourcePositions.getEndPosition(path.getCompilationUnit(), efl.getExpression()) >= pos) {
el = trees.getElement(new TreePath(path, efl.getVariable()));
if (el != null) {
refs.add(el);
}
}
}
path = path.getParentPath();
}
return refs;
}
示例6: tokensFor
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
/**Returns tokens for a given tree. Uses specified {@link SourcePositions}.
*/
public TokenSequence<JavaTokenId> tokensFor(Tree tree, SourcePositions sourcePositions) {
int start = (int)sourcePositions.getStartPosition(info.getCompilationUnit(), tree);
int end = (int)sourcePositions.getEndPosition(info.getCompilationUnit(), tree);
return info.getTokenHierarchy().tokenSequence(JavaTokenId.language()).subSequence(start, end);
}
示例7: run
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
/**
*/
public void run(CompilationController controller) throws IOException {
try {
controller.toPhase(Phase.RESOLVED); //cursor position needed
} catch (IOException ex) {
Logger.getLogger("global").log(Level.SEVERE, null, ex); //NOI18N
}
if (cancelled) {
return;
}
Element element = elemHandle.resolve(controller);
if (cancelled || (element == null)) {
return;
}
Trees trees = controller.getTrees();
CompilationUnitTree compUnit = controller.getCompilationUnit();
DeclarationTreeFinder treeFinder = new DeclarationTreeFinder(
element, trees);
treeFinder.scan(compUnit, null);
Tree tree = treeFinder.getDeclarationTree();
if (tree != null) {
SourcePositions srcPositions = trees.getSourcePositions();
long startPos = srcPositions.getStartPosition(compUnit, tree);
long endPos = srcPositions.getEndPosition(compUnit, tree);
if ((startPos >= 0) && (startPos <= (long) Integer.MAX_VALUE)
&& (endPos >= 0) && (endPos <= (long) Integer.MAX_VALUE)) {
positionRange = new int[2];
positionRange[0] = (int) startPos;
positionRange[1] = (int) endPos;
}
}
}
示例8: getPosition
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
private long[] getPosition( Element e ) {
Trees trees = cc.getTrees();
CompilationUnitTree cut = cc.getCompilationUnit();
Tree t = trees.getTree(e);
if ( t == null ) {
return new long[]{-1,-1};
}
SourcePositions sourcePositions = trees.getSourcePositions();
return new long[] {sourcePositions.getStartPosition(cut, t),sourcePositions.getEndPosition(cut, t)};
}
示例9: createHighlightImpl
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
private static Token<JavaTokenId> createHighlightImpl(CompilationInfo info, Document doc, TreePath tree) {
Tree leaf = tree.getLeaf();
SourcePositions positions = info.getTrees().getSourcePositions();
CompilationUnitTree cu = info.getCompilationUnit();
//XXX: do not use instanceof:
if (leaf instanceof MethodTree || leaf instanceof VariableTree || leaf instanceof ClassTree
|| leaf instanceof MemberSelectTree || leaf instanceof AnnotatedTypeTree || leaf instanceof MemberReferenceTree) {
return findIdentifierSpan(info, doc, tree);
}
int start = (int) positions.getStartPosition(cu, leaf);
int end = (int) positions.getEndPosition(cu, leaf);
if (start == Diagnostic.NOPOS || end == Diagnostic.NOPOS) {
return null;
}
TokenHierarchy<?> th = info.getTokenHierarchy();
TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
if (ts.move(start) == Integer.MAX_VALUE) {
return null;
}
if (ts.moveNext()) {
Token<JavaTokenId> token = ts.token();
if (ts.offset() == start && token != null) {
final JavaTokenId id = token.id();
if (id == JavaTokenId.IDENTIFIER) {
return token;
}
if (id == JavaTokenId.THIS || id == JavaTokenId.SUPER) {
return ts.offsetToken();
}
}
}
return null;
}
示例10: checkImports
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
@TriggerTreeKind(Kind.COMPILATION_UNIT)
public static ErrorDescription checkImports(final HintContext context) {
Source source = context.getInfo().getSnapshot().getSource();
ModificationResult result = null;
try {
result = ModificationResult.runModificationTask(Collections.singleton(source), new UserTask() {
@Override
public void run(ResultIterator resultIterator) throws Exception {
WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult());
copy.toPhase(Phase.RESOLVED);
doOrganizeImports(copy, context.isBulkMode());
}
});
} catch (ParseException ex) {
Exceptions.printStackTrace(ex);
}
List<? extends Difference> diffs = result != null ? result.getDifferences(source.getFileObject()) : null;
if (diffs != null && !diffs.isEmpty()) {
Fix fix = new OrganizeImportsFix(context.getInfo(), context.getPath(), context.isBulkMode()).toEditorFix();
SourcePositions sp = context.getInfo().getTrees().getSourcePositions();
int offset = diffs.get(0).getStartPosition().getOffset();
CompilationUnitTree cu = context.getInfo().getCompilationUnit();
for (ImportTree imp : cu.getImports()) {
if (sp.getEndPosition(cu, imp) >= offset)
return ErrorDescriptionFactory.forTree(context, imp, NbBundle.getMessage(OrganizeImports.class, "MSG_OragnizeImports"), fix); //NOI18N
}
return ErrorDescriptionFactory.forTree(context, context.getInfo().getCompilationUnit().getImports().get(0), NbBundle.getMessage(OrganizeImports.class, "MSG_OragnizeImports"), fix); //NOI18N
}
return null;
}
示例11: findBodyStartImpl
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
private static int findBodyStartImpl(Tree cltree, CompilationUnitTree cu, SourcePositions positions, Document doc) {
int start = (int)positions.getStartPosition(cu, cltree);
int end = (int)positions.getEndPosition(cu, cltree);
if (start == (-1) || end == (-1)) {
return -1;
}
if (start > doc.getLength() || end > doc.getLength()) {
return (-1);
}
try {
String text = doc.getText(start, end - start);
int index = text.indexOf('{');
if (index == (-1)) {
return -1;
// throw new IllegalStateException("Should NEVER happen.");
}
return start + index;
} catch (BadLocationException e) {
LOG.log(Level.INFO, null, e);
}
return (-1);
}
示例12: isSynthetic
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
static boolean isSynthetic(CompilationInfo info, CompilationUnitTree cut, Tree leaf) throws NullPointerException {
JCTree tree = (JCTree) leaf;
if (tree.pos == (-1))
return true;
if (leaf.getKind() == Kind.METHOD) {
//check for synthetic constructor:
return (((JCTree.JCMethodDecl)leaf).mods.flags & Flags.GENERATEDCONSTR) != 0L;
}
//check for synthetic superconstructor call:
if (leaf.getKind() == Kind.EXPRESSION_STATEMENT) {
ExpressionStatementTree est = (ExpressionStatementTree) leaf;
if (est.getExpression().getKind() == Kind.METHOD_INVOCATION) {
MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
if (mit.getMethodSelect().getKind() == Kind.IDENTIFIER) {
IdentifierTree it = (IdentifierTree) mit.getMethodSelect();
if ("super".equals(it.getName().toString())) {
SourcePositions sp = info.getTrees().getSourcePositions();
return sp.getEndPosition(cut, leaf) == (-1);
}
}
}
}
return false;
}
示例13: widenToElementBounds
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
private TreePath widenToElementBounds(CompilationInfo cInfo, BaseDocument doc, int[] bounds) {
SourcePositions sp = cInfo.getTrees().getSourcePositions();
TreeUtilities tu = cInfo.getTreeUtilities();
int startOffset = getLineStart(doc, bounds[0]);
int endOffset = getLineEnd(doc, bounds[1]);
while (true) {
TokenSequence<JavaTokenId> ts = SourceUtils.getJavaTokenSequence(cInfo.getTokenHierarchy(), startOffset);
if (ts != null && (ts.moveNext() || ts.movePrevious())) {
if (ts.offset() < startOffset && ts.token().id() != JavaTokenId.WHITESPACE) {
startOffset = getLineStart(doc, ts.offset());
continue;
}
}
ts = SourceUtils.getJavaTokenSequence(cInfo.getTokenHierarchy(), endOffset);
if (ts != null && (ts.moveNext() || ts.movePrevious())) {
if (ts.offset() < endOffset && ts.token().id() != JavaTokenId.WHITESPACE) {
endOffset = getLineEnd(doc, ts.offset() + ts.token().length());
continue;
}
}
boolean finish = true;
TreePath tp = tu.pathFor(startOffset);
while (tp != null) {
Tree leaf = tp.getLeaf();
List<? extends Tree> children = null;
switch (leaf.getKind()) {
case BLOCK:
if (endOffset < sp.getEndPosition(tp.getCompilationUnit(), leaf)) {
children = ((BlockTree) leaf).getStatements();
}
break;
case CLASS:
case INTERFACE:
case ANNOTATION_TYPE:
case ENUM:
if (endOffset < sp.getEndPosition(tp.getCompilationUnit(), leaf)) {
children = ((ClassTree) leaf).getMembers();
}
break;
}
if (children != null) {
for (Tree tree : children) {
int startPos = (int) sp.getStartPosition(tp.getCompilationUnit(), tree);
int endPos = (int) sp.getEndPosition(tp.getCompilationUnit(), tree);
if (endPos > startOffset) {
if (startPos < startOffset) {
startOffset = getLineStart(doc, startPos);
finish = false;
}
if (startPos < endOffset && endOffset < endPos) {
endOffset = getLineEnd(doc, endPos);
finish = false;
}
}
}
break;
}
tp = tp.getParentPath();
}
if (finish) {
bounds[0] = startOffset;
bounds[1] = endOffset;
return tp;
}
}
}
示例14: getEnd
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
private int getEnd(TreeUtilities tu, SourcePositions sp, CompilationUnitTree cut, Tree tree) {
List<Comment> comments = tu.getComments(tree, false);
return comments.isEmpty() ? (int) sp.getEndPosition(cut, tree) : comments.get(comments.size() - 1).endPos();
}
示例15: testForJean
import com.sun.source.util.SourcePositions; //导入方法依赖的package包/类
public void testForJean() throws FileStateInvalidException, IOException {
File tutorialFile = getFile(getSourceDir(), "/org/netbeans/test/codegen/Tutorial2.java");
JavaSource tutorialSource = JavaSource.forFileObject(FileUtil.toFileObject(tutorialFile));
Task task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws java.io.IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
// we know there is exactly one class, named Tutorial2.
// (ommiting test for kind corectness!)
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
// get the method, there is a default constructor and
// exactly one method, named demoMethod().
MethodTree method = (MethodTree) clazz.getMembers().get(1);
BlockTree body = method.getBody();
// get SourcePositions instance for your working copy and
// fetch out start and end position.
SourcePositions sp = workingCopy.getTrees().getSourcePositions();
int start = (int) sp.getStartPosition(cut, body);
int end = (int) sp.getEndPosition(cut, body);
// get body text from source text
String bodyText = workingCopy.getText().substring(start, end);
MethodTree modified = make.Method(
method.getModifiers(), // copy original values
method.getName(),
method.getReturnType(),
method.getTypeParameters(),
method.getParameters(),
method.getThrows(),
bodyText.replace("{0}", "-tag-replace-"), // replace body with the new text
null // not applicable here
);
// rewrite the original modifiers with the new one:
workingCopy.rewrite(method, modified);
}
};
tutorialSource.runModificationTask(task).commit();
// print the result to the System.err to see the changes in console.
BufferedReader in = new BufferedReader(new FileReader(tutorialFile));
PrintStream out = System.out;
String str;
while ((str = in.readLine()) != null) {
out.println(str);
}
in.close();
}