Skip to content

Commit

Permalink
update code to support 5.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb committed Jan 13, 2017
1 parent 6bddb50 commit 6721be5
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
package fr.inria.diversify.codeFragment;

import spoon.compiler.Environment;
import spoon.reflect.code.*;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtVariableAccess;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtFieldReference;
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 java.util.Stack;

public class CodeFragmentEqualPrinter extends DefaultJavaPrettyPrinter {

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

public CodeFragmentEqualPrinter(Environment env) {
super(env);

this.printer = new PrinterHelper(env);
this.elementPrinterHelper = new ElementPrinterHelper(this.printer, this, env);
}

public <T> void visitCtInvocation(CtInvocation<T> invocation) {
if(invocation.getTarget() == null){
write(invocation.getExecutable().getDeclaringType().toString());
write(".");
printer.write(invocation.getExecutable().getDeclaringType().toString());
printer.write(".");
}
super.visitCtInvocation(invocation);
}

public <T> void visitCtVariableAccess(CtVariableAccess<T> variableAccess) {
enterCtExpression(variableAccess);
write(variableAccess.getType().toString());
printer.write(variableAccess.getType().toString());
exitCtExpression(variableAccess);
}

public <T> void visitCtLocalVariableReference(
CtLocalVariableReference<T> reference) {
write(reference.getType().toString());
printer.write(reference.getType().toString());
}

public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
Expand All @@ -43,7 +52,7 @@ public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
.getDeclaringType().isAnonymous())) {
context.ignoreGenerics = true;
scan(reference.getDeclaringType());
write(".");
printer.write(".");
context.ignoreGenerics = false;
}
} else {
Expand Down Expand Up @@ -78,22 +87,22 @@ public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
context.ignoreGenerics = true;
scan(reference.getDeclaringType());
context.ignoreGenerics = false;
write(".");
printer.write(".");
}
}
write(reference.getType().toString());
printer.write(reference.getType().toString());
}

public <T> DefaultJavaPrettyPrinter writeLocalVariable(
CtLocalVariable<T> localVariable) {
if (!context.noTypeDecl) {
writeModifiers(localVariable);
elementPrinterHelper.writeModifiers(localVariable);
scan(localVariable.getType());
write(" ");
printer.write(" ");
}
write(localVariable.getType().toString());
printer.write(localVariable.getType().toString());
if (localVariable.getDefaultExpression() != null) {
write(" = ");
printer.write(" = ");
scan(localVariable.getDefaultExpression());
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fr.inria.diversify.codeFragment.CodeFragmentList;
import fr.inria.diversify.codeFragment.Statement;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.cu.SourcePosition;

/**
* Created by marodrig on 20/01/2015.
Expand All @@ -18,4 +18,9 @@ public void process(CtStatement ctElement) {
addCf(stmt);
}
}

@Override
public boolean isToBeProcessed(CtStatement candidate) {
return candidate.getPosition() != SourcePosition.NOPOSITION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public void setCustomClassLoader(DiversifyClassLoader customClassLoader) {


public boolean compileFileIn(File directory, boolean withLog) {
initInputClassLoader();
// initInputClassLoader();
javaCompliance = factory.getEnvironment().getComplianceLevel();

MainCompiler compiler = new MainCompiler(this, true, environment);
MainCompiler compiler = new MainCompiler(this, environment);

final SourceOptions sourcesOptions = new SourceOptions();
sourcesOptions.sources((new FileSystemFolder(directory).getAllJavaFiles()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
public class MainCompiler extends JDTBatchCompiler {
FileSystem environment;

public MainCompiler(JDTBasedSpoonCompiler jdtCompiler, boolean useFactory, FileSystem environment ) {
super(jdtCompiler, useFactory);
public MainCompiler(JDTBasedSpoonCompiler jdtCompiler, FileSystem environment ) {
super(jdtCompiler);
this.environment = environment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public CtCodeElement buildReplacement() throws BuildTransplantException {
*/
@Test
public void testTransformation() throws Exception {

Factory f = new SpoonMetaFactoryTest().build();
InputProgram p = new InputProgram();
p.setFactory(f);
Expand All @@ -38,12 +39,12 @@ public void testTransformation() throws Exception {

CtCodeSnippetStatement snippetStatement = new CtCodeSnippetStatementImpl();
snippetStatement.setValue("/** TRANSFORMATION FRAGMENT DELETED:\n" +
"super()\n" +
"return a + b\n" +
"**/");

//Test that the replacement was properly built
ASTDeleteForTest a = new ASTDeleteForTest();
a.setTransplantationPoint(p.getCodeFragments().get(0));
a.setTransplantationPoint(p.getCodeFragments().get(1));
assertEquals(snippetStatement.getValue(), ((CtCodeSnippetStatement)a.buildReplacement()).getValue());
}
}
5 changes: 0 additions & 5 deletions diversify-profiling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
<artifactId>diversify-profiling</artifactId>

<dependencies>
<dependency>
<groupId>fr.inria.gforge.spoon</groupId>
<artifactId>spoon-core</artifactId>
<version>5.1.1</version>
</dependency>

<dependency>
<groupId>fr.inria.diversify.sosiefier</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.QueryVisitor;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.reflect.code.CtTryImpl;

import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -98,7 +99,7 @@ protected CtTry tryFinallyBody(CtExecutable method) {
CtStatement thisStatement = getThisOrSuperCall(method.getBody());

CtTry ctTry = factory.Core().createTry();
ctTry.setBody(method.getBody());
((CtTryImpl)ctTry).setBody(method.getBody());

CtBlock finalizerBlock = factory.Core().createBlock();
ctTry.setFinalizer(finalizerBlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtType;

import java.io.BufferedReader;
Expand Down Expand Up @@ -80,7 +81,7 @@ public double codeFragmentCoverage(CodeFragment stmt) {

if (foundMethod ) {
String className = stmt.getCtCodeFragment().getParent(CtType.class).getQualifiedName();
return methodSignatures.contains(className + "." + e.getSignature()) ? 1.0 : 0.0;
return methodSignatures.contains(className + "." + ((CtNamedElement)e).getSimpleName()) ? 1.0 : 0.0;
}
return 0.0;
//throw new RuntimeException("Unable to find parent method");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ private void reWriteInvocation(CtInvocation inv, CtMethod newMethod, CtMethod ot

List<CtExpression> args = inv.getArguments();
if(isFirst) {
args.addAll(other.getFormalTypeParameters().stream().map(t -> defaultValue(t.getDeclaration())).collect(Collectors.toList()));
args.addAll(other.getFormalCtTypeParameters().stream().map(t -> defaultValue(t.getDeclaringType())).collect(Collectors.toList()));
args.add(factory.Code().createLiteral(true));
newInv.setExecutable(newMethod.getReference());
newInv.setTarget(inv.getTarget());
newInv.setArguments(args);
} else {
args.clear();
args.addAll(other.getFormalTypeParameters().stream().map(t -> defaultValue(t.getDeclaration())).collect(Collectors.toList()));
args.addAll(other.getFormalCtTypeParameters().stream().map(t -> defaultValue(t.getDeclaringType())).collect(Collectors.toList()));
args.addAll(inv.getArguments());
args.add(factory.Code().createLiteral(false));
newInv.setExecutable(newMethod.getReference());
Expand Down
Loading

0 comments on commit 6721be5

Please sign in to comment.