Skip to content

Commit

Permalink
test: add test for template substitution of enum values (#1389)
Browse files Browse the repository at this point in the history
* test SubstitutionVisitor enum value replace

* fix SubstitutionVisitor enum value replace

* check generated code

* fix variable name and create enum class reference in tmpl constructor
  • Loading branch information
pvojtechovsky authored and monperrus committed Jun 12, 2017
1 parent 42cf54e commit 526bf88
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/java/spoon/support/template/SubstitutionVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ private <T> void visitFieldAccess(CtFieldAccess<T> fieldAccess) {
.createClassAccess(factory.Type().createReference(((Class<?>) value).getName())));
} else if (value instanceof Enum) {
CtTypeReference<?> enumType = factory.Type().createReference(value.getClass());
toReplace.replace(factory.Code().createVariableRead(
factory.Field().createReference(enumType, enumType, ((Enum<?>) value).name()), true));
CtFieldRead<T> enumValueAccess = (CtFieldRead<T>) factory.Code().createVariableRead(
factory.Field().createReference(enumType, enumType, ((Enum<?>) value).name()), true);
enumValueAccess.setTarget(factory.Code().createTypeAccess(enumType));
toReplace.replace(enumValueAccess);
} else if (value instanceof List) {
// replace list of CtParameter for generic access to the
// parameters
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/spoon/test/template/TemplateEnumAccessTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package spoon.test.template;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.lang.annotation.ElementType;

import org.junit.Test;

import spoon.Launcher;
import spoon.OutputType;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.factory.Factory;
import spoon.support.compiler.FileSystemFile;
import spoon.test.template.testclasses.EnumAccessTemplate;
import spoon.testing.utils.ModelUtils;

public class TemplateEnumAccessTest {

@Test
public void testEnumAccessTest() throws Exception {
//contract: the template engine supports enum value access substitution
Launcher launcher = new Launcher();
launcher.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/EnumAccessTemplate.java"));

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

CtClass<?> resultKlass = factory.Class().create(factory.Package().getOrCreate("spoon.test.template"), "EnumAccessResult");
new EnumAccessTemplate(ElementType.FIELD, launcher.getFactory()).apply(resultKlass);
assertEquals("java.lang.annotation.ElementType.FIELD.name()", resultKlass.getMethod("method").getBody().getStatement(0).toString());
launcher.setSourceOutputDirectory(new File("./target/spooned/"));
launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
ModelUtils.canBeBuilt(new File("./target/spooned/spoon/test/template/EnumAccessResult.java"), 8);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package spoon.test.template.testclasses;

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

public class EnumAccessTemplate extends ExtensionTemplate {

public void method() throws Throwable {
_AnEnum_._value_.name();
}

@Parameter("_AnEnum_")
CtTypeReference<?> anEnum;

@Parameter
Enum _value_;

@Local
public EnumAccessTemplate(Enum enumValue, Factory factory) {
this._value_ = enumValue;
this.anEnum = factory.Type().createReference(enumValue.getClass());
}

@Local
enum _AnEnum_ {
@Parameter
_value_
}
}

0 comments on commit 526bf88

Please sign in to comment.