--- old/src/share/classes/com/sun/tools/javac/comp/Attr.java 2009-11-02 21:13:02.000000000 -0800 +++ new/src/share/classes/com/sun/tools/javac/comp/Attr.java 2009-11-02 21:13:02.000000000 -0800 @@ -115,6 +115,8 @@ allowBoxing = source.allowBoxing(); allowCovariantReturns = source.allowCovariantReturns(); allowAnonOuterThis = source.allowAnonOuterThis(); + allowStringsInSwitch = source.allowStringsInSwitch(); + sourceName = source.name; relax = (options.get("-retrofit") != null || options.get("-relax") != null); useBeforeDeclarationWarning = options.get("useBeforeDeclarationWarning") != null; @@ -167,6 +169,16 @@ */ boolean enableSunApiLintControl; + /** + * Switch: allow strings in switch? + */ + boolean allowStringsInSwitch; + + /** + * Switch: name of source level; used for error reporting. + */ + String sourceName; + /** Check kind and type of given tree against protokind and prototype. * If check succeeds, store type in tree and return it. * If check fails, store errType in tree and return it. @@ -886,9 +898,18 @@ boolean enumSwitch = allowEnums && (seltype.tsym.flags() & Flags.ENUM) != 0; - if (!enumSwitch) + boolean stringSwitch = false; + if (types.isSameType(seltype, syms.stringType)) { + if (allowStringsInSwitch) { + stringSwitch = true; + } else { + log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName); + } + } + if (!enumSwitch && !stringSwitch) seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType); + // Attribute all cases and // check that there are no duplicate case labels or default clauses. Set labels = new HashSet(); // The set of case labels. @@ -909,7 +930,8 @@ Type pattype = attribExpr(c.pat, switchEnv, seltype); if (pattype.tag != ERROR) { if (pattype.constValue() == null) { - log.error(c.pat.pos(), "const.expr.req"); + log.error(c.pat.pos(), + (stringSwitch ? "string.const.req" : "const.expr.req")); } else if (labels.contains(pattype.constValue())) { log.error(c.pos(), "duplicate.case.label"); } else {