Search in sources :

Example 11 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class AccessibleVariablesFinder method getVariable.

private List<CtVariable> getVariable(final CtElement parent) {
    final List<CtVariable> variables = new ArrayList<>();
    if (parent == null) {
        return variables;
    }
    class VariableScanner extends CtInheritanceScanner {

        @Override
        public void visitCtStatementList(CtStatementList e) {
            for (int i = 0; i < e.getStatements().size(); i++) {
                CtStatement ctStatement = e.getStatements().get(i);
                if (ctStatement.getPosition() == null) {
                }
                if (ctStatement.getPosition() != null && ctStatement.getPosition().getSourceStart() > expression.getPosition().getSourceEnd()) {
                    break;
                }
                if (ctStatement instanceof CtVariable) {
                    variables.add((CtVariable) ctStatement);
                }
            }
            super.visitCtStatementList(e);
        }

        @Override
        public <T> void scanCtType(CtType<T> type) {
            List<CtField<?>> fields = type.getFields();
            for (int i = 0; i < fields.size(); i++) {
                CtField<?> ctField = fields.get(i);
                if (ctField.hasModifier(ModifierKind.PUBLIC) || ctField.hasModifier(ModifierKind.PROTECTED)) {
                    variables.add(ctField);
                } else if (ctField.hasModifier(ModifierKind.PRIVATE)) {
                    if (expression.hasParent(type)) {
                        variables.add(ctField);
                    }
                } else if (expression.getParent(CtPackage.class).equals(type.getParent(CtPackage.class))) {
                    // default visibility
                    variables.add(ctField);
                }
            }
            CtTypeReference<?> superclass = type.getSuperclass();
            if (superclass != null) {
                variables.addAll(getVariable(superclass.getTypeDeclaration()));
            }
            Set<CtTypeReference<?>> superInterfaces = type.getSuperInterfaces();
            for (Iterator<CtTypeReference<?>> iterator = superInterfaces.iterator(); iterator.hasNext(); ) {
                CtTypeReference<?> typeReference = iterator.next();
                variables.addAll(getVariable(typeReference.getTypeDeclaration()));
            }
            super.scanCtType(type);
        }

        @Override
        public void visitCtTryWithResource(CtTryWithResource e) {
            variables.addAll(e.getResources());
            super.visitCtTryWithResource(e);
        }

        @Override
        public void scanCtExecutable(CtExecutable e) {
            variables.addAll(e.getParameters());
            super.scanCtExecutable(e);
        }

        @Override
        public void visitCtFor(CtFor e) {
            for (CtStatement ctStatement : e.getForInit()) {
                this.scan(ctStatement);
            }
            super.visitCtFor(e);
        }

        @Override
        public void visitCtForEach(CtForEach e) {
            variables.add(e.getVariable());
            super.visitCtForEach(e);
        }

        @Override
        public void visitCtMethod(CtMethod e) {
            this.scan(e.getBody());
            super.visitCtMethod(e);
        }

        @Override
        public void visitCtLocalVariable(CtLocalVariable e) {
            variables.add(e);
            super.visitCtLocalVariable(e);
        }

        @Override
        public void visitCtCatch(CtCatch e) {
            variables.add(e.getParameter());
            super.visitCtCatch(e);
        }
    }
    new VariableScanner().scan(parent);
    return variables;
}
Also used : ArrayList(java.util.ArrayList) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtForEach(spoon.reflect.code.CtForEach) CtType(spoon.reflect.declaration.CtType) CtStatement(spoon.reflect.code.CtStatement) CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtVariable(spoon.reflect.declaration.CtVariable) CtStatementList(spoon.reflect.code.CtStatementList) CtPackage(spoon.reflect.declaration.CtPackage) CtTryWithResource(spoon.reflect.code.CtTryWithResource) CtCatch(spoon.reflect.code.CtCatch) CtFor(spoon.reflect.code.CtFor) CtMethod(spoon.reflect.declaration.CtMethod)

Example 12 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class CtTypeParameterTest method checkParameterErasureOfExecutable.

private void checkParameterErasureOfExecutable(CtParameter<?> param) {
    CtExecutable<?> exec = param.getParent();
    CtTypeReference<?> typeErasure = param.getType().getTypeErasure();
    int paramIdx = exec.getParameters().indexOf(param);
    Class declClass = exec.getParent(CtType.class).getActualClass();
    Executable declExec;
    if (exec instanceof CtConstructor) {
        declExec = declClass.getDeclaredConstructors()[0];
    } else {
        declExec = getMethodByName(declClass, exec.getSimpleName());
    }
    Class<?> paramType = declExec.getParameterTypes()[paramIdx];
    assertEquals(0, typeErasure.getActualTypeArguments().size());
    // contract the type erasure of the method parameter given with Java reflection is the same as the one computed by spoon
    assertEquals("TypeErasure of executable " + exec.getSignature() + " parameter " + param.getSimpleName(), paramType.getName(), typeErasure.getQualifiedName());
}
Also used : CtType(spoon.reflect.declaration.CtType) CtClass(spoon.reflect.declaration.CtClass) Executable(java.lang.reflect.Executable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtConstructor(spoon.reflect.declaration.CtConstructor)

Example 13 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class CtTypeParameterTest method getTypeParamIdentification.

private String getTypeParamIdentification(CtTypeParameter typeParam) {
    String result = "<" + typeParam.getSimpleName() + ">";
    CtFormalTypeDeclarer l_decl = typeParam.getParent(CtFormalTypeDeclarer.class);
    if (l_decl instanceof CtType) {
        return ((CtType) l_decl).getQualifiedName() + result;
    }
    if (l_decl instanceof CtExecutable) {
        CtExecutable exec = (CtExecutable) l_decl;
        if (exec instanceof CtMethod) {
            result = exec.getSignature() + result;
        }
        return exec.getParent(CtType.class).getQualifiedName() + "#" + result;
    }
    throw new AssertionError();
}
Also used : CtType(spoon.reflect.declaration.CtType) CtFormalTypeDeclarer(spoon.reflect.declaration.CtFormalTypeDeclarer) CtExecutable(spoon.reflect.declaration.CtExecutable) CtMethod(spoon.reflect.declaration.CtMethod)

Example 14 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class CtTypeParameterTest method checkTypeParamErasureOfExecutable.

private void checkTypeParamErasureOfExecutable(CtTypeParameter typeParam) throws NoSuchFieldException, SecurityException {
    CtExecutable<?> exec = (CtExecutable<?>) typeParam.getParent();
    CtParameter<?> param = exec.filterChildren(new NamedElementFilter<>(CtParameter.class, "param" + typeParam.getSimpleName())).first();
    assertNotNull("Missing param" + typeParam.getSimpleName() + " in " + exec.getSignature(), param);
    int paramIdx = exec.getParameters().indexOf(param);
    Class declClass = exec.getParent(CtType.class).getActualClass();
    Executable declExec;
    if (exec instanceof CtConstructor) {
        declExec = declClass.getDeclaredConstructors()[0];
    } else {
        declExec = getMethodByName(declClass, exec.getSimpleName());
    }
    Class<?> paramType = declExec.getParameterTypes()[paramIdx];
    // contract the type erasure given with Java reflection is the same as the one computed by spoon
    assertEquals("TypeErasure of executable param " + getTypeParamIdentification(typeParam), paramType.getName(), typeParam.getTypeErasure().toString());
}
Also used : CtType(spoon.reflect.declaration.CtType) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) CtClass(spoon.reflect.declaration.CtClass) Executable(java.lang.reflect.Executable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtExecutable(spoon.reflect.declaration.CtExecutable) CtConstructor(spoon.reflect.declaration.CtConstructor)

Example 15 with CtExecutable

use of spoon.reflect.declaration.CtExecutable in project spoon by INRIA.

the class VariableAccessTest method testDeclaringTypeOfALambdaReferencedByParameterReference.

@Test
public void testDeclaringTypeOfALambdaReferencedByParameterReference() {
    final spoon.Launcher launcher = new spoon.Launcher();
    launcher.addInputResource("src/test/resources/noclasspath/Foo3.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setComplianceLevel(8);
    launcher.buildModel();
    launcher.getModel().getElements(new TypeFilter<CtExecutable<?>>(CtExecutable.class) {

        @Override
        public boolean matches(CtExecutable<?> exec) {
            final List<CtParameterReference<?>> guiParams = exec.getParameters().stream().map(CtParameter::getReference).collect(Collectors.toList());
            if (guiParams.size() != 1) {
                return false;
            }
            final CtParameterReference<?> param = guiParams.get(0);
            exec.getBody().getElements(new TypeFilter<CtParameterReference<?>>(CtParameterReference.class) {

                @Override
                public boolean matches(CtParameterReference<?> p) {
                    assertEquals(p.getSimpleName(), param.getSimpleName());
                    return super.matches(p);
                }
            });
            return super.matches(exec);
        }
    });
}
Also used : Launcher(spoon.Launcher) CtParameterReference(spoon.reflect.reference.CtParameterReference) Launcher(spoon.Launcher) List(java.util.List) CtParameter(spoon.reflect.declaration.CtParameter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtExecutable(spoon.reflect.declaration.CtExecutable) Test(org.junit.Test)

Aggregations

CtExecutable (spoon.reflect.declaration.CtExecutable)26 Test (org.junit.Test)10 CtType (spoon.reflect.declaration.CtType)9 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)7 Launcher (spoon.Launcher)6 CtElement (spoon.reflect.declaration.CtElement)6 Factory (spoon.reflect.factory.Factory)6 File (java.io.File)5 CtConstructor (spoon.reflect.declaration.CtConstructor)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SpoonException (spoon.SpoonException)4 CtField (spoon.reflect.declaration.CtField)4 CtMethod (spoon.reflect.declaration.CtMethod)4 SpoonModelBuilder (spoon.SpoonModelBuilder)3 CtParameterRemoveRefactoring (spoon.refactoring.CtParameterRemoveRefactoring)3 CtInvocation (spoon.reflect.code.CtInvocation)3 CtLambda (spoon.reflect.code.CtLambda)3 CtClass (spoon.reflect.declaration.CtClass)3 CtParameter (spoon.reflect.declaration.CtParameter)3