Source:
private VariantDeclaration parseVariant(CommonTree variant) throws ParseException { List<CommonTree> children = variant.getChildren(); VariantDeclaration variantDeclaration = null; boolean hasName = false; String variantName = null; boolean hasBody = false; CommonTree variantBody = null; boolean hasTag = false; String variantTag = null; for (CommonTree child : children) { switch (child.getType()) { case CTFParser.VARIANT_NAME: hasName = true; CommonTree variantNameIdentifier = (CommonTree) child.getChild(0); variantName = variantNameIdentifier.getText(); break; case CTFParser.VARIANT_TAG: hasTag = true; CommonTree variantTagIdentifier = (CommonTree) child.getChild(0); variantTag = variantTagIdentifier.getText(); break; case CTFParser.VARIANT_BODY: hasBody = true; variantBody = child; break; default: childTypeError(child); break; } } if (hasBody) { if (hasName && (getCurrentScope().lookupVariant(variantName) != null)) { throw new ParseException("variant " + variantName + " already defined."); } variantDeclaration = new VariantDeclaration(); parseVariantBody(variantBody, variantDeclaration); if (hasName) { getCurrentScope().registerVariant(variantName, variantDeclaration); } } else { if (hasName) { variantDeclaration = getCurrentScope().lookupVariantRecursive( variantName); if (variantDeclaration == null) { throw new ParseException("variant " + variantName + " is not defined"); } } else { throw new ParseException("variant with no name and no body"); } } if (hasTag) { variantDeclaration.setTag(variantTag); IDeclaration decl = getCurrentScope().lookupIdentifierRecursive(variantTag); if (decl == null) { throw new ParseException("Variant tag not found" + ':' + variantTag); } if (!(decl instanceof EnumDeclaration)) { throw new ParseException("Variant tag must be an enum"); } EnumDeclaration tagDecl = (EnumDeclaration) decl; Set<String> intersection = new HashSet<String>(tagDecl.getLabels()); intersection.retainAll(variantDeclaration.getFields().keySet()); if (intersection.isEmpty()) { throw new ParseException("Variant contains no values of the tag, impossible to use: " + variantName); } } return variantDeclaration; }

Target:
private VariantDeclaration parseVariant(CommonTree variant) throws ParseException { List<CommonTree> children = variant.getChildren(); VariantDeclaration variantDeclaration = null; boolean hasName = false; String variantName = null; boolean hasBody = false; CommonTree variantBody = null; boolean hasTag = false; String variantTag = null; for (CommonTree child : children) { switch (child.getType()) { case CTFParser.VARIANT_NAME: hasName = true; CommonTree variantNameIdentifier = (CommonTree) child.getChild(0); variantName = variantNameIdentifier.getText(); break; case CTFParser.VARIANT_TAG: hasTag = true; CommonTree variantTagIdentifier = (CommonTree) child.getChild(0); variantTag = variantTagIdentifier.getText(); break; case CTFParser.VARIANT_BODY: hasBody = true; variantBody = child; break; default: childTypeError(child); break; } } if (hasBody) { if (hasName && (getCurrentScope().lookupVariant(variantName) != null)) { throw new ParseException("variant " + variantName + " already defined."); } variantDeclaration = new VariantDeclaration(); parseVariantBody(variantBody, variantDeclaration); if (hasName) { getCurrentScope().registerVariant(variantName, variantDeclaration); } } else { if (hasName) { variantDeclaration = getCurrentScope().lookupVariantRecursive( variantName); if (variantDeclaration == null) { throw new ParseException("variant " + variantName + " is not defined"); } } else { throw new ParseException("variant with no name and no body"); } } if (hasTag) { variantDeclaration.setTag(variantTag); IDeclaration decl = getCurrentScope().lookupIdentifierRecursive(variantTag); if (decl == null) { throw new ParseException("Variant tag not found: " + variantTag); } if (!(decl instanceof EnumDeclaration)) { throw new ParseException("Variant tag must be an enum: " + variantTag); } EnumDeclaration tagDecl = (EnumDeclaration) decl; Set<String> intersection = new HashSet<String>(tagDecl.getLabels()); intersection.retainAll(variantDeclaration.getFields().keySet()); if (intersection.isEmpty()) { throw new ParseException("Variant contains no values of the tag, impossible to use: " + variantName); } } return variantDeclaration; }