src/share/classes/com/sun/tools/javac/comp/Attr.java
Print this page
*** 113,122 ****
--- 113,124 ----
allowVarargs = source.allowVarargs();
allowEnums = source.allowEnums();
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;
allowInvokedynamic = options.get("invokedynamic") != null;
enableSunApiLintControl = options.get("enableSunApiLintControl") != null;
*** 165,174 ****
--- 167,186 ----
* Switch: allow lint infrastructure to control Sun proprietary
* API warnings.
*/
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.
* No checks are performed if the prototype is a method type.
* It is not necessary in this case since we know that kind and type
*** 884,896 ****
env.dup(tree, env.info.dup(env.info.scope.dup()));
boolean enumSwitch =
allowEnums &&
(seltype.tsym.flags() & Flags.ENUM) != 0;
! if (!enumSwitch)
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<Object> labels = new HashSet<Object>(); // The set of case labels.
boolean hasDefault = false; // Is there a default label?
for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
--- 896,917 ----
env.dup(tree, env.info.dup(env.info.scope.dup()));
boolean enumSwitch =
allowEnums &&
(seltype.tsym.flags() & Flags.ENUM) != 0;
! 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<Object> labels = new HashSet<Object>(); // The set of case labels.
boolean hasDefault = false; // Is there a default label?
for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
*** 907,917 ****
}
} else {
Type pattype = attribExpr(c.pat, switchEnv, seltype);
if (pattype.tag != ERROR) {
if (pattype.constValue() == null) {
! log.error(c.pat.pos(), "const.expr.req");
} else if (labels.contains(pattype.constValue())) {
log.error(c.pos(), "duplicate.case.label");
} else {
labels.add(pattype.constValue());
}
--- 928,939 ----
}
} else {
Type pattype = attribExpr(c.pat, switchEnv, seltype);
if (pattype.tag != ERROR) {
if (pattype.constValue() == null) {
! 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 {
labels.add(pattype.constValue());
}