Fri, 30 Nov 2012 15:14:48 +0000
8002099: Add support for intersection types in cast expression
Summary: Add parser and type-checking support for intersection types in cast expressions
Reviewed-by: jjg
1 /*
2 * Copyright (c) 1997, 2012, 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.javadoc;
28 import java.lang.reflect.Modifier;
29 import java.text.CollationKey;
31 import com.sun.javadoc.*;
32 import com.sun.tools.javac.code.Attribute;
33 import com.sun.tools.javac.code.Symbol;
34 import com.sun.tools.javac.code.Symbol.ClassSymbol;
35 import com.sun.tools.javac.tree.JCTree;
36 import com.sun.tools.javac.util.Position;
38 /**
39 * Represents a java program element: class, interface, field,
40 * constructor, or method.
41 * This is an abstract class dealing with information common to
42 * these elements.
43 *
44 * <p><b>This is NOT part of any supported API.
45 * If you write code that depends on this, you do so at your own risk.
46 * This code and its internal interfaces are subject to change or
47 * deletion without notice.</b>
48 *
49 * @see MemberDocImpl
50 * @see ClassDocImpl
51 *
52 * @author Robert Field
53 * @author Neal Gafter (rewrite)
54 * @author Scott Seligman (generics, enums, annotations)
55 */
56 public abstract class ProgramElementDocImpl
57 extends DocImpl implements ProgramElementDoc {
59 private final Symbol sym;
61 // For source position information.
62 JCTree tree = null;
63 Position.LineMap lineMap = null;
66 // Cache for getModifiers().
67 private int modifiers = -1;
69 protected ProgramElementDocImpl(DocEnv env, Symbol sym,
70 String doc, JCTree tree, Position.LineMap lineMap) {
71 super(env, doc);
72 this.sym = sym;
73 this.tree = tree;
74 this.lineMap = lineMap;
75 }
77 void setTree(JCTree tree) {
78 this.tree = tree;
79 }
81 /**
82 * Subclasses override to identify the containing class
83 */
84 protected abstract ClassSymbol getContainingClass();
86 /**
87 * Returns the flags in terms of javac's flags
88 */
89 abstract protected long getFlags();
91 /**
92 * Returns the modifier flags in terms of java.lang.reflect.Modifier.
93 */
94 protected int getModifiers() {
95 if (modifiers == -1) {
96 modifiers = DocEnv.translateModifiers(getFlags());
97 }
98 return modifiers;
99 }
101 /**
102 * Get the containing class of this program element.
103 *
104 * @return a ClassDocImpl for this element's containing class.
105 * If this is a class with no outer class, return null.
106 */
107 public ClassDoc containingClass() {
108 if (getContainingClass() == null) {
109 return null;
110 }
111 return env.getClassDoc(getContainingClass());
112 }
114 /**
115 * Return the package that this member is contained in.
116 * Return "" if in unnamed package.
117 */
118 public PackageDoc containingPackage() {
119 return env.getPackageDoc(getContainingClass().packge());
120 }
122 /**
123 * Get the modifier specifier integer.
124 *
125 * @see java.lang.reflect.Modifier
126 */
127 public int modifierSpecifier() {
128 int modifiers = getModifiers();
129 if (isMethod() && containingClass().isInterface())
130 // Remove the implicit abstract modifier.
131 return modifiers & ~Modifier.ABSTRACT;
132 return modifiers;
133 }
135 /**
136 * Get modifiers string.
137 * <pre>
138 * Example, for:
139 * public abstract int foo() { ... }
140 * modifiers() would return:
141 * 'public abstract'
142 * </pre>
143 * Annotations are not included.
144 */
145 public String modifiers() {
146 int modifiers = getModifiers();
147 if (isAnnotationTypeElement() ||
148 (isMethod() && containingClass().isInterface())) {
149 // Remove the implicit abstract modifier.
150 return Modifier.toString(modifiers & ~Modifier.ABSTRACT);
151 } else {
152 return Modifier.toString(modifiers);
153 }
154 }
156 /**
157 * Get the annotations of this program element.
158 * Return an empty array if there are none.
159 */
160 public AnnotationDesc[] annotations() {
161 AnnotationDesc res[] = new AnnotationDesc[sym.getAnnotationMirrors().length()];
162 int i = 0;
163 for (Attribute.Compound a : sym.getAnnotationMirrors()) {
164 res[i++] = new AnnotationDescImpl(env, a);
165 }
166 return res;
167 }
169 /**
170 * Return true if this program element is public
171 */
172 public boolean isPublic() {
173 int modifiers = getModifiers();
174 return Modifier.isPublic(modifiers);
175 }
177 /**
178 * Return true if this program element is protected
179 */
180 public boolean isProtected() {
181 int modifiers = getModifiers();
182 return Modifier.isProtected(modifiers);
183 }
185 /**
186 * Return true if this program element is private
187 */
188 public boolean isPrivate() {
189 int modifiers = getModifiers();
190 return Modifier.isPrivate(modifiers);
191 }
193 /**
194 * Return true if this program element is package private
195 */
196 public boolean isPackagePrivate() {
197 return !(isPublic() || isPrivate() || isProtected());
198 }
200 /**
201 * Return true if this program element is static
202 */
203 public boolean isStatic() {
204 int modifiers = getModifiers();
205 return Modifier.isStatic(modifiers);
206 }
208 /**
209 * Return true if this program element is final
210 */
211 public boolean isFinal() {
212 int modifiers = getModifiers();
213 return Modifier.isFinal(modifiers);
214 }
216 /**
217 * Generate a key for sorting.
218 */
219 CollationKey generateKey() {
220 String k = name();
221 // System.out.println("COLLATION KEY FOR " + this + " is \"" + k + "\"");
222 return env.doclocale.collator.getCollationKey(k);
223 }
225 }