Skip to content

Commit

Permalink
Try to replicate issue INRIA#1191
Browse files Browse the repository at this point in the history
  • Loading branch information
surli committed Feb 27, 2017
1 parent e7acf58 commit ee33c91
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/test/java/spoon/test/template/TemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import spoon.support.template.Parameters;
import spoon.template.TemplateMatcher;
import spoon.test.template.testclasses.SecurityCheckerTemplate;
import spoon.test.template.testclasses.transfo.Hello;
import spoon.test.template.testclasses.transfo.MyTrans;

import java.io.File;
import java.io.Serializable;
Expand Down Expand Up @@ -436,4 +438,18 @@ public void testTemplateMatcherMatchTwoSnippets() throws Exception {

assertTrue(match1.equals(match2));
}

@Test
public void testTransfo() throws Exception {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/transfo/Hello.java");
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/transfo/MyTrans.java"));
spoon.addProcessor(new MyTrans());
spoon.buildModel();
Factory factory = spoon.getFactory();

CtClass<?> helloClass = factory.Class().get(Hello.class);
CtMethod helloMethod = helloClass.getElements(new NameFilter<CtMethod>("print")).get(0);
assertTrue(helloMethod.toString().contains("throws Exception"));
}
}
18 changes: 18 additions & 0 deletions src/test/java/spoon/test/template/testclasses/transfo/Hello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package spoon.test.template.testclasses.transfo;

public class Hello {
int a;
int b;
class Inner {
public int i;
}
public void print(boolean tt) throws Exception{
a = 0;
//asdasdsda
System.out.println("a: " + a);
}
}

class Test {
float q;
}
61 changes: 61 additions & 0 deletions src/test/java/spoon/test/template/testclasses/transfo/MyTrans.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package spoon.test.template.testclasses.transfo;

import spoon.processing.AbstractProcessor;
import spoon.template.Substitution;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtType;
import spoon.template.ExtensionTemplate;
import spoon.reflect.declaration.CtParameter;
import spoon.support.reflect.declaration.CtParameterImpl;
import spoon.reflect.reference.CtTypeReference;

import spoon.template.Parameter;
import spoon.template.Local;

import java.util.*;

public class MyTrans extends AbstractProcessor<CtClass> {
public void process (CtClass ctClass) {
CtParameter newPara = getFactory().createParameter();
newPara.setSimpleName("abc");
newPara.setType(getFactory().Type().floatPrimitiveType());

ParaTemp temp = new ParaTemp();

if (ctClass.getSimpleName().equals("Hello")) {
for (Object mo : ctClass.getAllMethods()) {
CtMethod m = (CtMethod)mo;
if (m.getSimpleName().equals("print")) {
temp.type = m.getType();
temp.mName = m.getSimpleName();
temp.body = m.getBody();
temp.thrownType = m.getThrownTypes();

temp.params = m.getParameters();
temp.params.add(newPara);
}
}
Substitution.insertAll(ctClass, temp);
}
}
}

class ParaTemp extends ExtensionTemplate {
public void mName(Object params) {
body.S();
}

@Parameter
public List<CtParameter> params;
@Parameter
public String mName;
@Parameter
public CtBlock body;
@Parameter
public Set<CtTypeReference> thrownType;
@Parameter
public CtTypeReference type;
}

0 comments on commit ee33c91

Please sign in to comment.