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

changeset 430
8fb9b4be3cb1
parent 383
8109aa93b212
child 504
6eca0895a644
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Oct 30 10:55:00 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Nov 02 21:36:59 2009 -0800
     1.3 @@ -115,6 +115,8 @@
     1.4          allowBoxing = source.allowBoxing();
     1.5          allowCovariantReturns = source.allowCovariantReturns();
     1.6          allowAnonOuterThis = source.allowAnonOuterThis();
     1.7 +        allowStringsInSwitch = source.allowStringsInSwitch();
     1.8 +        sourceName = source.name;
     1.9          relax = (options.get("-retrofit") != null ||
    1.10                   options.get("-relax") != null);
    1.11          useBeforeDeclarationWarning = options.get("useBeforeDeclarationWarning") != null;
    1.12 @@ -167,6 +169,16 @@
    1.13       */
    1.14      boolean enableSunApiLintControl;
    1.15  
    1.16 +    /**
    1.17 +     * Switch: allow strings in switch?
    1.18 +     */
    1.19 +    boolean allowStringsInSwitch;
    1.20 +
    1.21 +    /**
    1.22 +     * Switch: name of source level; used for error reporting.
    1.23 +     */
    1.24 +    String sourceName;
    1.25 +
    1.26      /** Check kind and type of given tree against protokind and prototype.
    1.27       *  If check succeeds, store type in tree and return it.
    1.28       *  If check fails, store errType in tree and return it.
    1.29 @@ -886,7 +898,15 @@
    1.30          boolean enumSwitch =
    1.31              allowEnums &&
    1.32              (seltype.tsym.flags() & Flags.ENUM) != 0;
    1.33 -        if (!enumSwitch)
    1.34 +        boolean stringSwitch = false;
    1.35 +        if (types.isSameType(seltype, syms.stringType)) {
    1.36 +            if (allowStringsInSwitch) {
    1.37 +                stringSwitch = true;
    1.38 +            } else {
    1.39 +                log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName);
    1.40 +            }
    1.41 +        }
    1.42 +        if (!enumSwitch && !stringSwitch)
    1.43              seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
    1.44  
    1.45          // Attribute all cases and
    1.46 @@ -909,7 +929,8 @@
    1.47                      Type pattype = attribExpr(c.pat, switchEnv, seltype);
    1.48                      if (pattype.tag != ERROR) {
    1.49                          if (pattype.constValue() == null) {
    1.50 -                            log.error(c.pat.pos(), "const.expr.req");
    1.51 +                            log.error(c.pat.pos(),
    1.52 +                                      (stringSwitch ? "string.const.req" : "const.expr.req"));
    1.53                          } else if (labels.contains(pattype.constValue())) {
    1.54                              log.error(c.pos(), "duplicate.case.label");
    1.55                          } else {

mercurial