Skip to content

Commit

Permalink
update spoon version
Browse files Browse the repository at this point in the history
  • Loading branch information
nharrand committed Feb 28, 2018
1 parent ab4e4e5 commit 84953ed
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Sosiefier
Sosiefier is a project that aims at exporing the space of sosie of java programs.
Sosiefier is a project that aims at exploring the space of sosie of java programs. (Sosie means here a program variant that passes the original test suit.)
It can be used to:
* Explore java sources in order to build source transformations
* Apply transformation and run tests in order to determine if the variant obtained is a sosie or not.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.inria.diversify.codeFragment;

import spoon.compiler.Environment;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtVariableAccess;
Expand All @@ -9,21 +10,25 @@
import spoon.reflect.reference.CtLocalVariableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.reflect.visitor.printer.ElementPrinterHelper;
import spoon.reflect.visitor.printer.PrinterHelper;
import spoon.reflect.visitor.DefaultTokenWriter;
import spoon.reflect.visitor.ElementPrinterHelper;
import spoon.reflect.visitor.PrinterHelper;
import spoon.reflect.visitor.TokenWriter;

import java.util.Stack;

public class CodeFragmentEqualPrinter extends DefaultJavaPrettyPrinter {

private final PrinterHelper printer;
private final ElementPrinterHelper elementPrinterHelper;
private final TokenWriter printerTokenWriter;
public PrintingContext context = new PrintingContext();

public CodeFragmentEqualPrinter(Environment env) {
super(env);
this.printer = new PrinterHelper(env);
this.elementPrinterHelper = new ElementPrinterHelper(this.printer, this, env);
this.printerTokenWriter = new DefaultTokenWriter(this.printer);
this.elementPrinterHelper = new ElementPrinterHelper(this.printerTokenWriter, this, env);
}

public <T> void visitCtInvocation(CtInvocation<T> invocation) {
Expand Down Expand Up @@ -125,5 +130,5 @@ private class PrintingContext {

boolean ignoreStaticAccess = false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public boolean compileFileIn(File directory, boolean withLog) {
finalClassPath[i] = urls[i].getFile();
}


final ClasspathOptions classpathOptions = new ClasspathOptions()
.encoding(this.encoding)
.encoding(this.getEnvironment().getEncoding().name())
.classpath(finalClassPath)
.binaries(getBinaryOutputDirectory());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.QueryVisitor;
//import spoon.reflect.visitor.QueryVisitor;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.QueueProcessingManager;

Expand Down Expand Up @@ -438,12 +438,14 @@ public synchronized List<CtMethod> getJavassistMethods() {

public synchronized <T extends CtElement> List<T> getAllElement(Class cl) {
if (!typeToObject.containsKey(cl)) {
QueryVisitor<T> query = new QueryVisitor(new TypeFilter(cl));
List<T> elements = new ArrayList<>();
elements.addAll(getFactory().getModel().getElements(new TypeFilter(cl)));
/*QueryVisitor<T> query = new QueryVisitor(new TypeFilter(cl));
List<T> elements = new ArrayList<>();
for (CtElement e : getRoots()) {
e.accept(query);
elements.addAll(query.getResult());
}
}*/
typeToObject.put(cl, elements);
}
return (List<T>)typeToObject.get(cl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.QueryVisitor;
//import spoon.reflect.visitor.QueryVisitor;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.reflect.code.CtTryImpl;

Expand Down Expand Up @@ -82,9 +82,10 @@ protected String stmtWithoutLog(CtElement stmt) {
}

protected boolean containsGoto(CtElement elem) {
QueryVisitor query = new QueryVisitor(new TypeFilter(CtBreak.class));
/*QueryVisitor query = new QueryVisitor(new TypeFilter(CtBreak.class));
elem.accept(query);
for(Object o : query.getResult()) {
for(Object o : query.getResult()) {*/
for(Object o : elem.getElements(new TypeFilter(CtBreak.class))) {
CtBreak ctBreak = (CtBreak) o;
if(ctBreak.getTargetLabel() != null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtTry;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.visitor.QueryVisitor;
//import spoon.reflect.visitor.QueryVisitor;
import spoon.reflect.visitor.filter.TypeFilter;

;
Expand Down Expand Up @@ -53,10 +53,11 @@ public void process(CtMethod candidate) {
}

protected boolean hasCall(CtMethod method) {
QueryVisitor query = new QueryVisitor(new TypeFilter(CtInvocation.class));
/*QueryVisitor query = new QueryVisitor(new TypeFilter(CtInvocation.class));
method.accept(query);
for(Object o : query.getResult()) {
for(Object o : query.getResult()) {*/
for(Object o : method.getElements(new TypeFilter(CtInvocation.class))) {
CtInvocation target = (CtInvocation) o;
if(target.getExecutable() != null && target.getExecutable().getDeclaration() != null)
if (inputProgram.getAllElement(CtMethod.class).contains(target.getExecutable().getDeclaration())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package fr.inria.diversify.runner;

import fr.inria.diversify.statistic.SinglePointSessionResults;
import fr.inria.diversify.transformation.AddMethodInvocation;
import fr.inria.diversify.transformation.MultiSwapSubType;
import fr.inria.diversify.transformation.SwapSubType;
import fr.inria.diversify.transformation.Transformation;
import fr.inria.diversify.util.Log;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtParameter;

import java.util.*;

/**
* Created by nharrand on 09/10/17.
*/
public class ApplyMultiTransRunner extends SinglePointRunner {
public ApplyMultiTransRunner(InputConfiguration inputConfiguration, String projectDir, String srcDir) {
super(inputConfiguration, projectDir, srcDir);
}


@Override
public void run(int n) throws Exception {
int i = new Random().nextInt(n-1);
Set<SwapSubType> transformations = new HashSet<>();
while (transQuery.hasNextTransformation() && i >= 0) {
transformations.add((SwapSubType) transQuery.query());
}
Transformation trans = new MultiSwapSubType(transformations);
try {
applyTransformation(trans);
int status;
status = runTest(tmpDir);
} catch (Exception e) {
trans.setStatus(-2);
tryRestore(trans, e);
}
}

protected void applyTransformation(Transformation trans) throws Exception {
if(withParent) {
if(acceptedErrors) {
builder.setAcceptedErrors(trans.getParent().getFailures());
}
trans.applyWithParent(tmpDir + "/" + sourceDir);
} else {
trans.apply(tmpDir + "/" + sourceDir);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AddMethodInvocation extends SingleTransformation {
CtInvocation aInv;

boolean jStatic;
String jPos, jType, jSC;
String jPos, jType, jSC, jPath;

public CtStatement getTryInv() {
return tryInv;
Expand Down Expand Up @@ -177,6 +177,7 @@ public void setTp(CtStatement tp) {
this.parentClass = tp.getParent(CtClass.class);
this.parentMethod = tp.getParent(CtMethod.class);
jPos = tp.getParent(CtType.class).getQualifiedName() + ":" + tp.getPosition().getLine();
jPath = tp.getPath().toString();
jType = tp.getClass().getName();
jSC = tp.toString();
jStatic = parentMethod.getModifiers().contains(ModifierKind.STATIC);
Expand Down Expand Up @@ -278,6 +279,7 @@ public JSONObject toJSONObject() throws JSONException {

JSONObject tpJSON = new JSONObject();
tpJSON.put("position",jPos);
tpJSON.put("path",jPath);
tpJSON.put("type", jType);
tpJSON.put("sourcecode", jSC);
tpJSON.put("static", jStatic);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package fr.inria.diversify.transformation;

import fr.inria.diversify.transformation.exception.RestoreTransformationException;
import fr.inria.diversify.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import spoon.compiler.Environment;
import spoon.reflect.code.CtStatement;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.support.JavaOutputProcessor;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Created by nharrand on 09/10/17.
*/
public class MultiSwapSubType extends Transformation {
Set<SwapSubType> transformations;

public MultiSwapSubType(Set<SwapSubType> transformations) {
this.transformations = transformations;
}

@Override
public String getTransformationString() throws Exception {
return null;
}

@Override
public void apply(String srcDir) throws Exception {
try {
for(SwapSubType t : transformations) {
if(t.newCall == null) t.newCall = t.newCallSnippet;
t.tp.replace(t.newCall);
}
printJavaFile(srcDir);
} catch (Exception e) {
throw new RestoreTransformationException("", e);
}
}

@Override
public void applyWithParent(String srcDir) throws Exception {

}

@Override
public void restore(String srcDir) throws RestoreTransformationException {

try {
transformations.stream()
.forEach(t -> t.newCall.replace((CtStatement) t.tp));
printJavaFile(srcDir);

} catch (Exception e) {
throw new RestoreTransformationException("", e);
}
}

@Override
public List<SourcePosition> getPositions() {
return transformations.stream().map(t -> t.getPosition()).collect(Collectors.toList());
}

public void printJavaFile(String directory) throws IOException {
if(!transformations.isEmpty()) {

Factory factory = transformations.iterator().next().getPosition().getCompilationUnit().getMainType().getFactory();
Environment env = factory.getEnvironment();

JavaOutputProcessor processor = new JavaOutputProcessor(new File(directory), new DefaultJavaPrettyPrinter(env));
processor.setFactory(factory);
transformations.stream()
.map(t -> t.getPosition().getCompilationUnit().getMainType())
.distinct()
.forEach(t -> processor.createJavaFile(t));
}
}

@Override
public JSONObject toJSONObject() throws JSONException {
JSONObject res = super.toJSONObject();
JSONArray list = new JSONArray();
for(SwapSubType t : transformations) {
JSONObject object = super.toJSONObject();
if (t.newCall != null) object.put("newCall", t.newCall.toString());
else object.put("newCall", t.newCallSnippet.toString());
JSONObject tpJSON = new JSONObject();
tpJSON.put("position", t.tp.getParent(CtType.class).getQualifiedName() + ":" + t.tp.getPosition().getLine());
tpJSON.put("type", t.tp.getClass().getName());
tpJSON.put("sourcecode", t.tp.toString());
object.put("transplantationPoint", tpJSON);
}
res.put("type", "MultiSwapSubType");
res.put("transformations", list);
return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,43 @@ public void init() {
interfaces.put("java.util.NavigableMap", lNavigableMap);
interfaces.put("java.util.SortedMap", lSortedMap);
interfaces.put("java.util.Map", lMap);

commonsCollection();
}


public void commonsCollection() {

Set<String> lList = interfaces.get("java.util.List");
lList.add("org.apache.commons.collections4.list.TreeList");
lList.add("org.apache.commons.collections4.list.GrowthList");
lList.add("org.apache.commons.collections4.list.NodeCachingLinkedList");
lList.add("org.apache.commons.collections4.list.CursorableLinkedList");
lList.add("org.apache.commons.collections4.ArrayStack");

Set<String> lSet = interfaces.get("java.util.Set");
lSet.add("org.apache.commons.collections4.set.ListOrderedSet");

// Bag
//org.apache.commons.collections4.bag.HashBag
//org.apache.commons.collections4.bag.TreeBag

Set<String> lQueue = interfaces.get("java.util.Queue");
lQueue.add("org.apache.commons.collections4.queue.CircularFifoQueue");

Set<String> lMap = interfaces.get("java.util.Map");
lMap.add("org.apache.commons.collections4.map.CaseInsensitiveMap");
lMap.add("org.apache.commons.collections4.map.Flat3Map");
lMap.add("org.apache.commons.collections4.map.HashedMap");
lMap.add("org.apache.commons.collections4.map.LRUMap");
lMap.add("org.apache.commons.collections4.map.LinkedMap");
lMap.add("org.apache.commons.collections4.map.ListOrderedMap");
lMap.add("org.apache.commons.collections4.map.PassiveExpiringMap");
lMap.add("org.apache.commons.collections4.map.ReferenceIdentityMap");
lMap.add("org.apache.commons.collections4.map.ReferenceMap");
lMap.add("org.apache.commons.collections4.map.SingletonMap");
lMap.add("org.apache.commons.collections4.map.StaticBucketMap");

}

public SwapSubTypeQuery(InputProgram inputProgram) {
Expand Down Expand Up @@ -190,7 +227,7 @@ public List<CtCodeSnippetExpression> buildConstructorCall(CtConstructorCall call
if(!call.getType().getQualifiedName().equals(c)) {
//try {
//res.add((CtConstructorCall) f.Code().createCodeSnippetExpression("new " + c + "<" + type + ">("+ param + ")").compile());
res.add(f.Code().createCodeSnippetExpression("new " + c + "<" + type + ">("+ param + ")"));
res.add(f.Code().createCodeSnippetExpression("new " + c + "<" + type + ">(" + param + ")"));
//} catch (Exception ex) {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public static Set<CtMethod> getAccessibleMethods(CtElement el, boolean acceptSta
CtClass elClass = el.getParent(CtClass.class);
CtPackage elPackage = elClass.getPackage();
for(CtClass c : getAccessibleClasses(el)) {
Set<CtMethod> methods = c.getAllMethods();
//Set<CtMethod> methods = c.getAllMethods();
Set<CtMethod> methods = c.getMethods();
res.addAll(
methods.stream()
/*.filter(
Expand Down Expand Up @@ -124,7 +125,8 @@ public static Set<CtMethod> getInternalMethods(CtElement el, boolean acceptStati
Set<CtMethod> res = new HashSet<>();
CtClass elClass = el.getParent(CtClass.class);
CtPackage elPackage = elClass.getPackage();
Set<CtMethod> methods = elClass.getAllMethods();
//Set<CtMethod> methods = elClass.getAllMethods();
Set<CtMethod> methods = elClass.getMethods();
res.addAll(methods.stream()
.filter(
m -> (
Expand Down Expand Up @@ -161,7 +163,8 @@ public static Set<CtMethod> getTargetableMethods(CtElement el) {
if(VarFinder.notPrimitiveNotAnArray(v)) {
try {
CtClass target = (CtClass) v.getType().getDeclaration();
res.addAll(target.getAllMethods());
//res.addAll(target.getAllMethods());
res.addAll(target.getMethods());
} catch (Exception e) {}
}
}
Expand Down
Loading

0 comments on commit 84953ed

Please sign in to comment.