Java Code Examples for org.netbeans.api.java.source.CompilationInfo#getTokenHierarchy()

The following examples show how to use org.netbeans.api.java.source.CompilationInfo#getTokenHierarchy() . 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: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static Token<JavaTokenId> findTokenWithText(CompilationInfo info, String text, int start, int end) {
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()).subSequence(start, end);
    
    while (ts.moveNext()) {
        Token<JavaTokenId> t = ts.token();
        
        if (t.id() == JavaTokenId.IDENTIFIER) {
            boolean nameMatches;
            
            if (!(nameMatches = text.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString()))) {
                ExpressionTree expr = info.getTreeUtilities().parseExpression(t.text().toString(), new SourcePositions[1]);
                
                nameMatches = expr.getKind() == Kind.IDENTIFIER && text.contentEquals(((IdentifierTree) expr).getName());
            }
            
            if (nameMatches) {
                return t;
            }
        }
    }
    
    return null;
}
 
Example 2
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Token<JavaTokenId> findIdentifierSpanImpl(CompilationInfo info, MemberSelectTree 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;

    String member = tree.getIdentifier().toString();

    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());

    if (ts.move(endPosition) == Integer.MAX_VALUE) {
        return null;
    }

    if (ts.moveNext()) {
        while (ts.offset() >= start) {
            Token<JavaTokenId> t = ts.token();

            if (t.id() == JavaTokenId.IDENTIFIER && member.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString())) {
                return t;
            }

            if (!ts.movePrevious())
                break;
        }
    }
    return null;
}
 
Example 4
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Token<JavaTokenId> findIdentifierSpanImpl(CompilationInfo info, MemberReferenceTree 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;

    String member = tree.getName().toString();

    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());

    if (ts.move(endPosition) == Integer.MAX_VALUE) {
        return null;
    }

    if (ts.moveNext()) {
        while (ts.offset() >= start) {
            Token<JavaTokenId> t = ts.token();

            if (t.id() == JavaTokenId.IDENTIFIER && member.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString())) {
                return t;
            }

            if (!ts.movePrevious())
                break;
        }
    }
    return null;
}
 
Example 5
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Token<JavaTokenId> findIdentifierSpanImplForBindingPattern(CompilationInfo info, Tree 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;

    String member = TreeShims.getBinding(tree).toString();

    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());

    if (ts.move(endPosition) == Integer.MAX_VALUE) {
        return null;
    }

    if (ts.moveNext()) {
        while (ts.offset() >= start) {
            Token<JavaTokenId> t = ts.token();

            if (t.id() == JavaTokenId.IDENTIFIER && member.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString())) {
                return t;
            }

            if (!ts.movePrevious())
                break;
        }
    }
    return null;
}
 
Example 6
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static int findBodyStartImpl(CompilationInfo info, 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;
    }
    int startPos = -1;
    switch (cltree.getKind()) {
        case CLASS: case INTERFACE: case ENUM: {
            ClassTree ct = (ClassTree)cltree;
            startPos = findSubtreeEnd(cu, positions,
                    ct.getImplementsClause(),
                    ct.getExtendsClause(),
                    ct.getTypeParameters(),
                    ct.getModifiers()
            );
            break;
        }
            
        case METHOD:  {
            // if the method contains some parameters, skip to the end of the parameter list
            MethodTree mt = (MethodTree)cltree;
            startPos = findSubtreeEnd(cu, positions, mt.getDefaultValue(), mt.getThrows(), mt.getParameters(), mt.getModifiers());
            break;
        }
    }
    if (startPos > start) {
        start = startPos;
    }
    
    if (start > doc.getLength() || end > doc.getLength()) {
        if (DEBUG) {
            System.err.println("Log: position outside document: ");
            System.err.println("decl = " + cltree);
            System.err.println("startOffset = " + start);
            System.err.println("endOffset = " + end);
            Thread.dumpStack();
        }
        
        return (-1);
    }
    TokenHierarchy<JavaTokenId> th = (TokenHierarchy<JavaTokenId>)info.getTokenHierarchy();
    TokenSequence<JavaTokenId> seq = (TokenSequence<JavaTokenId>)th.tokenSequence();
    seq.move(start);
    while (seq.moveNext()) {
        if (seq.token().id() == JavaTokenId.LBRACE) {
            return seq.offset();
        }
    }
    return (-1);
}
 
Example 7
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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
            || "BINDING_PATTERN".equals(leaf.getKind().name())) {
        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;
}
 
Example 8
Source File: ErrorHintsProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static Token findUnresolvedElementToken(CompilationInfo info, int offset) throws IOException {
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
    
    if (ts == null) {
        return null;
    }
    
    ts.move(offset);
    if (ts.moveNext()) {
        Token<JavaTokenId> t = ts.token();

        if (t.id() == JavaTokenId.DOT) {
            ts.moveNext();
            t = ts.token();
        } else {
            if (t.id() == JavaTokenId.LT) {
                ts.moveNext();
                t = ts.token();
            } else {
                if (t.id() == JavaTokenId.NEW || t.id() == JavaTokenId.WHITESPACE) {
                    t = skipWhitespaces(ts);

                    if (t == null) return null;
                } else if (t.id() == JavaTokenId.IMPORT) {
                    t = skipWhitespaces(ts);

                    if (t == null) return null;
                }
            }
        }

        while (t.id() == JavaTokenId.WHITESPACE) {
            ts.moveNext();
            t = ts.token();
        }
        
        switch (t.id()) {
            case IDENTIFIER:
            case INT_LITERAL:
            case LONG_LITERAL:
            case FLOAT_LITERAL:
            case DOUBLE_LITERAL:
            case CHAR_LITERAL:
            case STRING_LITERAL:
            case TRUE:
            case FALSE:
            case NULL:
                return ts.offsetToken();
        }
    }
    return null;
}
 
Example 9
Source File: SearchClassDependencyInRepo.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static Token findUnresolvedElementToken(CompilationInfo info, int offset) throws IOException {
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());

    if (ts == null) {
        return null;
    }

    ts.move(offset);
    if (ts.moveNext()) {
        Token t = ts.token();

        if (t.id() == JavaTokenId.DOT) {
            ts.moveNext();
            t = ts.token();
        } else {
            if (t.id() == JavaTokenId.LT) {
                ts.moveNext();
                t = ts.token();
            } else {
                if (t.id() == JavaTokenId.NEW) {
                    boolean cont = ts.moveNext();

                    while (cont && ts.token().id() == JavaTokenId.WHITESPACE) {
                        cont = ts.moveNext();
                    }

                    if (!cont) {
                        return null;
                    }
                    t = ts.token();
                }
            }
        }

        if (t.id() == JavaTokenId.IDENTIFIER) {
            return ts.offsetToken();
        }
    }
    return null;
}
 
Example 10
Source File: SearchModuleDependency.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static Token findUnresolvedElementToken(CompilationInfo info, int offset) throws IOException {
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());

    if (ts == null) {
        return null;
    }

    ts.move(offset);
    if (ts.moveNext()) {
        Token t = ts.token();

        if (t.id() == JavaTokenId.DOT) {
            ts.moveNext();
            t = ts.token();
        } else {
            if (t.id() == JavaTokenId.LT) {
                ts.moveNext();
                t = ts.token();
            } else {
                if (t.id() == JavaTokenId.NEW) {
                    boolean cont = ts.moveNext();

                    while (cont && ts.token().id() == JavaTokenId.WHITESPACE) {
                        cont = ts.moveNext();
                    }

                    if (!cont) {
                        return null;
                    }
                    t = ts.token();
                }
            }
        }

        if (t.id() == JavaTokenId.IDENTIFIER) {
            return ts.offsetToken();
        }
    }
    return null;
}
 
Example 11
Source File: CreateQualifier.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Token<?> findUnresolvedElementToken(CompilationInfo info, int offset) 
    throws IOException 
{
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language());
    
    if (ts == null) {
        return null;
    }
    
    ts.move(offset);
    if (ts.moveNext()) {
        Token<?> t = ts.token();

        if (t.id() == JavaTokenId.DOT) {
            ts.moveNext();
            t = ts.token();
        } else {
            if (t.id() == JavaTokenId.LT) {
                ts.moveNext();
                t = ts.token();
            } else {
                if (t.id() == JavaTokenId.NEW || t.id() == JavaTokenId.WHITESPACE) {
                    boolean cont = ts.moveNext();
                    
                    while (cont && ts.token().id() == JavaTokenId.WHITESPACE) {
                        cont = ts.moveNext();
                    }
                    
                    if (!cont)
                        return null;
                    
                    t = ts.token();
                }
            }
        }

        if (t.id() == JavaTokenId.IDENTIFIER) {
            return ts.offsetToken();
        }
    }
    return null;
}