use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class ImportScannerImpl method lookForLocalVariables.
protected Set<String> lookForLocalVariables(CtElement parent) {
Set<String> result = new HashSet<>();
// if the first container is the class, then we are not in a block and we can quit now.
while (parent != null && !(parent instanceof CtBlock)) {
if (parent instanceof CtClass) {
return result;
}
parent = parent.getParent();
}
if (parent != null) {
CtBlock block = (CtBlock) parent;
boolean innerClass = false;
// now we have the first container block, we want to check if we're not in an inner class
while (parent != null && !(parent instanceof CtClass)) {
parent = parent.getParent();
}
if (parent != null) {
// let's find the last block BEFORE the class call: some collision could occur because of variables defined in that block
if (!(parent.getParent() instanceof CtPackage)) {
while (parent != null && !(parent instanceof CtBlock)) {
parent = parent.getParent();
}
if (parent != null) {
block = (CtBlock) parent;
}
}
}
AccessibleVariablesFinder avf = new AccessibleVariablesFinder(block);
List<CtVariable> variables = avf.find();
for (CtVariable variable : variables) {
result.add(variable.getSimpleName());
}
}
return result;
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class OverriddenMethodQuery method apply.
@Override
public void apply(CtMethod<?> input, CtConsumer<Object> outputConsumer) {
CtPackage searchScope = input.getFactory().Package().getRootPackage();
searchScope.filterChildren(new OverriddenMethodFilter(input)).forEach(outputConsumer);
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class PotentialVariableDeclarationFunction method apply.
@Override
public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
isTypeOnTheWay = false;
isInStaticScope = false;
// Search previous siblings for element which may represents the declaration of this local variable
CtQuery siblingsQuery = input.getFactory().createQuery().map(new SiblingsFunction().mode(SiblingsFunction.Mode.PREVIOUS)).select(new TypeFilter<>(CtVariable.class));
if (variableName != null) {
// variable name is defined so we have to search only for variables with that name
siblingsQuery = siblingsQuery.select(new NamedElementFilter<>(CtNamedElement.class, variableName));
}
CtElement scopeElement = input;
// Search input and then all parents until first CtPackage for element which may represents the declaration of this local variable
while (scopeElement != null && !(scopeElement instanceof CtPackage) && scopeElement.isParentInitialized()) {
CtElement parent = scopeElement.getParent();
if (parent instanceof CtType<?>) {
isTypeOnTheWay = true;
// visit each CtField of `parent` CtType
CtQuery q = parent.map(new AllTypeMembersFunction(CtField.class));
q.forEach((CtField<?> field) -> {
if (isInStaticScope && field.hasModifier(ModifierKind.STATIC) == false) {
/*
* the variable reference is used in static scope,
* but the field is not static - ignore it
*/
return;
}
// else send field as potential variable declaration
if (sendToOutput(field, outputConsumer)) {
// and terminate the internal query q if outer query is already terminated
q.terminate();
}
});
if (query.isTerminated()) {
return;
}
} else if (parent instanceof CtBodyHolder || parent instanceof CtStatementList) {
// visit all previous CtVariable siblings of scopeElement element in parent BodyHolder or Statement list
siblingsQuery.setInput(scopeElement).forEach(outputConsumer);
if (query.isTerminated()) {
return;
}
// visit parameters of CtCatch and CtExecutable (method, lambda)
if (parent instanceof CtCatch) {
CtCatch ctCatch = (CtCatch) parent;
if (sendToOutput(ctCatch.getParameter(), outputConsumer)) {
return;
}
} else if (parent instanceof CtExecutable) {
CtExecutable<?> exec = (CtExecutable<?>) parent;
for (CtParameter<?> param : exec.getParameters()) {
if (sendToOutput(param, outputConsumer)) {
return;
}
}
}
}
if (parent instanceof CtModifiable) {
isInStaticScope = isInStaticScope || ((CtModifiable) parent).hasModifier(ModifierKind.STATIC);
}
scopeElement = parent;
}
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class CtPackageImpl method setPackages.
@Override
public <T extends CtPackage> T setPackages(Set<CtPackage> packs) {
if (packs == null || packs.isEmpty()) {
this.packs = CtElementImpl.emptySet();
return (T) this;
}
getFactory().getEnvironment().getModelChangeListener().onSetDeleteAll(this, SUB_PACKAGE, this.packs, new HashSet<>(this.packs));
this.packs.clear();
for (CtPackage p : packs) {
addPackage(p);
}
return (T) this;
}
use of spoon.reflect.declaration.CtPackage in project spoon by INRIA.
the class AnnotationTest method testAnnotatedElementTypes.
@Test
public void testAnnotatedElementTypes() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/");
launcher.buildModel();
Factory factory = launcher.getFactory();
// load package of the test classes
CtPackage pkg = factory.Package().get("spoon.test.annotation.testclasses");
// check annotated element type of the package annotation
List<CtAnnotation<?>> annotations = pkg.getAnnotations();
assertEquals(2, annotations.size());
assertTrue(annotations.get(0).getAnnotatedElement().equals(pkg));
assertEquals(CtAnnotatedElementType.PACKAGE, annotations.get(0).getAnnotatedElementType());
// load class Main from package and check annotated element type of the class annotation
CtClass<?> clazz = pkg.getType("Main");
assertEquals(Main.class, clazz.getActualClass());
annotations = clazz.getAnnotations();
assertEquals(1, annotations.size());
assertTrue(annotations.get(0).getAnnotatedElement().equals(clazz));
assertEquals(CtAnnotatedElementType.TYPE, clazz.getAnnotations().get(0).getAnnotatedElementType());
// load method toString() from class and check annotated element type of the annotation
List<CtMethod<?>> methods = clazz.getMethodsByName("toString");
assertEquals(1, methods.size());
CtMethod<?> method = methods.get(0);
assertEquals("toString", method.getSimpleName());
annotations = method.getAnnotations();
assertEquals(1, annotations.size());
assertTrue(annotations.get(0).getAnnotatedElement().equals(method));
assertEquals(CtAnnotatedElementType.METHOD, annotations.get(0).getAnnotatedElementType());
// load parameter of method m(int) and check annotated element type of the parameter annotation
methods = clazz.getMethodsByName("m");
assertEquals(1, methods.size());
method = methods.get(0);
assertEquals("m", method.getSimpleName());
List<CtParameter<?>> parameters = method.getParameters();
assertEquals(1, parameters.size());
CtParameter<?> parameter = parameters.get(0);
annotations = parameter.getAnnotations();
assertEquals(1, annotations.size());
assertTrue(annotations.get(0).getAnnotatedElement().equals(parameter));
assertEquals(CtAnnotatedElementType.PARAMETER, annotations.get(0).getAnnotatedElementType());
// load constructor of the clazz and check annotated element type of the constructor annotation
Set<? extends CtConstructor<?>> constructors = clazz.getConstructors();
assertEquals(1, constructors.size());
CtConstructor<?> constructor = constructors.iterator().next();
annotations = constructor.getAnnotations();
assertEquals(1, annotations.size());
assertTrue(annotations.get(0).getAnnotatedElement().equals(constructor));
assertEquals(CtAnnotatedElementType.CONSTRUCTOR, annotations.get(0).getAnnotatedElementType());
// load value ia of the m1() method annotation, which is also an annotation
// and check the annotated element type of the inner annotation.
methods = clazz.getMethodsByName("m1");
assertEquals(1, methods.size());
method = methods.get(0);
annotations = method.getAnnotations();
assertEquals(1, annotations.size());
CtAnnotation<?> annotation = annotations.get(0);
assertTrue(annotations.get(0).getAnnotatedElement().equals(method));
assertEquals(CtAnnotatedElementType.METHOD, annotations.get(0).getAnnotatedElementType());
Object element = annotation.getValues().get("ia");
assertNotNull(element);
assertTrue(element instanceof CtAnnotation);
assertTrue(((CtAnnotation<?>) element).getAnnotatedElement().equals(annotation));
assertEquals(CtAnnotatedElementType.ANNOTATION_TYPE, ((CtAnnotation<?>) element).getAnnotatedElementType());
// load enum AnnotParamTypeEnum and check the annotated element type of the annotation of the enum and of the fields
CtEnum<?> enumeration = pkg.getType("AnnotParamTypeEnum");
assertEquals(AnnotParamTypeEnum.class, enumeration.getActualClass());
annotations = enumeration.getAnnotations();
assertEquals(1, annotations.size());
assertTrue(annotations.get(0).getAnnotatedElement().equals(enumeration));
assertEquals(CtAnnotatedElementType.TYPE, annotations.get(0).getAnnotatedElementType());
List<CtEnumValue<?>> fields = enumeration.getEnumValues();
assertEquals(3, fields.size());
annotations = fields.get(0).getAnnotations();
assertEquals(1, annotations.size());
assertTrue(annotations.get(0).getAnnotatedElement().equals(fields.get(0)));
assertEquals(CtAnnotatedElementType.FIELD, annotations.get(0).getAnnotatedElementType());
// load interface type TestInterface and check the annotated element type of the annotation
CtInterface<?> ctInterface = pkg.getType("TestInterface");
assertEquals(TestInterface.class, ctInterface.getActualClass());
annotations = ctInterface.getAnnotations();
assertEquals(1, annotations.size());
assertTrue(annotations.get(0).getAnnotatedElement().equals(ctInterface));
assertEquals(CtAnnotatedElementType.TYPE, annotations.get(0).getAnnotatedElementType());
// load annotation type Bound and check the annotated element type of the annotations
CtAnnotationType<?> annotationType = pkg.getType("Bound");
assertEquals(Bound.class, annotationType.getActualClass());
assertNull(annotationType.getSuperclass());
assertEquals(1, annotationType.getMethods().size());
assertEquals(0, annotationType.getSuperInterfaces().size());
annotations = annotationType.getAnnotations();
assertEquals(1, annotations.size());
assertTrue(annotations.get(0).getAnnotatedElement().equals(annotationType));
assertEquals(CtAnnotatedElementType.ANNOTATION_TYPE, annotations.get(0).getAnnotatedElementType());
}
Aggregations