Skip to content

Commit

Permalink
Merge pull request #4 from Ahmedfir/experimental
Browse files Browse the repository at this point in the history
bug fixing #2 and implementation of full conditions masking
  • Loading branch information
Ahmedfir authored Jun 19, 2024
2 parents 08c8b71 + 256f002 commit f9d65c1
Show file tree
Hide file tree
Showing 32 changed files with 749 additions and 52 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

<groupId>edu.lu.uni.serval.javabusinesslocs</groupId>
<artifactId>javabusinesslocs</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.2.2-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>
<version.spoon>10.0.0</version.spoon>
<!-- plugin versions -->
<pVersion.compiler>3.8.0</pVersion.compiler>
<pVersion.surefire>2.22.1</pVersion.surefire>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.StringWriter;
import java.util.Arrays;


public class GetLocations {

public static void main(String... args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private static FileRequest parseFileArgs(String argBody, CliArgPrefix cliArgPref
assert cliArgPrefix == FILE_INCLUDE_REQUEST || cliArgPrefix == FILE_EXCLUDE_REQUEST;
String[] splits = argBody.split(":");
String filePath = splits[0];
System.out.println("PATH " + filePath);
if (!new File(filePath).exists()) {
correctUssage("wrong file path", new IllegalArgumentException(filePath));
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class BusinessLocation<T extends CtElement> extends Location {
* default value is false
*/
public static boolean IF_CONDITIONS_AS_TKN = Boolean.getBoolean("IF_CONDITIONS_AS_TKN");
public static boolean CONDITIONS_AS_TKN = Boolean.getBoolean("CONDITIONS_AS_TKN");

/**
* @param firstMutantId
Expand All @@ -39,6 +40,7 @@ public class BusinessLocation<T extends CtElement> extends Location {
*/
public static Set<BusinessLocation> createBusinessLocation(int firstMutantId, CtElement ctElement) throws UnhandledElementException {
Set<BusinessLocation> res = new LinkedHashSet<>();
// simple tokens parsing first
if (ctElement instanceof CtBinaryOperator) {
res.add(new BinaryOperatorLocation(firstMutantId, (CtBinaryOperator) ctElement));
} else if (ctElement instanceof CtUnaryOperator) {
Expand All @@ -54,12 +56,27 @@ public static Set<BusinessLocation> createBusinessLocation(int firstMutantId, Ct
res.add(new TypeReferenceLocation(firstMutantId, (CtTypeReference) ctElement));
} else if (ctElement instanceof CtInvocation) {
res.add(new InvocationLocation(firstMutantId, (CtInvocation) ctElement));
} else {
} else if(!(ctElement instanceof CtLoop || ctElement instanceof CtIf)){
//check that it's not a loop or if otherwise it enters here
res.add(new BusinessLocation(firstMutantId, ctElement));
}
if (IF_CONDITIONS_AS_TKN && ctElement.getParent() instanceof CtIf) {
res.add(new IfConditionReferenceLocation(firstMutantId, (CtExpression<Boolean>) ctElement));

// parse complex (full conditions) tokens.
if (IF_CONDITIONS_AS_TKN || CONDITIONS_AS_TKN){
if (ctElement instanceof CtIf) {
res.add(new IfConditionReferenceLocation(firstMutantId, ((CtIf) ctElement).getCondition()));
}
}

if(CONDITIONS_AS_TKN) {
if(ctElement instanceof CtWhile) {
res.add(new WhileConditionLocation(firstMutantId, ((CtWhile) ctElement).getLoopingExpression()));
} else if(ctElement instanceof CtFor) {
res.add(new ForConditionLocation(firstMutantId, ((CtFor) ctElement).getExpression()));
} else if (ctElement instanceof CtDo)
res.add(new DoConditionLocation(firstMutantId, ((CtDo) ctElement).getLoopingExpression()));
}

return res;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.lu.uni.serval.javabusinesslocs.locations;

import edu.lu.uni.serval.javabusinesslocs.locator.LocsUtils;
import edu.lu.uni.serval.javabusinesslocs.output.CodePosition;
import edu.lu.uni.serval.javabusinesslocs.output.Operators;
import spoon.reflect.code.CtExpression;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.support.reflect.cu.position.SourcePositionImpl;

import static edu.lu.uni.serval.javabusinesslocs.output.Operators.DoConditionLocation;

public class DoConditionLocation extends BusinessLocation<CtExpression<Boolean>>{

protected DoConditionLocation(int firstMutantId, CtExpression<Boolean> ctElement) throws BusinessLocation.UnhandledElementException {
super(firstMutantId, ctElement);
}

@Override
protected CodePosition getCodePosition(CtExpression<Boolean> ctElement) throws BusinessLocation.UnhandledElementException {
SourcePosition origPosition = LocsUtils.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(CtExpression<Boolean> ctElement) {
return ctElement.toString();
}

@Override
protected Operators getOperatorByType(CtExpression<Boolean> ctElement) {
return DoConditionLocation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.lu.uni.serval.javabusinesslocs.locations;

import edu.lu.uni.serval.javabusinesslocs.locator.LocsUtils;
import edu.lu.uni.serval.javabusinesslocs.output.CodePosition;
import edu.lu.uni.serval.javabusinesslocs.output.Operators;
import spoon.reflect.code.CtExpression;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.support.reflect.cu.position.SourcePositionImpl;

import static edu.lu.uni.serval.javabusinesslocs.output.Operators.ForConditionLocation;

public class ForConditionLocation extends BusinessLocation<CtExpression<Boolean>>{
protected ForConditionLocation(int firstMutantId, CtExpression<Boolean> ctElement) throws BusinessLocation.UnhandledElementException {
super(firstMutantId, ctElement);
}

@Override
protected CodePosition getCodePosition(CtExpression<Boolean> ctElement) throws BusinessLocation.UnhandledElementException {
SourcePosition origPosition = LocsUtils.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(CtExpression<Boolean> ctElement) {
return ctElement.toString();
}

@Override
protected Operators getOperatorByType(CtExpression<Boolean> ctElement) {
return ForConditionLocation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.lu.uni.serval.javabusinesslocs.locations;

import edu.lu.uni.serval.javabusinesslocs.locator.LocsUtils;
import edu.lu.uni.serval.javabusinesslocs.output.CodePosition;
import edu.lu.uni.serval.javabusinesslocs.output.Operators;
import spoon.reflect.code.CtExpression;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.support.reflect.cu.position.SourcePositionImpl;

import static edu.lu.uni.serval.javabusinesslocs.output.Operators.WhileConditionLocation;

public class WhileConditionLocation extends BusinessLocation<CtExpression<Boolean>>{

protected WhileConditionLocation(int firstMutantId, CtExpression<Boolean> ctElement) throws BusinessLocation.UnhandledElementException {
super(firstMutantId, ctElement);
}

@Override
protected CodePosition getCodePosition(CtExpression<Boolean> ctElement) throws BusinessLocation.UnhandledElementException {
SourcePosition origPosition = LocsUtils.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(CtExpression<Boolean> ctElement) {
return ctElement.toString();
}

@Override
protected Operators getOperatorByType(CtExpression<Boolean> ctElement) {
return WhileConditionLocation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public boolean isLineToMutate(int line) {

while (selector.hasNext()) {
Element element = selector.next();

if (element != null) {
try {
Set<BusinessLocation> businessLocs = BusinessLocation.createBusinessLocation(nextMutantId, element.ctElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.reference.CtTypeReference;

import static edu.lu.uni.serval.javabusinesslocs.locations.BusinessLocation.CONDITIONS_AS_TKN;
import static edu.lu.uni.serval.javabusinesslocs.locations.BusinessLocation.IF_CONDITIONS_AS_TKN;


public final class LocsUtils {

private LocsUtils() {
Expand All @@ -24,31 +28,44 @@ public static boolean inheritsFromAssertion(CtElement e) {


public static SourcePosition getSourcePosition(CtElement e) {
if (e == null)
if (e == null) {
return null;
if (e.getPosition() != null && e.getPosition().isValidPosition())
}
if (e.getPosition() != null && e.getPosition().isValidPosition()) {
return e.getPosition();
if (e.getParent() != null)
System.err.println("returning parent position for " + e.getClass());
}
//if (e.getParent() != null){
// System.err.println("returning parent position for " + e.getClass());
//}
return getSourcePosition(e.getParent());
}


public static boolean isImplicit(CtElement e) {
//makes the children of else if(condition) not implicit
//allows parsing locations in else if
if(e.getParent() instanceof CtIf)
e.setImplicit(false);

if (e == null || isMethod(e) || 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))
//while, for, do and if are labeled implicit by the method isImplicit
if((CONDITIONS_AS_TKN && candidate instanceof CtLoop)
||((CONDITIONS_AS_TKN || IF_CONDITIONS_AS_TKN) && candidate instanceof CtIf)){
return !candidate.isImplicit();
}

if (isImplicit(candidate)) {
return false;
}
if (candidate instanceof CtConstructorCall ||
candidate instanceof CtTypeAccess ||
candidate instanceof CtNewArray ||
Expand All @@ -59,6 +76,7 @@ public static boolean isToBeProcessed(CtElement candidate) {
if (candidate instanceof CtExpression
|| candidate instanceof CtFieldReference)
return true;

if (candidate instanceof CtTypeReference && candidate.getParent() != null
&& candidate.getParent() instanceof CtTypeAccess
&& !inheritsFromConstructorCall(candidate)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.lu.uni.serval.javabusinesslocs.locator.selection;

import spoon.reflect.code.CtIf;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
Expand Down Expand Up @@ -31,6 +32,7 @@ public boolean hasNext() {

@Override
public Element next() {

if (notLastElement()) {
currentElementPos++;
CtElement ctElement = methodsElementsToBeMutated.get(currentElementPos);
Expand All @@ -53,6 +55,7 @@ private void nextMethod() {
if (sourcePosition != null) {
methodsElementsToBeMutated = ctMethod.getElements(arg0 ->
isToBeProcessed(arg0) && isLineToMutate(getSourcePosition(arg0).getLine()));

this.currentMethod = new Method(ctMethod.getSignature(), sourcePosition);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ public enum Operators {
InvocationMutator,
TypeReferenceMutator,
UnaryOperatorMutator,
IfConditionReferenceLocation
IfConditionReferenceLocation,
LoopConditionLocation,
ForConditionLocation,
WhileConditionLocation,
DoConditionLocation,
}
Loading

0 comments on commit f9d65c1

Please sign in to comment.