src/share/classes/com/sun/tools/doclint/Env.java

Fri, 10 May 2013 15:15:50 +0200

author
jlahoda
date
Fri, 10 May 2013 15:15:50 +0200
changeset 1734
8dd528992c15
parent 1668
991f11e13598
child 1895
37031963493e
permissions
-rw-r--r--

8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
Reviewed-by: jjg
Contributed-by: Dusan Balek <dbalek@netbeans.org>, Jan Lahoda <jlahoda@netbeans.org>

jjg@1455 1 /*
jjg@1668 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1455 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1455 4 *
jjg@1455 5 * This code is free software; you can redistribute it and/or modify it
jjg@1455 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1455 7 * published by the Free Software Foundation. Oracle designates this
jjg@1455 8 * particular file as subject to the "Classpath" exception as provided
jjg@1455 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1455 10 *
jjg@1455 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1455 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1455 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1455 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1455 15 * accompanied this code).
jjg@1455 16 *
jjg@1455 17 * You should have received a copy of the GNU General Public License version
jjg@1455 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1455 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1455 20 *
jjg@1455 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1455 22 * or visit www.oracle.com if you need additional information or have any
jjg@1455 23 * questions.
jjg@1455 24 */
jjg@1455 25
jjg@1455 26 package com.sun.tools.doclint;
jjg@1455 27
jjg@1455 28
jjg@1455 29 import java.util.Set;
jjg@1455 30
jjg@1455 31 import javax.lang.model.element.Element;
jlahoda@1734 32 import javax.lang.model.element.ElementKind;
jjg@1455 33 import javax.lang.model.element.ExecutableElement;
jjg@1455 34 import javax.lang.model.element.Modifier;
jjg@1455 35 import javax.lang.model.type.TypeMirror;
jjg@1455 36 import javax.lang.model.util.Elements;
jjg@1455 37 import javax.lang.model.util.Types;
jjg@1455 38
jjg@1455 39 import com.sun.source.doctree.DocCommentTree;
jjg@1455 40 import com.sun.source.util.DocTrees;
jjg@1455 41 import com.sun.source.util.JavacTask;
jjg@1455 42 import com.sun.source.util.SourcePositions;
jjg@1455 43 import com.sun.source.util.TreePath;
jjg@1455 44 import com.sun.tools.javac.model.JavacTypes;
jjg@1455 45 import com.sun.tools.javac.tree.JCTree;
jjg@1455 46
jjg@1455 47 /**
jjg@1455 48 * Utility container for current execution environment,
jjg@1455 49 * providing the current declaration and its doc comment.
jjg@1455 50 *
jjg@1455 51 * <p><b>This is NOT part of any supported API.
jjg@1455 52 * If you write code that depends on this, you do so at your own
jjg@1455 53 * risk. This code and its internal interfaces are subject to change
jjg@1455 54 * or deletion without notice.</b></p>
jjg@1455 55 */
jjg@1455 56 public class Env {
jjg@1455 57 /**
jjg@1455 58 * Access kinds for declarations.
jjg@1455 59 */
jjg@1455 60 public enum AccessKind {
jjg@1455 61 PRIVATE,
jjg@1455 62 PACKAGE,
jjg@1455 63 PROTECTED,
jjg@1455 64 PUBLIC;
jjg@1455 65
jjg@1455 66 static boolean accepts(String opt) {
jjg@1455 67 for (AccessKind g: values())
jjg@1455 68 if (opt.equals(g.name().toLowerCase())) return true;
jjg@1455 69 return false;
jjg@1455 70 }
jjg@1455 71
jjg@1455 72 static AccessKind of(Set<Modifier> mods) {
jjg@1455 73 if (mods.contains(Modifier.PUBLIC))
jjg@1455 74 return AccessKind.PUBLIC;
jjg@1455 75 else if (mods.contains(Modifier.PROTECTED))
jjg@1455 76 return AccessKind.PROTECTED;
jjg@1455 77 else if (mods.contains(Modifier.PRIVATE))
jjg@1455 78 return AccessKind.PRIVATE;
jjg@1455 79 else
jjg@1455 80 return AccessKind.PACKAGE;
jjg@1455 81 }
jjg@1455 82 };
jjg@1455 83
jjg@1455 84 /** Message handler. */
jjg@1455 85 final Messages messages;
jjg@1455 86
jjg@1668 87 int implicitHeaderLevel = 0;
jjg@1668 88
jjg@1455 89 // Utility classes
jjg@1455 90 DocTrees trees;
jjg@1455 91 Elements elements;
jjg@1455 92 Types types;
jjg@1455 93
jjg@1455 94 // Types used when analysing doc comments.
jjg@1455 95 TypeMirror java_lang_Error;
jjg@1455 96 TypeMirror java_lang_RuntimeException;
jjg@1455 97 TypeMirror java_lang_Throwable;
jjg@1455 98 TypeMirror java_lang_Void;
jjg@1455 99
jjg@1455 100 /** The path for the declaration containing the comment currently being analyzed. */
jjg@1455 101 TreePath currPath;
jjg@1455 102 /** The element for the declaration containing the comment currently being analyzed. */
jjg@1455 103 Element currElement;
jjg@1455 104 /** The comment current being analyzed. */
jjg@1455 105 DocCommentTree currDocComment;
jjg@1455 106 /**
jjg@1455 107 * The access kind of the declaration containing the comment currently being analyzed.
jjg@1668 108 * This is the minimum (most restrictive) access kind of the declaration itself
jjg@1455 109 * and that of its containers. For example, a public method in a private class is
jjg@1455 110 * noted as private.
jjg@1455 111 */
jjg@1455 112 AccessKind currAccess;
jjg@1455 113 /** The set of methods, if any, that the current declaration overrides. */
jjg@1455 114 Set<? extends ExecutableElement> currOverriddenMethods;
jjg@1455 115
jjg@1455 116 Env() {
jjg@1455 117 messages = new Messages(this);
jjg@1455 118 }
jjg@1455 119
jjg@1455 120 void init(JavacTask task) {
jjg@1455 121 init(DocTrees.instance(task), task.getElements(), task.getTypes());
jjg@1455 122 }
jjg@1455 123
jjg@1455 124 void init(DocTrees trees, Elements elements, Types types) {
jjg@1455 125 this.trees = trees;
jjg@1455 126 this.elements = elements;
jjg@1455 127 this.types = types;
jjg@1455 128 java_lang_Error = elements.getTypeElement("java.lang.Error").asType();
jjg@1455 129 java_lang_RuntimeException = elements.getTypeElement("java.lang.RuntimeException").asType();
jjg@1455 130 java_lang_Throwable = elements.getTypeElement("java.lang.Throwable").asType();
jjg@1455 131 java_lang_Void = elements.getTypeElement("java.lang.Void").asType();
jjg@1455 132 }
jjg@1455 133
jjg@1668 134 void setImplicitHeaders(int n) {
jjg@1668 135 implicitHeaderLevel = n;
jjg@1668 136 }
jjg@1668 137
jjg@1455 138 /** Set the current declaration and its doc comment. */
jjg@1455 139 void setCurrent(TreePath path, DocCommentTree comment) {
jjg@1455 140 currPath = path;
jjg@1455 141 currDocComment = comment;
jjg@1455 142 currElement = trees.getElement(currPath);
jjg@1455 143 currOverriddenMethods = ((JavacTypes) types).getOverriddenMethods(currElement);
jjg@1455 144
jjg@1455 145 AccessKind ak = null;
jjg@1455 146 for (TreePath p = path; p != null; p = p.getParentPath()) {
jjg@1455 147 Element e = trees.getElement(p);
jlahoda@1734 148 if (e != null && e.getKind() != ElementKind.PACKAGE) {
jjg@1455 149 ak = min(ak, AccessKind.of(e.getModifiers()));
jjg@1455 150 }
jjg@1455 151 }
jjg@1455 152 currAccess = ak;
jjg@1455 153 }
jjg@1455 154
jjg@1455 155 AccessKind getAccessKind() {
jjg@1455 156 return currAccess;
jjg@1455 157 }
jjg@1455 158
jjg@1455 159 long getPos(TreePath p) {
jjg@1455 160 return ((JCTree) p.getLeaf()).pos;
jjg@1455 161 }
jjg@1455 162
jjg@1455 163 long getStartPos(TreePath p) {
jjg@1455 164 SourcePositions sp = trees.getSourcePositions();
jjg@1455 165 return sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
jjg@1455 166 }
jjg@1455 167
jjg@1455 168 private <T extends Comparable<T>> T min(T item1, T item2) {
jjg@1455 169 return (item1 == null) ? item2
jjg@1455 170 : (item2 == null) ? item1
jjg@1455 171 : item1.compareTo(item2) <= 0 ? item1 : item2;
jjg@1455 172 }
jjg@1455 173 }

mercurial