src/share/classes/com/sun/tools/javac/code/Symbol.java

changeset 2100
1e7ad879f15e
parent 2081
c13305cf8528
child 2149
e5d3cd43c85e
equal deleted inserted replaced
2099:1b469fd31e35 2100:1e7ad879f15e
23 * questions. 23 * questions.
24 */ 24 */
25 25
26 package com.sun.tools.javac.code; 26 package com.sun.tools.javac.code;
27 27
28 import java.lang.annotation.Annotation;
29 import java.lang.annotation.Inherited;
28 import java.util.Set; 30 import java.util.Set;
29 import java.util.concurrent.Callable; 31 import java.util.concurrent.Callable;
30 32
31 import javax.lang.model.element.*; 33 import javax.lang.model.element.*;
32 import javax.tools.JavaFileObject; 34 import javax.tools.JavaFileObject;
35 import com.sun.tools.javac.comp.Annotate; 37 import com.sun.tools.javac.comp.Annotate;
36 import com.sun.tools.javac.comp.Attr; 38 import com.sun.tools.javac.comp.Attr;
37 import com.sun.tools.javac.comp.AttrContext; 39 import com.sun.tools.javac.comp.AttrContext;
38 import com.sun.tools.javac.comp.Env; 40 import com.sun.tools.javac.comp.Env;
39 import com.sun.tools.javac.jvm.*; 41 import com.sun.tools.javac.jvm.*;
40 import com.sun.tools.javac.model.*;
41 import com.sun.tools.javac.tree.JCTree;
42 import com.sun.tools.javac.util.*; 42 import com.sun.tools.javac.util.*;
43 import com.sun.tools.javac.util.Name; 43 import com.sun.tools.javac.util.Name;
44 import static com.sun.tools.javac.code.Flags.*; 44 import static com.sun.tools.javac.code.Flags.*;
45 import static com.sun.tools.javac.code.Kinds.*; 45 import static com.sun.tools.javac.code.Kinds.*;
46 import static com.sun.tools.javac.code.TypeTag.CLASS; 46 import static com.sun.tools.javac.code.TypeTag.CLASS;
56 * <p><b>This is NOT part of any supported API. 56 * <p><b>This is NOT part of any supported API.
57 * If you write code that depends on this, you do so at your own risk. 57 * If you write code that depends on this, you do so at your own risk.
58 * This code and its internal interfaces are subject to change or 58 * This code and its internal interfaces are subject to change or
59 * deletion without notice.</b> 59 * deletion without notice.</b>
60 */ 60 */
61 public abstract class Symbol implements Element { 61 public abstract class Symbol extends AnnoConstruct implements Element {
62 // public Throwable debug = new Throwable();
63 62
64 /** The kind of this symbol. 63 /** The kind of this symbol.
65 * @see Kinds 64 * @see Kinds
66 */ 65 */
67 public int kind; 66 public int kind;
100 99
101 /** The attributes of this symbol are contained in this 100 /** The attributes of this symbol are contained in this
102 * SymbolMetadata. The SymbolMetadata instance is NOT immutable. 101 * SymbolMetadata. The SymbolMetadata instance is NOT immutable.
103 */ 102 */
104 protected SymbolMetadata annotations; 103 protected SymbolMetadata annotations;
104
105 105
106 /** An accessor method for the attributes of this symbol. 106 /** An accessor method for the attributes of this symbol.
107 * Attributes of class symbols should be accessed through the accessor 107 * Attributes of class symbols should be accessed through the accessor
108 * method to make sure that the class symbol is loaded. 108 * method to make sure that the class symbol is loaded.
109 */ 109 */
594 @Override 594 @Override
595 public List<Attribute.Compound> getAnnotationMirrors() { 595 public List<Attribute.Compound> getAnnotationMirrors() {
596 return getRawAttributes(); 596 return getRawAttributes();
597 } 597 }
598 598
599 /**
600 * @deprecated this method should never be used by javac internally.
601 */
602 @Deprecated
603 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) {
604 return JavacAnnoConstructs.getAnnotation(this, annoType);
605 }
606
607 // This method is part of the javax.lang.model API, do not use this in javac code.
608 public <A extends java.lang.annotation.Annotation> A[] getAnnotationsByType(Class<A> annoType) {
609 return JavacAnnoConstructs.getAnnotationsByType(this, annoType);
610 }
611 599
612 // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList 600 // TODO: getEnclosedElements should return a javac List, fix in FilteredMemberList
613 public java.util.List<Symbol> getEnclosedElements() { 601 public java.util.List<Symbol> getEnclosedElements() {
614 return List.nil(); 602 return List.nil();
615 } 603 }
791 } 779 }
792 780
793 return res = res.reverse(); 781 return res = res.reverse();
794 } 782 }
795 783
784
785
786 // Helper to getAnnotation[s]
787 @Override
788 public <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
789
790 String name = annoType.getName();
791
792 // Declaration annotations on type variables are stored in type attributes
793 // on the owner of the TypeVariableSymbol
794 List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
795 for (Attribute.TypeCompound anno : candidates)
796 if (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
797 anno.position.type == TargetType.METHOD_TYPE_PARAMETER)
798 if (name.contentEquals(anno.type.tsym.flatName()))
799 return anno;
800
801 return null;
802 }
803
804
805
796 @Override 806 @Override
797 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 807 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
798 return v.visitTypeParameter(this, p); 808 return v.visitTypeParameter(this, p);
799 } 809 }
800 } 810 }
1047 } else { 1057 } else {
1048 return Type.noType; 1058 return Type.noType;
1049 } 1059 }
1050 } 1060 }
1051 1061
1062 /**
1063 * Returns the next class to search for inherited annotations or {@code null}
1064 * if the next class can't be found.
1065 */
1066 private ClassSymbol getSuperClassToSearchForAnnotations() {
1067
1068 Type sup = getSuperclass();
1069
1070 if (!sup.hasTag(CLASS) || sup.isErroneous())
1071 return null;
1072
1073 return (ClassSymbol) sup.tsym;
1074 }
1075
1076
1077 @Override
1078 protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
1079
1080 ClassSymbol sup = getSuperClassToSearchForAnnotations();
1081
1082 return sup == null ? super.getInheritedAnnotations(annoType)
1083 : sup.getAnnotationsByType(annoType);
1084 }
1085
1086
1052 public ElementKind getKind() { 1087 public ElementKind getKind() {
1053 long flags = flags(); 1088 long flags = flags();
1054 if ((flags & ANNOTATION) != 0) 1089 if ((flags & ANNOTATION) != 0)
1055 return ElementKind.ANNOTATION_TYPE; 1090 return ElementKind.ANNOTATION_TYPE;
1056 else if ((flags & INTERFACE) != 0) 1091 else if ((flags & INTERFACE) != 0)
1077 return NestingKind.LOCAL; 1112 return NestingKind.LOCAL;
1078 else 1113 else
1079 return NestingKind.MEMBER; 1114 return NestingKind.MEMBER;
1080 } 1115 }
1081 1116
1082 /** 1117
1083 * Since this method works in terms of the runtime representation
1084 * of annotations, it should never be used by javac internally.
1085 */
1086 @Override 1118 @Override
1087 public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> annoType) { 1119 protected <A extends Annotation> Attribute.Compound getAttribute(final Class<A> annoType) {
1088 return JavacAnnoConstructs.getAnnotation(this, annoType); 1120
1089 } 1121 Attribute.Compound attrib = super.getAttribute(annoType);
1122
1123 boolean inherited = annoType.isAnnotationPresent(Inherited.class);
1124 if (attrib != null || !inherited)
1125 return attrib;
1126
1127 // Search supertypes
1128 ClassSymbol superType = getSuperClassToSearchForAnnotations();
1129 return superType == null ? null
1130 : superType.getAttribute(annoType);
1131 }
1132
1133
1134
1090 1135
1091 public <R, P> R accept(ElementVisitor<R, P> v, P p) { 1136 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1092 return v.visitType(this, p); 1137 return v.visitType(this, p);
1093 } 1138 }
1094 1139

mercurial