Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Template substitution of CtExecutableReference#simpleName #1325

Merged
merged 3 commits into from
May 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/main/java/spoon/support/template/SubstitutionVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.reference.CtReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtInheritanceScanner;
import spoon.reflect.visitor.CtScanner;
Expand Down Expand Up @@ -131,22 +132,30 @@ public void scanCtNamedElement(CtNamedElement element) {
element.setDocComment(substituteInDocComment(element.getDocComment()));
}
// replace parameters in names
String name = element.getSimpleName();
element.setSimpleName(substituteName(element.getSimpleName(), element));
super.scanCtNamedElement(element);
}

@Override
public void scanCtReference(CtReference reference) {
reference.setSimpleName(substituteName(reference.getSimpleName(), reference));
super.scanCtReference(reference);
}

private String substituteName(String name, CtElement element) {
for (String pname : parameterNames) {
if (name.contains(pname)) {
Object value = Parameters.getValue(template, pname, null);
if (value instanceof String) {
// replace with the string value
name = name.replace(pname, (String) value);
element.setSimpleName(name);
} else if ((value instanceof CtTypeReference) && (element instanceof CtType)) {
// replace with the type reference's name
name = name.replace(pname, ((CtTypeReference<?>) value).getSimpleName());
element.setSimpleName(name);
}
}
}
super.scanCtNamedElement(element);
return name;
}

private String substituteInDocComment(String docComment) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/spoon/test/template/InvocationTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package spoon.test.template;

import spoon.reflect.reference.CtTypeReference;
import spoon.template.ExtensionTemplate;
import spoon.template.Local;
import spoon.template.Parameter;

public class InvocationTemplate extends ExtensionTemplate {

IFace iface;

void invoke() {
iface.$method$();
}

@Local
public InvocationTemplate(CtTypeReference<?> ifaceType, String methodName) {
this.ifaceType = ifaceType;
this.methodName = methodName;
}

@Parameter("IFace")
CtTypeReference<?> ifaceType;

@Parameter("$method$")
String methodName;



@Local
interface IFace {
void $method$();
}
}
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 @@ -436,4 +436,20 @@ public void testTemplateMatcherMatchTwoSnippets() throws Exception {

assertTrue(match1.equals(match2));
}
@Test
public void testTemplateInvocationSubstitution() throws Exception {
//contract: the template engine supports substitution of method names in method calls.
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/InvocationTemplate.java"));

spoon.buildModel();
Factory factory = spoon.getFactory();

CtClass<?> resultKlass = factory.Class().create("Result");
new InvocationTemplate(factory.Type().OBJECT, "hashCode").apply(resultKlass);
CtMethod<?> templateMethod = (CtMethod<?>) resultKlass.getElements(new NameFilter("invoke")).get(0);
CtStatement templateRoot = (CtStatement) templateMethod.getBody().getStatement(0);
//iface.$method$() becomes iface.hashCode()
assertEquals("iface.hashCode()", templateRoot.toString());
}
}