4. API Documentation

The 2 main modules of coastSHARK at the moment are the extract_ast.py and smartshark_plugin.py. The first contains all classes used for AST extraction and the latter contains the code for recursively finding the files in the folder, calling the classes from extract_ast.py and then saving them in the MongoDB. In version 1.0.1 the complexity_java.py module was added. It uses the already extracted AST to compute method complexity metrics.

For saving the information into the MongoDB the util.write_mongo module contains a class that wraps the code for the operations on the pycoshark models.

4.1. util.extract_ast

This module contains the AST node types and the classes for extracting them from Java and Python.

The most important classes here are ExtractAstPython and ExtractAstJava.

class util.extract_ast.ExtractAstJava(filename)[source]

Extracts the AST from .java Files.

Uses the javalang Library.

load()[source]

Read the AST.

class util.extract_ast.ExtractAstPython(filename)[source]

Extracts the AST from .py Files.

Uses the build in ast and the visitor pattern.

load()[source]

Read the AST.

We add a

at the end because 2to3 dies otherwise.

class util.extract_ast.NodePathVisitor[source]

Overwrite ast.NodeVisitor because we also want the level for pretty printing.

This just includes the level for the NodePrintVisitor.

generic_visit(node, level)[source]

Called if no explicit visitor function exists for a node.

visit(node, level=0)[source]

Visit a node.

class util.extract_ast.NodePrintVisitor[source]

Prints AST incl. depth.

generic_visit(node, level)[source]

Called if no explicit visitor function exists for a node.

class util.extract_ast.NodeTypeCountVisitor[source]

Used to count imports, node types and nodes for Python.

generic_visit(node)[source]

Called if no explicit visitor function exists for a node.

util.extract_ast.convert_2to3(file_content, file_name)[source]

Quick helper function to convert python2 to python3 so that we can keep the ast buildin.

4.2. util.complexity_java

In this module we extract complexity measures from Java ASTs.

class util.complexity_java.ComplexityJava(compilation_unit_ast)[source]

Extracts complexity measures from the AST.

cognitive_complexity()[source]

Extract complexity metrics for all methods of the current file.

cognitive_complexity_sonar(method)[source]

Extract cognitive complexity.

Description here: https://www.sonarsource.com/docs/CognitiveComplexity.pdf

cyclomatic_complexity(method)[source]

Extract cyclomatic complexity (not really, we just count branch types).

class util.complexity_java.SourcemeterConversion[source]

This is a utility class for matching against Sourcemeter long_names.

get_sm_long_name(method)[source]

Return a collapsed Sourcemeter long_name from our own method data.

get_sm_long_name2(long_name)[source]

Return a collapsed Sourcemeter long_name.

get_sm_params(long_name)[source]

Take a Sourcemeter long_name for a method and return a Mapping to our method parameter types and return types.

This is used to match overloaded functions.

4.3. util.write_mongo

4.4. smartshark_plugin