Skip to content

Commit

Permalink
test type access and type arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Sep 6, 2017
1 parent 25286d9 commit 1416ee5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
33 changes: 33 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,7 @@
import spoon.reflect.declaration.CtTypeMember;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.ModelConsistencyChecker;
import spoon.reflect.visitor.filter.NameFilter;
import spoon.support.compiler.FileSystemFile;
Expand All @@ -44,6 +45,7 @@
import spoon.test.template.testclasses.SubStringTemplate;
import spoon.test.template.testclasses.SubstituteLiteralTemplate;
import spoon.test.template.testclasses.SubstituteRootTemplate;
import spoon.test.template.testclasses.TypeReferenceClassAccessTemplate;
import spoon.test.template.testclasses.bounds.CheckBound;
import spoon.test.template.testclasses.bounds.CheckBoundMatcher;
import spoon.test.template.testclasses.bounds.CheckBoundTemplate;
Expand All @@ -62,6 +64,7 @@
import spoon.test.template.testclasses.types.AClassModel;
import spoon.test.template.testclasses.types.AnEnumModel;
import spoon.test.template.testclasses.types.AnIfaceModel;
import spoon.testing.utils.ModelUtils;

import java.io.File;
import java.io.Serializable;
Expand Down Expand Up @@ -1014,4 +1017,34 @@ public void testAnotherFieldAccessNameSubstitution() throws Exception {
assertEquals("java.lang.System.out.println(((x) + (m_x)))", result.getAnonymousExecutables().get(0).getBody().getStatement(0).toString());
}
}

@Test
public void substituteTypeAccessReference() throws Exception {
//contract: the substitution of CtTypeAccess expression ignores actual type arguments if it have to
Launcher spoon = new Launcher();
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/TypeReferenceClassAccessTemplate.java"));
String outputDir = "./target/spooned/test/template/testclasses";
spoon.setSourceOutputDirectory(outputDir);

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

//contract: String value is substituted in substring of literal, named element and reference
CtTypeReference<?> typeRef = factory.Type().createReference(TypeReferenceClassAccessTemplate.Example.class);
typeRef.addActualTypeArgument(factory.Type().DATE);

final CtClass<?> result = (CtClass<?>) new TypeReferenceClassAccessTemplate(typeRef).apply(factory.Class().create("spoon.test.template.TypeReferenceClassAccess"));
spoon.prettyprint();
ModelUtils.canBeBuilt(outputDir, 8);
CtMethod<?> method = result.getMethodsByName("someMethod").get(0);
assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>", method.getType().toString());
assertEquals("spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>", method.getParameters().get(0).getType().toString());
assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.out", method.getBody().getStatement(0).toString());
assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.currentTimeMillis()", method.getBody().getStatement(1).toString());
assertEquals("o = spoon.test.template.TypeReferenceClassAccess.Example.class", method.getBody().getStatement(2).toString());
assertEquals("o = (o) instanceof spoon.test.template.TypeReferenceClassAccess.Example", method.getBody().getStatement(3).toString());
assertEquals("java.util.function.Supplier<java.lang.Long> p = spoon.test.template.TypeReferenceClassAccess.Example::currentTimeMillis", method.getBody().getStatement(4).toString());
assertEquals("return new spoon.test.template.TypeReferenceClassAccess.Example<java.util.Date>()", method.getBody().getStatement(5).toString());

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

import java.util.function.Supplier;

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

public class TypeReferenceClassAccessTemplate extends ExtensionTemplate {
Object o;

$Type$ someMethod($Type$ param) {
o = $Type$.out;
o = $Type$.currentTimeMillis();
o = $Type$.class;
o = o instanceof $Type$;
Supplier<Long> p = $Type$::currentTimeMillis;
return new $Type$();
}

@Local
public TypeReferenceClassAccessTemplate(CtTypeReference<?> typeRef) {
this.typeRef = typeRef;
}

@Parameter("$Type$")
CtTypeReference<?> typeRef;

@Local
static class $Type$ {
static final String out = "";
static long currentTimeMillis(){
return 0;
}
}

public static class Example<T> {
static final String out = "";
static long currentTimeMillis(){
return 0;
}
}
}

0 comments on commit 1416ee5

Please sign in to comment.