Defines the AbstractMethod class which represents a token-abstracted Java method.

class AbstractMethod[source]

AbstractMethod(tokens:Union[str, List[str]], delimiter:str=' ')

Creates an AbstractMethod from the given tokens, which can be either:

  • a string with tokens delimited by delimiter (defaults to a single space)
  • a list of tokens

Note that empty tokens are ignored.

method1 = AbstractMethod("private static int METHOD_1 ( ) { return 0 ; }")
method1
private static int METHOD_1 ( ) { return 0 ; }
method2 = AbstractMethod(["public", "double", "METHOD_1", "(", "double", "VAR_1", ")", "{", "return", "VAR_1", ";", "}"])
method2
public double METHOD_1 ( double VAR_1 ) { return VAR_1 ; }

Interact with Edit Operations

AbstractMethod.getEditDistanceTo[source]

AbstractMethod.getEditDistanceTo(other:AbstractMethod)

Returns the Levenshtein edit distance to the AbstractMethod given by other.

method1.getEditDistanceTo(method2)
6

AbstractMethod.getEditOperationsTo[source]

AbstractMethod.getEditOperationsTo(other:AbstractMethod)

Returns the minimal list of basic edit operations (no CompoundOperations), which if applied, would result in the AbstractMethod given by other. The length of the returned list is the Levenshtein distance to other.

method1 = AbstractMethod("private static int METHOD_1 ( ) { return 0 ; }")
method2 = AbstractMethod("public double METHOD_1 ( double VAR_1 ) { return VAR_1 ; }")

operations = method1.getEditOperationsTo(method2)

operations
[DELETE 0,
 REPLACE 0 -> 'public',
 REPLACE 1 -> 'double',
 INSERT 4 -> 'double',
 INSERT 5 -> 'VAR_1',
 REPLACE 9 -> 'VAR_1']

AbstractMethod.applyEditOperation[source]

AbstractMethod.applyEditOperation(operation:Union[InsertOperation, DeleteOperation, ReplaceOperation, CompoundOperation])

Applies the given operation.

AbstractMethod.applyEditOperations[source]

AbstractMethod.applyEditOperations(operations:List[Union[InsertOperation, DeleteOperation, ReplaceOperation, CompoundOperation]])

Applies the given list of operations in order.

method1 = AbstractMethod("private static int METHOD_1 ( ) { return 0 ; }")
method2 = AbstractMethod("public double METHOD_1 ( double VAR_1 ) { return VAR_1 ; }")

operations = method1.getEditOperationsTo(method2)
method1.applyEditOperations(operations)
method1
public double METHOD_1 ( double VAR_1 ) { return VAR_1 ; }
method2
public double METHOD_1 ( double VAR_1 ) { return VAR_1 ; }
method1 == method2
True