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