Search in sources :

Example 6 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class TargetedExpressionTest method testClassDeclaredInALambda.

@Test
public void testClassDeclaredInALambda() throws Exception {
    // contract: A class can be declared in a lambda expression where we use final fields.
    final CtType<Tapas> type = buildClass(Tapas.class);
    final List<CtFieldAccess> elements = new SortedList(new CtLineElementComparator());
    elements.addAll(type.getElements(new TypeFilter<>(CtFieldAccess.class)));
    assertEquals(3, elements.size());
    final CtTypeReference<Object> firstExpected = type.getFactory().Type().createReference("spoon.test.targeted.testclasses.Tapas$1$InnerSubscriber");
    CtThisAccess<Object> expectedThisAccess = type.getFactory().Code().createThisAccess(firstExpected);
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(firstExpected).target(expectedThisAccess).type(CtFieldWrite.class).result("this.index"), elements.get(0));
    final CtTypeReference<Object> secondExpectedInner = type.getFactory().Type().createReference("spoon.test.targeted.testclasses.Tapas$3InnerSubscriber");
    expectedThisAccess = type.getFactory().Code().createThisAccess(secondExpectedInner);
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(secondExpectedInner).target(expectedThisAccess).type(CtFieldWrite.class).result("this.index").isLocal(), elements.get(1));
    final CtTypeReference<Object> thirdExpectedInner = type.getFactory().Type().createReference("spoon.test.targeted.testclasses.Tapas$4InnerSubscriber");
    expectedThisAccess = type.getFactory().Code().createThisAccess(thirdExpectedInner);
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(thirdExpectedInner).target(expectedThisAccess).type(CtFieldWrite.class).result("this.index").isLocal(), elements.get(2));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtFieldWrite(spoon.reflect.code.CtFieldWrite) Tapas(spoon.test.targeted.testclasses.Tapas) SortedList(spoon.support.util.SortedList) CtLineElementComparator(spoon.support.comparator.CtLineElementComparator) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 7 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class TargetedExpressionTest method testInitializeFieldAccessInNoclasspathMode.

@Test
public void testInitializeFieldAccessInNoclasspathMode() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/spoon/test/noclasspath/targeted/Foo.java");
    launcher.setSourceOutputDirectory("./target/noclasspath");
    launcher.run();
    final CtTypeReference<Object> expectedFoo = launcher.getFactory().Class().createReference("Foo");
    final CtThisAccess<Object> expectedThisAccess = launcher.getFactory().Code().createThisAccess(expectedFoo);
    final List<CtFieldAccess<?>> elements = launcher.getFactory().Class().get("Foo").getConstructor().getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(1, elements.size());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFoo).target(expectedThisAccess).result("this.bar"), elements.get(0));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 8 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class TargetedExpressionTest method testTargetsOfFieldAccessInInnerClass.

@Test
public void testTargetsOfFieldAccessInInnerClass() throws Exception {
    final Factory factory = build(Foo.class, Bar.class, SuperClass.class);
    final CtClass<Foo> type = factory.Class().get(Foo.class);
    final CtTypeReference<Foo> expectedType = type.getReference();
    final CtTypeReference<SuperClass> expectedSuperClassType = factory.Class().<SuperClass>get(SuperClass.class).getReference();
    final CtType<?> innerClass = type.getNestedType("InnerClass");
    final CtTypeReference<?> expectedInnerClass = innerClass.getReference();
    final CtType<?> nestedTypeScanner = type.getNestedType("1NestedTypeScanner");
    final CtTypeReference<?> expectedNested = nestedTypeScanner.getReference();
    final CtTypeAccess<Foo> fooTypeAccess = factory.Code().createTypeAccess(expectedType);
    final CtThisAccess<Foo> expectedThisAccess = factory.Code().createThisAccess(expectedType);
    final CtThisAccess<?> expectedInnerClassAccess = factory.Code().createThisAccess(expectedInnerClass);
    final CtThisAccess expectedNestedAccess = factory.Code().createThisAccess(expectedNested);
    final CtMethod<?> innerInvMethod = innerClass.getMethodsByName("innerField").get(0);
    final List<CtFieldAccess<?>> elements = innerInvMethod.getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(6, elements.size());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("this.i"), elements.get(0));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedInnerClass).target(expectedInnerClassAccess).result("i"), elements.get(1));
    assertEquals(true, elements.get(1).getTarget().isImplicit());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(expectedThisAccess).result("this.i"), elements.get(2));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedType).target(fooTypeAccess).result("spoon.test.targeted.testclasses.Foo.k"), elements.get(3));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedThisAccess).result("this.o"), elements.get(4));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedSuperClassType).target(expectedThisAccess).result("o"), elements.get(5));
    final List<CtFieldAccess<?>> newElements = nestedTypeScanner.getMethodsByName("checkField").get(0).getElements(new TypeFilter<>(CtFieldAccess.class));
    assertEquals(2, newElements.size());
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedNested).target(expectedNestedAccess).result("this.type").isLocal(), newElements.get(0));
    assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedNested).target(expectedNestedAccess).result("type").isLocal(), newElements.get(1));
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Foo(spoon.test.targeted.testclasses.Foo) SuperClass(spoon.test.targeted.testclasses.SuperClass) Factory(spoon.reflect.factory.Factory) CtThisAccess(spoon.reflect.code.CtThisAccess) Test(org.junit.Test)

Example 9 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class ImportScannerImpl method addClassImport.

/**
 * Adds a type to the classImports.
 */
protected boolean addClassImport(CtTypeReference<?> ref) {
    this.exploredReferences.add(ref);
    if (ref == null) {
        return false;
    }
    if (targetType != null && targetType.getSimpleName().equals(ref.getSimpleName()) && !targetType.equals(ref)) {
        return false;
    }
    if (classImports.containsKey(ref.getSimpleName())) {
        return isImportedInClassImports(ref);
    }
    // don't import unnamed package elements
    if (ref.getPackage() == null || ref.getPackage().isUnnamedPackage()) {
        return false;
    }
    if (targetType != null && targetType.canAccess(ref) == false) {
        // ref type is not visible in targetType we must not add import for it, java compiler would fail on that.
        return false;
    }
    if (this.isThereAnotherClassWithSameNameInAnotherPackage(ref)) {
        return false;
    }
    if (targetType != null) {
        try {
            CtElement parent = ref.getParent();
            if (parent != null) {
                parent = parent.getParent();
                if (parent != null) {
                    if ((parent instanceof CtFieldAccess) || (parent instanceof CtExecutable) || (parent instanceof CtInvocation)) {
                        CtTypeReference declaringType;
                        CtReference reference;
                        CtPackageReference pack = targetType.getPackage();
                        if (parent instanceof CtFieldAccess) {
                            CtFieldAccess field = (CtFieldAccess) parent;
                            CtFieldReference localReference = field.getVariable();
                            declaringType = localReference.getDeclaringType();
                            reference = localReference;
                        } else if (parent instanceof CtExecutable) {
                            CtExecutable exec = (CtExecutable) parent;
                            CtExecutableReference localReference = exec.getReference();
                            declaringType = localReference.getDeclaringType();
                            reference = localReference;
                        } else if (parent instanceof CtInvocation) {
                            CtInvocation invo = (CtInvocation) parent;
                            CtExecutableReference localReference = invo.getExecutable();
                            declaringType = localReference.getDeclaringType();
                            reference = localReference;
                        } else {
                            declaringType = null;
                            reference = null;
                        }
                        if (reference != null && isImported(reference)) {
                            // if we are in the **same** package we do the import for test with method isImported
                            if (declaringType != null) {
                                if (declaringType.getPackage() != null && !declaringType.getPackage().isUnnamedPackage()) {
                                    // ignore java.lang package
                                    if (!declaringType.getPackage().getSimpleName().equals("java.lang")) {
                                        // ignore type in same package
                                        if (declaringType.getPackage().getSimpleName().equals(pack.getSimpleName())) {
                                            classImports.put(ref.getSimpleName(), ref);
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (ParentNotInitializedException e) {
        }
        CtPackageReference pack = targetType.getPackage();
        if (pack != null && ref.getPackage() != null && !ref.getPackage().isUnnamedPackage()) {
            // ignore java.lang package
            if (!ref.getPackage().getSimpleName().equals("java.lang")) {
                // ignore type in same package
                if (ref.getPackage().getSimpleName().equals(pack.getSimpleName())) {
                    return false;
                }
            }
        }
    }
    classImports.put(ref.getSimpleName(), ref);
    return true;
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtInvocation(spoon.reflect.code.CtInvocation) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtReference(spoon.reflect.reference.CtReference) CtElement(spoon.reflect.declaration.CtElement) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtExecutable(spoon.reflect.declaration.CtExecutable)

Example 10 with CtFieldAccess

use of spoon.reflect.code.CtFieldAccess in project spoon by INRIA.

the class LambdaTest method testFieldAccessInLambdaNoClassPath.

@Test
public void testFieldAccessInLambdaNoClassPath() {
    final Launcher runLaunch = new Launcher();
    runLaunch.getEnvironment().setNoClasspath(true);
    runLaunch.addInputResource("./src/test/resources/noclasspath/lambdas/FieldAccessInLambda.java");
    runLaunch.addInputResource("./src/test/resources/noclasspath/lambdas/imported/SeparateInterfaceWithField.java");
    runLaunch.buildModel();
    final List<CtFieldAccess> fieldAccesses = runLaunch.getModel().getElements(new Filter<CtFieldAccess>() {

        @Override
        public boolean matches(final CtFieldAccess element) {
            final String name = element.getVariable().getSimpleName();
            return name.equals("localField") || name.equals("pathSeparator") || name.equals("fieldInSeparateInterface") || name.equals("fieldInClassBase") || name.equals("fieldInClass") || name.equals("fieldInInterfaceBase") || name.equals("fieldInInterface") || name.equals("iAmToLazyForAnotherFieldName");
        }
    });
    assertEquals(8, fieldAccesses.size());
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

CtFieldAccess (spoon.reflect.code.CtFieldAccess)22 Test (org.junit.Test)17 Factory (spoon.reflect.factory.Factory)11 Launcher (spoon.Launcher)7 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)7 CtInvocation (spoon.reflect.code.CtInvocation)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)4 Foo (spoon.test.targeted.testclasses.Foo)4 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 List (java.util.List)2 CtAssignment (spoon.reflect.code.CtAssignment)2 CtExpression (spoon.reflect.code.CtExpression)2 CtFieldWrite (spoon.reflect.code.CtFieldWrite)2 CtLocalVariable (spoon.reflect.code.CtLocalVariable)2 CtThisAccess (spoon.reflect.code.CtThisAccess)2 CtElement (spoon.reflect.declaration.CtElement)2 CtField (spoon.reflect.declaration.CtField)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtNamedElement (spoon.reflect.declaration.CtNamedElement)2 CtParameter (spoon.reflect.declaration.CtParameter)2