use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.
the class FieldAccessTest method testIncrementationOnAVarIsAUnaryOperator.
@Test
public void testIncrementationOnAVarIsAUnaryOperator() throws Exception {
// contract: When we use var++, the variable is a read access with an unary operator.
final CtType<Panini> aMole = buildClass(Panini.class);
final CtMethod<?> make = aMole.getMethodsByName("make").get(0);
final List<CtUnaryOperator<?>> unaryOperators = make.getElements(new TypeFilter<CtUnaryOperator<?>>(CtUnaryOperator.class));
final CtFieldWrite<Object> fieldRead = aMole.getFactory().Core().createFieldWrite();
fieldRead.setTarget(aMole.getFactory().Code().createThisAccess(aMole.getReference(), true));
final CtFieldReference fieldReference = aMole.getField("i").getReference();
fieldRead.setVariable(fieldReference);
assertEquals(2, unaryOperators.size());
final CtUnaryOperator<?> first = unaryOperators.get(0);
assertEquals(UnaryOperatorKind.POSTINC, first.getKind());
assertEquals(fieldRead, first.getOperand());
assertEquals("(i)++", first.toString());
final CtUnaryOperator<?> second = unaryOperators.get(1);
assertEquals(UnaryOperatorKind.PREINC, second.getKind());
assertEquals(fieldRead, second.getOperand());
assertEquals("++(i)", second.toString());
}
use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.
the class FieldAccessTest method testFieldAccessNoClasspath.
@Test
public void testFieldAccessNoClasspath() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/import-resources/fr/inria/");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
CtType<?> ctType = launcher.getFactory().Class().get("FooNoClassPath");
CtFieldAccess ctFieldAccess = ctType.getElements(new TypeFilter<>(CtFieldAccess.class)).get(0);
assertEquals("(game.board.width)", ctFieldAccess.toString());
CtFieldReference ctFieldReferenceWith = ctFieldAccess.getVariable();
assertEquals("width", ctFieldReferenceWith.getSimpleName());
CtFieldAccess ctFieldAccessBoard = (CtFieldAccess) ctFieldAccess.getTarget();
assertEquals("game.board", ctFieldAccessBoard.toString());
CtFieldReference ctFieldReferenceBoard = ctFieldAccessBoard.getVariable();
assertEquals("board", ctFieldReferenceBoard.getSimpleName());
CtFieldAccess ctFieldAccessGame = (CtFieldAccess) ctFieldAccessBoard.getTarget();
assertEquals("game.board", ctFieldAccessBoard.toString());
CtFieldReference ctFieldReferenceGame = ctFieldAccessGame.getVariable();
assertEquals("game", ctFieldReferenceGame.getSimpleName());
}
use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.
the class TemplateMatcher method findParameterMatcher.
/**
* returns an appropriate ParameterMatcher defined in a template parameter, or else a default one
*
* if a template parameter (field annotated with @Parameter) whose name (field name) is a substring of the template name, it also works
*/
private ParameterMatcher findParameterMatcher(CtNamedElement templateDeclaration) throws InstantiationException, IllegalAccessException {
if (templateDeclaration == null) {
return new DefaultParameterMatcher();
}
String name = templateDeclaration.getSimpleName();
CtClass<?> clazz = null;
try {
clazz = templateDeclaration.getParent(CtClass.class);
} catch (ParentNotInitializedException e) {
Launcher.LOGGER.error(e.getMessage(), e);
}
if (clazz == null) {
return new DefaultParameterMatcher();
}
Collection<CtFieldReference<?>> fields = clazz.getAllFields();
CtFieldReference<?> param = null;
for (CtFieldReference<?> fieldRef : fields) {
Parameter p = fieldRef.getDeclaration().getAnnotation(Parameter.class);
if (p == null) {
// not a parameter.
continue;
}
String proxy = p.value();
if (!"".equals(proxy)) {
if (name.contains(proxy)) {
param = fieldRef;
break;
}
}
if (name.contains(fieldRef.getSimpleName())) {
param = fieldRef;
break;
}
// todo: check for field hack.
}
return getParameterInstance(param);
}
use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.
the class CtTypeReferenceImpl method getDeclaredFieldReferences.
/**
* Collects all field references of the declared class.
*
* @return collection of field references
*/
private Collection<CtFieldReference<?>> getDeclaredFieldReferences() {
Collection<CtFieldReference<?>> references = new ArrayList<>();
for (Field f : getDeclaredFields(getActualClass())) {
references.add(getFactory().Field().createReference(f));
}
if (getActualClass().isAnnotation()) {
for (Method m : getActualClass().getDeclaredMethods()) {
CtTypeReference<?> retRef = getFactory().Type().createReference(m.getReturnType());
CtFieldReference<?> fr = getFactory().Field().createReference(this, retRef, m.getName());
references.add(fr);
}
}
return references;
}
use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.
the class VisibilityTest method testName.
@Test
public void testName() throws Exception {
final SpoonAPI launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/visibility/testclasses/Tacos.java", "-o", "./target/spooned/visibility" });
final List<CtFieldReference<?>> references = Query.getElements(launcher.getFactory(), new AbstractReferenceFilter<CtFieldReference<?>>(CtFieldReference.class) {
@Override
public boolean matches(CtFieldReference<?> reference) {
return "x".equals(reference.getSimpleName());
}
});
assertEquals(1, references.size());
final CtFieldReference<?> field = references.get(0);
assertNotNull(field.getDeclaration());
final CtClass<?> tacos = launcher.getFactory().Class().get("spoon.test.visibility.testclasses.Tacos");
assertEquals(tacos, field.getDeclaringType().getDeclaration());
assertEquals(tacos.getFields().get(0), field.getDeclaration());
}
Aggregations