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