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

Thu, 28 Mar 2013 10:49:39 -0700

author
jjg
date
Thu, 28 Mar 2013 10:49:39 -0700
changeset 1668
991f11e13598
parent 1455
75ab654b5cd5
child 1734
8dd528992c15
permissions
-rw-r--r--

8006346: doclint should make allowance for headers generated by standard doclet
Reviewed-by: mcimadamore

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;
jjg@1455 32 import javax.lang.model.element.ExecutableElement;
jjg@1455 33 import javax.lang.model.element.Modifier;
jjg@1455 34 import javax.lang.model.type.TypeMirror;
jjg@1455 35 import javax.lang.model.util.Elements;
jjg@1455 36 import javax.lang.model.util.Types;
jjg@1455 37
jjg@1455 38 import com.sun.source.doctree.DocCommentTree;
jjg@1455 39 import com.sun.source.util.DocTrees;
jjg@1455 40 import com.sun.source.util.JavacTask;
jjg@1455 41 import com.sun.source.util.SourcePositions;
jjg@1455 42 import com.sun.source.util.TreePath;
jjg@1455 43 import com.sun.tools.javac.model.JavacTypes;
jjg@1455 44 import com.sun.tools.javac.tree.JCTree;
jjg@1455 45
jjg@1455 46 /**
jjg@1455 47 * Utility container for current execution environment,
jjg@1455 48 * providing the current declaration and its doc comment.
jjg@1455 49 *
jjg@1455 50 * <p><b>This is NOT part of any supported API.
jjg@1455 51 * If you write code that depends on this, you do so at your own
jjg@1455 52 * risk. This code and its internal interfaces are subject to change
jjg@1455 53 * or deletion without notice.</b></p>
jjg@1455 54 */
jjg@1455 55 public class Env {
jjg@1455 56 /**
jjg@1455 57 * Access kinds for declarations.
jjg@1455 58 */
jjg@1455 59 public enum AccessKind {
jjg@1455 60 PRIVATE,
jjg@1455 61 PACKAGE,
jjg@1455 62 PROTECTED,
jjg@1455 63 PUBLIC;
jjg@1455 64
jjg@1455 65 static boolean accepts(String opt) {
jjg@1455 66 for (AccessKind g: values())
jjg@1455 67 if (opt.equals(g.name().toLowerCase())) return true;
jjg@1455 68 return false;
jjg@1455 69 }
jjg@1455 70
jjg@1455 71 static AccessKind of(Set<Modifier> mods) {
jjg@1455 72 if (mods.contains(Modifier.PUBLIC))
jjg@1455 73 return AccessKind.PUBLIC;
jjg@1455 74 else if (mods.contains(Modifier.PROTECTED))
jjg@1455 75 return AccessKind.PROTECTED;
jjg@1455 76 else if (mods.contains(Modifier.PRIVATE))
jjg@1455 77 return AccessKind.PRIVATE;
jjg@1455 78 else
jjg@1455 79 return AccessKind.PACKAGE;
jjg@1455 80 }
jjg@1455 81 };
jjg@1455 82
jjg@1455 83 /** Message handler. */
jjg@1455 84 final Messages messages;
jjg@1455 85
jjg@1668 86 int implicitHeaderLevel = 0;
jjg@1668 87
jjg@1455 88 // Utility classes
jjg@1455 89 DocTrees trees;
jjg@1455 90 Elements elements;
jjg@1455 91 Types types;
jjg@1455 92
jjg@1455 93 // Types used when analysing doc comments.
jjg@1455 94 TypeMirror java_lang_Error;
jjg@1455 95 TypeMirror java_lang_RuntimeException;
jjg@1455 96 TypeMirror java_lang_Throwable;
jjg@1455 97 TypeMirror java_lang_Void;
jjg@1455 98
jjg@1455 99 /** The path for the declaration containing the comment currently being analyzed. */
jjg@1455 100 TreePath currPath;
jjg@1455 101 /** The element for the declaration containing the comment currently being analyzed. */
jjg@1455 102 Element currElement;
jjg@1455 103 /** The comment current being analyzed. */
jjg@1455 104 DocCommentTree currDocComment;
jjg@1455 105 /**
jjg@1455 106 * The access kind of the declaration containing the comment currently being analyzed.
jjg@1668 107 * This is the minimum (most restrictive) access kind of the declaration itself
jjg@1455 108 * and that of its containers. For example, a public method in a private class is
jjg@1455 109 * noted as private.
jjg@1455 110 */
jjg@1455 111 AccessKind currAccess;
jjg@1455 112 /** The set of methods, if any, that the current declaration overrides. */
jjg@1455 113 Set<? extends ExecutableElement> currOverriddenMethods;
jjg@1455 114
jjg@1455 115 Env() {
jjg@1455 116 messages = new Messages(this);
jjg@1455 117 }
jjg@1455 118
jjg@1455 119 void init(JavacTask task) {
jjg@1455 120 init(DocTrees.instance(task), task.getElements(), task.getTypes());
jjg@1455 121 }
jjg@1455 122
jjg@1455 123 void init(DocTrees trees, Elements elements, Types types) {
jjg@1455 124 this.trees = trees;
jjg@1455 125 this.elements = elements;
jjg@1455 126 this.types = types;
jjg@1455 127 java_lang_Error = elements.getTypeElement("java.lang.Error").asType();
jjg@1455 128 java_lang_RuntimeException = elements.getTypeElement("java.lang.RuntimeException").asType();
jjg@1455 129 java_lang_Throwable = elements.getTypeElement("java.lang.Throwable").asType();
jjg@1455 130 java_lang_Void = elements.getTypeElement("java.lang.Void").asType();
jjg@1455 131 }
jjg@1455 132
jjg@1668 133 void setImplicitHeaders(int n) {
jjg@1668 134 implicitHeaderLevel = n;
jjg@1668 135 }
jjg@1668 136
jjg@1455 137 /** Set the current declaration and its doc comment. */
jjg@1455 138 void setCurrent(TreePath path, DocCommentTree comment) {
jjg@1455 139 currPath = path;
jjg@1455 140 currDocComment = comment;
jjg@1455 141 currElement = trees.getElement(currPath);
jjg@1455 142 currOverriddenMethods = ((JavacTypes) types).getOverriddenMethods(currElement);
jjg@1455 143
jjg@1455 144 AccessKind ak = null;
jjg@1455 145 for (TreePath p = path; p != null; p = p.getParentPath()) {
jjg@1455 146 Element e = trees.getElement(p);
jjg@1455 147 if (e != null) {
jjg@1455 148 ak = min(ak, AccessKind.of(e.getModifiers()));
jjg@1455 149 }
jjg@1455 150 }
jjg@1455 151 currAccess = ak;
jjg@1455 152 }
jjg@1455 153
jjg@1455 154 AccessKind getAccessKind() {
jjg@1455 155 return currAccess;
jjg@1455 156 }
jjg@1455 157
jjg@1455 158 long getPos(TreePath p) {
jjg@1455 159 return ((JCTree) p.getLeaf()).pos;
jjg@1455 160 }
jjg@1455 161
jjg@1455 162 long getStartPos(TreePath p) {
jjg@1455 163 SourcePositions sp = trees.getSourcePositions();
jjg@1455 164 return sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
jjg@1455 165 }
jjg@1455 166
jjg@1455 167 private <T extends Comparable<T>> T min(T item1, T item2) {
jjg@1455 168 return (item1 == null) ? item2
jjg@1455 169 : (item2 == null) ? item1
jjg@1455 170 : item1.compareTo(item2) <= 0 ? item1 : item2;
jjg@1455 171 }
jjg@1455 172 }

mercurial