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

Tue, 17 Dec 2013 10:55:59 +0100

author
jlahoda
date
Tue, 17 Dec 2013 10:55:59 +0100
changeset 2413
fe033d997ddf
parent 2169
667843bd2193
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029800: Flags.java uses String.toLowerCase without specifying Locale
Summary: Introducing StringUtils.toLowerCase/toUpperCase independent on the default locale, converting almost all usages of String.toLowerCase/toUpperCase to use the new methods.
Reviewed-by: jjg, bpatel

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

mercurial