private void checkSwitchExprType() { boolean validType = false; for (Class<?> clazz : supportedTypes) { if (MetaClassFactory.get(clazz).isAssignableFrom(switchExprStmt.getType().asBoxed())) { validType = true; break; } } if (!validType) throw new InvalidTypeException( "Type not permitted in switch statements:" + switchExprStmt.getType().getFullyQualifiedName()); }
@Override public String generate(Context context) { StringBuilder buf = new StringBuilder("switch ("); if (switchExpr == null) { buf.append(switchExprStmt.generate(context)).append(") {\n "); } else { buf.append(switchExpr).append(") {\n "); } checkSwitchExprType(); if (!caseBlocks.isEmpty()) { for (LiteralValue<?> value : caseBlocks.keySet()) { if (!switchExprStmt .getType() .getErased() .asBoxed() .isAssignableFrom(value.getType().getErased())) { throw new InvalidTypeException( value.generate(context) + " is not a valid value for " + switchExprStmt.getType().getFullyQualifiedName()); } // case labels must be unqualified String val = value.generate(context); int idx = val.lastIndexOf('.'); if (idx != -1) { val = val.substring(idx + 1); } buf.append("case ") .append(val) .append(": ") .append(getCaseBlock(value).generate(Context.create(context))) .append("\n"); } } if (defaultBlock != null) { buf.append("default: ").append(defaultBlock.generate(Context.create(context))).append("\n"); } return buf.append("}").toString(); }