Skip to content

Commit

Permalink
Feat: remove redundant cast FIX #688 (#693)
Browse files Browse the repository at this point in the history
* refactor: rename post_dspot package to prettifier

* feat: remove redundant casts

* pom: rename DSpot to DSpot Core

* ci: add now a travis job for dspot-pretiffier

* test: remove test resources that are unused

* test: add the sample properties
  • Loading branch information
danglotb authored Feb 5, 2019
1 parent dc51549 commit fe7955f
Show file tree
Hide file tree
Showing 85 changed files with 214 additions and 1,826 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ env:
- SCRIPT=travis-dhell.sh
- SCRIPT=travis-ci-xwiki.sh
- SCRIPT=travis-diff-test-selection.sh
# - SCRIPT=travis-dspot-maven-cmd-line.sh
- SCRIPT=travis-prettifier.sh
# - SCRIPT=travis-dspot-maven-cmd-line.sh TODO FIXME

cache:
directories:
Expand All @@ -40,4 +41,4 @@ after_success:

branch:
only:
- master
- master
5 changes: 5 additions & 0 deletions .travis/travis-prettifier.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

source /opt/jdk_switcher/jdk_switcher.sh

jdk_switcher use openjdk8 & mvn -Djava.src.version=1.8 test -f dspot-prettifier/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package eu.stamp_project.prettifier;

import eu.stamp_project.test_framework.TestFramework;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.reference.CtTypeReference;

import java.util.List;

/**
* created by Benjamin DANGLOT
* [email protected]
* on 04/02/19
*/
public class RedundantCastRemover {


public CtMethod<?> remove(CtMethod<?> testMethod) {
final List<CtInvocation<?>> assertions = testMethod.getElements(TestFramework.ASSERTIONS_FILTER);
for (CtInvocation<?> assertion : assertions) {
this.remove(assertion);
}
return testMethod;
}

private void remove(CtInvocation<?> assertion) {
final CtExpression<?> actualValue = assertion.getArguments().get(assertion.getArguments().size() - 1);
final CtExpression<?> expectedValue = assertion.getArguments().get(assertion.getArguments().size() - 2);
// top cast compared to the expected value
if (!actualValue.getTypeCasts().isEmpty() &&
actualValue.getTypeCasts().get(0).equals(expectedValue.getType())) {
actualValue.getTypeCasts().remove(0);
}
// inner casts that can be removed
removeCastInvocations(actualValue);
}

private void removeCastInvocations(CtExpression<?> current) {
while (current instanceof CtInvocation<?>) {
current = ((CtInvocation) current).getTarget();
if (!current.getTypeCasts().isEmpty() &&
matchTypes(current.getTypeCasts().get(0), current.getType())) {
current.getTypeCasts().remove(0);
}
}
}

private boolean matchTypes(CtTypeReference<?> toBeMatched, CtTypeReference<?> type) {
if (type == null) {
return false;
} else if (toBeMatched.equals(type)) {
return true;
} else {
return matchTypes(toBeMatched, type.getSuperclass());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.code2vec.builder;
package eu.stamp_project.prettifier.code2vec.builder;

import eu.stamp_project.utils.AmplificationHelper;
import org.apache.commons.io.FileUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.code2vec.builder;
package eu.stamp_project.prettifier.code2vec.builder;

import eu.stamp_project.utils.DSpotUtils;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.code2vec.builder;
package eu.stamp_project.prettifier.code2vec.builder;

public class NumbersOfMethodsPerSet {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.code2vec.builder;
package eu.stamp_project.prettifier.code2vec.builder;

import spoon.compiler.Environment;
import spoon.reflect.declaration.CtType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.code2vec.builder;
package eu.stamp_project.prettifier.code2vec.builder;

import java.io.BufferedReader;
import java.io.FileReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.code2vec.builder;
package eu.stamp_project.prettifier.code2vec.builder;

import eu.stamp_project.utils.AmplificationHelper;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.code2vec.builder;
package eu.stamp_project.prettifier.code2vec.builder;

import eu.stamp_project.test_framework.TestFramework;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.minimization;
package eu.stamp_project.prettifier.minimization;

import eu.stamp_project.minimization.GeneralMinimizer;
import eu.stamp_project.test_framework.TestFramework;
Expand All @@ -15,10 +15,7 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;

import javax.swing.plaf.nimbus.State;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.minimization;
package eu.stamp_project.prettifier.minimization;

import eu.stamp_project.minimization.Minimizer;
import eu.stamp_project.test_framework.TestFramework;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.minimization;
package eu.stamp_project.prettifier.minimization;

import spoon.reflect.declaration.CtMethod;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.minimization;
package eu.stamp_project.prettifier.minimization;

import eu.stamp_project.minimization.GeneralMinimizer;
import eu.stamp_project.utils.pit.AbstractPitResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package eu.stamp_project.prettifier;

import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;

import static org.junit.Assert.assertEquals;

/**
* created by Benjamin DANGLOT
* [email protected]
* on 04/02/19
*/
public class RedundantCastRemoverTest {

@Test
public void test() {

/*
This test that we can remove some redundant cast.
The last statement should keeps its cast
*/

Launcher launcher = new Launcher();
launcher.addInputResource("src/test/java/eu/stamp_project/resources/AmplifiedTest.java");
launcher.buildModel();

final Factory factory = launcher.getFactory();
final CtMethod<?> redundantCast = factory.Class().get("eu.stamp_project.resources.AmplifiedTest")
.getMethodsByName("redundantCast").get(0);

final RedundantCastRemover redundantCastRemover = new RedundantCastRemover();
final CtMethod<?> amplifiedTestWithoutRedundantCast = redundantCastRemover.remove(redundantCast);
assertEquals(expected,
amplifiedTestWithoutRedundantCast.getBody().toString()
);
}

private static final String expected = "{\n" +
" final eu.stamp_project.resources.AmplifiedTest amplifiedTest = new eu.stamp_project.resources.AmplifiedTest();\n" +
" final eu.stamp_project.resources.AmplifiedTest.MyObject myObject = new eu.stamp_project.resources.AmplifiedTest.MyObject();\n" +
" // should be removed\n" +
" org.junit.Assert.assertEquals(0, amplifiedTest.getInt());\n" +
" org.junit.Assert.assertEquals(0, amplifiedTest.getInt());\n" +
" org.junit.Assert.assertEquals(0, myObject.getInt());\n" +
" org.junit.Assert.assertEquals(0, myObject.getMyInternalObject().getInt());\n" +
" org.junit.Assert.assertEquals(0, myObject.getMyInternalObject().getMySecondIntegernalObject().getInt());\n" +
" org.junit.Assert.assertEquals(0, myObject.getMyInternalObject().getMySecondIntegernalObject().getInt());\n" +
" // should not be removed\n" +
" org.junit.Assert.assertEquals(0, ((eu.stamp_project.resources.AmplifiedTest.MySecondInternalObject) (myObject.getMyInternalObject().getMySecondIntegernalObject().getObject())).getSecondInt());\n" +
"}";
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.minimization;
package eu.stamp_project.prettifier.minimization;

import eu.stamp_project.AbstractTest;
import eu.stamp_project.Utils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.stamp_project.post_dspot.minimization;
package eu.stamp_project.prettifier.minimization;

import eu.stamp_project.AbstractTest;
import eu.stamp_project.Utils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package eu.stamp_project.resources;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* created by Benjamin DANGLOT
* [email protected]
* on 04/02/19
*/
public class AmplifiedTest {

public class MyObject {
public MyInternalObject getMyInternalObject() {
return new MyInternalObject();
}

public int getInt() {
return 0;
}
}

public class MyInternalObject {
public int getInt() {
return 0;
}

public MySecondInternalObject getMySecondIntegernalObject() {
return new MySecondInternalObject();
}
}

public class MySecondInternalObject extends MyObject {
public MyObject getObject() {
return this;
}

public int getSecondInt() {
return 0;
}
}

public int getInt() {
return 0;
}

public Integer getInteger() {
return 0;
}

@Test
public void redundantCast() {
final AmplifiedTest amplifiedTest = new AmplifiedTest();
final MyObject myObject = new MyObject();

// should be removed
assertEquals(0, (int) ((AmplifiedTest) amplifiedTest).getInt());
assertEquals(0, (int) amplifiedTest.getInt());
assertEquals(0, (int) ((MyObject) myObject).getInt());
assertEquals(0, (int) ((MyInternalObject) ((MyObject) myObject).getMyInternalObject()).getInt());
assertEquals(0, (int) ((MySecondInternalObject) ((MyInternalObject) ((MyObject) myObject).getMyInternalObject()).getMySecondIntegernalObject()).getInt());
assertEquals(0, (int) ((MyObject) ((MyInternalObject) ((MyObject) myObject).getMyInternalObject()).getMySecondIntegernalObject()).getInt());

// should not be removed
assertEquals(0, ((MySecondInternalObject) myObject.getMyInternalObject().getMySecondIntegernalObject().getObject()).getSecondInt());
}
}
77 changes: 0 additions & 77 deletions dspot-prettifier/src/test/resources/sample/.dspot__junit5_pom.xml

This file was deleted.

11 changes: 11 additions & 0 deletions dspot-prettifier/src/test/resources/sample/sample.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
project=src/test/resources/sample/
src=src/main/java
testSrc=src/test/java
testResources=src/test/resources
outputDirectory=target/trash/
filter=fr.inria.sample.*
systemProperties=admin=toto,passwd=tata
excludedClasses=fr.inria.filter.failing.*
excludedTestCases=failingTestCase
jvmArgs=-Xmx2048m,-Xms1024m,-Dis.admin.user=admin,-Dis.admin.passwd=$2pRSid#
pitVersion=1.4.0
Loading

0 comments on commit fe7955f

Please sign in to comment.