Package com.sun.source.tree

Examples of com.sun.source.tree.LineMap


    if (path == null) {
      return Collections.emptyList();
    }

    CompilationUnitTree unitTree = path.getCompilationUnit();
    LineMap lineMap = unitTree.getLineMap();
    SourcePositions positions = treeUtils.getSourcePositions();

    AnnotationTree annotationTree = (AnnotationTree) path.getLeaf();
    AssignmentTree assignTree =
        (AssignmentTree) annotationTree.getArguments().get(0);
    ExpressionTree exprTree = assignTree.getExpression();

    ArrayList<Long> lines = new ArrayList<Long>();
    if (exprTree.getKind() == Kind.STRING_LITERAL) {
      long pos = positions.getStartPosition(unitTree, exprTree);
      lines.add(lineMap.getLineNumber(pos));
    } else {
      NewArrayTree valuesTree = (NewArrayTree) exprTree;
      for (ExpressionTree valueTree : valuesTree.getInitializers()) {
        long pos = positions.getStartPosition(unitTree, valueTree);
        lines.add(lineMap.getLineNumber(pos));
      }
    }

    return lines;
  }
View Full Code Here


        //List<Tree> treeNodes = linearizeTree(expTrees);
        if (treeNodes == null) return null;
        if (indexes == null) return null;
        int length = treeNodes.size();
        List<EditorContext.Operation> operations = new ArrayList<EditorContext.Operation>(length);
        LineMap lineMap = cu.getLineMap();
        int indexesIndex = 0;
        int from = indexes[indexesIndex];
        int to = indexes[indexesIndex + 1];
        for (int treeIndex = 0; treeIndex < length; treeIndex++) {
            Tree node = treeNodes.get(treeIndex);
            Tree.Kind kind = node.getKind();
            EditorContext.Operation op = null;
            if (kind.equals(Tree.Kind.METHOD_INVOCATION) ||
                kind.equals(Tree.Kind.NEW_CLASS)) {
               
                int opcode;
                do {
                    do {
                        opcode = bytecodes[from] & 0xFF;
                        if (isMethodCall(opcode)) {
                            break;
                        }
                        from += getInstrSize(opcode, bytecodes, from);
                    } while (from < to);
                    if (from < to) {
                        break;
                    }
                    if ((indexesIndex + 2) < indexes.length) {
                        indexesIndex += 2;
                        from = indexes[indexesIndex];
                        to = indexes[indexesIndex + 1];
                    } else {
                        break;
                    }
                } while (true);
                if (from < to) { // We have the method call
                    if (!ci.getTreeUtilities().isSynthetic(ci.getTrees().getPath(cu, node))) {
                        int pos = (int) sp.getStartPosition(cu, node);
                        EditorContext.Position startPosition =
                                opCreationDelegate.createPosition(
                                        pos,
                                        (int) lineMap.getLineNumber(pos),
                                        (int) lineMap.getColumnNumber(pos)
                                );
                        pos = (int) sp.getEndPosition(cu, node);
                        EditorContext.Position endPosition =
                                opCreationDelegate.createPosition(
                                        pos,
                                        (int) lineMap.getLineNumber(pos),
                                        (int) lineMap.getColumnNumber(pos)
                                );
                        Tree identifier;
                        String methodName;
                        String methodClassType;
                        boolean getStartPosFromMethodLength = false;
                        if (kind.equals(Tree.Kind.NEW_CLASS)) {
                            identifier = ((NewClassTree) node).getIdentifier();
                            methodName = "<init>";
                            TreePath iPath = TreePath.getPath(cu, identifier);
                            TypeMirror type = trees.getTypeMirror(iPath);
                            if (type.getKind() == TypeKind.ERROR) {
                                // There are errors, give it up.
                                return null;
                            }
                            assert type.getKind() == TypeKind.DECLARED;
                            TypeElement te = (TypeElement) types.asElement(type);
                            methodClassType = ElementUtilities.getBinaryName(te);
                        } else {
                            //identifier = ((MemberSelectTree) ((MethodInvocationTree) node).getMethodSelect()).getIdentifier();
                            identifier = ((MethodInvocationTree) node).getMethodSelect();
                            if (identifier.getKind() == Tree.Kind.IDENTIFIER) {
                                methodName = ((IdentifierTree) identifier).getName().toString();
                                TreePath iPath = TreePath.getPath(cu, identifier);
                                TypeElement te = trees.getScope(iPath).getEnclosingClass();
                                if (te == null) {
                                    // No enclosing class? Some error, give it up.
                                    return null;
                                }
                                methodClassType = ElementUtilities.getBinaryName(te);
                            } else {
                                methodName = ((MemberSelectTree) identifier).getIdentifier().toString();
                                getStartPosFromMethodLength = true;
                                ExpressionTree exp = ((MemberSelectTree) identifier).getExpression();
                                TreePath expPath = TreePath.getPath(cu, exp);
                                TypeMirror type = trees.getTypeMirror(expPath);
                                if (type.getKind() == TypeKind.ERROR) {
                                    // There are errors, give it up.
                                    return null;
                                }
                                TypeElement te;
                                if (type.getKind() == TypeKind.DECLARED) {
                                    te = (TypeElement) types.asElement(type);
                                } else if (type.getKind() == TypeKind.TYPEVAR) {
                                    TypeParameterElement tpe = (TypeParameterElement) types.asElement(type);
                                    List<? extends TypeMirror> exts = tpe.getBounds();
                                    if (exts.size() == 1) {
                                        type = exts.get(0);
                                        if (type.getKind() == TypeKind.DECLARED) {
                                            te = (TypeElement) types.asElement(type);
                                        } else {
                                            return null; // Unsupported
                                        }
                                    } else {
                                        return null; // Unsupported
                                    }
                                } else {
                                    ErrorManager.getDefault().notify(new IllegalStateException("Unexpected type "+type+" in "+treeNodes));
                                    return null;
                                }
                                methodClassType = ElementUtilities.getBinaryName(te);
                            }
                        }
                        pos = (int) sp.getEndPosition(cu, identifier);
                        EditorContext.Position methodEndPosition =
                                opCreationDelegate.createPosition(
                                        pos,
                                        (int) lineMap.getLineNumber(pos),
                                        (int) lineMap.getColumnNumber(pos)
                                );
                        if (getStartPosFromMethodLength) {
                            pos = pos - methodName.length();
                        } else {
                            pos = (int) sp.getStartPosition(cu, identifier);
                        }
                        EditorContext.Position methodStartPosition =
                                opCreationDelegate.createPosition(
                                        pos,
                                        (int) lineMap.getLineNumber(pos),
                                        (int) lineMap.getColumnNumber(pos)
                                );
                        /*
                        EditorContext.Operation op =
                                opCreationDelegate.createOperation(
                                        startPosition,
View Full Code Here

     * Invoked by {@link AstGenerator}'s constructor to complete the initialization.
     * <p>
     * This is where the actual annotation of the source code happens.
     */
    protected void configure(final CompilationUnitTree cu, final AstGenerator gen) throws IOException {
        final LineMap lineMap = cu.getLineMap();

        // add lexical markers
        JavaLexer lexer = new JavaLexer(new CharSequenceReader(gen.sourceFile));
        lexer.setTabSize(tabWidth);
        try {
View Full Code Here

                if(type == JavaTokenTypes.EOF)
                    break;
                if(type == JavaTokenTypes.IDENT) {
                    if(id!=null && !token.getText().equals(id))
                        continue;
                    LineMap lm = compUnit.getLineMap();
                    if(token.getLine()==1) {
                        token.setColumn(token.getColumn()+(int)lm.getColumnNumber(pos)-1);
                    }
                    token.setLine(token.getLine()+(int)lm.getLineNumber(pos)-1);
                    return token;
                }
            }
        } catch (TokenStreamException e) {
            // the analysis phase should have reported all the errors,
View Full Code Here

TOP

Related Classes of com.sun.source.tree.LineMap

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.