Thu, 04 Oct 2012 13:04:53 +0100
7177387: Add target-typing support in method context
Summary: Add support for deferred types and speculative attribution
Reviewed-by: jjg, dlsmith
1 /*
2 * Copyright (c) 2003, 2011, 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;
29 import com.sun.javadoc.*;
31 import com.sun.tools.javac.code.Kinds;
32 import com.sun.tools.javac.code.Symbol;
33 import com.sun.tools.javac.code.Symbol.ClassSymbol;
34 import com.sun.tools.javac.code.Symbol.MethodSymbol;
35 import com.sun.tools.javac.code.Type;
36 import com.sun.tools.javac.code.Type.TypeVar;
37 import com.sun.tools.javac.util.List;
38 import com.sun.tools.javac.util.Name;
39 import com.sun.tools.javac.util.Names;
41 /**
42 * Implementation of <code>TypeVariable</code>, which
43 * represents a type variable.
44 *
45 * @author Scott Seligman
46 * @since 1.5
47 */
48 public class TypeVariableImpl extends AbstractTypeImpl implements TypeVariable {
50 TypeVariableImpl(DocEnv env, TypeVar type) {
51 super(env, type);
52 }
54 /**
55 * Return the bounds of this type variable.
56 */
57 public com.sun.javadoc.Type[] bounds() {
58 return TypeMaker.getTypes(env, getBounds((TypeVar)type, env));
59 }
61 /**
62 * Return the class, interface, method, or constructor within
63 * which this type variable is declared.
64 */
65 public ProgramElementDoc owner() {
66 Symbol osym = type.tsym.owner;
67 if ((osym.kind & Kinds.TYP) != 0) {
68 return env.getClassDoc((ClassSymbol)osym);
69 }
70 Names names = osym.name.table.names;
71 if (osym.name == names.init) {
72 return env.getConstructorDoc((MethodSymbol)osym);
73 } else {
74 return env.getMethodDoc((MethodSymbol)osym);
75 }
76 }
78 /**
79 * Return the ClassDoc of the erasure of this type variable.
80 */
81 @Override
82 public ClassDoc asClassDoc() {
83 return env.getClassDoc((ClassSymbol)env.types.erasure(type).tsym);
84 }
86 @Override
87 public TypeVariable asTypeVariable() {
88 return this;
89 }
91 @Override
92 public String toString() {
93 return typeVarToString(env, (TypeVar)type, true);
94 }
97 /**
98 * Return the string form of a type variable along with any
99 * "extends" clause. Class names are qualified if "full" is true.
100 */
101 static String typeVarToString(DocEnv env, TypeVar v, boolean full) {
102 StringBuilder s = new StringBuilder(v.toString());
103 List<Type> bounds = getBounds(v, env);
104 if (bounds.nonEmpty()) {
105 boolean first = true;
106 for (Type b : bounds) {
107 s.append(first ? " extends " : " & ");
108 s.append(TypeMaker.getTypeString(env, b, full));
109 first = false;
110 }
111 }
112 return s.toString();
113 }
115 /**
116 * Get the bounds of a type variable as listed in the "extends" clause.
117 */
118 private static List<Type> getBounds(TypeVar v, DocEnv env) {
119 Name boundname = v.getUpperBound().tsym.getQualifiedName();
120 if (boundname == boundname.table.names.java_lang_Object) {
121 return List.nil();
122 } else {
123 return env.types.getBounds(v);
124 }
125 }
126 }