langtools/src/share/classes/com/sun/tools/javac/parser/LazyDocCommentTable.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 14541 36f9d11fc9aa
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
     1
/*
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
     2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
     4
 *
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    10
 *
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    15
 * accompanied this code).
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    16
 *
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    20
 *
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    23
 * questions.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    24
 */
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    25
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.parser;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    27
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    28
import java.util.HashMap;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    29
import java.util.Map;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    30
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    31
import com.sun.tools.javac.parser.Tokens.Comment;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    32
import com.sun.tools.javac.tree.DCTree.DCDocComment;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    33
import com.sun.tools.javac.tree.DocCommentTable;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.tree.JCTree;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.util.DiagnosticSource;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    36
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    37
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    38
/**
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    39
 *
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    40
 *  <p><b>This is NOT part of any supported API.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    41
 *  If you write code that depends on this, you do so at your own risk.
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    42
 *  This code and its internal interfaces are subject to change or
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    43
 *  deletion without notice.</b>
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    44
 */
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    45
public class LazyDocCommentTable implements DocCommentTable {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    46
    private static class Entry {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    47
        final Comment comment;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    48
        DCDocComment tree;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    49
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    50
        Entry(Comment c) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    51
            comment = c;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    52
        }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    53
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    54
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    55
    ParserFactory fac;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    56
    DiagnosticSource diagSource;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    57
    Map<JCTree, Entry> table;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    58
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    59
    LazyDocCommentTable(ParserFactory fac) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    60
        this.fac = fac;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    61
        diagSource = fac.log.currentSource();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 14541
diff changeset
    62
        table = new HashMap<>();
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    63
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    64
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    65
    public boolean hasComment(JCTree tree) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    66
        return table.containsKey(tree);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    67
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    68
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    69
    public Comment getComment(JCTree tree) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    70
        Entry e = table.get(tree);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    71
        return (e == null) ? null : e.comment;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    72
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    73
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    74
    public String getCommentText(JCTree tree) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    75
        Comment c = getComment(tree);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    76
        return (c == null) ? null : c.getText();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    77
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    78
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    79
    public DCDocComment getCommentTree(JCTree tree) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    80
        Entry e = table.get(tree);
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    81
        if (e == null)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    82
            return null;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    83
        if (e.tree == null)
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    84
            e.tree = new DocCommentParser(fac, diagSource, e.comment).parse();
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    85
        return e.tree;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    86
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    87
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    88
    public void putComment(JCTree tree, Comment c) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    89
        table.put(tree, new Entry(c));
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    90
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    91
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents:
diff changeset
    92
}