Skip to content

Commit

Permalink
removing fieldReference in InputContext
Browse files Browse the repository at this point in the history
  • Loading branch information
simonAllier committed Nov 20, 2013
1 parent bd255b7 commit f847992
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,11 @@ public Map<String,String> randomVariableMapping(CodeFragment other) {
Map<String,String> varMap = new HashMap<String, String>();
Random r = new Random();

for (CtVariableReference<?> variable : other.getInputContext().getLocalVar()) {
for (CtVariableReference<?> variable : other.getInputContext().getVar()) {
List<Object> list = getInputContext().allCandidate(variable.getType());
Object candidate = list.get(r.nextInt(list.size()));
varMap.put(variable.toString(), candidate.toString());
}
for (CtFieldAccess<?> variable : other.getInputContext().getField()) {
List<CtFieldAccess> list = getInputContext().allCandidateForFieldAccess(variable.getType());
Object candidate = list.get(r.nextInt(list.size()));
varMap.put(variable.toString(), candidate.toString());
}
return varMap;
}

Expand Down
64 changes: 6 additions & 58 deletions src/main/java/fr/inria/diversify/codeFragment/InputContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@

public class InputContext {
protected Set<CtVariableReference<?>> localVariableReferences;
protected Set<CtFieldAccess<?>> fieldReferences = new HashSet<CtFieldAccess<?>>();
protected Integer hashCode = null;

public InputContext(Set<CtVariableReference<?>> inputContext, Set<CtFieldAccess<?>> fieldReferences) {
public InputContext(Set<CtVariableReference<?>> inputContext) {
this.localVariableReferences = inputContext;
this.fieldReferences = fieldReferences;
}

@Override
Expand All @@ -31,34 +29,20 @@ protected Set<String> inputContextToString() {
Set<String> set = new HashSet<String>();
for (CtVariableReference<?> var : localVariableReferences)
set.add(var.getType().toString());
for (CtFieldAccess<?> var : fieldReferences)
set.add(var.getVariable().getType().toString());
return set;
}

public Object candidate(CtTypeReference<?> type){
Object candidate = candidateForLocalVar(type);
if(candidate == null)
candidate = candidateForFieldAccess(type);
Object candidate = candidateForLocalVar(type);;
return candidate;
}

public List<Object> allCandidate(CtTypeReference<?> type){
List<Object> candidate = new ArrayList<Object>();
candidate.addAll(allCandidateForFieldAccess(type));
candidate.addAll(allCandidateForLocalVar(type));

return candidate;
}
public List<CtFieldAccess> allCandidateForFieldAccess(CtTypeReference<?> type){
List<CtFieldAccess> candidate = new ArrayList<CtFieldAccess>();
for (CtFieldAccess<?> var : fieldReferences)
if(var.getVariable().getType().equals(type) && var.getType().getActualTypeArguments().equals(type.getActualTypeArguments())) {
candidate.add(var);
}

return candidate;
}

public List<CtVariableReference> allCandidateForLocalVar(CtTypeReference<?> type){
List<CtVariableReference> candidate = new ArrayList<CtVariableReference>();
Expand All @@ -70,18 +54,7 @@ public List<CtVariableReference> allCandidateForLocalVar(CtTypeReference<?> type

return candidate;
}
public CtFieldAccess<?> candidateForFieldAccess(CtTypeReference<?> type){
CtFieldAccess<?> candidate = null;
for (CtFieldAccess<?> var : fieldReferences) {
CtTypeReference<?> varType = var.getType();
if(varType.equals(type) && varType.getActualTypeArguments().equals(type.getActualTypeArguments())) {

candidate = var;
break;
}
}
return candidate;
}

public CtVariableReference<?> candidateForLocalVar(CtTypeReference<?> type){
CtVariableReference<?> candidate = null;
Expand All @@ -99,16 +72,12 @@ public boolean isInclude(InputContext other){
boolean isReplace = true;
for (CtVariableReference<?> variable : other.localVariableReferences)
isReplace = isReplace && hasCandidate(variable.getType());

for (CtFieldAccess<?> field : other.fieldReferences)
isReplace = isReplace && hasCandidateForFieldAccess(field.getVariable().getType());

return isReplace;
}

public Object getVariableOrFieldNamed(String name) {
Object o = null;
for(Object vf : getVarAndField())
for(Object vf : getVar())
if(vf.toString().equals(name)) {
o = vf;
break;
Expand All @@ -118,38 +87,21 @@ public Object getVariableOrFieldNamed(String name) {
return o;
}

protected boolean hasCandidateForLocalVar(CtTypeReference<?> type) {
return candidateForLocalVar(type) != null;
}
protected boolean hasCandidateForFieldAccess(CtTypeReference<?> type) {
return candidateForFieldAccess(type) != null;
}

protected boolean hasCandidate(CtTypeReference<?> type) {
return candidate(type) != null;
}

public Set<CtVariableReference<?>> getLocalVar() {
public Set<CtVariableReference<?>> getVar() {
return localVariableReferences;
}

public Set<CtFieldAccess<?>> getField() {
return fieldReferences;
}
public List<Object> getVarAndField() {
List<Object> list = new ArrayList<Object>();
list.addAll(fieldReferences);
list.addAll(localVariableReferences);

return list;
}
public String equalString() {
return inputContextToString().toString();
}

@Override
public String toString() {
return getVarAndField().toString();
return getVar().toString();
}

@Override
Expand All @@ -160,19 +112,15 @@ public int hashCode() {
}

public int size() {
return localVariableReferences.size() + fieldReferences.size();
return localVariableReferences.size();
}

public List<CtTypeReference<?>> getTypes() {
List<CtTypeReference<?>> types = new ArrayList<CtTypeReference<?>>();

for (CtFieldAccess field: fieldReferences)
types.add(field.getType());

for (CtVariableReference var: localVariableReferences) {
types.add(var.getType());
}

return types;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@

public class VariableVisitor extends CtScanner {
protected Set<CtVariableReference<?>> localVariableReferences = new HashSet<CtVariableReference<?>>();
protected Set<CtFieldAccess<?>> fieldReferences = new HashSet<CtFieldAccess<?>>();
protected Set<CtVariableReference<?>> localVariableCreate = new HashSet<CtVariableReference<?>>();
protected Set<CtVariableReference<?>> localVariableCreate = new HashSet<CtVariableReference<?>>();
protected CtTypeReference<?> refThis;

public InputContext input() {
localVariableReferences.removeAll(localVariableCreate);

if(refThis != null)
localVariableReferences.add(getThis());
return new InputContext(localVariableReferences, fieldReferences);
return new InputContext(localVariableReferences);
}

public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@

import fr.inria.diversify.CodeFragmentList;
import fr.inria.diversify.codeFragment.CodeFragment;
import fr.inria.diversify.codeFragment.Statement;
import fr.inria.diversify.transformation.ast.ASTReplace;
import fr.inria.diversify.transformation.ast.ASTAdd;
import fr.inria.diversify.transformation.ast.ASTDelete;
import fr.inria.diversify.transformation.ast.ASTTransformation;
import spoon.reflect.Factory;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.reference.CtVariableReference;

Expand Down Expand Up @@ -96,20 +89,14 @@ public List<CodeFragment> findCandidate(CodeFragment cf) {
protected List<Map<String, String>> getAllVarMapping(CodeFragment before, CodeFragment after) {
List<List<String>> vars = new ArrayList<List<String>>();

for (CtVariableReference<?> variable : after.getInputContext().getLocalVar()) {
for (CtVariableReference<?> variable : after.getInputContext().getVar()) {
List<String> mapping = new ArrayList<String>();
vars.add(mapping);
for (Object candidate : before.getInputContext().allCandidate(variable.getType()))
mapping.add(variable.toString()+"==="+candidate.toString() );

}
for (CtFieldAccess<?> variable : after.getInputContext().getField()) {
List<String> mapping = new ArrayList<String>();
vars.add(mapping);
for (Object candidate : before.getInputContext().allCandidateForFieldAccess(variable.getType()))
mapping.add(variable.getVariable().toString()+"==="+candidate.toString() );

}
return computeVarMapping(vars);
}

Expand Down
15 changes: 2 additions & 13 deletions src/main/java/fr/inria/diversify/statistic/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import fr.inria.diversify.transformation.ast.ASTReplace;
import fr.inria.diversify.transformation.query.ast.ASTTransformationQuery;
import spoon.reflect.Factory;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.reference.CtVariableReference;
Expand Down Expand Up @@ -90,14 +89,10 @@ public List<CodeFragment> findCandidate(CodeFragment cf) {
protected BigInteger getNumberOfVarMapping(CodeFragment before, CodeFragment after) {
BigInteger nb = new BigInteger("1");

for (CtVariableReference<?> variable : after.getInputContext().getLocalVar()) {
for (CtVariableReference<?> variable : after.getInputContext().getVar()) {
BigInteger tmp = new BigInteger(before.getInputContext().allCandidate(variable.getType()).size()+"");
nb = nb.multiply(tmp);
}
for (CtFieldAccess<?> variable : after.getInputContext().getField()) {
BigInteger tmp = new BigInteger(before.getInputContext().allCandidateForFieldAccess(variable.getType()).size()+"");
nb = nb.multiply(tmp);
}
return nb;
}

Expand Down Expand Up @@ -201,20 +196,14 @@ public void run() {
protected List<Map<String, String>> getAllVarMapping(CodeFragment before, CodeFragment after) {
List<List<String>> vars = new ArrayList<List<String>>();

for (CtVariableReference<?> variable : after.getInputContext().getLocalVar()) {
for (CtVariableReference<?> variable : after.getInputContext().getVar()) {
List<String> mapping = new ArrayList<String>();
vars.add(mapping);
for (Object candidate : before.getInputContext().allCandidate(variable.getType()))
mapping.add(variable.toString()+"==="+candidate.toString() );

}
for (CtFieldAccess<?> variable : after.getInputContext().getField()) {
List<String> mapping = new ArrayList<String>();
vars.add(mapping);
for (Object candidate : before.getInputContext().allCandidateForFieldAccess(variable.getType()))
mapping.add(variable.getVariable().toString()+"==="+candidate.toString() );

}
return computeVarMapping(vars);
}

Expand Down

0 comments on commit f847992

Please sign in to comment.