use of spoon.reflect.code.CtLiteral in project spoon by INRIA.
the class LiteralTest method testBuildLiternal.
@Test
public void testBuildLiternal() throws Exception {
CtType<Tacos> ctType = buildClass(Tacos.class);
TypeFactory typeFactory = ctType.getFactory().Type();
CtLiteral<?> literal = (CtLiteral<?>) ctType.getField("a").getDefaultExpression();
assertEquals(0, literal.getValue());
assertTrue(literal.getType().isPrimitive());
assertEquals(typeFactory.INTEGER_PRIMITIVE, literal.getType());
literal = (CtLiteral<?>) ctType.getField("b").getDefaultExpression();
assertEquals(0x0, literal.getValue());
assertTrue(literal.getType().isPrimitive());
assertEquals(typeFactory.INTEGER_PRIMITIVE, literal.getType());
literal = (CtLiteral<?>) ctType.getField("c").getDefaultExpression();
assertEquals(0f, literal.getValue());
assertTrue(literal.getType().isPrimitive());
assertEquals(typeFactory.FLOAT_PRIMITIVE, literal.getType());
literal = (CtLiteral<?>) ctType.getField("d").getDefaultExpression();
assertEquals(0l, literal.getValue());
assertTrue(literal.getType().isPrimitive());
assertEquals(typeFactory.LONG_PRIMITIVE, literal.getType());
literal = (CtLiteral<?>) ctType.getField("e").getDefaultExpression();
assertEquals(0d, literal.getValue());
assertTrue(literal.getType().isPrimitive());
assertEquals(typeFactory.DOUBLE_PRIMITIVE, literal.getType());
literal = (CtLiteral<?>) ctType.getField("f").getDefaultExpression();
assertEquals('0', literal.getValue());
assertTrue(literal.getType().isPrimitive());
assertEquals(typeFactory.CHARACTER_PRIMITIVE, literal.getType());
literal = (CtLiteral<?>) ctType.getField("g").getDefaultExpression();
assertEquals("0", literal.getValue());
assertFalse(literal.getType().isPrimitive());
assertEquals(typeFactory.STRING, literal.getType());
literal = (CtLiteral<?>) ctType.getField("h").getDefaultExpression();
assertEquals(null, literal.getValue());
assertFalse(literal.getType().isPrimitive());
assertEquals(typeFactory.NULL_TYPE, literal.getType());
}
use of spoon.reflect.code.CtLiteral in project spoon by INRIA.
the class LiteralTest method testFactoryLiternal.
@Test
public void testFactoryLiternal() {
Launcher runLaunch = new Launcher();
Factory factory = runLaunch.getFactory();
CodeFactory code = factory.Code();
CtLiteral literal = code.createLiteral(1);
assertEquals(1, literal.getValue());
assertEquals(factory.Type().integerPrimitiveType(), literal.getType());
literal = code.createLiteral(new Integer(1));
assertEquals(1, literal.getValue());
assertEquals(factory.Type().integerPrimitiveType(), literal.getType());
literal = code.createLiteral(1.0);
assertEquals(1.0, literal.getValue());
assertEquals(factory.Type().doublePrimitiveType(), literal.getType());
literal = code.createLiteral("literal");
assertEquals("literal", literal.getValue());
assertEquals(factory.Type().stringType(), literal.getType());
}
use of spoon.reflect.code.CtLiteral in project spoon by INRIA.
the class QualifiedThisRefTest method testPrintCtFieldAccessWorkEvenWhenParentNotInitialized.
@Test
public void testPrintCtFieldAccessWorkEvenWhenParentNotInitialized() throws Exception {
CtClass zeclass = factory.Class().get(QualifiedThisRef.class);
List<CtMethod> methods = zeclass.getMethodsByName("bla");
assertEquals(1, methods.size());
CtStatement invocation = methods.get(0).getBody().getStatement(0);
assertTrue(invocation instanceof CtInvocation);
CtInvocation<?> arg0 = (CtInvocation) invocation;
CtExpression param = arg0.getArguments().get(0);
CtExecutableReference execref = factory.Core().createExecutableReference();
execref.setDeclaringType(factory.Type().createReference("java.util.Map"));
execref.setSimpleName("exorcise");
execref.setStatic(true);
CtTypeReference tmp = param.getType();
CtExpression arg = null;
CtFieldReference ctfe = factory.createFieldReference();
ctfe.setSimpleName("class");
ctfe.setDeclaringType(tmp.box());
arg = factory.Core().createFieldRead();
((CtFieldAccessImpl) arg).setVariable(ctfe);
CtLiteral location = factory.Core().createLiteral();
location.setType(factory.Type().createReference(String.class));
CtTypeReference tmpref = factory.Core().clone(tmp);
CtInvocation invoc = factory.Core().createInvocation();
invoc.setExecutable(execref);
invoc.setArguments(Arrays.asList(new CtExpression[] { param, arg, location }));
execref.setActualTypeArguments(Arrays.asList(new CtTypeReference<?>[] { tmpref }));
// succeeds
arg0.getArguments().set(0, invoc);
DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(factory.getEnvironment());
printer.visitCtClass(zeclass);
assertFalse(printer.getResult().isEmpty());
}
use of spoon.reflect.code.CtLiteral in project Ex2Amplifier by STAMP-project.
the class CATGAmplifier method buildMethodFromValues.
private CtMethod<?> buildMethodFromValues(List<String> values, CtMethod originalTestMethod) {
final Iterator<String> iteratorOnNewValues = values.iterator();
final CtMethod<?> clone = new AssertionRemover().removeAssertion(AmplificationHelper.cloneTestMethodForAmp(originalTestMethod, "_Ex2_CATG"));
final List<CtLiteral<?>> originalLiterals = clone.getBody().getElements(CT_LITERAL_TYPE_FILTER);
originalLiterals.forEach(ctLiteral -> ctLiteral.replace(buildNewLiteralFromString(iteratorOnNewValues.next(), ctLiteral)));
return clone;
}
use of spoon.reflect.code.CtLiteral in project dspot by STAMP-project.
the class TestDataMutatorTest method testStringMutation.
@Test
public void testStringMutation() throws Exception {
/*
Test the amplification on string literal
3 operations: remove 1 random char, replace 1 random char, add 1 random char
Additionally, it replace by totally random string with the same size than the original one,
and by 1 literals present that is different that the muted
*/
final String nameMethod = "methodString";
final String originalValue = "MyStringLiteral";
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
AmplificationHelper.setSeedRandom(42L);
TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<CtMethod> mutantMethods = amplificator.apply(method);
assertEquals(12, mutantMethods.size());
for (int i = 0; i < 6; i++) {
CtMethod mutantMethod = mutantMethods.get(i);
assertEquals(nameMethod + SUFFIX_MUTATION + "String" + (i + 1), mutantMethod.getSimpleName());
CtLiteral mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtLiteral.class)).get(0);
assertNotEquals(originalValue, mutantLiteral.getValue());
assertDistanceBetweenOriginalAndMuted(originalValue, (String) mutantLiteral.getValue());
}
}
Aggregations