< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java

Print this page




  77  *  deletion without notice.</b>
  78  */
  79 public class Check {
  80     protected static final Context.Key<Check> checkKey = new Context.Key<>();
  81 
  82     private final Names names;
  83     private final Log log;
  84     private final Resolve rs;
  85     private final Symtab syms;
  86     private final Enter enter;
  87     private final DeferredAttr deferredAttr;
  88     private final Infer infer;
  89     private final Types types;
  90     private final TypeAnnotations typeAnnotations;
  91     private final JCDiagnostic.Factory diags;
  92     private final JavaFileManager fileManager;
  93     private final Source source;
  94     private final Target target;
  95     private final Profile profile;
  96     private final boolean warnOnAnyAccessToMembers;

  97 
  98     // The set of lint options currently in effect. It is initialized
  99     // from the context, and then is set/reset as needed by Attr as it
 100     // visits all the various parts of the trees during attribution.
 101     private Lint lint;
 102 
 103     // The method being analyzed in Attr - it is set/reset as needed by
 104     // Attr as it visits new method declarations.
 105     private MethodSymbol method;
 106 
 107     public static Check instance(Context context) {
 108         Check instance = context.get(checkKey);
 109         if (instance == null)
 110             instance = new Check(context);
 111         return instance;
 112     }
 113 
 114     protected Check(Context context) {
 115         context.put(checkKey, this);
 116 


 138         Target target = Target.instance(context);
 139         syntheticNameChar = target.syntheticNameChar();
 140 
 141         profile = Profile.instance(context);
 142 
 143         boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
 144         boolean verboseRemoval = lint.isEnabled(LintCategory.REMOVAL);
 145         boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
 146         boolean enforceMandatoryWarnings = true;
 147 
 148         deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated,
 149                 enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION);
 150         removalHandler = new MandatoryWarningHandler(log, verboseRemoval,
 151                 enforceMandatoryWarnings, "removal", LintCategory.REMOVAL);
 152         uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked,
 153                 enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED);
 154         sunApiHandler = new MandatoryWarningHandler(log, false,
 155                 enforceMandatoryWarnings, "sunapi", null);
 156 
 157         deferredLintHandler = DeferredLintHandler.instance(context);

 158     }
 159 
 160     /** Character for synthetic names
 161      */
 162     char syntheticNameChar;
 163 
 164     /** A table mapping flat names of all compiled classes for each module in this run
 165      *  to their symbols; maintained from outside.
 166      */
 167     private Map<Pair<ModuleSymbol, Name>,ClassSymbol> compiled = new HashMap<>();
 168 
 169     /** A handler for messages about deprecated usage.
 170      */
 171     private MandatoryWarningHandler deprecationHandler;
 172 
 173     /** A handler for messages about deprecated-for-removal usage.
 174      */
 175     private MandatoryWarningHandler removalHandler;
 176 
 177     /** A handler for messages about unchecked or unsafe usage.


1120      *  @param pos           Position to be used for error reporting.
1121      *  @param flags         The set of modifiers given in a definition.
1122      *  @param sym           The defined symbol.
1123      */
1124     long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
1125         long mask;
1126         long implicit = 0;
1127 
1128         switch (sym.kind) {
1129         case VAR:
1130             if (TreeInfo.isReceiverParam(tree))
1131                 mask = ReceiverParamFlags;
1132             else if (sym.owner.kind != TYP)
1133                 mask = LocalVarFlags;
1134             else if ((sym.owner.flags_field & INTERFACE) != 0)
1135                 mask = implicit = InterfaceVarFlags;
1136             else
1137                 mask = VarFlags;
1138             break;
1139         case MTH:



1140             if (sym.name == names.init) {
1141                 if ((sym.owner.flags_field & ENUM) != 0) {
1142                     // enum constructors cannot be declared public or
1143                     // protected and must be implicitly or explicitly
1144                     // private
1145                     implicit = PRIVATE;
1146                     mask = PRIVATE;
1147                 } else
1148                     mask = ConstructorFlags;
1149             }  else if ((sym.owner.flags_field & INTERFACE) != 0) {
1150                 if ((sym.owner.flags_field & ANNOTATION) != 0) {
1151                     mask = AnnotationTypeElementMask;
1152                     implicit = PUBLIC | ABSTRACT;
1153                 } else if ((flags & (DEFAULT | STATIC | PRIVATE)) != 0) {
1154                     mask = InterfaceMethodMask;
1155                     implicit = (flags & PRIVATE) != 0 ? 0 : PUBLIC;
1156                     if ((flags & DEFAULT) != 0) {
1157                         implicit |= ABSTRACT;
1158                     }
1159                 } else {
1160                     mask = implicit = InterfaceMethodFlags;
1161                 }
1162             } else {
1163                 mask = MethodFlags;
1164             }
1165             // Imply STRICTFP if owner has STRICTFP set.
1166             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1167                 ((flags) & Flags.DEFAULT) != 0)
1168                 implicit |= sym.owner.flags_field & STRICTFP;
1169             break;
1170         case TYP:



1171             if (sym.isLocal()) {
1172                 mask = LocalClassFlags;
1173                 if ((sym.owner.flags_field & STATIC) == 0 &&
1174                     (flags & ENUM) != 0)
1175                     log.error(pos, Errors.EnumsMustBeStatic);




1176             } else if (sym.owner.kind == TYP) {
1177                 mask = MemberClassFlags;
1178                 if (sym.owner.owner.kind == PCK ||
1179                     (sym.owner.flags_field & STATIC) != 0)
1180                     mask |= STATIC;
1181                 else if ((flags & ENUM) != 0)
1182                     log.error(pos, Errors.EnumsMustBeStatic);

1183                 // Nested interfaces and enums are always STATIC (Spec ???)
1184                 if ((flags & (INTERFACE | ENUM)) != 0 ) implicit = STATIC;
1185             } else {
1186                 mask = ClassFlags;
1187             }
1188             // Interfaces are always ABSTRACT
1189             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1190 
1191             if ((flags & ENUM) != 0) {
1192                 // enums can't be declared abstract or final
1193                 mask &= ~(ABSTRACT | FINAL);
1194                 implicit |= implicitEnumFinalFlag(tree);
1195             }




1196             // Imply STRICTFP if owner has STRICTFP set.
1197             implicit |= sym.owner.flags_field & STRICTFP;
1198             break;
1199         default:
1200             throw new AssertionError();
1201         }
1202         long illegal = flags & ExtendedStandardFlags & ~mask;



1203         if (illegal != 0) {
1204             if ((illegal & INTERFACE) != 0) {
1205                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1206                 mask |= INTERFACE;
1207             }
1208             else {
1209                 log.error(pos,
1210                           Errors.ModNotAllowedHere(asFlagSet(illegal)));
1211             }
1212         }
1213         else if ((sym.kind == TYP ||
1214                   // ISSUE: Disallowing abstract&private is no longer appropriate
1215                   // in the presence of inner classes. Should it be deleted here?
1216                   checkDisjoint(pos, flags,
1217                                 ABSTRACT,
1218                                 PRIVATE | STATIC | DEFAULT))
1219                  &&
1220                  checkDisjoint(pos, flags,
1221                                 STATIC | PRIVATE,
1222                                 DEFAULT)
1223                  &&
1224                  checkDisjoint(pos, flags,
1225                                ABSTRACT | INTERFACE,
1226                                FINAL | NATIVE | SYNCHRONIZED)
1227                  &&
1228                  checkDisjoint(pos, flags,
1229                                PUBLIC,
1230                                PRIVATE | PROTECTED)
1231                  &&
1232                  checkDisjoint(pos, flags,
1233                                PRIVATE,
1234                                PUBLIC | PROTECTED)
1235                  &&
1236                  checkDisjoint(pos, flags,
1237                                FINAL,
1238                                VOLATILE)
1239                  &&
1240                  (sym.kind == TYP ||
1241                   checkDisjoint(pos, flags,
1242                                 ABSTRACT | NATIVE,
1243                                 STRICTFP))) {






1244             // skip
1245         }
1246         return flags & (mask | ~ExtendedStandardFlags) | implicit;
1247     }
1248 
1249 
1250     /** Determine if this enum should be implicitly final.
1251      *
1252      *  If the enum has no specialized enum contants, it is final.
1253      *
1254      *  If the enum does have specialized enum contants, it is
1255      *  <i>not</i> final.
1256      */
1257     private long implicitEnumFinalFlag(JCTree tree) {
1258         if (!tree.hasTag(CLASSDEF)) return 0;
1259         class SpecialTreeVisitor extends JCTree.Visitor {
1260             boolean specialized;
1261             SpecialTreeVisitor() {
1262                 this.specialized = false;
1263             }


2796      * annotation type has a signature that is override-equivalent to
2797      * that of any public or protected method declared in class Object
2798      * or in the interface annotation.Annotation."
2799      *
2800      * @jls 9.6 Annotation Types
2801      */
2802     void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) {
2803         for (Type sup = syms.annotationType; sup.hasTag(CLASS); sup = types.supertype(sup)) {
2804             Scope s = sup.tsym.members();
2805             for (Symbol sym : s.getSymbolsByName(m.name)) {
2806                 if (sym.kind == MTH &&
2807                     (sym.flags() & (PUBLIC | PROTECTED)) != 0 &&
2808                     types.overrideEquivalent(m.type, sym.type))
2809                     log.error(pos, Errors.IntfAnnotationMemberClash(sym, sup));
2810             }
2811         }
2812     }
2813 
2814     /** Check the annotations of a symbol.
2815      */
2816     public void validateAnnotations(List<JCAnnotation> annotations, Symbol s) {
2817         for (JCAnnotation a : annotations)
2818             validateAnnotation(a, s);
2819     }
2820 
2821     /** Check the type annotations.
2822      */
2823     public void validateTypeAnnotations(List<JCAnnotation> annotations, boolean isTypeParameter) {
2824         for (JCAnnotation a : annotations)
2825             validateTypeAnnotation(a, isTypeParameter);
2826     }
2827 
2828     /** Check an annotation of a symbol.
2829      */
2830     private void validateAnnotation(JCAnnotation a, Symbol s) {
2831         validateAnnotationTree(a);
2832 
2833         if (a.type.tsym.isAnnotationType() && !annotationApplicable(a, s))
2834             log.error(a.pos(), Errors.AnnotationTypeNotApplicable);







































2835 
2836         if (a.annotationType.type.tsym == syms.functionalInterfaceType.tsym) {
2837             if (s.kind != TYP) {
2838                 log.error(a.pos(), Errors.BadFunctionalIntfAnno);
2839             } else if (!s.isInterface() || (s.flags() & ANNOTATION) != 0) {
2840                 log.error(a.pos(), Errors.BadFunctionalIntfAnno1(Fragments.NotAFunctionalIntf(s)));
2841             }
2842         }
2843     }
2844 
2845     public void validateTypeAnnotation(JCAnnotation a, boolean isTypeParameter) {
2846         Assert.checkNonNull(a.type);
2847         validateAnnotationTree(a);
2848 
2849         if (a.hasTag(TYPE_ANNOTATION) &&
2850                 !a.annotationType.type.isErroneous() &&
2851                 !isTypeAnnotation(a, isTypeParameter)) {
2852             log.error(a.pos(), Errors.AnnotationTypeNotApplicableToType(a.type));
2853         }
2854     }


3064         }
3065         return false;
3066     }
3067 
3068     /** Is the annotation applicable to types? */
3069     protected boolean isTypeAnnotation(JCAnnotation a, boolean isTypeParameter) {
3070         List<Attribute> targets = typeAnnotations.annotationTargets(a.annotationType.type.tsym);
3071         return (targets == null) ?
3072                 false :
3073                 targets.stream()
3074                         .anyMatch(attr -> isTypeAnnotation(attr, isTypeParameter));
3075     }
3076     //where
3077         boolean isTypeAnnotation(Attribute a, boolean isTypeParameter) {
3078             Attribute.Enum e = (Attribute.Enum)a;
3079             return (e.value.name == names.TYPE_USE ||
3080                     (isTypeParameter && e.value.name == names.TYPE_PARAMETER));
3081         }
3082 
3083     /** Is the annotation applicable to the symbol? */
3084     boolean annotationApplicable(JCAnnotation a, Symbol s) {
3085         Attribute.Array arr = getAttributeTargetAttribute(a.annotationType.type.tsym);
3086         Name[] targets;
3087 
3088         if (arr == null) {
3089             targets = defaultTargetMetaInfo(a, s);
3090         } else {
3091             // TODO: can we optimize this?
3092             targets = new Name[arr.values.length];
3093             for (int i=0; i<arr.values.length; ++i) {
3094                 Attribute app = arr.values[i];
3095                 if (!(app instanceof Attribute.Enum)) {
3096                     return true; // recovery
3097                 }
3098                 Attribute.Enum e = (Attribute.Enum) app;
3099                 targets[i] = e.value.name;
3100             }
3101         }









3102         for (Name target : targets) {
3103             if (target == names.TYPE) {
3104                 if (s.kind == TYP)
3105                     return true;
3106             } else if (target == names.FIELD) {
3107                 if (s.kind == VAR && s.owner.kind != MTH)
3108                     return true;
3109             } else if (target == names.METHOD) {
3110                 if (s.kind == MTH && !s.isConstructor())
3111                     return true;
3112             } else if (target == names.PARAMETER) {
3113                 if (s.kind == VAR && s.owner.kind == MTH &&
3114                       (s.flags() & PARAMETER) != 0) {
3115                     return true;
3116                 }
3117             } else if (target == names.CONSTRUCTOR) {
3118                 if (s.kind == MTH && s.isConstructor())
3119                     return true;
3120             } else if (target == names.LOCAL_VARIABLE) {
3121                 if (s.kind == VAR && s.owner.kind == MTH &&
3122                       (s.flags() & PARAMETER) == 0) {
3123                     return true;
3124                 }
3125             } else if (target == names.ANNOTATION_TYPE) {
3126                 if (s.kind == TYP && (s.flags() & ANNOTATION) != 0) {
3127                     return true;
3128                 }
3129             } else if (target == names.PACKAGE) {
3130                 if (s.kind == PCK)
3131                     return true;
3132             } else if (target == names.TYPE_USE) {
3133                 if (s.kind == VAR && s.owner.kind == MTH && s.type.hasTag(NONE)) {
3134                     //cannot type annotate implictly typed locals


3433     boolean checkUnique(DiagnosticPosition pos, Symbol sym, Scope s) {
3434         if (sym.type.isErroneous())
3435             return true;
3436         if (sym.owner.name == names.any) return false;
3437         for (Symbol byName : s.getSymbolsByName(sym.name, NON_RECURSIVE)) {
3438             if (sym != byName &&
3439                     (byName.flags() & CLASH) == 0 &&
3440                     sym.kind == byName.kind &&
3441                     sym.name != names.error &&
3442                     (sym.kind != MTH ||
3443                      types.hasSameArgs(sym.type, byName.type) ||
3444                      types.hasSameArgs(types.erasure(sym.type), types.erasure(byName.type)))) {
3445                 if ((sym.flags() & VARARGS) != (byName.flags() & VARARGS)) {
3446                     sym.flags_field |= CLASH;
3447                     varargsDuplicateError(pos, sym, byName);
3448                     return true;
3449                 } else if (sym.kind == MTH && !types.hasSameArgs(sym.type, byName.type, false)) {
3450                     duplicateErasureError(pos, sym, byName);
3451                     sym.flags_field |= CLASH;
3452                     return true;





3453                 } else {
3454                     duplicateError(pos, byName);
3455                     return false;
3456                 }
3457             }
3458         }
3459         return true;
3460     }
3461 
3462     /** Report duplicate declaration error.
3463      */
3464     void duplicateErasureError(DiagnosticPosition pos, Symbol sym1, Symbol sym2) {
3465         if (!sym1.type.isErroneous() && !sym2.type.isErroneous()) {
3466             log.error(pos, Errors.NameClashSameErasure(sym1, sym2));
3467         }
3468     }
3469 
3470     /**Check that types imported through the ordinary imports don't clash with types imported
3471      * by other (static or ordinary) imports. Note that two static imports may import two clashing
3472      * types without an error on the imports.




  77  *  deletion without notice.</b>
  78  */
  79 public class Check {
  80     protected static final Context.Key<Check> checkKey = new Context.Key<>();
  81 
  82     private final Names names;
  83     private final Log log;
  84     private final Resolve rs;
  85     private final Symtab syms;
  86     private final Enter enter;
  87     private final DeferredAttr deferredAttr;
  88     private final Infer infer;
  89     private final Types types;
  90     private final TypeAnnotations typeAnnotations;
  91     private final JCDiagnostic.Factory diags;
  92     private final JavaFileManager fileManager;
  93     private final Source source;
  94     private final Target target;
  95     private final Profile profile;
  96     private final boolean warnOnAnyAccessToMembers;
  97     private final boolean debug;
  98 
  99     // The set of lint options currently in effect. It is initialized
 100     // from the context, and then is set/reset as needed by Attr as it
 101     // visits all the various parts of the trees during attribution.
 102     private Lint lint;
 103 
 104     // The method being analyzed in Attr - it is set/reset as needed by
 105     // Attr as it visits new method declarations.
 106     private MethodSymbol method;
 107 
 108     public static Check instance(Context context) {
 109         Check instance = context.get(checkKey);
 110         if (instance == null)
 111             instance = new Check(context);
 112         return instance;
 113     }
 114 
 115     protected Check(Context context) {
 116         context.put(checkKey, this);
 117 


 139         Target target = Target.instance(context);
 140         syntheticNameChar = target.syntheticNameChar();
 141 
 142         profile = Profile.instance(context);
 143 
 144         boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
 145         boolean verboseRemoval = lint.isEnabled(LintCategory.REMOVAL);
 146         boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
 147         boolean enforceMandatoryWarnings = true;
 148 
 149         deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated,
 150                 enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION);
 151         removalHandler = new MandatoryWarningHandler(log, verboseRemoval,
 152                 enforceMandatoryWarnings, "removal", LintCategory.REMOVAL);
 153         uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked,
 154                 enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED);
 155         sunApiHandler = new MandatoryWarningHandler(log, false,
 156                 enforceMandatoryWarnings, "sunapi", null);
 157 
 158         deferredLintHandler = DeferredLintHandler.instance(context);
 159         debug = options.isSet("debug");
 160     }
 161 
 162     /** Character for synthetic names
 163      */
 164     char syntheticNameChar;
 165 
 166     /** A table mapping flat names of all compiled classes for each module in this run
 167      *  to their symbols; maintained from outside.
 168      */
 169     private Map<Pair<ModuleSymbol, Name>,ClassSymbol> compiled = new HashMap<>();
 170 
 171     /** A handler for messages about deprecated usage.
 172      */
 173     private MandatoryWarningHandler deprecationHandler;
 174 
 175     /** A handler for messages about deprecated-for-removal usage.
 176      */
 177     private MandatoryWarningHandler removalHandler;
 178 
 179     /** A handler for messages about unchecked or unsafe usage.


1122      *  @param pos           Position to be used for error reporting.
1123      *  @param flags         The set of modifiers given in a definition.
1124      *  @param sym           The defined symbol.
1125      */
1126     long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
1127         long mask;
1128         long implicit = 0;
1129 
1130         switch (sym.kind) {
1131         case VAR:
1132             if (TreeInfo.isReceiverParam(tree))
1133                 mask = ReceiverParamFlags;
1134             else if (sym.owner.kind != TYP)
1135                 mask = LocalVarFlags;
1136             else if ((sym.owner.flags_field & INTERFACE) != 0)
1137                 mask = implicit = InterfaceVarFlags;
1138             else
1139                 mask = VarFlags;
1140             break;
1141         case MTH:
1142             if (debug) {
1143                 System.out.println("checking method with flags " + Flags.toString(flags));
1144             }
1145             if (sym.name == names.init) {
1146                 if ((sym.owner.flags_field & ENUM) != 0) {
1147                     // enum constructors cannot be declared public or
1148                     // protected and must be implicitly or explicitly
1149                     // private
1150                     implicit = PRIVATE;
1151                     mask = PRIVATE;
1152                 } else
1153                     mask = ConstructorFlags;
1154             }  else if ((sym.owner.flags_field & INTERFACE) != 0) {
1155                 if ((sym.owner.flags_field & ANNOTATION) != 0) {
1156                     mask = AnnotationTypeElementMask;
1157                     implicit = PUBLIC | ABSTRACT;
1158                 } else if ((flags & (DEFAULT | STATIC | PRIVATE)) != 0) {
1159                     mask = InterfaceMethodMask;
1160                     implicit = (flags & PRIVATE) != 0 ? 0 : PUBLIC;
1161                     if ((flags & DEFAULT) != 0) {
1162                         implicit |= ABSTRACT;
1163                     }
1164                 } else {
1165                     mask = implicit = InterfaceMethodFlags;
1166                 }
1167             } else {
1168                 mask = MethodFlags;
1169             }
1170             // Imply STRICTFP if owner has STRICTFP set.
1171             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1172                 ((flags) & Flags.DEFAULT) != 0)
1173                 implicit |= sym.owner.flags_field & STRICTFP;
1174             break;
1175         case TYP:
1176             if (debug) {
1177                 System.out.println("checking type with flags " + Flags.toString(flags));
1178             }
1179             if (sym.isLocal()) {
1180                 mask = (flags & RECORD) != 0 ? LocalRecordFlags : ExtendedLocalClassFlags;
1181                 if ((sym.owner.flags_field & STATIC) == 0 &&
1182                     (flags & ENUM) != 0) {
1183                     log.error(pos, Errors.EnumsMustBeStatic);
1184                 }
1185                 if ((flags & RECORD) != 0 && (flags & STATIC) == 0) {
1186                     log.error(pos, Errors.NestedRecordsMustBeStatic);
1187                 }
1188             } else if (sym.owner.kind == TYP) {
1189                 mask = (flags & RECORD) != 0 ? ExtendedMemberRecordClassFlags : ExtendedMemberClassFlags;
1190                 if (sym.owner.owner.kind == PCK ||
1191                     (sym.owner.flags_field & STATIC) != 0)
1192                     mask |= STATIC;
1193                 else if ((flags & ENUM) != 0) {
1194                     log.error(pos, Errors.EnumsMustBeStatic);
1195                 }
1196                 // Nested interfaces and enums are always STATIC (Spec ???)
1197                 if ((flags & (INTERFACE | ENUM)) != 0 ) implicit = STATIC;
1198             } else {
1199                 mask = ExtendedClassFlags;
1200             }
1201             // Interfaces are always ABSTRACT
1202             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1203 
1204             if ((flags & ENUM) != 0) {
1205                 // enums can't be declared abstract or final
1206                 mask &= ~(ABSTRACT | FINAL);
1207                 implicit |= implicitEnumFinalFlag(tree);
1208             }
1209             if ((flags & RECORD) != 0) {
1210                 // records can't be declared abstract
1211                 mask &= ~ABSTRACT;
1212             }
1213             // Imply STRICTFP if owner has STRICTFP set.
1214             implicit |= sym.owner.flags_field & STRICTFP;
1215             break;
1216         default:
1217             throw new AssertionError();
1218         }
1219         long illegal = flags & ExtendedStandardFlags & ~mask;
1220         if (debug) {
1221             System.out.println("illegal flags: " + Flags.toString(illegal));
1222         }
1223         if (illegal != 0) {
1224             if ((illegal & INTERFACE) != 0) {
1225                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1226                 mask |= INTERFACE;
1227             }
1228             else {
1229                 log.error(pos,
1230                           Errors.ModNotAllowedHere(asFlagSet(illegal)));
1231             }
1232         }
1233         else if ((sym.kind == TYP ||
1234                   // ISSUE: Disallowing abstract&private is no longer appropriate
1235                   // in the presence of inner classes. Should it be deleted here?
1236                   checkDisjoint(pos, flags,
1237                                 ABSTRACT,
1238                                 PRIVATE | STATIC | DEFAULT))
1239                  &&
1240                  checkDisjoint(pos, flags,
1241                                 STATIC | PRIVATE,
1242                                 DEFAULT)
1243                  &&
1244                  checkDisjoint(pos, flags,
1245                                ABSTRACT | INTERFACE,
1246                                FINAL | NATIVE | SYNCHRONIZED)
1247                  &&
1248                  checkDisjoint(pos, flags,
1249                                PUBLIC,
1250                                PRIVATE | PROTECTED)
1251                  &&
1252                  checkDisjoint(pos, flags,
1253                                PRIVATE,
1254                                PUBLIC | PROTECTED)
1255                  &&
1256                  checkDisjoint(pos, flags,
1257                                FINAL,
1258                                VOLATILE)
1259                  &&
1260                  (sym.kind == TYP ||
1261                   checkDisjoint(pos, flags,
1262                                 ABSTRACT | NATIVE,
1263                                 STRICTFP))
1264                  && checkDisjoint(pos, flags,
1265                                 FINAL,
1266                            SEALED | NON_SEALED)
1267                  && checkDisjoint(pos, flags,
1268                                 SEALED,
1269                            FINAL | NON_SEALED)) {
1270             // skip
1271         }
1272         return flags & (mask | ~ExtendedStandardFlags) | implicit;
1273     }
1274 
1275 
1276     /** Determine if this enum should be implicitly final.
1277      *
1278      *  If the enum has no specialized enum contants, it is final.
1279      *
1280      *  If the enum does have specialized enum contants, it is
1281      *  <i>not</i> final.
1282      */
1283     private long implicitEnumFinalFlag(JCTree tree) {
1284         if (!tree.hasTag(CLASSDEF)) return 0;
1285         class SpecialTreeVisitor extends JCTree.Visitor {
1286             boolean specialized;
1287             SpecialTreeVisitor() {
1288                 this.specialized = false;
1289             }


2822      * annotation type has a signature that is override-equivalent to
2823      * that of any public or protected method declared in class Object
2824      * or in the interface annotation.Annotation."
2825      *
2826      * @jls 9.6 Annotation Types
2827      */
2828     void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) {
2829         for (Type sup = syms.annotationType; sup.hasTag(CLASS); sup = types.supertype(sup)) {
2830             Scope s = sup.tsym.members();
2831             for (Symbol sym : s.getSymbolsByName(m.name)) {
2832                 if (sym.kind == MTH &&
2833                     (sym.flags() & (PUBLIC | PROTECTED)) != 0 &&
2834                     types.overrideEquivalent(m.type, sym.type))
2835                     log.error(pos, Errors.IntfAnnotationMemberClash(sym, sup));
2836             }
2837         }
2838     }
2839 
2840     /** Check the annotations of a symbol.
2841      */
2842     public void validateAnnotations(List<JCAnnotation> annotations, JCTree declarationTree, Symbol s) {
2843         for (JCAnnotation a : annotations)
2844             validateAnnotation(a, declarationTree, s);
2845     }
2846 
2847     /** Check the type annotations.
2848      */
2849     public void validateTypeAnnotations(List<JCAnnotation> annotations, boolean isTypeParameter) {
2850         for (JCAnnotation a : annotations)
2851             validateTypeAnnotation(a, isTypeParameter);
2852     }
2853 
2854     /** Check an annotation of a symbol.
2855      */
2856     private void validateAnnotation(JCAnnotation a, JCTree declarationTree, Symbol s) {
2857         validateAnnotationTree(a);
2858 
2859         if (s.isRecord() && s.flags_field == (Flags.PRIVATE | Flags.FINAL | Flags.MANDATED | Flags.RECORD) && declarationTree.hasTag(VARDEF)) {
2860             // we are seeing a record field, which had the original annotations, now is the moment,
2861             // before stripping some of them just below, to check if the original annotations
2862             // applied to records at all, first version only cares about declaration annotations
2863             // we will add type annotations later on
2864             Name[] targets = getTargetNames(a, s);
2865             boolean appliesToRecords = false;
2866             for (Name target : targets) {
2867                 appliesToRecords = target == names.FIELD || target == names.PARAMETER || target == names.METHOD;
2868                 if (appliesToRecords) {
2869                     break;
2870                 }
2871             }
2872             if (!appliesToRecords) {
2873                 log.error(a.pos(), Errors.AnnotationTypeNotApplicable);
2874             }
2875         }
2876 
2877         //System.out.println("at Check.validateAnnotation: flags: " + Flags.toString(s.flags_field) + ", declaration tree " + declarationTree);
2878 
2879         if (a.type.tsym.isAnnotationType() && !annotationApplicable(a, s)) {
2880             // debug
2881             //System.out.println("at Check.validateAnnotation: flags: " + Flags.toString(s.flags_field) + ", declaration tree " + declarationTree);
2882             if (s.isRecord() || s.owner.isRecord() && (s.flags_field & Flags.MANDATED) != 0) {
2883                 JCModifiers modifiers = TreeInfo.getModifiers(declarationTree);
2884                 // lets first remove the annotation from the modifier
2885                 if (modifiers != null) {
2886                     ListBuffer<JCAnnotation> newAnnotations = new ListBuffer<>();
2887                     for (JCAnnotation anno : modifiers.annotations) {
2888                         if (anno != a) {
2889                             newAnnotations.add(anno);
2890                         }
2891                     }
2892                     modifiers.annotations = newAnnotations.toList();
2893                 }
2894                 // now lets remove it from the symbol
2895                 s.getMetadata().remove(a.attribute);
2896             } else {
2897                 log.error(a.pos(), Errors.AnnotationTypeNotApplicable);
2898             }
2899         }
2900 
2901         if (a.annotationType.type.tsym == syms.functionalInterfaceType.tsym) {
2902             if (s.kind != TYP) {
2903                 log.error(a.pos(), Errors.BadFunctionalIntfAnno);
2904             } else if (!s.isInterface() || (s.flags() & ANNOTATION) != 0) {
2905                 log.error(a.pos(), Errors.BadFunctionalIntfAnno1(Fragments.NotAFunctionalIntf(s)));
2906             }
2907         }
2908     }
2909 
2910     public void validateTypeAnnotation(JCAnnotation a, boolean isTypeParameter) {
2911         Assert.checkNonNull(a.type);
2912         validateAnnotationTree(a);
2913 
2914         if (a.hasTag(TYPE_ANNOTATION) &&
2915                 !a.annotationType.type.isErroneous() &&
2916                 !isTypeAnnotation(a, isTypeParameter)) {
2917             log.error(a.pos(), Errors.AnnotationTypeNotApplicableToType(a.type));
2918         }
2919     }


3129         }
3130         return false;
3131     }
3132 
3133     /** Is the annotation applicable to types? */
3134     protected boolean isTypeAnnotation(JCAnnotation a, boolean isTypeParameter) {
3135         List<Attribute> targets = typeAnnotations.annotationTargets(a.annotationType.type.tsym);
3136         return (targets == null) ?
3137                 false :
3138                 targets.stream()
3139                         .anyMatch(attr -> isTypeAnnotation(attr, isTypeParameter));
3140     }
3141     //where
3142         boolean isTypeAnnotation(Attribute a, boolean isTypeParameter) {
3143             Attribute.Enum e = (Attribute.Enum)a;
3144             return (e.value.name == names.TYPE_USE ||
3145                     (isTypeParameter && e.value.name == names.TYPE_PARAMETER));
3146         }
3147 
3148     /** Is the annotation applicable to the symbol? */
3149     Name[] getTargetNames(JCAnnotation a, Symbol s) {
3150         Attribute.Array arr = getAttributeTargetAttribute(a.annotationType.type.tsym);
3151         Name[] targets;

3152         if (arr == null) {
3153             targets = defaultTargetMetaInfo(a, s);
3154         } else {
3155             // TODO: can we optimize this?
3156             targets = new Name[arr.values.length];
3157             for (int i=0; i<arr.values.length; ++i) {
3158                 Attribute app = arr.values[i];
3159                 if (!(app instanceof Attribute.Enum)) {
3160                     return new Name[0];
3161                 }
3162                 Attribute.Enum e = (Attribute.Enum) app;
3163                 targets[i] = e.value.name;
3164             }
3165         }
3166         return targets;
3167     }
3168 
3169     boolean annotationApplicable(JCAnnotation a, Symbol s) {
3170         Name[] targets = getTargetNames(a, s);
3171         if (targets.length == 0) {
3172             // recovery
3173             return true;
3174         }
3175         for (Name target : targets) {
3176             if (target == names.TYPE) {
3177                 if (s.kind == TYP)
3178                     return true;
3179             } else if (target == names.FIELD) {
3180                 if (s.kind == VAR && s.owner.kind != MTH)
3181                     return true;
3182             } else if (target == names.METHOD) {
3183                 if (s.kind == MTH && !s.isConstructor())
3184                     return true;
3185             } else if (target == names.PARAMETER) {
3186                 if (s.kind == VAR &&
3187                     (s.owner.kind == MTH && (s.flags() & PARAMETER) != 0)) {
3188                     return true;
3189                 }
3190             } else if (target == names.CONSTRUCTOR) {
3191                 if (s.kind == MTH && s.isConstructor())
3192                     return true;
3193             } else if (target == names.LOCAL_VARIABLE) {
3194                 if (s.kind == VAR && s.owner.kind == MTH &&
3195                       (s.flags() & PARAMETER) == 0) {
3196                     return true;
3197                 }
3198             } else if (target == names.ANNOTATION_TYPE) {
3199                 if (s.kind == TYP && (s.flags() & ANNOTATION) != 0) {
3200                     return true;
3201                 }
3202             } else if (target == names.PACKAGE) {
3203                 if (s.kind == PCK)
3204                     return true;
3205             } else if (target == names.TYPE_USE) {
3206                 if (s.kind == VAR && s.owner.kind == MTH && s.type.hasTag(NONE)) {
3207                     //cannot type annotate implictly typed locals


3506     boolean checkUnique(DiagnosticPosition pos, Symbol sym, Scope s) {
3507         if (sym.type.isErroneous())
3508             return true;
3509         if (sym.owner.name == names.any) return false;
3510         for (Symbol byName : s.getSymbolsByName(sym.name, NON_RECURSIVE)) {
3511             if (sym != byName &&
3512                     (byName.flags() & CLASH) == 0 &&
3513                     sym.kind == byName.kind &&
3514                     sym.name != names.error &&
3515                     (sym.kind != MTH ||
3516                      types.hasSameArgs(sym.type, byName.type) ||
3517                      types.hasSameArgs(types.erasure(sym.type), types.erasure(byName.type)))) {
3518                 if ((sym.flags() & VARARGS) != (byName.flags() & VARARGS)) {
3519                     sym.flags_field |= CLASH;
3520                     varargsDuplicateError(pos, sym, byName);
3521                     return true;
3522                 } else if (sym.kind == MTH && !types.hasSameArgs(sym.type, byName.type, false)) {
3523                     duplicateErasureError(pos, sym, byName);
3524                     sym.flags_field |= CLASH;
3525                     return true;
3526                 } else if ((sym.flags() & MATCH_BINDING) != 0 &&
3527                            (byName.flags() & MATCH_BINDING) != 0 &&
3528                            (byName.flags() & MATCH_BINDING_TO_OUTER) == 0) {
3529                     //this error will be reported separatelly in MatchBindingsComputer
3530                     return false;
3531                 } else {
3532                     duplicateError(pos, byName);
3533                     return false;
3534                 }
3535             }
3536         }
3537         return true;
3538     }
3539 
3540     /** Report duplicate declaration error.
3541      */
3542     void duplicateErasureError(DiagnosticPosition pos, Symbol sym1, Symbol sym2) {
3543         if (!sym1.type.isErroneous() && !sym2.type.isErroneous()) {
3544             log.error(pos, Errors.NameClashSameErasure(sym1, sym2));
3545         }
3546     }
3547 
3548     /**Check that types imported through the ordinary imports don't clash with types imported
3549      * by other (static or ordinary) imports. Note that two static imports may import two clashing
3550      * types without an error on the imports.


< prev index next >