use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class VariableReferencesTest method testVariableScopeFunction.
@Test
public void testVariableScopeFunction() throws Exception {
// visits all the CtVariable elements whose name is "field" and search for all elements in their scopes
// Comparing with the result found by basic functions
List list = modelClass.filterChildren((CtVariable<?> var) -> {
if (var.getSimpleName().equals("field")) {
if (var instanceof CtField) {
// field scope is not supported
return false;
}
CtElement[] real = var.map(new VariableScopeFunction()).list().toArray(new CtElement[0]);
if (var instanceof CtLocalVariable) {
assertArrayEquals(var.map(new LocalVariableScopeFunction()).list().toArray(new CtElement[0]), real);
} else if (var instanceof CtField) {
// assertArrayEquals(var.map(new FieldScopeFunction()).list().toArray(new CtElement[0]), real);
} else if (var instanceof CtParameter) {
assertArrayEquals(var.map(new ParameterScopeFunction()).list().toArray(new CtElement[0]), real);
} else if (var instanceof CtCatchVariable) {
assertArrayEquals(var.map(new CatchVariableScopeFunction()).list().toArray(new CtElement[0]), real);
} else {
fail("Unexpected variable of type " + var.getClass().getName());
}
return true;
}
return false;
}).list();
assertTrue(list.size() > 0);
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class SnippetTest method testCompileStatementWithReturn.
@Test
public void testCompileStatementWithReturn() throws Exception {
// contract: a snippet with return can be compiled.
CtElement el = SnippetCompilationHelper.compileStatement(factory.Code().createCodeSnippetStatement("return 3"), factory.Type().INTEGER);
assertTrue(CtReturn.class.isAssignableFrom(el.getClass()));
assertEquals("return 3", el.toString());
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class SerializationModelStreamer method load.
public Factory load(InputStream in) throws IOException {
try {
ObjectInputStream ois = new ObjectInputStream(in);
final Factory f = (Factory) ois.readObject();
// create query using factory directly
// because any try to call CtElement#map or CtElement#filterChildren will fail on uninitialized factory
f.createQuery(f.Module().getAllModules().toArray()).filterChildren(new Filter<CtElement>() {
@Override
public boolean matches(CtElement e) {
e.setFactory(f);
return false;
}
}).list();
ois.close();
return f;
} catch (ClassNotFoundException e) {
Launcher.LOGGER.error(e.getMessage(), e);
throw new IOException(e.getMessage());
}
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class ContextBuilder method exit.
void exit(ASTNode node) {
ASTPair pair = stack.pop();
if (pair.node != node) {
throw new RuntimeException("Inconsistent Stack " + node + "\n" + pair.node);
}
CtElement current = pair.element;
if (!stack.isEmpty()) {
this.jdtTreeBuilder.getExiter().setChild(current);
this.jdtTreeBuilder.getExiter().setChild(pair.node);
this.jdtTreeBuilder.getExiter().scan(stack.peek().element);
}
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class FieldReferenceFunction method apply.
@Override
public void apply(CtElement fieldOrScope, CtConsumer<Object> outputConsumer) {
CtElement scope;
CtField<?> field = this.field;
if (field == null) {
if (fieldOrScope instanceof CtField) {
field = (CtField<?>) fieldOrScope;
} else {
throw new SpoonException("The input of FieldReferenceFunction must be a CtField but is " + fieldOrScope.getClass().getSimpleName());
}
scope = field.getFactory().getModel().getUnnamedModule();
} else {
scope = fieldOrScope;
}
scope.filterChildren(new DirectReferenceFilter<CtFieldReference<?>>(field.getReference())).forEach(outputConsumer);
}
Aggregations