From 7ec83022fda8387d85561e19218638a9ea4e34f9 Mon Sep 17 00:00:00 2001 From: ahmed khanfir Date: Fri, 11 Mar 2022 12:47:56 +0100 Subject: [PATCH] Initial commit. --- .gitignore | 2 + pom.xml | 90 ++++++++++ .../lu/uni/serval/mbertloc/GetLocations.java | 97 ++++++++++ .../mbertlocations/ArrayAccessLocation.java | 34 ++++ .../mbertlocations/AssignmentLocation.java | 50 ++++++ .../BinaryOperatorLocation.java | 37 ++++ .../FieldReferenceLocation.java | 42 +++++ .../mbertlocations/InvocationLocation.java | 45 +++++ .../mbertlocations/MBertLocation.java | 130 ++++++++++++++ .../mbertlocations/TypeReferenceLocation.java | 40 +++++ .../mbertlocations/UnaryOperatorLocation.java | 54 ++++++ .../mbertloc/mbertlocator/FileRequest.java | 168 ++++++++++++++++++ .../mbertlocator/LocationsCollector.java | 94 ++++++++++ .../mbertloc/mbertlocator/MBertUtils.java | 87 +++++++++ .../mbertloc/mbertlocator/MethodRequest.java | 57 ++++++ .../mbertloc/output/ClassLocations.java | 84 +++++++++ .../serval/mbertloc/output/CodePosition.java | 46 +++++ .../serval/mbertloc/output/FileLocations.java | 88 +++++++++ .../serval/mbertloc/output/LineLocations.java | 61 +++++++ .../uni/serval/mbertloc/output/Location.java | 31 ++++ .../mbertloc/output/MBertOperators.java | 17 ++ .../uni/serval/mbertloc/output/Mappable.java | 18 ++ .../mbertloc/output/MethodLocations.java | 93 ++++++++++ .../uni/serval/mbertloc/output/Queryable.java | 6 + .../uni/serval/mbertloc/utils/GsonHolder.java | 18 ++ 25 files changed, 1489 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/GetLocations.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/ArrayAccessLocation.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/AssignmentLocation.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/BinaryOperatorLocation.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/FieldReferenceLocation.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/InvocationLocation.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/MBertLocation.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/TypeReferenceLocation.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/UnaryOperatorLocation.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/FileRequest.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/LocationsCollector.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/MBertUtils.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/MethodRequest.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/output/ClassLocations.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/output/CodePosition.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/output/FileLocations.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/output/LineLocations.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/output/Location.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/output/MBertOperators.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/output/Mappable.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/output/MethodLocations.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/output/Queryable.java create mode 100644 src/main/java/edu/lu/uni/serval/mbertloc/utils/GsonHolder.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..744289d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Project exclude paths +/target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3b22527 --- /dev/null +++ b/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + + edu.lu.uni.serval.mbertloc + mBERT-locations + 1.0-SNAPSHOT + + + + UTF-8 + + 4.12 + 1.3.0 + 8.2.0 + + 3.8.0 + 2.22.1 + + + + + + + fr.inria.gforge.spoon + spoon-core + ${version.spoon} + + + junit + junit + ${version.junit} + + + + com.google.code.gson + gson + 2.9.0 + + + + + + + + src/main/resource/ + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 11 + 11 + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + + + + edu.lu.uni.serval.ibir.main.Main + + + + + jar-with-dependencies + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/GetLocations.java b/src/main/java/edu/lu/uni/serval/mbertloc/GetLocations.java new file mode 100644 index 0000000..8180c9c --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/GetLocations.java @@ -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 methods = null; + List 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 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 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" ); + } + + +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/ArrayAccessLocation.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/ArrayAccessLocation.java new file mode 100644 index 0000000..b7cce3c --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/ArrayAccessLocation.java @@ -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 { + + 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; + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/AssignmentLocation.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/AssignmentLocation.java new file mode 100644 index 0000000..943a968 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/AssignmentLocation.java @@ -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 { + + 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; + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/BinaryOperatorLocation.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/BinaryOperatorLocation.java new file mode 100644 index 0000000..efc616f --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/BinaryOperatorLocation.java @@ -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 { + + 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; + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/FieldReferenceLocation.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/FieldReferenceLocation.java new file mode 100644 index 0000000..85ca636 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/FieldReferenceLocation.java @@ -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 { + + 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; + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/InvocationLocation.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/InvocationLocation.java new file mode 100644 index 0000000..480a9f3 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/InvocationLocation.java @@ -0,0 +1,45 @@ +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.CtInvocation; +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.CodeBERTInvocationMutator; + +public class InvocationLocation extends MBertLocation { + + protected InvocationLocation(int firstMutantId, CtInvocation ctElement) throws UnhandledElementException { + super(firstMutantId, ctElement); + } + + @Override + protected CodePosition getCodePosition(CtInvocation original) throws UnhandledElementException { + String originalOp = getOriginalValueOfTheNode(original); + + //compute token to mutate position + int start = original.getPosition().getSourceStart(); + if (original.getTarget()!= null && !original.getTarget().isImplicit() && original.getTarget().toString().length() > 0) + start = original.getTarget().getPosition().getSourceStart() + original.getTarget().toString().length()+1; + int end = start + originalOp.length()-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()); + } + + @Override + protected String getOriginalValueOfTheNode(CtInvocation ctElement) { + return ctElement.getExecutable().getSimpleName(); + } + + @Override + protected MBertOperators getOperatorByType(CtInvocation ctElement) { + return CodeBERTInvocationMutator; + } + + +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/MBertLocation.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/MBertLocation.java new file mode 100644 index 0000000..2882740 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/MBertLocation.java @@ -0,0 +1,130 @@ +package edu.lu.uni.serval.mbertloc.mbertlocations; + +import edu.lu.uni.serval.mbertloc.output.CodePosition; +import edu.lu.uni.serval.mbertloc.output.Location; + +import edu.lu.uni.serval.mbertloc.output.MBertOperators; +import spoon.reflect.code.*; +import spoon.reflect.cu.SourcePosition; +import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtElement; +import spoon.reflect.reference.CtFieldReference; +import spoon.reflect.reference.CtTypeReference; + +import static edu.lu.uni.serval.mbertloc.output.MBertOperators.CodeBERTLiteralMutator; + +public class MBertLocation extends Location { + + private final static int MAX_EXPECTED_TOKENS = 5; + + /** + * @param firstMutantId + * @param ctElement + * @return + * @see {CodeBERTOperatorMutator#process(spoon.reflect.declaration.CtElement)} + */ + public static MBertLocation createMBertLocation(int firstMutantId, CtElement ctElement) throws UnhandledElementException { + if (ctElement instanceof CtBinaryOperator) { + return new BinaryOperatorLocation(firstMutantId, (CtBinaryOperator) ctElement); + } else if (ctElement instanceof CtUnaryOperator) { + return new UnaryOperatorLocation(firstMutantId, (CtUnaryOperator) ctElement); + } else if (ctElement instanceof CtAssignment) { + return new AssignmentLocation(firstMutantId, (CtAssignment) ctElement); + } else if (ctElement instanceof CtArrayRead || + ctElement instanceof CtArrayWrite) { + return new ArrayAccessLocation(firstMutantId, (CtArrayAccess) ctElement); + } else if (ctElement instanceof CtFieldReference) { + return new FieldReferenceLocation(firstMutantId, (CtFieldReference) ctElement); + } else if (ctElement instanceof CtTypeReference) { + return new TypeReferenceLocation(firstMutantId, (CtTypeReference) ctElement); + } else if (ctElement instanceof CtInvocation) { + return new InvocationLocation(firstMutantId, (CtInvocation) ctElement); + } else { + return new MBertLocation(firstMutantId, ctElement); + } + } + + + protected MBertLocation(int firstMutantId, T ctElement) throws UnhandledElementException { + this.operator = getOperatorByType(ctElement).name(); + this.nodeType = getNodeTypeName(ctElement); + this.firstMutantId = firstMutantId; + this.codePosition = getCodePosition(ctElement); + this.node = getOriginalValueOfTheNode(ctElement); + this.suffix = getSuffix(); + } + + protected String getOriginalValueOfTheNode(T ctElement) { + return ctElement.toString(); + } + + + protected CodePosition getCodePosition(T ctElement) throws UnhandledElementException { + SourcePosition pos = ctElement.getPosition(); + if (pos == null || !pos.isValidPosition()) { + throw new UnhandledElementException(getNodeTypeName(ctElement), "getCodePosition = " + pos); + } + return new CodePosition(pos.getSourceStart(), pos.getSourceEnd()); + } + + protected CtClass getParentClass(T ctElement) { + CtClass ctClass = ctElement.getParent(CtClass.class); + return ctClass; + } + + protected String getParentClassName(T ctElement) { + return getParentClass(ctElement).getSimpleName(); + } + + protected Class getNodeType(T ctElement) { + return ctElement.getClass(); + } + + protected String getNodeTypeName(T ctElement) { + return getNodeType(ctElement).getSimpleName(); + } + + public int getExpectedMutants(){ + return MAX_EXPECTED_TOKENS; + } + + /** + * @see {CodeBERTOperatorMutator#process(spoon.reflect.declaration.CtElement)} + */ + protected MBertOperators getOperatorByType(T ctElement) throws UnhandledElementException { + if (ctElement instanceof CtVariableRead || + ctElement instanceof CtVariableWrite || + ctElement instanceof CtConditional || + ctElement instanceof CtThisAccess || + ctElement instanceof CtLiteral) { + return CodeBERTLiteralMutator; + } else { + throw new UnhandledElementException(getNodeTypeName(ctElement), "operatorByType"); + } + } + + protected String getSuffix() { + return ""; + } + + public static class UnhandledElementException extends Exception { + private final String nodeType; + + public UnhandledElementException(String nodeType, String message) { + super(message); + this.nodeType = nodeType; + } + + public String getNodeType() { + return nodeType; + } + + @Override + public String toString() { + return "UnhandledElementException{" + + "nodeType='" + nodeType + '\'' + + '}'; + } + } + +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/TypeReferenceLocation.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/TypeReferenceLocation.java new file mode 100644 index 0000000..aebc7b7 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/TypeReferenceLocation.java @@ -0,0 +1,40 @@ +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.CtTypeReference; +import spoon.support.reflect.cu.position.SourcePositionImpl; + +import static edu.lu.uni.serval.mbertloc.output.MBertOperators.CodeBERTTypeReferenceMutator; + +public class TypeReferenceLocation extends MBertLocation { + + protected TypeReferenceLocation(int firstMutantId, CtTypeReference ctElement) throws UnhandledElementException { + super(firstMutantId, ctElement); + } + + @Override + protected CodePosition getCodePosition(CtTypeReference ctElement) throws UnhandledElementException { + String originalStr = getOriginalValueOfTheNode(ctElement); + SourcePosition origPosition = MBertUtils.getSourcePosition(ctElement); + + //compute token to mutate position + int start = origPosition.getSourceStart(); + int end = start + originalStr.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 MBertOperators getOperatorByType(CtTypeReference ctElement) { + return CodeBERTTypeReferenceMutator; + } + +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/UnaryOperatorLocation.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/UnaryOperatorLocation.java new file mode 100644 index 0000000..585af3b --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocations/UnaryOperatorLocation.java @@ -0,0 +1,54 @@ +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.CtUnaryOperator; +import spoon.reflect.code.UnaryOperatorKind; +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.CodeBERTUnaryOperatorMutator; + +public class UnaryOperatorLocation extends MBertLocation { + + protected UnaryOperatorLocation(int firstMutantId, CtUnaryOperator ctElement) throws UnhandledElementException { + super(firstMutantId, ctElement); + } + + @Override + protected CodePosition getCodePosition(CtUnaryOperator original) throws UnhandledElementException { + int start = original.getPosition().getSourceStart(); + int end = original.getPosition().getSourceEnd(); + if (isPre(original.getKind())) + end = original.getOperand().getPosition().getSourceStart() - 1; + else + start = original.getOperand().getPosition().getSourceEnd() + 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()); + } + + /** + * @see {mbert.spoon.reflect.code.UnaryOperatorKind} + * @param op + * @return + */ + private boolean isPre(UnaryOperatorKind op) { + return op != UnaryOperatorKind.POSTDEC && op != UnaryOperatorKind.POSTINC; + } + + @Override + protected String getOriginalValueOfTheNode(CtUnaryOperator ctElement) { + return ctElement.getKind().toString(); + } + + @Override + protected MBertOperators getOperatorByType(CtUnaryOperator ctElement) { + return CodeBERTUnaryOperatorMutator; + } + +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/FileRequest.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/FileRequest.java new file mode 100644 index 0000000..6c172f3 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/FileRequest.java @@ -0,0 +1,168 @@ +package edu.lu.uni.serval.mbertloc.mbertlocator; + +import edu.lu.uni.serval.mbertloc.mbertlocations.MBertLocation; +import edu.lu.uni.serval.mbertloc.output.CodePosition; +import spoon.Launcher; +import spoon.reflect.CtModel; +import spoon.reflect.cu.SourcePosition; +import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtElement; +import spoon.reflect.declaration.CtMethod; +import spoon.reflect.visitor.filter.TypeFilter; + +import java.util.List; + +import static edu.lu.uni.serval.mbertloc.mbertlocator.MBertUtils.getSourcePosition; +import static edu.lu.uni.serval.mbertloc.mbertlocator.MBertUtils.isToBeProcessed; + + +public class FileRequest { + private LocationsCollector locationsCollector; + private String javaFilePath; + private List methodsToMutate; + private List linesToMutate; + + private int nextMutantId = 0; + + public FileRequest(String javaFilePath, LocationsCollector locationsCollector) { + this.javaFilePath = javaFilePath; + this.locationsCollector = locationsCollector; + } + + public FileRequest(String filePath, List methods, List lines) { + this.javaFilePath = filePath; + this.linesToMutate = lines; + this.methodsToMutate = methods; + } + + public String getJavaFilePath() { + return javaFilePath; + } + + public void setJavaFilePath(String javaFilePath) { + this.javaFilePath = javaFilePath; + } + + public List getMethodsToMutate() { + return methodsToMutate; + } + + public void setMethodsToMutate(List methodsToMutate) { + this.methodsToMutate = methodsToMutate; + } + + public List getLinesToMutate() { + return linesToMutate; + } + + public void setLinesToMutate(List linesToMutate) { + this.linesToMutate = linesToMutate; + } + + private Launcher createLauncher() { + Launcher l = new Launcher(); + l.addInputResource(javaFilePath); + l.getEnvironment().setCommentEnabled(false); + CtModel model = l.buildModel(); + return l; + } + + /** + * collects a list of mutation locations. + */ + public void locateTokens() { + Launcher l = createLauncher(); + + CtClass origClass = (CtClass) l.getFactory().Package().getRootPackage() + .getElements(new TypeFilter(CtClass.class)).get(0); + + // iterate on each method + List methodsToBeMutated = origClass.getElements(arg0 -> (arg0 instanceof CtMethod && isMethodToMutate((CtMethod) arg0))); + + for (CtElement met : methodsToBeMutated) { + // iterate on each method + CtMethod method = (CtMethod) met; + // mutate the element + SourcePosition sourcePosition = getSourcePosition(method); + int methodStartLineNumber = sourcePosition.getLine(); + int methodEndLine = sourcePosition.getEndLine(); + CodePosition methodCodePosition = new CodePosition(sourcePosition.getSourceStart(), sourcePosition.getSourceEnd()); + + List elementsToBeMutated = method.getElements(arg0 -> + isToBeProcessed(arg0) && isLineToMutate(getSourcePosition(arg0))); + + for (CtElement e : elementsToBeMutated) { + + try { + locationsCollector + .addLocation(javaFilePath, origClass.getQualifiedName(), + method.getSignature(), + getSourcePosition(e).getLine(), + MBertLocation.createMBertLocation(nextMutantId, e), + methodStartLineNumber, methodEndLine, methodCodePosition); + nextMutantId += 5; + } catch (MBertLocation.UnhandledElementException exception) { + locationsCollector.addUnhandledMutations(exception.getNodeType()); + System.err.println(exception); + } + + } + } + } + + private boolean isMethodToMutate(CtMethod arg0) { + if ((methodsToMutate == null || methodsToMutate.isEmpty()) && (linesToMutate == null || linesToMutate.isEmpty())) // exhaustive search. + { + System.out.println("Exhaustive search in " + javaFilePath + " \n - no line or method specified."); + return true; + } + if (methodsToMutate != null) { + for (MethodRequest methodRequest : methodsToMutate) { + if (methodRequest.getMethodName().equals(arg0.getSimpleName())) return true; + } + } + if (linesToMutate != null && !linesToMutate.isEmpty()) { + SourcePosition sourcePosition = getSourcePosition(arg0); + int startLine = sourcePosition.getLine(); + int endLine = sourcePosition.getEndLine(); + for (Integer line : linesToMutate) { + if (startLine <= line && endLine >= line) return true; + } + } + return false; + } + + + public boolean isLineToMutate(SourcePosition e) { + //is the line selected to be mutated? + if (linesToMutate.isEmpty()) + return true; + if (linesToMutate.contains(e.getLine())) + return true; + return false; + } + + @Override + public String toString() { + return "FileRequest{" + + "javaFilePath='" + javaFilePath + '\'' + + ", methodsToMutate=" + methodsToMutate + + ", linesToMutate=" + linesToMutate + + ", mutantId=" + nextMutantId + + '}'; + } + + public void setLocationsCollector(LocationsCollector locationsCollector) { + this.locationsCollector = locationsCollector; + } + + + public int getNextMutantId() { + return nextMutantId; + } + + public void setNextMutantId(int nextMutantId) { + this.nextMutantId = nextMutantId; + } + +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/LocationsCollector.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/LocationsCollector.java new file mode 100644 index 0000000..1b4a213 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/LocationsCollector.java @@ -0,0 +1,94 @@ +package edu.lu.uni.serval.mbertloc.mbertlocator; + +import com.google.gson.Gson; +import edu.lu.uni.serval.mbertloc.output.CodePosition; +import edu.lu.uni.serval.mbertloc.output.FileLocations; +import edu.lu.uni.serval.mbertloc.output.Location; +import edu.lu.uni.serval.mbertloc.output.Mappable; +import edu.lu.uni.serval.mbertloc.utils.GsonHolder; + + +import java.io.FileWriter; +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * -DPRINT_JSON_STATS=true -DSTATS_DIR_PATH=/Users/ahmed.khanfir/IdeaProjects/codebertmutation/CLI_40/mutants/ + * Singleton class to collect all mutants. + */ +public class LocationsCollector extends Mappable { + + private String locationsFileName = "locations.json"; + + private final List fileLocations; + private final Map unhandledTokens; + private String locationsDirPath; + + + public LocationsCollector(String locationsDirPath) { + this.fileLocations = new ArrayList<>(); + this.unhandledTokens = new HashMap<>(); + this.locationsDirPath = locationsDirPath; + } + + + public void outputResults() { + + // print the results to file as json. + Gson gson = GsonHolder.getGson(); + try { + Files.createDirectories(Paths.get(locationsDirPath)); + String statsFilePath = Paths.get(locationsDirPath).resolve(locationsFileName).toString(); + System.out.println("printing_json_results in " + statsFilePath); + FileWriter writer = new FileWriter(statsFilePath); + gson.toJson(fileLocations, writer); + writer.flush(); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + System.out.println("++++"); + System.out.println(gson.toJson(fileLocations)); + } + } + + public void addLocation(String fileToBeMutated, String className, + String methodSignature, + int line, + Location location, int methodStartLineNumber, int methodEndLine, CodePosition methodCodePosition) { + FileLocations filePrediction = getChildrenByQuery(fileToBeMutated); + if (filePrediction == null) { + filePrediction = FileLocations.newInstance(fileToBeMutated, className, methodSignature, line, location, + methodStartLineNumber, methodEndLine, methodCodePosition); + fileLocations.add(filePrediction); + } else { + filePrediction.addPredictions(className, methodSignature, line, location, + methodStartLineNumber, methodEndLine, methodCodePosition); + } + } + + + @Override + public List getItems() { + return fileLocations; + } + + public void addUnhandledMutations(String nodeType) { + Integer count = unhandledTokens.get(nodeType); + if (count == null) + count = 0; + unhandledTokens.put(nodeType, count + 1); + } + + public void outputUnhandledTokens(){ + System.out.println(" - unhandled tokens stats : \ncount , nodeType \n" ); + for (String nodeType : unhandledTokens.keySet()) { + System.out.println(unhandledTokens.get(nodeType) +" , " +nodeType +" \n"); + } + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/MBertUtils.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/MBertUtils.java new file mode 100644 index 0000000..1ae221d --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/MBertUtils.java @@ -0,0 +1,87 @@ +package edu.lu.uni.serval.mbertloc.mbertlocator; + +import spoon.reflect.code.*; +import spoon.reflect.cu.SourcePosition; +import spoon.reflect.declaration.CtAnnotation; +import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtElement; +import spoon.reflect.declaration.CtMethod; +import spoon.reflect.reference.CtFieldReference; +import spoon.reflect.reference.CtTypeReference; + +public final class MBertUtils { + + private MBertUtils() { + } + + public static boolean inheritsFromAssertion(CtElement e) { + if (e == null || e instanceof CtMethod || e instanceof CtClass) + return false; + if (e instanceof CtAssert) + return true; + if (e.getParent() == null) + return false; + + return inheritsFromAssertion(e.getParent()); + } + + + public static SourcePosition getSourcePosition(CtElement e) { + if (e == null) + return null; + if (e.getPosition() != null && e.getPosition().isValidPosition()) + return e.getPosition(); + if (e.getParent() != null) + return getSourcePosition(e.getParent()); + return null; + } + + + + public static boolean isImplicit(CtElement e) { + if (e == null || e instanceof CtMethod || e instanceof CtClass) + return false; + if (e.isImplicit()) + return true; + if (e.getParent() == null) + return false; + + return isImplicit(e.getParent()); + } + + public static boolean isToBeProcessed(CtElement candidate) { + //first we list exceptions + if (isImplicit(candidate)) + return false; + if (candidate instanceof CtConstructorCall || + candidate instanceof CtTypeAccess || + candidate instanceof CtNewArray || + candidate instanceof CtAnnotation || + inheritsFromAssertion(candidate) + ) + return false; + if (candidate instanceof CtExpression + || candidate instanceof CtFieldReference) + return true; + if (candidate instanceof CtTypeReference && candidate.getParent() != null + && candidate.getParent() instanceof CtTypeAccess + && !inheritsFromConstructorCall(candidate)) { + return true; + } + return false; + } + + + public static boolean inheritsFromConstructorCall(CtElement e) { + if (e == null || e instanceof CtMethod || e instanceof CtClass) + return false; + if (e instanceof CtConstructorCall) + return true; + if (e.getParent() == null) + return false; + + return inheritsFromConstructorCall(e.getParent()); + } + + +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/MethodRequest.java b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/MethodRequest.java new file mode 100644 index 0000000..07bcb85 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/mbertlocator/MethodRequest.java @@ -0,0 +1,57 @@ +package edu.lu.uni.serval.mbertloc.mbertlocator; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class MethodRequest { + private String methodName; + private List linesToMutate; + + public MethodRequest(String methodName, List linesToMutate) { + this.methodName = methodName; + this.linesToMutate = linesToMutate; + } + + public MethodRequest(String methodName) { + this.methodName = methodName; + this.linesToMutate = new ArrayList<>(); + } + + public String getMethodName() { + return methodName; + } + + public void setMethodName(String methodName) { + this.methodName = methodName; + } + + public List getLinesToMutate() { + return linesToMutate; + } + + public void setLinesToMutate(List linesToMutate) { + this.linesToMutate = linesToMutate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MethodRequest that = (MethodRequest) o; + return Objects.equals(methodName, that.methodName) && Objects.equals(linesToMutate, that.linesToMutate); + } + + @Override + public int hashCode() { + return Objects.hash(methodName, linesToMutate); + } + + @Override + public String toString() { + return "MethodRequest{" + + "methodName='" + methodName + '\'' + + ", linesToMutate=" + linesToMutate + + '}'; + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/output/ClassLocations.java b/src/main/java/edu/lu/uni/serval/mbertloc/output/ClassLocations.java new file mode 100644 index 0000000..6bcfd68 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/output/ClassLocations.java @@ -0,0 +1,84 @@ +package edu.lu.uni.serval.mbertloc.output; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class ClassLocations extends Mappable implements Queryable { + + + private String qualifiedName; + private List methodPredictions; + + public ClassLocations() { + } + + private ClassLocations(String qualifiedName) { + this.qualifiedName = qualifiedName; + } + + public static ClassLocations newInstance(String className, String methodSignature, int line, Location location, + int methodStartLineNumber, int methodEndLine, CodePosition methodCodePosition) { + ClassLocations pred = new ClassLocations(className); + pred.addPredictions(methodSignature, line, location, methodStartLineNumber, methodEndLine, methodCodePosition); + return pred; + } + + public void addPredictions(String methodSignature, int line, Location location, + int methodStartLineNumber, int methodEndLine, CodePosition methodCodePosition) { + MethodLocations pred = getChildrenByQuery(methodSignature); + if (pred == null){ + pred = MethodLocations.newInstance(methodSignature, line, location, + methodStartLineNumber, methodEndLine, methodCodePosition); + if (methodPredictions == null){ + methodPredictions = new ArrayList<>(); + } + methodPredictions.add(pred); + } else { + pred.addPredictions(line, location); + } + } + + + public void setQualifiedName(String qualifiedName) { + this.qualifiedName = qualifiedName; + } + + public List getMethodPredictions() { + return methodPredictions; + } + + public void setMethodPredictions(List methodPredictions) { + this.methodPredictions = methodPredictions; + } + + public void addPredictions(MethodLocations methodLocations) { + if (methodPredictions == null) { + methodPredictions = new ArrayList<>(); + } + methodPredictions.add(methodLocations); + } + + @Override + public String getQueryable() { + return qualifiedName; + } + + @Override + public List getItems() { + return methodPredictions; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ClassLocations that = (ClassLocations) o; + return Objects.equals(qualifiedName, that.qualifiedName) && Objects.equals(methodPredictions, that.methodPredictions); + } + + @Override + public int hashCode() { + return Objects.hash(qualifiedName, methodPredictions); + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/output/CodePosition.java b/src/main/java/edu/lu/uni/serval/mbertloc/output/CodePosition.java new file mode 100644 index 0000000..c99e13a --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/output/CodePosition.java @@ -0,0 +1,46 @@ +package edu.lu.uni.serval.mbertloc.output; + +import java.io.Serializable; +import java.util.Objects; + +public class CodePosition implements Serializable { + private int startPosition; + private int endPosition; + + public CodePosition() { + } + + public CodePosition(int startPosition, int endPosition) { + this.startPosition = startPosition; + this.endPosition = endPosition; + } + + public int getStartPosition() { + return startPosition; + } + + public void setStartPosition(int startPosition) { + this.startPosition = startPosition; + } + + public int getEndPosition() { + return endPosition; + } + + public void setEndPosition(int endPosition) { + this.endPosition = endPosition; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CodePosition that = (CodePosition) o; + return startPosition == that.startPosition && endPosition == that.endPosition; + } + + @Override + public int hashCode() { + return Objects.hash(startPosition, endPosition); + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/output/FileLocations.java b/src/main/java/edu/lu/uni/serval/mbertloc/output/FileLocations.java new file mode 100644 index 0000000..db963c4 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/output/FileLocations.java @@ -0,0 +1,88 @@ +package edu.lu.uni.serval.mbertloc.output; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class FileLocations extends Mappable implements Queryable { + + + private String file_path; + private List classPredictions; + + public FileLocations() { + } + + private FileLocations(String file_path) { + this.file_path = file_path; + } + + public static FileLocations newInstance(String file_path, String className, String methodSignature, int line, Location location, int methodStartLineNumber, int methodEndLine, CodePosition methodCodePosition) { + FileLocations filePredictions = new FileLocations(file_path); + filePredictions.addPredictions(className, methodSignature, line, location, methodStartLineNumber, methodEndLine, methodCodePosition); + return filePredictions; + } + + public void addPredictions(String className, String methodSignature, int line, Location location, + int methodStartLineNumber, int methodEndLine, CodePosition methodCodePosition) { + ClassLocations classPrediction = getChildrenByQuery(className); + if (classPrediction == null){ + classPrediction = ClassLocations.newInstance(className, methodSignature, line, location, + methodStartLineNumber, methodEndLine, methodCodePosition); + if (classPredictions == null){ + classPredictions = new ArrayList<>(); + } + classPredictions.add(classPrediction); + } else { + classPrediction.addPredictions(methodSignature, line, location, + methodStartLineNumber, methodEndLine, methodCodePosition); + } + } + + public String getFile_path() { + return file_path; + } + + public void setFile_path(String file_path) { + this.file_path = file_path; + } + + public List getClassPredictions() { + return classPredictions; + } + + public void setClassPredictions(List classPredictions) { + this.classPredictions = classPredictions; + } + + public void addPredictions(ClassLocations predictions) { + if (classPredictions == null) { + classPredictions = new ArrayList<>(); + } + classPredictions.add(predictions); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + FileLocations that = (FileLocations) o; + return Objects.equals(file_path, that.file_path) && Objects.equals(classPredictions, that.classPredictions); + } + + @Override + public int hashCode() { + return Objects.hash(file_path, classPredictions); + } + + @Override + public String getQueryable() { + return file_path; + } + + @Override + public List getItems() { + return classPredictions; + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/output/LineLocations.java b/src/main/java/edu/lu/uni/serval/mbertloc/output/LineLocations.java new file mode 100644 index 0000000..b8bf06d --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/output/LineLocations.java @@ -0,0 +1,61 @@ +package edu.lu.uni.serval.mbertloc.output; + + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class LineLocations implements Queryable { + private int line_number; + private List locations; + + public LineLocations() { + } + + private LineLocations(int line_number) { + this.line_number = line_number; + } + + public static LineLocations newInstance(int line, Location location) { + LineLocations pred = new LineLocations(line); + pred.addPredictions(location); + return pred; + } + + public void setLine_number(int line_number) { + this.line_number = line_number; + } + + public void addPredictions(Location location) { + if (locations == null) { + locations = new ArrayList<>(); + } + locations.add(location); + } + + public List getLocations() { + return locations; + } + + public int getLine_number() { + return line_number; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LineLocations that = (LineLocations) o; + return line_number == that.line_number && Objects.equals(locations, that.locations); + } + + @Override + public int hashCode() { + return Objects.hash(line_number, locations); + } + + @Override + public Integer getQueryable() { + return line_number; + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/output/Location.java b/src/main/java/edu/lu/uni/serval/mbertloc/output/Location.java new file mode 100644 index 0000000..5427d44 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/output/Location.java @@ -0,0 +1,31 @@ +package edu.lu.uni.serval.mbertloc.output; + +import java.io.Serializable; +import java.util.Objects; + +// {'score': 0.34352532029151917, 'token': 23796, 'token_str': 'null', 'match': False, 'cosine': 0.77328160405159} +public class Location implements Serializable { + + protected String node; + protected CodePosition codePosition; + protected String nodeType; + protected int firstMutantId; + protected String operator; + protected String suffix; + + public Location() { + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Location location = (Location) o; + return firstMutantId == location.firstMutantId && Objects.equals(codePosition, location.codePosition) && Objects.equals(node, location.node) && Objects.equals(operator, location.operator); + } + + @Override + public int hashCode() { + return Objects.hash(codePosition, node, firstMutantId, operator); + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/output/MBertOperators.java b/src/main/java/edu/lu/uni/serval/mbertloc/output/MBertOperators.java new file mode 100644 index 0000000..c8317ef --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/output/MBertOperators.java @@ -0,0 +1,17 @@ +package edu.lu.uni.serval.mbertloc.output; + +/** + * @see {CodeBERTOperatorMutator#process(spoon.reflect.declaration.CtElement)} + * @see {mBERT/src/main/java/operators} + */ +public enum MBertOperators { + CodeBERTLiteralMutator, + CodeBERTArrayMutator, + CodeBERTAssignmentMutator, + CodeBERTBinaryOperatorMutator, + CodeBERTFieldReferenceMutator, + CodeBERTInvocationMutator, + CodeBERTTypeReferenceMutator, + CodeBERTUnaryOperatorMutator + +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/output/Mappable.java b/src/main/java/edu/lu/uni/serval/mbertloc/output/Mappable.java new file mode 100644 index 0000000..06f4208 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/output/Mappable.java @@ -0,0 +1,18 @@ +package edu.lu.uni.serval.mbertloc.output; + +import java.util.List; + +public abstract class Mappable> { + + public abstract List getItems(); + + public T getChildrenByQuery(Q query) { + if (getItems() != null && !getItems().isEmpty()) { + for (T item : getItems()) { + if (query.equals(item.getQueryable())) + return item; + } + } + return null; + } +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/output/MethodLocations.java b/src/main/java/edu/lu/uni/serval/mbertloc/output/MethodLocations.java new file mode 100644 index 0000000..f36410d --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/output/MethodLocations.java @@ -0,0 +1,93 @@ +package edu.lu.uni.serval.mbertloc.output; + + +import java.util.ArrayList; + +import java.util.List; +import java.util.Objects; + +public class MethodLocations extends Mappable implements Queryable { + + private int startLineNumber; + private int endLineNumber; + private String methodSignature; + private CodePosition codePosition; + private List line_predictions; + + public MethodLocations() { + } + + private MethodLocations(int startLineNumber, int endLineNumber, String methodSignature, CodePosition codePosition) { + this.startLineNumber = startLineNumber; + this.endLineNumber = endLineNumber; + this.methodSignature = methodSignature; + this.codePosition = codePosition; + } + + public static MethodLocations newInstance(String methodSignature, int line, Location location, + int methodStartLineNumber, int methodEndLine, CodePosition methodCodePosition) { + MethodLocations pred = new MethodLocations(methodStartLineNumber, methodEndLine, methodSignature, methodCodePosition); + pred.addPredictions(line, location); + return pred; + } + + public void addPredictions(int line, Location location) { + LineLocations pred = getChildrenByQuery(line); + if (pred == null){ + pred = LineLocations.newInstance(line, location); + if (line_predictions == null) { + line_predictions = new ArrayList<>(); + } + line_predictions.add(pred); + } else { + pred.addPredictions(location); + } + } + + public String getMethodSignature() { + return methodSignature; + } + + public void setMethodSignature(String methodSignature) { + this.methodSignature = methodSignature; + } + + public List getLine_predictions() { + return line_predictions; + } + + public void setLine_predictions(List line_predictions) { + this.line_predictions = line_predictions; + } + + public void addPredictions(LineLocations lineLocations) { + if (line_predictions == null) { + line_predictions = new ArrayList<>(); + } + line_predictions.add(lineLocations); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MethodLocations that = (MethodLocations) o; + return Objects.equals(methodSignature, that.methodSignature) && Objects.equals(line_predictions, that.line_predictions); + } + + @Override + public int hashCode() { + return Objects.hash(methodSignature, line_predictions); + } + + @Override + public String getQueryable() { + return methodSignature; + } + + @Override + public List getItems() { + return line_predictions; + } + +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/output/Queryable.java b/src/main/java/edu/lu/uni/serval/mbertloc/output/Queryable.java new file mode 100644 index 0000000..39fb3e0 --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/output/Queryable.java @@ -0,0 +1,6 @@ +package edu.lu.uni.serval.mbertloc.output; + +public interface Queryable { + + Q getQueryable(); +} diff --git a/src/main/java/edu/lu/uni/serval/mbertloc/utils/GsonHolder.java b/src/main/java/edu/lu/uni/serval/mbertloc/utils/GsonHolder.java new file mode 100644 index 0000000..ade85ae --- /dev/null +++ b/src/main/java/edu/lu/uni/serval/mbertloc/utils/GsonHolder.java @@ -0,0 +1,18 @@ +package edu.lu.uni.serval.mbertloc.utils; + +import com.google.gson.Gson; + +public class GsonHolder { + + public static Gson gson; + + public static Gson getGson() { + if (gson == null) gson = new Gson(); + return gson; + } + + private GsonHolder() { + throw new IllegalAccessError("Utility class: No instance allowed, static access only."); + } + +}