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

Conversation

pvojtechovsky
Copy link
Collaborator

There was not possible to substitute name of invoked method.

Example template:

public class InvocationTemplate extends ExtensionTemplate {

	IFace iface;
	
	void invoke() {
//this method name was not substituted. This PR fixes it
		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$();
	}
}

@@ -436,4 +436,18 @@ public void testTemplateMatcherMatchTwoSnippets() throws Exception {

assertTrue(match1.equals(match2));
}
@Test
public void testTemplateInvocationSubstitution() throws Exception {
Launcher spoon = new Launcher();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contract: the template engine supports substitution of method names in method calls.

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);
assertEquals("iface.hashCode()", templateRoot.toString());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// iface.$method$() becomes iface.hashCode()

@monperrus
Copy link
Collaborator

interesting!

it looks more like a new feature than a fix :-)

@monperrus monperrus merged commit ceaa18b into INRIA:master May 29, 2017
@monperrus
Copy link
Collaborator

Thanks Pavel! you can be proud of being the first external contributor contributing to the awesome yet complex template engine!!!

@pvojtechovsky pvojtechovsky deleted the templateExecutable branch May 29, 2017 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants