Source:
<code>private void addSymbolsToTree(FileInputImpl fileInput) { for (Scope scope : scopesByRootTree.values()) { if (scope.rootTree instanceof FunctionLike) { FunctionLike funcDef = (FunctionLike) scope.rootTree; for (Symbol symbol : scope.symbols()) { if (funcDef.is(Kind.LAMBDA)) { ((LambdaExpressionImpl) funcDef).addLocalVariableSymbol(symbol); } else { ((FunctionDefImpl) funcDef).addLocalVariableSymbol(symbol); } } } else if (scope.rootTree.is(Kind.CLASSDEF)) { ClassDefImpl classDef = (ClassDefImpl) scope.rootTree; scope.symbols().forEach(classDef::addClassField); scope.instanceAttributesByName.values().forEach(classDef::addInstanceField); Symbol classSymbol = classDef.name().symbol(); Optional.ofNullable(classSymbol) <START> .filter(symbol -> symbol.kind() == Symbol.Kind.CLASS) <END> .ifPresent(symbol -> { ((ClassSymbolImpl) symbol).addMembers(scope.symbols()); List<SymbolImpl> uniqueInstanceAttributes = scope.instanceAttributesByName.values().stream() .filter(instanceSymbol -> !scope.symbolsByName.containsKey(instanceSymbol.name())) .collect(Collectors.toList()); ((ClassSymbolImpl) symbol).addMembers(Collections.unmodifiableCollection(uniqueInstanceAttributes)); }); } else if (scope.rootTree.is(Kind.FILE_INPUT)) { scope.symbols().stream().filter(s -> !scope.builtinSymbols.contains(s)).forEach(fileInput::addGlobalVariables); } else if (scope.rootTree.is(Kind.DICT_COMPREHENSION)) { scope.symbols().forEach(((DictCompExpressionImpl) scope.rootTree)::addLocalVariableSymbol); } else if (scope.rootTree instanceof ComprehensionExpression) { scope.symbols().forEach(((ComprehensionExpressionImpl) scope.rootTree)::addLocalVariableSymbol); } } }</code><technical_language>I add a map operation ClassSymbolImpl cast in order avoid repeated casts below</technical_language>

Target:
private void addSymbolsToTree(FileInputImpl fileInput) { for (Scope scope : scopesByRootTree.values()) { if (scope.rootTree instanceof FunctionLike) { FunctionLike funcDef = (FunctionLike) scope.rootTree; for (Symbol symbol : scope.symbols()) { if (funcDef.is(Kind.LAMBDA)) { ((LambdaExpressionImpl) funcDef).addLocalVariableSymbol(symbol); } else { ((FunctionDefImpl) funcDef).addLocalVariableSymbol(symbol); } } } else if (scope.rootTree.is(Kind.CLASSDEF)) { ClassDefImpl classDef = (ClassDefImpl) scope.rootTree; scope.symbols().forEach(classDef::addClassField); scope.instanceAttributesByName.values().forEach(classDef::addInstanceField); Symbol classSymbol = classDef.name().symbol(); Optional.ofNullable(classSymbol) .filter(symbol -> symbol.kind() == Symbol.Kind.CLASS) .map(ClassSymbolImpl.class::cast) .ifPresent(symbol -> { symbol.addMembers(scope.symbols()); List<SymbolImpl> uniqueInstanceAttributes = scope.instanceAttributesByName.values().stream() .filter(instanceSymbol -> !scope.symbolsByName.containsKey(instanceSymbol.name())) .collect(Collectors.toList()); symbol.addMembers(Collections.unmodifiableCollection(uniqueInstanceAttributes)); }); } else if (scope.rootTree.is(Kind.FILE_INPUT)) { scope.symbols().stream().filter(s -> !scope.builtinSymbols.contains(s)).forEach(fileInput::addGlobalVariables); } else if (scope.rootTree.is(Kind.DICT_COMPREHENSION)) { scope.symbols().forEach(((DictCompExpressionImpl) scope.rootTree)::addLocalVariableSymbol); } else if (scope.rootTree instanceof ComprehensionExpression) { scope.symbols().forEach(((ComprehensionExpressionImpl) scope.rootTree)::addLocalVariableSymbol); } } }