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

review: test SubstitionVisitor enum replace #1389

Merged
merged 4 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
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().Type().createReference(ElementType.class)).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,31 @@
package spoon.test.template.testclasses;

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 anEmum, CtTypeReference<?> anEnum) {
this._value_ = anEmum;
this.anEnum = anEnum;
Copy link
Collaborator

Choose a reason for hiding this comment

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

rename anEmum to enumValue?

could this be created from "anEmum" directly (Reference().create(anEmum.getClass())?

}

@Local
enum _AnEnum_ {
@Parameter
_value_
}
}