src/share/classes/com/sun/tools/javac/model/JavacElements.java

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1559
01af1b5c631d
child 1645
97f6839673d6
permissions
-rw-r--r--

Merge

duke@1 1 /*
jfranck@1491 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.model;
duke@1 27
duke@1 28 import java.lang.annotation.Annotation;
duke@1 29 import java.lang.annotation.Inherited;
jfranck@1491 30 import java.lang.reflect.InvocationTargetException;
jfranck@1491 31 import java.lang.reflect.Method;
duke@1 32 import java.util.Map;
jjg@1280 33
duke@1 34 import javax.lang.model.SourceVersion;
duke@1 35 import javax.lang.model.element.*;
duke@1 36 import javax.lang.model.type.DeclaredType;
duke@1 37 import javax.lang.model.util.Elements;
duke@1 38 import javax.tools.JavaFileObject;
jjg@1280 39 import static javax.lang.model.util.ElementFilter.methodsIn;
jjg@1280 40
duke@1 41 import com.sun.tools.javac.code.*;
duke@1 42 import com.sun.tools.javac.code.Symbol.*;
jjg@1374 43 import com.sun.tools.javac.code.TypeTag;
duke@1 44 import com.sun.tools.javac.comp.AttrContext;
duke@1 45 import com.sun.tools.javac.comp.Enter;
duke@1 46 import com.sun.tools.javac.comp.Env;
duke@1 47 import com.sun.tools.javac.main.JavaCompiler;
duke@1 48 import com.sun.tools.javac.processing.PrintingProcessor;
duke@1 49 import com.sun.tools.javac.tree.JCTree;
duke@1 50 import com.sun.tools.javac.tree.JCTree.*;
duke@1 51 import com.sun.tools.javac.tree.TreeInfo;
duke@1 52 import com.sun.tools.javac.tree.TreeScanner;
jjg@113 53 import com.sun.tools.javac.util.*;
duke@1 54 import com.sun.tools.javac.util.Name;
jjg@1374 55 import static com.sun.tools.javac.code.TypeTag.CLASS;
jjg@1127 56 import static com.sun.tools.javac.tree.JCTree.Tag.*;
duke@1 57
duke@1 58 /**
duke@1 59 * Utility methods for operating on program elements.
duke@1 60 *
jjg@581 61 * <p><b>This is NOT part of any supported API.
duke@1 62 * If you write code that depends on this, you do so at your own
duke@1 63 * risk. This code and its internal interfaces are subject to change
duke@1 64 * or deletion without notice.</b></p>
duke@1 65 */
duke@1 66 public class JavacElements implements Elements {
duke@1 67
duke@1 68 private JavaCompiler javaCompiler;
duke@1 69 private Symtab syms;
jjg@113 70 private Names names;
duke@1 71 private Types types;
duke@1 72 private Enter enter;
duke@1 73
duke@1 74 public static JavacElements instance(Context context) {
jjg@706 75 JavacElements instance = context.get(JavacElements.class);
jjg@706 76 if (instance == null)
duke@1 77 instance = new JavacElements(context);
duke@1 78 return instance;
duke@1 79 }
duke@1 80
duke@1 81 /**
duke@1 82 * Public for use only by JavacProcessingEnvironment
duke@1 83 */
jjg@706 84 protected JavacElements(Context context) {
duke@1 85 setContext(context);
duke@1 86 }
duke@1 87
duke@1 88 /**
duke@1 89 * Use a new context. May be called from outside to update
duke@1 90 * internal state for a new annotation-processing round.
duke@1 91 */
duke@1 92 public void setContext(Context context) {
jjg@706 93 context.put(JavacElements.class, this);
duke@1 94 javaCompiler = JavaCompiler.instance(context);
duke@1 95 syms = Symtab.instance(context);
jjg@113 96 names = Names.instance(context);
duke@1 97 types = Types.instance(context);
duke@1 98 enter = Enter.instance(context);
duke@1 99 }
duke@1 100
duke@1 101 /**
jfranck@1491 102 * An internal-use utility that creates a runtime view of an
jfranck@1491 103 * annotation. This is the implementation of
jfranck@1491 104 * Element.getAnnotation(Class).
duke@1 105 */
duke@1 106 public static <A extends Annotation> A getAnnotation(Symbol annotated,
duke@1 107 Class<A> annoType) {
duke@1 108 if (!annoType.isAnnotation())
duke@1 109 throw new IllegalArgumentException("Not an annotation type: "
duke@1 110 + annoType);
jfranck@1491 111 Attribute.Compound c;
jfranck@1491 112 if (annotated.kind == Kinds.TYP && annotated instanceof ClassSymbol) {
jfranck@1491 113 c = getAttributeOnClass((ClassSymbol)annotated, annoType);
jfranck@1491 114 } else {
jfranck@1491 115 c = getAttribute(annotated, annoType);
jfranck@1491 116 }
jfranck@1491 117 return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
jfranck@1491 118 }
jfranck@1491 119
jfranck@1491 120 // Helper to getAnnotation[s]
jfranck@1491 121 private static <A extends Annotation> Attribute.Compound getAttribute(Symbol annotated,
jfranck@1491 122 Class<A> annoType) {
duke@1 123 String name = annoType.getName();
jfranck@1491 124
jfranck@1491 125 for (Attribute.Compound anno : annotated.getRawAttributes())
duke@1 126 if (name.equals(anno.type.tsym.flatName().toString()))
jfranck@1491 127 return anno;
jfranck@1491 128
duke@1 129 return null;
duke@1 130 }
jfranck@1491 131 // Helper to getAnnotation[s]
jfranck@1491 132 private static <A extends Annotation> Attribute.Compound getAttributeOnClass(ClassSymbol annotated,
jfranck@1491 133 Class<A> annoType) {
duke@1 134 boolean inherited = annoType.isAnnotationPresent(Inherited.class);
jfranck@1491 135 Attribute.Compound result = null;
jjg@113 136 while (annotated.name != annotated.name.table.names.java_lang_Object) {
jfranck@1491 137 result = getAttribute(annotated, annoType);
duke@1 138 if (result != null || !inherited)
duke@1 139 break;
duke@1 140 Type sup = annotated.getSuperclass();
jjg@1374 141 if (!sup.hasTag(CLASS) || sup.isErroneous())
duke@1 142 break;
duke@1 143 annotated = (ClassSymbol) sup.tsym;
duke@1 144 }
duke@1 145 return result;
duke@1 146 }
duke@1 147
jfranck@1491 148 /**
jfranck@1491 149 * An internal-use utility that creates a runtime view of
jfranck@1491 150 * annotations. This is the implementation of
jfranck@1491 151 * Element.getAnnotations(Class).
jfranck@1491 152 */
jfranck@1491 153 public static <A extends Annotation> A[] getAnnotations(Symbol annotated,
jfranck@1491 154 Class<A> annoType) {
jfranck@1491 155 if (!annoType.isAnnotation())
jfranck@1491 156 throw new IllegalArgumentException("Not an annotation type: "
jfranck@1491 157 + annoType);
jfranck@1491 158 // If annoType does not declare a container this is equivalent to wrapping
jfranck@1491 159 // getAnnotation(...) in an array.
jfranck@1491 160 Class <? extends Annotation> containerType = getContainer(annoType);
jfranck@1491 161 if (containerType == null) {
jfranck@1491 162 A res = getAnnotation(annotated, annoType);
jfranck@1491 163 int size;
jfranck@1491 164 if (res == null) {
jfranck@1491 165 size = 0;
jfranck@1491 166 } else {
jfranck@1491 167 size = 1;
jfranck@1491 168 }
jfranck@1491 169 @SuppressWarnings("unchecked") // annoType is the Class for A
jfranck@1491 170 A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
jfranck@1491 171 if (res != null)
jfranck@1491 172 arr[0] = res;
jfranck@1491 173 return arr;
jfranck@1491 174 }
jfranck@1491 175
jfranck@1491 176 // So we have a containing type
jfranck@1491 177 String name = annoType.getName();
jfranck@1491 178 String annoTypeName = annoType.getSimpleName();
jfranck@1491 179 String containerTypeName = containerType.getSimpleName();
jfranck@1491 180 int directIndex = -1, containerIndex = -1;
jfranck@1491 181 Attribute.Compound direct = null, container = null;
jfranck@1491 182 Attribute.Compound[] rawAttributes = annotated.getRawAttributes().toArray(new Attribute.Compound[0]);
jfranck@1491 183
jfranck@1491 184 // Find directly present annotations
jfranck@1491 185 for (int i = 0; i < rawAttributes.length; i++) {
jfranck@1491 186 if (annoTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) {
jfranck@1491 187 directIndex = i;
jfranck@1491 188 direct = rawAttributes[i];
jfranck@1491 189 } else if(containerTypeName != null &&
jfranck@1491 190 containerTypeName.equals(rawAttributes[i].type.tsym.flatName().toString())) {
jfranck@1491 191 containerIndex = i;
jfranck@1491 192 container = rawAttributes[i];
jfranck@1491 193 }
jfranck@1491 194 }
jfranck@1491 195 // Deal with inherited annotations
jfranck@1491 196 if (annotated.kind == Kinds.TYP &&
jfranck@1491 197 (annotated instanceof ClassSymbol)) {
jfranck@1491 198 ClassSymbol s = (ClassSymbol)annotated;
jfranck@1491 199 if (direct == null && container == null) {
jfranck@1491 200 direct = getAttributeOnClass(s, annoType);
jfranck@1491 201 container = getAttributeOnClass(s, containerType);
jfranck@1491 202
jfranck@1491 203 // both are inherited and found, put container last
jfranck@1491 204 if (direct != null && container != null) {
jfranck@1491 205 directIndex = 0;
jfranck@1491 206 containerIndex = 1;
jfranck@1491 207 } else if (direct != null) {
jfranck@1491 208 directIndex = 0;
jfranck@1491 209 } else {
jfranck@1491 210 containerIndex = 0;
jfranck@1491 211 }
jfranck@1491 212 } else if (direct == null) {
jfranck@1491 213 direct = getAttributeOnClass(s, annoType);
jfranck@1491 214 if (direct != null)
jfranck@1491 215 directIndex = containerIndex + 1;
jfranck@1491 216 } else if (container == null) {
jfranck@1491 217 container = getAttributeOnClass(s, containerType);
jfranck@1491 218 if (container != null)
jfranck@1491 219 containerIndex = directIndex + 1;
jfranck@1491 220 }
jfranck@1491 221 }
jfranck@1491 222
jfranck@1491 223 // Pack them in an array
jfranck@1491 224 Attribute[] contained0 = new Attribute[0];
jfranck@1491 225 if (container != null)
jfranck@1491 226 contained0 = unpackAttributes(container);
jfranck@1491 227 ListBuffer<Attribute.Compound> compounds = ListBuffer.lb();
jfranck@1491 228 for (Attribute a : contained0)
jfranck@1491 229 if (a instanceof Attribute.Compound)
jfranck@1491 230 compounds = compounds.append((Attribute.Compound)a);
jfranck@1491 231 Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[0]);
jfranck@1491 232
jfranck@1491 233 int size = (direct == null ? 0 : 1) + contained.length;
jfranck@1491 234 @SuppressWarnings("unchecked") // annoType is the Class for A
jfranck@1491 235 A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
jfranck@1491 236
jfranck@1491 237 // if direct && container, which is first?
jfranck@1491 238 int insert = -1;
jfranck@1491 239 int length = arr.length;
jfranck@1491 240 if (directIndex >= 0 && containerIndex >= 0) {
jfranck@1491 241 if (directIndex < containerIndex) {
jfranck@1491 242 arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
jfranck@1491 243 insert = 1;
jfranck@1491 244 } else {
jfranck@1491 245 arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
jfranck@1491 246 insert = 0;
jfranck@1491 247 length--;
jfranck@1491 248 }
jfranck@1491 249 } else if (directIndex >= 0) {
jfranck@1491 250 arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
jfranck@1491 251 return arr;
jfranck@1491 252 } else {
jfranck@1491 253 // Only container
jfranck@1491 254 insert = 0;
jfranck@1491 255 }
jfranck@1491 256
jfranck@1491 257 for (int i = 0; i + insert < length; i++)
jfranck@1491 258 arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
jfranck@1491 259
jfranck@1491 260 return arr;
jfranck@1491 261 }
jfranck@1491 262
jfranck@1491 263 // Needed to unpack the runtime view of containing annotations
jjg@1492 264 private static final Class<? extends Annotation> REPEATABLE_CLASS = initRepeatable();
jfranck@1491 265 private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod();
jfranck@1491 266
jjg@1492 267 private static Class<? extends Annotation> initRepeatable() {
jfranck@1491 268 try {
darcy@1501 269 // Repeatable will not be available when bootstrapping on
darcy@1501 270 // JDK 7 so use a reflective lookup instead of a class
darcy@1501 271 // literal for Repeatable.class.
darcy@1501 272 return Class.forName("java.lang.annotation.Repeatable").asSubclass(Annotation.class);
jfranck@1491 273 } catch (ClassNotFoundException e) {
jfranck@1491 274 return null;
jfranck@1491 275 } catch (SecurityException e) {
jfranck@1491 276 return null;
jfranck@1491 277 }
jfranck@1491 278 }
jfranck@1491 279 private static Method initValueElementMethod() {
jjg@1492 280 if (REPEATABLE_CLASS == null)
jfranck@1491 281 return null;
jfranck@1491 282
jfranck@1491 283 Method m = null;
jfranck@1491 284 try {
jjg@1492 285 m = REPEATABLE_CLASS.getMethod("value");
jfranck@1491 286 if (m != null)
jfranck@1491 287 m.setAccessible(true);
jfranck@1491 288 return m;
jfranck@1491 289 } catch (NoSuchMethodException e) {
jfranck@1491 290 return null;
jfranck@1491 291 }
jfranck@1491 292 }
jfranck@1491 293
jfranck@1491 294 // Helper to getAnnotations
jfranck@1491 295 private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
jjg@1492 296 // Since we can not refer to java.lang.annotation.Repeatable until we are
jjg@1492 297 // bootstrapping with java 8 we need to get the Repeatable annotation using
jfranck@1491 298 // reflective invocations instead of just using its type and element method.
jjg@1492 299 if (REPEATABLE_CLASS != null &&
jfranck@1491 300 VALUE_ELEMENT_METHOD != null) {
jjg@1492 301 // Get the Repeatable instance on the annotations declaration
jjg@1492 302 Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS);
jjg@1492 303 if (repeatable != null) {
jfranck@1491 304 try {
jfranck@1491 305 // Get the value element, it should be a class
jfranck@1491 306 // indicating the containing annotation type
jfranck@1491 307 @SuppressWarnings("unchecked")
jjg@1492 308 Class<? extends Annotation> containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable);
jfranck@1491 309 if (containerType == null)
jfranck@1491 310 return null;
jfranck@1491 311
jfranck@1491 312 return containerType;
jfranck@1491 313 } catch (ClassCastException e) {
jfranck@1491 314 return null;
jfranck@1491 315 } catch (IllegalAccessException e) {
jfranck@1491 316 return null;
jfranck@1491 317 } catch (InvocationTargetException e ) {
jfranck@1491 318 return null;
jfranck@1491 319 }
jfranck@1491 320 }
jfranck@1491 321 }
jfranck@1491 322 return null;
jfranck@1491 323 }
jfranck@1491 324 // Helper to getAnnotations
jfranck@1491 325 private static Attribute[] unpackAttributes(Attribute.Compound container) {
jfranck@1491 326 // We now have an instance of the container,
jfranck@1491 327 // unpack it returning an instance of the
jfranck@1491 328 // contained type or null
jfranck@1491 329 return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values;
jfranck@1491 330 }
duke@1 331
duke@1 332 public PackageSymbol getPackageElement(CharSequence name) {
duke@1 333 String strName = name.toString();
duke@1 334 if (strName.equals(""))
duke@1 335 return syms.unnamedPackage;
duke@1 336 return SourceVersion.isName(strName)
duke@1 337 ? nameToSymbol(strName, PackageSymbol.class)
duke@1 338 : null;
duke@1 339 }
duke@1 340
duke@1 341 public ClassSymbol getTypeElement(CharSequence name) {
duke@1 342 String strName = name.toString();
duke@1 343 return SourceVersion.isName(strName)
duke@1 344 ? nameToSymbol(strName, ClassSymbol.class)
duke@1 345 : null;
duke@1 346 }
duke@1 347
duke@1 348 /**
duke@1 349 * Returns a symbol given the type's or packages's canonical name,
duke@1 350 * or null if the name isn't found.
duke@1 351 */
duke@1 352 private <S extends Symbol> S nameToSymbol(String nameStr, Class<S> clazz) {
duke@1 353 Name name = names.fromString(nameStr);
duke@1 354 // First check cache.
duke@1 355 Symbol sym = (clazz == ClassSymbol.class)
duke@1 356 ? syms.classes.get(name)
duke@1 357 : syms.packages.get(name);
duke@1 358
duke@1 359 try {
duke@1 360 if (sym == null)
duke@1 361 sym = javaCompiler.resolveIdent(nameStr);
duke@1 362
duke@1 363 sym.complete();
duke@1 364
duke@1 365 return (sym.kind != Kinds.ERR &&
duke@1 366 sym.exists() &&
duke@1 367 clazz.isInstance(sym) &&
duke@1 368 name.equals(sym.getQualifiedName()))
duke@1 369 ? clazz.cast(sym)
duke@1 370 : null;
duke@1 371 } catch (CompletionFailure e) {
duke@1 372 return null;
duke@1 373 }
duke@1 374 }
duke@1 375
duke@1 376 public JavacSourcePosition getSourcePosition(Element e) {
duke@1 377 Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
duke@1 378 if (treeTop == null)
duke@1 379 return null;
duke@1 380 JCTree tree = treeTop.fst;
duke@1 381 JCCompilationUnit toplevel = treeTop.snd;
duke@1 382 JavaFileObject sourcefile = toplevel.sourcefile;
duke@1 383 if (sourcefile == null)
duke@1 384 return null;
duke@1 385 return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap);
duke@1 386 }
duke@1 387
duke@1 388 public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a) {
duke@1 389 Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
duke@1 390 if (treeTop == null)
duke@1 391 return null;
duke@1 392 JCTree tree = treeTop.fst;
duke@1 393 JCCompilationUnit toplevel = treeTop.snd;
duke@1 394 JavaFileObject sourcefile = toplevel.sourcefile;
duke@1 395 if (sourcefile == null)
duke@1 396 return null;
duke@1 397
duke@1 398 JCTree annoTree = matchAnnoToTree(a, e, tree);
duke@1 399 if (annoTree == null)
duke@1 400 return null;
duke@1 401 return new JavacSourcePosition(sourcefile, annoTree.pos,
duke@1 402 toplevel.lineMap);
duke@1 403 }
duke@1 404
duke@1 405 public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a,
duke@1 406 AnnotationValue v) {
duke@1 407 // TODO: better accuracy in getSourcePosition(... AnnotationValue)
duke@1 408 return getSourcePosition(e, a);
duke@1 409 }
duke@1 410
duke@1 411 /**
duke@1 412 * Returns the tree for an annotation given the annotated element
duke@1 413 * and the element's own tree. Returns null if the tree cannot be found.
duke@1 414 */
duke@1 415 private JCTree matchAnnoToTree(AnnotationMirror findme,
duke@1 416 Element e, JCTree tree) {
duke@1 417 Symbol sym = cast(Symbol.class, e);
duke@1 418 class Vis extends JCTree.Visitor {
duke@1 419 List<JCAnnotation> result = null;
duke@1 420 public void visitTopLevel(JCCompilationUnit tree) {
duke@1 421 result = tree.packageAnnotations;
duke@1 422 }
duke@1 423 public void visitClassDef(JCClassDecl tree) {
duke@1 424 result = tree.mods.annotations;
duke@1 425 }
duke@1 426 public void visitMethodDef(JCMethodDecl tree) {
duke@1 427 result = tree.mods.annotations;
duke@1 428 }
duke@1 429 public void visitVarDef(JCVariableDecl tree) {
duke@1 430 result = tree.mods.annotations;
duke@1 431 }
duke@1 432 }
duke@1 433 Vis vis = new Vis();
duke@1 434 tree.accept(vis);
duke@1 435 if (vis.result == null)
duke@1 436 return null;
jfranck@1491 437
jfranck@1491 438 List<Attribute.Compound> annos = sym.getRawAttributes();
duke@1 439 return matchAnnoToTree(cast(Attribute.Compound.class, findme),
jfranck@1491 440 annos,
duke@1 441 vis.result);
duke@1 442 }
duke@1 443
duke@1 444 /**
duke@1 445 * Returns the tree for an annotation given a list of annotations
duke@1 446 * in which to search (recursively) and their corresponding trees.
duke@1 447 * Returns null if the tree cannot be found.
duke@1 448 */
duke@1 449 private JCTree matchAnnoToTree(Attribute.Compound findme,
duke@1 450 List<Attribute.Compound> annos,
duke@1 451 List<JCAnnotation> trees) {
duke@1 452 for (Attribute.Compound anno : annos) {
duke@1 453 for (JCAnnotation tree : trees) {
duke@1 454 JCTree match = matchAnnoToTree(findme, anno, tree);
duke@1 455 if (match != null)
duke@1 456 return match;
duke@1 457 }
duke@1 458 }
duke@1 459 return null;
duke@1 460 }
duke@1 461
duke@1 462 /**
duke@1 463 * Returns the tree for an annotation given an Attribute to
duke@1 464 * search (recursively) and its corresponding tree.
duke@1 465 * Returns null if the tree cannot be found.
duke@1 466 */
duke@1 467 private JCTree matchAnnoToTree(final Attribute.Compound findme,
duke@1 468 final Attribute attr,
duke@1 469 final JCTree tree) {
duke@1 470 if (attr == findme)
duke@1 471 return (tree.type.tsym == findme.type.tsym) ? tree : null;
duke@1 472
duke@1 473 class Vis implements Attribute.Visitor {
duke@1 474 JCTree result = null;
duke@1 475 public void visitConstant(Attribute.Constant value) {
duke@1 476 }
duke@1 477 public void visitClass(Attribute.Class clazz) {
duke@1 478 }
duke@1 479 public void visitCompound(Attribute.Compound anno) {
duke@1 480 for (Pair<MethodSymbol, Attribute> pair : anno.values) {
duke@1 481 JCExpression expr = scanForAssign(pair.fst, tree);
duke@1 482 if (expr != null) {
duke@1 483 JCTree match = matchAnnoToTree(findme, pair.snd, expr);
duke@1 484 if (match != null) {
duke@1 485 result = match;
duke@1 486 return;
duke@1 487 }
duke@1 488 }
duke@1 489 }
duke@1 490 }
duke@1 491 public void visitArray(Attribute.Array array) {
jjg@1127 492 if (tree.hasTag(NEWARRAY) &&
duke@1 493 types.elemtype(array.type).tsym == findme.type.tsym) {
duke@1 494 List<JCExpression> elems = ((JCNewArray) tree).elems;
duke@1 495 for (Attribute value : array.values) {
duke@1 496 if (value == findme) {
duke@1 497 result = elems.head;
duke@1 498 return;
duke@1 499 }
duke@1 500 elems = elems.tail;
duke@1 501 }
duke@1 502 }
duke@1 503 }
duke@1 504 public void visitEnum(Attribute.Enum e) {
duke@1 505 }
duke@1 506 public void visitError(Attribute.Error e) {
duke@1 507 }
duke@1 508 }
duke@1 509 Vis vis = new Vis();
duke@1 510 attr.accept(vis);
duke@1 511 return vis.result;
duke@1 512 }
duke@1 513
duke@1 514 /**
duke@1 515 * Scans for a JCAssign node with a LHS matching a given
duke@1 516 * symbol, and returns its RHS. Does not scan nested JCAnnotations.
duke@1 517 */
duke@1 518 private JCExpression scanForAssign(final MethodSymbol sym,
duke@1 519 final JCTree tree) {
duke@1 520 class TS extends TreeScanner {
duke@1 521 JCExpression result = null;
duke@1 522 public void scan(JCTree t) {
duke@1 523 if (t != null && result == null)
duke@1 524 t.accept(this);
duke@1 525 }
duke@1 526 public void visitAnnotation(JCAnnotation t) {
duke@1 527 if (t == tree)
duke@1 528 scan(t.args);
duke@1 529 }
duke@1 530 public void visitAssign(JCAssign t) {
jjg@1127 531 if (t.lhs.hasTag(IDENT)) {
duke@1 532 JCIdent ident = (JCIdent) t.lhs;
duke@1 533 if (ident.sym == sym)
duke@1 534 result = t.rhs;
duke@1 535 }
duke@1 536 }
duke@1 537 }
duke@1 538 TS scanner = new TS();
duke@1 539 tree.accept(scanner);
duke@1 540 return scanner.result;
duke@1 541 }
duke@1 542
duke@1 543 /**
duke@1 544 * Returns the tree node corresponding to this element, or null
duke@1 545 * if none can be found.
duke@1 546 */
duke@1 547 public JCTree getTree(Element e) {
duke@1 548 Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e);
duke@1 549 return (treeTop != null) ? treeTop.fst : null;
duke@1 550 }
duke@1 551
duke@1 552 public String getDocComment(Element e) {
duke@1 553 // Our doc comment is contained in a map in our toplevel,
duke@1 554 // indexed by our tree. Find our enter environment, which gives
duke@1 555 // us our toplevel. It also gives us a tree that contains our
duke@1 556 // tree: walk it to find our tree. This is painful.
duke@1 557 Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
duke@1 558 if (treeTop == null)
duke@1 559 return null;
duke@1 560 JCTree tree = treeTop.fst;
duke@1 561 JCCompilationUnit toplevel = treeTop.snd;
duke@1 562 if (toplevel.docComments == null)
duke@1 563 return null;
jjg@1280 564 return toplevel.docComments.getCommentText(tree);
duke@1 565 }
duke@1 566
duke@1 567 public PackageElement getPackageOf(Element e) {
duke@1 568 return cast(Symbol.class, e).packge();
duke@1 569 }
duke@1 570
duke@1 571 public boolean isDeprecated(Element e) {
duke@1 572 Symbol sym = cast(Symbol.class, e);
duke@1 573 return (sym.flags() & Flags.DEPRECATED) != 0;
duke@1 574 }
duke@1 575
duke@1 576 public Name getBinaryName(TypeElement type) {
duke@1 577 return cast(TypeSymbol.class, type).flatName();
duke@1 578 }
duke@1 579
duke@1 580 public Map<MethodSymbol, Attribute> getElementValuesWithDefaults(
duke@1 581 AnnotationMirror a) {
duke@1 582 Attribute.Compound anno = cast(Attribute.Compound.class, a);
duke@1 583 DeclaredType annotype = a.getAnnotationType();
duke@1 584 Map<MethodSymbol, Attribute> valmap = anno.getElementValues();
duke@1 585
duke@1 586 for (ExecutableElement ex :
duke@1 587 methodsIn(annotype.asElement().getEnclosedElements())) {
duke@1 588 MethodSymbol meth = (MethodSymbol) ex;
duke@1 589 Attribute defaultValue = meth.getDefaultValue();
duke@1 590 if (defaultValue != null && !valmap.containsKey(meth)) {
duke@1 591 valmap.put(meth, defaultValue);
duke@1 592 }
duke@1 593 }
duke@1 594 return valmap;
duke@1 595 }
duke@1 596
duke@1 597 /**
duke@1 598 * {@inheritDoc}
duke@1 599 */
duke@1 600 public FilteredMemberList getAllMembers(TypeElement element) {
duke@1 601 Symbol sym = cast(Symbol.class, element);
duke@1 602 Scope scope = sym.members().dupUnshared();
duke@1 603 List<Type> closure = types.closure(sym.asType());
duke@1 604 for (Type t : closure)
duke@1 605 addMembers(scope, t);
duke@1 606 return new FilteredMemberList(scope);
duke@1 607 }
duke@1 608 // where
duke@1 609 private void addMembers(Scope scope, Type type) {
duke@1 610 members:
duke@1 611 for (Scope.Entry e = type.asElement().members().elems; e != null; e = e.sibling) {
duke@1 612 Scope.Entry overrider = scope.lookup(e.sym.getSimpleName());
duke@1 613 while (overrider.scope != null) {
duke@1 614 if (overrider.sym.kind == e.sym.kind
duke@1 615 && (overrider.sym.flags() & Flags.SYNTHETIC) == 0)
duke@1 616 {
duke@1 617 if (overrider.sym.getKind() == ElementKind.METHOD
duke@1 618 && overrides((ExecutableElement)overrider.sym, (ExecutableElement)e.sym, (TypeElement)type.asElement())) {
duke@1 619 continue members;
duke@1 620 }
duke@1 621 }
duke@1 622 overrider = overrider.next();
duke@1 623 }
duke@1 624 boolean derived = e.sym.getEnclosingElement() != scope.owner;
duke@1 625 ElementKind kind = e.sym.getKind();
duke@1 626 boolean initializer = kind == ElementKind.CONSTRUCTOR
duke@1 627 || kind == ElementKind.INSTANCE_INIT
duke@1 628 || kind == ElementKind.STATIC_INIT;
duke@1 629 if (!derived || (!initializer && e.sym.isInheritedIn(scope.owner, types)))
duke@1 630 scope.enter(e.sym);
duke@1 631 }
duke@1 632 }
duke@1 633
duke@1 634 /**
duke@1 635 * Returns all annotations of an element, whether
duke@1 636 * inherited or directly present.
duke@1 637 *
duke@1 638 * @param e the element being examined
duke@1 639 * @return all annotations of the element
duke@1 640 */
duke@1 641 public List<Attribute.Compound> getAllAnnotationMirrors(Element e) {
duke@1 642 Symbol sym = cast(Symbol.class, e);
jfranck@1491 643 List<Attribute.Compound> annos = sym.getRawAttributes();
duke@1 644 while (sym.getKind() == ElementKind.CLASS) {
duke@1 645 Type sup = ((ClassSymbol) sym).getSuperclass();
jjg@1374 646 if (!sup.hasTag(CLASS) || sup.isErroneous() ||
duke@1 647 sup.tsym == syms.objectType.tsym) {
duke@1 648 break;
duke@1 649 }
duke@1 650 sym = sup.tsym;
duke@1 651 List<Attribute.Compound> oldAnnos = annos;
jfranck@1491 652 List<Attribute.Compound> newAnnos = sym.getRawAttributes();
jfranck@1491 653 for (Attribute.Compound anno : newAnnos) {
duke@1 654 if (isInherited(anno.type) &&
duke@1 655 !containsAnnoOfType(oldAnnos, anno.type)) {
duke@1 656 annos = annos.prepend(anno);
duke@1 657 }
duke@1 658 }
duke@1 659 }
duke@1 660 return annos;
duke@1 661 }
duke@1 662
duke@1 663 /**
duke@1 664 * Tests whether an annotation type is @Inherited.
duke@1 665 */
duke@1 666 private boolean isInherited(Type annotype) {
jfranck@1491 667 return annotype.tsym.attribute(syms.inheritedType.tsym) != null;
duke@1 668 }
duke@1 669
duke@1 670 /**
duke@1 671 * Tests whether a list of annotations contains an annotation
duke@1 672 * of a given type.
duke@1 673 */
duke@1 674 private static boolean containsAnnoOfType(List<Attribute.Compound> annos,
duke@1 675 Type type) {
duke@1 676 for (Attribute.Compound anno : annos) {
duke@1 677 if (anno.type.tsym == type.tsym)
duke@1 678 return true;
duke@1 679 }
duke@1 680 return false;
duke@1 681 }
duke@1 682
duke@1 683 public boolean hides(Element hiderEl, Element hideeEl) {
duke@1 684 Symbol hider = cast(Symbol.class, hiderEl);
duke@1 685 Symbol hidee = cast(Symbol.class, hideeEl);
duke@1 686
duke@1 687 // Fields only hide fields; methods only methods; types only types.
duke@1 688 // Names must match. Nothing hides itself (just try it).
duke@1 689 if (hider == hidee ||
duke@1 690 hider.kind != hidee.kind ||
duke@1 691 hider.name != hidee.name) {
duke@1 692 return false;
duke@1 693 }
duke@1 694
duke@1 695 // Only static methods can hide other methods.
duke@1 696 // Methods only hide methods with matching signatures.
duke@1 697 if (hider.kind == Kinds.MTH) {
duke@1 698 if (!hider.isStatic() ||
duke@1 699 !types.isSubSignature(hider.type, hidee.type)) {
duke@1 700 return false;
duke@1 701 }
duke@1 702 }
duke@1 703
duke@1 704 // Hider must be in a subclass of hidee's class.
duke@1 705 // Note that if M1 hides M2, and M2 hides M3, and M3 is accessible
duke@1 706 // in M1's class, then M1 and M2 both hide M3.
duke@1 707 ClassSymbol hiderClass = hider.owner.enclClass();
duke@1 708 ClassSymbol hideeClass = hidee.owner.enclClass();
duke@1 709 if (hiderClass == null || hideeClass == null ||
duke@1 710 !hiderClass.isSubClass(hideeClass, types)) {
duke@1 711 return false;
duke@1 712 }
duke@1 713
duke@1 714 // Hidee must be accessible in hider's class.
duke@1 715 // The method isInheritedIn is poorly named: it checks only access.
duke@1 716 return hidee.isInheritedIn(hiderClass, types);
duke@1 717 }
duke@1 718
duke@1 719 public boolean overrides(ExecutableElement riderEl,
duke@1 720 ExecutableElement rideeEl, TypeElement typeEl) {
duke@1 721 MethodSymbol rider = cast(MethodSymbol.class, riderEl);
duke@1 722 MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
duke@1 723 ClassSymbol origin = cast(ClassSymbol.class, typeEl);
duke@1 724
duke@1 725 return rider.name == ridee.name &&
duke@1 726
duke@1 727 // not reflexive as per JLS
duke@1 728 rider != ridee &&
duke@1 729
duke@1 730 // we don't care if ridee is static, though that wouldn't
duke@1 731 // compile
duke@1 732 !rider.isStatic() &&
duke@1 733
duke@1 734 // Symbol.overrides assumes the following
duke@1 735 ridee.isMemberOf(origin, types) &&
duke@1 736
duke@1 737 // check access and signatures; don't check return types
duke@1 738 rider.overrides(ridee, origin, types, false);
duke@1 739 }
duke@1 740
duke@1 741 public String getConstantExpression(Object value) {
duke@1 742 return Constants.format(value);
duke@1 743 }
duke@1 744
duke@1 745 /**
duke@1 746 * Print a representation of the elements to the given writer in
duke@1 747 * the specified order. The main purpose of this method is for
duke@1 748 * diagnostics. The exact format of the output is <em>not</em>
duke@1 749 * specified and is subject to change.
duke@1 750 *
duke@1 751 * @param w the writer to print the output to
duke@1 752 * @param elements the elements to print
duke@1 753 */
duke@1 754 public void printElements(java.io.Writer w, Element... elements) {
duke@1 755 for (Element element : elements)
duke@1 756 (new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush();
duke@1 757 }
duke@1 758
duke@1 759 public Name getName(CharSequence cs) {
jjg@113 760 return names.fromString(cs.toString());
duke@1 761 }
duke@1 762
darcy@1559 763 @Override
darcy@1559 764 public boolean isFunctionalInterface(TypeElement element) {
darcy@1559 765 if (element.getKind() != ElementKind.INTERFACE)
darcy@1559 766 return false;
darcy@1559 767 else {
darcy@1559 768 TypeSymbol tsym = cast(TypeSymbol.class, element);
darcy@1559 769 return types.isFunctionalInterface(tsym);
darcy@1559 770 }
darcy@1559 771 }
darcy@1559 772
duke@1 773 /**
duke@1 774 * Returns the tree node and compilation unit corresponding to this
duke@1 775 * element, or null if they can't be found.
duke@1 776 */
duke@1 777 private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) {
duke@1 778 Symbol sym = cast(Symbol.class, e);
duke@1 779 Env<AttrContext> enterEnv = getEnterEnv(sym);
duke@1 780 if (enterEnv == null)
duke@1 781 return null;
duke@1 782 JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
duke@1 783 if (tree == null || enterEnv.toplevel == null)
duke@1 784 return null;
duke@1 785 return new Pair<JCTree,JCCompilationUnit>(tree, enterEnv.toplevel);
duke@1 786 }
duke@1 787
duke@1 788 /**
duke@1 789 * Returns the best approximation for the tree node and compilation unit
duke@1 790 * corresponding to the given element, annotation and value.
duke@1 791 * If the element is null, null is returned.
duke@1 792 * If the annotation is null or cannot be found, the tree node and
duke@1 793 * compilation unit for the element is returned.
duke@1 794 * If the annotation value is null or cannot be found, the tree node and
duke@1 795 * compilation unit for the annotation is returned.
duke@1 796 */
duke@1 797 public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(
duke@1 798 Element e, AnnotationMirror a, AnnotationValue v) {
duke@1 799 if (e == null)
duke@1 800 return null;
duke@1 801
duke@1 802 Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e);
duke@1 803 if (elemTreeTop == null)
duke@1 804 return null;
duke@1 805
duke@1 806 if (a == null)
duke@1 807 return elemTreeTop;
duke@1 808
duke@1 809 JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
duke@1 810 if (annoTree == null)
duke@1 811 return elemTreeTop;
duke@1 812
duke@1 813 // 6388543: if v != null, we should search within annoTree to find
duke@1 814 // the tree matching v. For now, we ignore v and return the tree of
duke@1 815 // the annotation.
duke@1 816 return new Pair<JCTree, JCCompilationUnit>(annoTree, elemTreeTop.snd);
duke@1 817 }
duke@1 818
duke@1 819 /**
duke@1 820 * Returns a symbol's enter environment, or null if it has none.
duke@1 821 */
duke@1 822 private Env<AttrContext> getEnterEnv(Symbol sym) {
duke@1 823 // Get enclosing class of sym, or sym itself if it is a class
duke@1 824 // or package.
duke@1 825 TypeSymbol ts = (sym.kind != Kinds.PCK)
duke@1 826 ? sym.enclClass()
duke@1 827 : (PackageSymbol) sym;
duke@1 828 return (ts != null)
duke@1 829 ? enter.getEnv(ts)
duke@1 830 : null;
duke@1 831 }
duke@1 832
duke@1 833 /**
duke@1 834 * Returns an object cast to the specified type.
duke@1 835 * @throws NullPointerException if the object is {@code null}
duke@1 836 * @throws IllegalArgumentException if the object is of the wrong type
duke@1 837 */
duke@1 838 private static <T> T cast(Class<T> clazz, Object o) {
duke@1 839 if (! clazz.isInstance(o))
duke@1 840 throw new IllegalArgumentException(o.toString());
duke@1 841 return clazz.cast(o);
duke@1 842 }
duke@1 843 }

mercurial