def value(self, ast: AST): lines: List[int] = [] for binary_operator in ast.get_proxy_nodes(ASTNodeType.BINARY_OPERATION): if binary_operator.operator == 'instanceof': lines.append(binary_operator.line) for method_invocation in ast.get_proxy_nodes(ASTNodeType.METHOD_INVOCATION): if method_invocation.member == 'isInstance': lines.append(method_invocation.line) return lines
def value(self, ast: AST): lines: List[int] = [] for method_declaration in ast.get_proxy_nodes( ASTNodeType.METHOD_DECLARATION): if self._check_public_static(method_declaration): lines.append(method_declaration.line) return lines
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for method_declaration in ast.get_proxy_nodes( ASTNodeType.METHOD_DECLARATION): if {'private', 'static'}.issubset(method_declaration.modifiers): lines.append(method_declaration.line) return lines
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for method_declaration in ast.get_proxy_nodes(ASTNodeType.METHOD_DECLARATION, ASTNodeType.CONSTRUCTOR_DECLARATION): if any(len(parameter.type.dimensions) > 0 for parameter in method_declaration.parameters): lines.append(method_declaration.line) return lines
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for if_statement in ast.get_proxy_nodes(ASTNodeType.IF_STATEMENT): if self._is_logical_or_used_in_expression(if_statement.condition) and \ self._is_block_consist_of_single_throw(if_statement.then_statement): lines.append(if_statement.line) return lines
def _get_all_method_invocation_params( self, ast: AST) -> Set[_MethodInvocationParams]: return { self._create_method_invocation_params(method_invocation) for method_invocation in ast.get_proxy_nodes( ASTNodeType.METHOD_INVOCATION) }
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for method_declaration in ast.get_proxy_nodes( ASTNodeType.METHOD_DECLARATION): if 'protected' in method_declaration.modifiers: lines.append(method_declaration.line) return lines
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for node in ast.get_proxy_nodes(ASTNodeType.METHOD_DECLARATION): method_name = node.name if method_name.startswith('get') and self._check_body_nodes(node.body): lines.append(node.line) return sorted(lines)
def value(self, ast: AST) -> List[int]: lines: Set[int] = set() for node in ast.get_proxy_nodes(ASTNodeType.BINARY_OPERATION): if node.operator == '+' and self._check_left_right_operator(node): lines.add(node.line) return sorted(lines)
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for node in ast.get_proxy_nodes(ASTNodeType.CLASS_DECLARATION): class_name = node.name.lower() if any(forbiden_word in class_name for forbiden_word in self.forbiden_words_in_class_names): lines.append(node.line) return lines
def value(self, ast: AST) -> int: rfc = 0 for class_declaration in ast.get_proxy_nodes( ASTNodeType.CLASS_DECLARATION): rfc += self._calculate_class_RFC( ast.get_subtree(class_declaration)) return rfc
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for return_statement in ast.get_proxy_nodes( ASTNodeType.RETURN_STATEMENT): if self._check_null_return_statement(return_statement): lines.append(return_statement.line) return lines
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for if_statement in ast.get_proxy_nodes(ASTNodeType.IF_STATEMENT): if if_statement.else_statement is not None and \ self._is_then_branch_return(if_statement): lines.append(if_statement.line) return lines
def value(self, ast: AST) -> List[int]: lines: List[int] = list() for class_declaration in ast.get_proxy_nodes( ASTNodeType.CLASS_DECLARATION): primary_lines = self.__find_primary(ast, class_declaration.body) if len(primary_lines) > 1: lines.extend(primary_lines) return lines
def _find_local_method_invocations(method_ast: AST) -> Set[str]: invoked_methods: Set[str] = set() for method_invocation in method_ast.get_proxy_nodes( ASTNodeType.METHOD_INVOCATION): if method_invocation.qualifier is None: invoked_methods.add(method_invocation.member) return invoked_methods
def _find_fields_usage(method_ast: AST) -> Set[str]: local_variables: Set[str] = set() for variable_declaration in method_ast.get_proxy_nodes( ASTNodeType.LOCAL_VARIABLE_DECLARATION): local_variables.update(variable_declaration.name) method_declaration = method_ast.get_root() for parameter in method_declaration.parameters: local_variables.add(parameter.name) used_fields: Set[str] = set() for member_reference in method_ast.get_proxy_nodes( ASTNodeType.MEMBER_REFERENCE): if member_reference.qualifier is None and \ member_reference.member not in local_variables: used_fields.add(member_reference.member) return used_fields
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for class_declaration in ast.get_proxy_nodes( ASTNodeType.CLASS_DECLARATION): if len(class_declaration.modifiers & NonFinalClass._allowed_class_modifiers) == 0: lines.append(class_declaration.line) return lines
def value(self, ast: AST) -> List[int]: total_code_lines: List[int] = [] for try_node in ast.get_proxy_nodes(ASTNodeType.TRY_STATEMENT): catch_clauses = try_node.catches if catch_clauses: total_code_lines.extend(self._process_catch( ast, catch_clauses)) return sorted(total_code_lines)
def value(self, ast: AST) -> List[int]: total_code_lines: List[int] = [] for method_declaration in ast.get_proxy_nodes( ASTNodeType.METHOD_DECLARATION): try_nodes = list( ast.get_subtree(method_declaration).get_proxy_nodes( ASTNodeType.TRY_STATEMENT)) if len(try_nodes) > 1: total_code_lines.append(method_declaration.line) return total_code_lines
def value(self, ast: AST) -> List[int]: lines: List[int] = list() for method_declaration in ast.get_proxy_nodes( ASTNodeType.METHOD_DECLARATION): for bin_operation in ast.get_subtree( method_declaration).get_proxy_nodes( ASTNodeType.BINARY_OPERATION): if self._check_null(bin_operation): lines.append(bin_operation.operandr.line) return lines
def value(self, ast: AST) -> List[int]: numbers: List[int] = [] method_nodes = ast.get_proxy_nodes(ASTNodeType.METHOD_DECLARATION) for node in method_nodes: for new_node in method_nodes: if node.node_index < new_node.node_index and self._is_method_names_close( node, new_node): numbers.extend([node.line, new_node.line]) return sorted(list(set(numbers)))
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for node in ast.get_proxy_nodes(ASTNodeType.CLASS_CREATOR, ASTNodeType.METHOD_INVOCATION, ASTNodeType.THIS): selectors_qty = self._get_selectors_qty(node) if selectors_qty > MethodChainFind._allowed_number_of_selectord[ node.node_type]: lines.append(node.line) return lines
def value(self, ast: AST) -> List[int]: lines = set() invocatios_types = [ ASTNodeType.METHOD_INVOCATION, ASTNodeType.EXPLICIT_CONSTRUCTOR_INVOCATION, ASTNodeType.CLASS_CREATOR ] for node in ast.get_proxy_nodes(*invocatios_types): for argument in node.arguments: if (argument.node_type == ASTNodeType.LITERAL) and (argument.value == "null"): lines.add(argument.line) for node in ast.get_proxy_nodes(ASTNodeType.TERNARY_EXPRESSION): if self.__is_null(node.if_false) or self.__is_null(node.if_true): lines.add(node.line) lst = sorted(lines) return lst
def _collect_method_variables_names(self, ast: AST, node: ASTNode) -> Dict[str, int]: assert node.node_type == ASTNodeType.METHOD_DECLARATION vars_lines: Dict[str, int] = {} for local_var_node in ast.get_proxy_nodes(ASTNodeType.LOCAL_VARIABLE_DECLARATION): for var_declaration in local_var_node.declarators: var_name = var_declaration.name if var_name not in vars_lines: # to filter not complex names temp_name = re.split('([A-Z][^A-Z]*)', var_name) if len(temp_name) > 1: vars_lines[var_name] = local_var_node.line return vars_lines
def value(self, ast: AST) -> List[int]: lines = [] for node in ast.get_proxy_nodes(ASTNodeType.CONSTRUCTOR_DECLARATION): exp_ctrs_decls: List[Any] = [] other_statements: List[Any] = [] for statement in node.body: self.traverse(statement, exp_ctrs_decls, other_statements) if len(exp_ctrs_decls) > 0: if len(other_statements) > 0: lines.append(node.line) return lines
def value(self, ast: AST) -> int: metric = 0 for node in ast.get_proxy_nodes(*NCSSMetric._keyword_node_types, *NCSSMetric._declarations_node_types, *NCSSMetric._misc_node_types): metric += 1 if node.node_type == ASTNodeType.IF_STATEMENT and self._has_pure_else_statements( node): metric += 1 elif node.node_type == ASTNodeType.TRY_STATEMENT and self._has_finally_block( node): metric += 1 return metric
def _calculate_class_fan_out(self, java_class: AST) -> int: class_declaration = java_class.get_root() assert class_declaration.node_type == ASTNodeType.CLASS_DECLARATION used_classes_names: Set[str] = set() for type_reference in java_class.get_proxy_nodes( ASTNodeType.REFERENCE_TYPE): used_class_name = self._get_class_name_from_type_reference( type_reference) if used_class_name not in FanOut._excluded_class_names: used_classes_names.add(used_class_name) # remove name of the class used_classes_names -= {class_declaration.name} return len(used_classes_names)
def _extract_semantic_from_ast(statement_ast: AST) -> StatementSemantic: statement_semantic = StatementSemantic() for node in statement_ast.get_proxy_nodes(ASTNodeType.MEMBER_REFERENCE, ASTNodeType.METHOD_INVOCATION, ASTNodeType.VARIABLE_DECLARATOR): if node.node_type == ASTNodeType.MEMBER_REFERENCE: statement_semantic.used_variables.add(node.member) if node.qualifier is not None: statement_semantic.used_objects.add(node.qualifier) elif node.node_type == ASTNodeType.METHOD_INVOCATION: statement_semantic.used_methods.add(node.member) if node.qualifier is not None: statement_semantic.used_objects.add(node.qualifier) elif node.node_type == ASTNodeType.VARIABLE_DECLARATOR: statement_semantic.used_variables.add(node.name) return statement_semantic
def value(self, ast: AST) -> List[int]: lines: List[int] = [] excluded_nodes: List[int] = [] for method_declaration in ast.get_proxy_nodes( ASTNodeType.METHOD_DECLARATION, ASTNodeType.CONSTRUCTOR_DECLARATION): method_throw_names = method_declaration.throws for try_node in ast.get_subtree( method_declaration).get_proxy_nodes( ASTNodeType.TRY_STATEMENT): if method_throw_names is not None and \ try_node.catches is not None and \ self._is_redundant(method_throw_names, try_node): lines.append(try_node.line) for lambda_node in ast.get_subtree( method_declaration).get_proxy_nodes( ASTNodeType.LAMBDA_EXPRESSION): excluded_nodes.extend( self._get_lambda_try_nodes(ast, lambda_node)) return sorted(list(set(lines).difference(set(excluded_nodes))))
def value(self, ast: AST) -> List[int]: lines: List[int] = [] for field in ast.get_proxy_nodes(ASTNodeType.FIELD_DECLARATION): if 'final' not in field.modifiers: lines.append(field.line) return lines