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

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1455
75ab654b5cd5
child 1668
991f11e13598
permissions
-rw-r--r--

Merge

jjg@1455 1 /*
jjg@1455 2 * Copyright (c) 2012, 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@1455 86 // Utility classes
jjg@1455 87 DocTrees trees;
jjg@1455 88 Elements elements;
jjg@1455 89 Types types;
jjg@1455 90
jjg@1455 91 // Types used when analysing doc comments.
jjg@1455 92 TypeMirror java_lang_Error;
jjg@1455 93 TypeMirror java_lang_RuntimeException;
jjg@1455 94 TypeMirror java_lang_Throwable;
jjg@1455 95 TypeMirror java_lang_Void;
jjg@1455 96
jjg@1455 97 /** The path for the declaration containing the comment currently being analyzed. */
jjg@1455 98 TreePath currPath;
jjg@1455 99 /** The element for the declaration containing the comment currently being analyzed. */
jjg@1455 100 Element currElement;
jjg@1455 101 /** The comment current being analyzed. */
jjg@1455 102 DocCommentTree currDocComment;
jjg@1455 103 /**
jjg@1455 104 * The access kind of the declaration containing the comment currently being analyzed.
jjg@1455 105 * This is the minimum (most restrictive) access kind of the declaration iteself
jjg@1455 106 * and that of its containers. For example, a public method in a private class is
jjg@1455 107 * noted as private.
jjg@1455 108 */
jjg@1455 109 AccessKind currAccess;
jjg@1455 110 /** The set of methods, if any, that the current declaration overrides. */
jjg@1455 111 Set<? extends ExecutableElement> currOverriddenMethods;
jjg@1455 112
jjg@1455 113 Env() {
jjg@1455 114 messages = new Messages(this);
jjg@1455 115 }
jjg@1455 116
jjg@1455 117 void init(JavacTask task) {
jjg@1455 118 init(DocTrees.instance(task), task.getElements(), task.getTypes());
jjg@1455 119 }
jjg@1455 120
jjg@1455 121 void init(DocTrees trees, Elements elements, Types types) {
jjg@1455 122 this.trees = trees;
jjg@1455 123 this.elements = elements;
jjg@1455 124 this.types = types;
jjg@1455 125 java_lang_Error = elements.getTypeElement("java.lang.Error").asType();
jjg@1455 126 java_lang_RuntimeException = elements.getTypeElement("java.lang.RuntimeException").asType();
jjg@1455 127 java_lang_Throwable = elements.getTypeElement("java.lang.Throwable").asType();
jjg@1455 128 java_lang_Void = elements.getTypeElement("java.lang.Void").asType();
jjg@1455 129 }
jjg@1455 130
jjg@1455 131 /** Set the current declaration and its doc comment. */
jjg@1455 132 void setCurrent(TreePath path, DocCommentTree comment) {
jjg@1455 133 currPath = path;
jjg@1455 134 currDocComment = comment;
jjg@1455 135 currElement = trees.getElement(currPath);
jjg@1455 136 currOverriddenMethods = ((JavacTypes) types).getOverriddenMethods(currElement);
jjg@1455 137
jjg@1455 138 AccessKind ak = null;
jjg@1455 139 for (TreePath p = path; p != null; p = p.getParentPath()) {
jjg@1455 140 Element e = trees.getElement(p);
jjg@1455 141 if (e != null) {
jjg@1455 142 ak = min(ak, AccessKind.of(e.getModifiers()));
jjg@1455 143 }
jjg@1455 144 }
jjg@1455 145 currAccess = ak;
jjg@1455 146 }
jjg@1455 147
jjg@1455 148 AccessKind getAccessKind() {
jjg@1455 149 return currAccess;
jjg@1455 150 }
jjg@1455 151
jjg@1455 152 long getPos(TreePath p) {
jjg@1455 153 return ((JCTree) p.getLeaf()).pos;
jjg@1455 154 }
jjg@1455 155
jjg@1455 156 long getStartPos(TreePath p) {
jjg@1455 157 SourcePositions sp = trees.getSourcePositions();
jjg@1455 158 return sp.getStartPosition(p.getCompilationUnit(), p.getLeaf());
jjg@1455 159 }
jjg@1455 160
jjg@1455 161 private <T extends Comparable<T>> T min(T item1, T item2) {
jjg@1455 162 return (item1 == null) ? item2
jjg@1455 163 : (item2 == null) ? item1
jjg@1455 164 : item1.compareTo(item2) <= 0 ? item1 : item2;
jjg@1455 165 }
jjg@1455 166 }

mercurial