Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedfir committed Mar 11, 2022
0 parents commit 7ec8302
Show file tree
Hide file tree
Showing 25 changed files with 1,489 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Project exclude paths
/target/
90 changes: 90 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>edu.lu.uni.serval.mbertloc</groupId>
<artifactId>mBERT-locations</artifactId>
<version>1.0-SNAPSHOT</version>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- versions -->
<version.junit>4.12</version.junit>
<version.memcompiler>1.3.0</version.memcompiler>
<version.spoon>8.2.0</version.spoon>
<!-- plugin versions -->
<pVersion.compiler>3.8.0</pVersion.compiler>
<pVersion.surefire>2.22.1</pVersion.surefire>
</properties>

<!-- SPOON dependency -->
<dependencies>

<dependency>
<groupId>fr.inria.gforge.spoon</groupId>
<artifactId>spoon-core</artifactId>
<version>${version.spoon}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>

</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resource/</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
edu.lu.uni.serval.ibir.main.Main
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>
97 changes: 97 additions & 0 deletions src/main/java/edu/lu/uni/serval/mbertloc/GetLocations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package edu.lu.uni.serval.mbertloc;


import edu.lu.uni.serval.mbertloc.mbertlocator.FileRequest;
import edu.lu.uni.serval.mbertloc.mbertlocator.LocationsCollector;
import edu.lu.uni.serval.mbertloc.mbertlocator.MethodRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GetLocations {

// todo add some variable checking : files exists...
public static FileRequest parseFileArgs(String arg){
String request = arg.replace("-in=", "");
String[] splits = request.split(":");
String filePath = splits[0];
List<MethodRequest> methods = null;
List<Integer> lines = null;
if (splits.length > 1){
if (splits[1].isEmpty()){
lines = new ArrayList<>();
String linesReq = splits[2];
String[] linesReqArr = linesReq.split("@");
for (String s : linesReqArr) {
lines.add(Integer.parseInt(s));
}
} else {
String[] methodsRequests = Arrays.copyOfRange(splits, 1, splits.length);
methods = new ArrayList<>();
// todo test - refactor (move) this somewhere else.
for (String methodsRequest : methodsRequests) {
String[] methodsRequestArr = methodsRequest.split("@");
MethodRequest methodRequest = new MethodRequest(methodsRequestArr[0]);
if (methodsRequestArr.length > 1){
List<Integer> linesPerMethod = new ArrayList<>();
for (int lineIndex = 1; lineIndex< methodsRequestArr.length; lineIndex++){
linesPerMethod.add(Integer.valueOf(methodsRequestArr[lineIndex]));
}
methodRequest.setLinesToMutate(linesPerMethod);
}
methods.add(methodRequest);
}
}
}

FileRequest fileRequest = new FileRequest(filePath,methods,lines);
return fileRequest;
}

public static void main(String...args) throws IOException {
List<FileRequest> fileRequests = new ArrayList<>();
String locationsOutputDirectory = "output";


for (int i = 0; i< args.length; i++ ) {
if (args[i].startsWith("-in=")) {
fileRequests.add(parseFileArgs(args[i]));
} else if (args[i].startsWith("-out=")) {
locationsOutputDirectory = args[i].replace("-out=", "");
}
}

if (fileRequests.isEmpty()) {
correctUssage("No File passed!");
System.exit(1);
}

System.out.println("--- Initialisation ---");
System.out.println("--- Output to the directory: \n" + locationsOutputDirectory);
System.out.println("--- Target locations : \n" + fileRequests);

LocationsCollector locationsCollector = new LocationsCollector(locationsOutputDirectory);
int nextMutantId = 0;
for (FileRequest fileRequest : fileRequests) {
fileRequest.setLocationsCollector(locationsCollector);
fileRequest.setNextMutantId(nextMutantId);
System.out.println("--- locating... \n" + fileRequests + "\n");
fileRequest.locateTokens();
nextMutantId = fileRequest.getNextMutantId();
}

locationsCollector.outputResults();
}

private static void correctUssage(String s) {
System.err.println("error: "+s);
System.err.println("java edu.lu.uni.serval.mbertloc.GetLocations \n" +
"-in=source_file_name:method_name@line@line@line:method_name@line@line@line \n" +
"-in=source_file_name::line@line@line \n" +
"-out=locations_directory\n" );
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package edu.lu.uni.serval.mbertloc.mbertlocations;

import edu.lu.uni.serval.mbertloc.output.CodePosition;
import edu.lu.uni.serval.mbertloc.output.MBertOperators;
import spoon.reflect.code.CtArrayAccess;
import spoon.reflect.code.CtExpression;
import spoon.reflect.cu.SourcePosition;

import static edu.lu.uni.serval.mbertloc.output.MBertOperators.CodeBERTArrayMutator;

public class ArrayAccessLocation extends MBertLocation<CtArrayAccess> {

protected ArrayAccessLocation(int firstMutantId, CtArrayAccess ctElement) throws UnhandledElementException {
super(firstMutantId, ctElement);
}

@Override
protected CodePosition getCodePosition(CtArrayAccess ctElement) throws UnhandledElementException {
CtExpression index_expr = ctElement.getIndexExpression();
SourcePosition position = index_expr.getPosition();
if (position == null || !position.isValidPosition()) return super.getCodePosition(ctElement);
return new CodePosition(position.getSourceStart(), position.getSourceEnd());
}

@Override
protected String getOriginalValueOfTheNode(CtArrayAccess ctElement) {
return ctElement.getIndexExpression().toString();
}

@Override
protected MBertOperators getOperatorByType(CtArrayAccess ctElement) {
return CodeBERTArrayMutator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package edu.lu.uni.serval.mbertloc.mbertlocations;

import edu.lu.uni.serval.mbertloc.output.CodePosition;
import edu.lu.uni.serval.mbertloc.output.MBertOperators;


import spoon.reflect.code.CtAssignment;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.support.reflect.cu.position.SourcePositionImpl;

import static edu.lu.uni.serval.mbertloc.output.MBertOperators.CodeBERTAssignmentMutator;

public class AssignmentLocation extends MBertLocation<CtAssignment> {

protected AssignmentLocation(int firstMutantId, CtAssignment ctElement) throws UnhandledElementException {
super(firstMutantId, ctElement);
}

@Override
protected CodePosition getCodePosition(CtAssignment original) throws UnhandledElementException {
//compute token to mutate position
int start = original.getAssigned().getPosition().getSourceEnd() + 1;
int end = original.getAssignment().getPosition().getSourceStart() - 1;
CompilationUnit origUnit = original.getPosition().getCompilationUnit();
SourcePosition position = new SourcePositionImpl(origUnit, start, end, origUnit.getLineSeparatorPositions());

if (!position.isValidPosition()) return super.getCodePosition(original);
return new CodePosition(position.getSourceStart(), position.getSourceEnd());
}

/**
* @return string to add after the predicted token.
* @see {CodeBERTAssignmentMutator#mutate(spoon.reflect.code.CtAssignment, spoon.reflect.declaration.CtClass)}
*/
@Override
protected String getSuffix() {
return "=";
}

@Override
public int getExpectedMutants() {
return 2 * super.getExpectedMutants(); // 5 with prefix an 5 without.
}

@Override
protected MBertOperators getOperatorByType(CtAssignment ctElement) {
return CodeBERTAssignmentMutator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package edu.lu.uni.serval.mbertloc.mbertlocations;

import edu.lu.uni.serval.mbertloc.output.MBertOperators;
import edu.lu.uni.serval.mbertloc.output.CodePosition;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.support.reflect.cu.position.SourcePositionImpl;

import static edu.lu.uni.serval.mbertloc.output.MBertOperators.CodeBERTBinaryOperatorMutator;

public class BinaryOperatorLocation extends MBertLocation<CtBinaryOperator> {

protected BinaryOperatorLocation(int firstMutantId, CtBinaryOperator ctElement) throws UnhandledElementException {
super(firstMutantId, ctElement);
}

@Override
protected CodePosition getCodePosition(CtBinaryOperator ctElement) throws UnhandledElementException {
int start = ctElement.getLeftHandOperand().getPosition().getSourceEnd()+1;
int end = ctElement.getRightHandOperand().getPosition().getSourceStart() -1;
CompilationUnit origUnit = ctElement.getPosition().getCompilationUnit();
SourcePosition position = new SourcePositionImpl(origUnit,start,end,origUnit.getLineSeparatorPositions());
if (!position.isValidPosition()) return super.getCodePosition(ctElement);
return new CodePosition(position.getSourceStart(), position.getSourceEnd());
}

@Override
protected String getOriginalValueOfTheNode(CtBinaryOperator ctElement) {
return ctElement.getKind().toString();
}

@Override
protected MBertOperators getOperatorByType(CtBinaryOperator ctElement) {
return CodeBERTBinaryOperatorMutator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package edu.lu.uni.serval.mbertloc.mbertlocations;

import edu.lu.uni.serval.mbertloc.mbertlocator.MBertUtils;
import edu.lu.uni.serval.mbertloc.output.CodePosition;
import edu.lu.uni.serval.mbertloc.output.MBertOperators;

import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.reference.CtFieldReference;
import spoon.support.reflect.cu.position.SourcePositionImpl;

import static edu.lu.uni.serval.mbertloc.output.MBertOperators.CodeBERTFieldReferenceMutator;

public class FieldReferenceLocation extends MBertLocation<CtFieldReference> {

protected FieldReferenceLocation(int firstMutantId, CtFieldReference ctElement) throws UnhandledElementException {
super(firstMutantId, ctElement);
}

@Override
protected CodePosition getCodePosition(CtFieldReference ctElement) throws UnhandledElementException {
SourcePosition origPosition = MBertUtils.getSourcePosition(ctElement);
int end = origPosition.getSourceEnd();

int start = end - (getOriginalValueOfTheNode(ctElement).length() - 1);
CompilationUnit origUnit = origPosition.getCompilationUnit();
SourcePosition position = new SourcePositionImpl(origUnit,start,end,origUnit.getLineSeparatorPositions());

if (!position.isValidPosition()) return super.getCodePosition(ctElement);
return new CodePosition(position.getSourceStart(), position.getSourceEnd());
}

@Override
protected String getOriginalValueOfTheNode(CtFieldReference ctElement) {
return ctElement.getSimpleName();
}

@Override
protected MBertOperators getOperatorByType(CtFieldReference ctElement) {
return CodeBERTFieldReferenceMutator;
}
}
Loading

0 comments on commit 7ec8302

Please sign in to comment.