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

Print this page




2663      *  Anything>, arrays of the preceding.
2664      *  }
2665      */
2666     void validateAnnotationType(JCTree restype) {
2667         // restype may be null if an error occurred, so don't bother validating it
2668         if (restype != null) {
2669             validateAnnotationType(restype.pos(), restype.type);
2670         }
2671     }
2672 
2673     void validateAnnotationType(DiagnosticPosition pos, Type type) {
2674         if (type.isPrimitive()) return;
2675         if (types.isSameType(type, syms.stringType)) return;
2676         if ((type.tsym.flags() & Flags.ENUM) != 0) return;
2677         if ((type.tsym.flags() & Flags.ANNOTATION) != 0) return;
2678         if (types.lowerBound(type).tsym == syms.classType.tsym) return;
2679         if (types.isArray(type) && !types.isArray(types.elemtype(type))) {
2680             validateAnnotationType(pos, types.elemtype(type));
2681             return;
2682         }
2683         log.error(pos, "invalid.annotation.member.type");
2684     }
2685 
2686     /**
2687      * "It is also a compile-time error if any method declared in an
2688      * annotation type has a signature that is override-equivalent to
2689      * that of any public or protected method declared in class Object
2690      * or in the interface annotation.Annotation."
2691      *
2692      * @jls 9.6 Annotation Types
2693      */
2694     void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) {
2695         for (Type sup = syms.annotationType; sup.hasTag(CLASS); sup = types.supertype(sup)) {
2696             Scope s = sup.tsym.members();
2697             for (Scope.Entry e = s.lookup(m.name); e.scope != null; e = e.next()) {
2698                 if (e.sym.kind == MTH &&
2699                     (e.sym.flags() & (PUBLIC | PROTECTED)) != 0 &&
2700                     types.overrideEquivalent(m.type, e.sym.type))
2701                     log.error(pos, "intf.annotation.member.clash", e.sym, sup);
2702             }
2703         }


3078 
3079     private boolean validateAnnotation(JCAnnotation a) {
3080         boolean isValid = true;
3081         // collect an inventory of the annotation elements
3082         Set<MethodSymbol> members = new LinkedHashSet<MethodSymbol>();
3083         for (Scope.Entry e = a.annotationType.type.tsym.members().elems;
3084                 e != null;
3085                 e = e.sibling)
3086             if (e.sym.kind == MTH && e.sym.name != names.clinit &&
3087                     (e.sym.flags() & SYNTHETIC) == 0)
3088                 members.add((MethodSymbol) e.sym);
3089 
3090         // remove the ones that are assigned values
3091         for (JCTree arg : a.args) {
3092             if (!arg.hasTag(ASSIGN)) continue; // recovery
3093             JCAssign assign = (JCAssign) arg;
3094             Symbol m = TreeInfo.symbol(assign.lhs);
3095             if (m == null || m.type.isErroneous()) continue;
3096             if (!members.remove(m)) {
3097                 isValid = false;
3098                 log.error(assign.lhs.pos(), "duplicate.annotation.member.value",
3099                           m.name, a.type);
3100             }
3101         }
3102 
3103         // all the remaining ones better have default values
3104         List<Name> missingDefaults = List.nil();
3105         for (MethodSymbol m : members) {
3106             if (m.defaultValue == null && !m.type.isErroneous()) {
3107                 missingDefaults = missingDefaults.append(m.name);
3108             }
3109         }
3110         missingDefaults = missingDefaults.reverse();
3111         if (missingDefaults.nonEmpty()) {
3112             isValid = false;
3113             String key = (missingDefaults.size() > 1)
3114                     ? "annotation.missing.default.value.1"
3115                     : "annotation.missing.default.value";
3116             log.error(a.pos(), key, a.type, missingDefaults);
3117         }
3118 


3403     boolean checkUniqueStaticImport(DiagnosticPosition pos, Symbol sym, Scope s) {
3404         return checkUniqueImport(pos, sym, s, true);
3405     }
3406 
3407     /** Check that single-type import is not already imported or top-level defined,
3408      *  but make an exception for two single-type imports which denote the same type.
3409      *  @param pos           Position for error reporting.
3410      *  @param sym           The symbol.
3411      *  @param s             The scope.
3412      *  @param staticImport  Whether or not this was a static import
3413      */
3414     private boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s, boolean staticImport) {
3415         for (Scope.Entry e = s.lookup(sym.name); e.scope != null; e = e.next()) {
3416             // is encountered class entered via a class declaration?
3417             boolean isClassDecl = e.scope == s;
3418             if ((isClassDecl || sym != e.sym) &&
3419                 sym.kind == e.sym.kind &&
3420                 sym.name != names.error &&
3421                 (!staticImport || !e.isStaticallyImported())) {
3422                 if (!e.sym.type.isErroneous()) {
3423                     String what = e.sym.toString();
3424                     if (!isClassDecl) {
3425                         if (staticImport)
3426                             log.error(pos, "already.defined.static.single.import", what);
3427                         else
3428                         log.error(pos, "already.defined.single.import", what);
3429                     }
3430                     else if (sym != e.sym)
3431                         log.error(pos, "already.defined.this.unit", what);
3432                 }
3433                 return false;
3434             }
3435         }
3436         return true;
3437     }
3438 
3439     /** Check that a qualified name is in canonical form (for import decls).
3440      */
3441     public void checkCanonical(JCTree tree) {
3442         if (!isCanonical(tree))
3443             log.error(tree.pos(), "import.requires.canonical",




2663      *  Anything>, arrays of the preceding.
2664      *  }
2665      */
2666     void validateAnnotationType(JCTree restype) {
2667         // restype may be null if an error occurred, so don't bother validating it
2668         if (restype != null) {
2669             validateAnnotationType(restype.pos(), restype.type);
2670         }
2671     }
2672 
2673     void validateAnnotationType(DiagnosticPosition pos, Type type) {
2674         if (type.isPrimitive()) return;
2675         if (types.isSameType(type, syms.stringType)) return;
2676         if ((type.tsym.flags() & Flags.ENUM) != 0) return;
2677         if ((type.tsym.flags() & Flags.ANNOTATION) != 0) return;
2678         if (types.lowerBound(type).tsym == syms.classType.tsym) return;
2679         if (types.isArray(type) && !types.isArray(types.elemtype(type))) {
2680             validateAnnotationType(pos, types.elemtype(type));
2681             return;
2682         }
2683         log.error(pos, "annotation.invalid.element.type");
2684     }
2685 
2686     /**
2687      * "It is also a compile-time error if any method declared in an
2688      * annotation type has a signature that is override-equivalent to
2689      * that of any public or protected method declared in class Object
2690      * or in the interface annotation.Annotation."
2691      *
2692      * @jls 9.6 Annotation Types
2693      */
2694     void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) {
2695         for (Type sup = syms.annotationType; sup.hasTag(CLASS); sup = types.supertype(sup)) {
2696             Scope s = sup.tsym.members();
2697             for (Scope.Entry e = s.lookup(m.name); e.scope != null; e = e.next()) {
2698                 if (e.sym.kind == MTH &&
2699                     (e.sym.flags() & (PUBLIC | PROTECTED)) != 0 &&
2700                     types.overrideEquivalent(m.type, e.sym.type))
2701                     log.error(pos, "intf.annotation.member.clash", e.sym, sup);
2702             }
2703         }


3078 
3079     private boolean validateAnnotation(JCAnnotation a) {
3080         boolean isValid = true;
3081         // collect an inventory of the annotation elements
3082         Set<MethodSymbol> members = new LinkedHashSet<MethodSymbol>();
3083         for (Scope.Entry e = a.annotationType.type.tsym.members().elems;
3084                 e != null;
3085                 e = e.sibling)
3086             if (e.sym.kind == MTH && e.sym.name != names.clinit &&
3087                     (e.sym.flags() & SYNTHETIC) == 0)
3088                 members.add((MethodSymbol) e.sym);
3089 
3090         // remove the ones that are assigned values
3091         for (JCTree arg : a.args) {
3092             if (!arg.hasTag(ASSIGN)) continue; // recovery
3093             JCAssign assign = (JCAssign) arg;
3094             Symbol m = TreeInfo.symbol(assign.lhs);
3095             if (m == null || m.type.isErroneous()) continue;
3096             if (!members.remove(m)) {
3097                 isValid = false;
3098                 log.error(assign.lhs.pos(), "annotation.duplicate.element",
3099                           m.name, a.type);
3100             }
3101         }
3102 
3103         // all the remaining ones better have default values
3104         List<Name> missingDefaults = List.nil();
3105         for (MethodSymbol m : members) {
3106             if (m.defaultValue == null && !m.type.isErroneous()) {
3107                 missingDefaults = missingDefaults.append(m.name);
3108             }
3109         }
3110         missingDefaults = missingDefaults.reverse();
3111         if (missingDefaults.nonEmpty()) {
3112             isValid = false;
3113             String key = (missingDefaults.size() > 1)
3114                     ? "annotation.missing.default.value.1"
3115                     : "annotation.missing.default.value";
3116             log.error(a.pos(), key, a.type, missingDefaults);
3117         }
3118 


3403     boolean checkUniqueStaticImport(DiagnosticPosition pos, Symbol sym, Scope s) {
3404         return checkUniqueImport(pos, sym, s, true);
3405     }
3406 
3407     /** Check that single-type import is not already imported or top-level defined,
3408      *  but make an exception for two single-type imports which denote the same type.
3409      *  @param pos           Position for error reporting.
3410      *  @param sym           The symbol.
3411      *  @param s             The scope.
3412      *  @param staticImport  Whether or not this was a static import
3413      */
3414     private boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s, boolean staticImport) {
3415         for (Scope.Entry e = s.lookup(sym.name); e.scope != null; e = e.next()) {
3416             // is encountered class entered via a class declaration?
3417             boolean isClassDecl = e.scope == s;
3418             if ((isClassDecl || sym != e.sym) &&
3419                 sym.kind == e.sym.kind &&
3420                 sym.name != names.error &&
3421                 (!staticImport || !e.isStaticallyImported())) {
3422                 if (!e.sym.type.isErroneous()) {
3423                     String what = e.sym.getSimpleName().toString();
3424                     if (!isClassDecl) {
3425                         if (staticImport)
3426                             log.error(pos, "already.defined.static.single.import", what);
3427                         else
3428                         log.error(pos, "already.defined.single.import", what);
3429                     }
3430                     else if (sym != e.sym)
3431                         log.error(pos, "already.defined.this.unit", what);
3432                 }
3433                 return false;
3434             }
3435         }
3436         return true;
3437     }
3438 
3439     /** Check that a qualified name is in canonical form (for import decls).
3440      */
3441     public void checkCanonical(JCTree tree) {
3442         if (!isCanonical(tree))
3443             log.error(tree.pos(), "import.requires.canonical",