Search in sources :

Example 1 with CtCatchVariable

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

the class ContextBuilder method enter.

@SuppressWarnings("unchecked")
void enter(CtElement e, ASTNode node) {
    stack.push(new ASTPair(e, node));
    if (!(e instanceof CtPackage) || (compilationUnitSpoon.getFile() != null && compilationUnitSpoon.getFile().getName().equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION))) {
        if (compilationunitdeclaration != null && !e.isImplicit()) {
            e.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPositionCtElement(e, node));
        }
    }
    ASTPair pair = stack.peek();
    CtElement current = pair.element;
    if (current instanceof CtExpression) {
        while (!casts.isEmpty()) {
            ((CtExpression<?>) current).addTypeCast(casts.remove(0));
        }
    }
    if (current instanceof CtStatement && !this.label.isEmpty()) {
        ((CtStatement) current).setLabel(this.label.pop());
    }
    try {
        if (e instanceof CtTypedElement && !(e instanceof CtConstructorCall) && !(e instanceof CtCatchVariable) && node instanceof Expression) {
            if (((CtTypedElement<?>) e).getType() == null) {
                ((CtTypedElement<Object>) e).setType(this.jdtTreeBuilder.getReferencesBuilder().getTypeReference(((Expression) node).resolvedType));
            }
        }
    } catch (UnsupportedOperationException ignore) {
    // For some element, we throw an UnsupportedOperationException when we call setType().
    }
}
Also used : CtExpression(spoon.reflect.code.CtExpression) CtStatement(spoon.reflect.code.CtStatement) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) CtExpression(spoon.reflect.code.CtExpression) CtElement(spoon.reflect.declaration.CtElement) CtPackage(spoon.reflect.declaration.CtPackage) CtTypedElement(spoon.reflect.declaration.CtTypedElement) CtCatchVariable(spoon.reflect.code.CtCatchVariable)

Example 2 with CtCatchVariable

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

the class ExceptionTest method testUnionCatchExceptionInsideLambdaInNoClasspath.

@Test
public void testUnionCatchExceptionInsideLambdaInNoClasspath() {
    // contract: the model should be built when defining a union catch inside a lambda which is not known (noclasspath)
    // and the catch variable types should be the same than outside a lambda
    Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/UnionCatch.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    List<CtCatch> catches = launcher.getFactory().getModel().getElements(new TypeFilter<>(CtCatch.class));
    assertEquals(2, catches.size());
    // inside a lambda
    CtCatchVariable variable1 = catches.get(0).getParameter();
    // outside the lambda
    CtCatchVariable variable2 = catches.get(1).getParameter();
    assertEquals(variable1.getMultiTypes(), variable2.getMultiTypes());
// for now the type of CtCatchVariable is not the same
// this should be fix in the future (see: https://github.com/INRIA/spoon/issues/1420)
// assertEquals(variable2, variable1);
}
Also used : Launcher(spoon.Launcher) CtCatch(spoon.reflect.code.CtCatch) CtCatchVariable(spoon.reflect.code.CtCatchVariable) Test(org.junit.Test)

Example 3 with CtCatchVariable

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

the class CtCatchVariableReferenceImpl method getDeclaration.

@Override
public CtCatchVariable<T> getDeclaration() {
    CtElement element = this;
    String name = getSimpleName();
    CtCatchVariable var;
    try {
        do {
            CtCatch catchBlock = element.getParent(CtCatch.class);
            if (catchBlock == null) {
                return null;
            }
            var = catchBlock.getParameter();
            element = catchBlock;
        } while (!name.equals(var.getSimpleName()));
    } catch (ParentNotInitializedException e) {
        return null;
    }
    return var;
}
Also used : ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) CtElement(spoon.reflect.declaration.CtElement) CtCatch(spoon.reflect.code.CtCatch) CtCatchVariable(spoon.reflect.code.CtCatchVariable)

Example 4 with CtCatchVariable

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

the class CtRenameLocalVariableRefactoring method detectNameConflicts.

@Override
protected void detectNameConflicts() {
    /*
		 * There can be these conflicts
		 * 1) target variable would shadow before declared variable (parameter, localVariable, catchVariable)
		 * --------------------------------------------------------------------------------------------------
		 */
    PotentialVariableDeclarationFunction potentialDeclarationFnc = new PotentialVariableDeclarationFunction(newName);
    CtVariable<?> var = getTarget().map(potentialDeclarationFnc).first();
    if (var != null) {
        if (var instanceof CtField) {
        /*
				 * we have found a field of same name.
				 * It is not problem, because variables can hide field declaration.
				 * Do nothing - OK
				 */
        } else if (potentialDeclarationFnc.isTypeOnTheWay()) {
            /*
				 * There is a local class declaration between future variable reference and variable declaration `var`.
				 * The found variable declaration `var` can be hidden by target variable with newName
				 * as long as there is no reference to `var` in visibility scope of the target variable.
				 * So search for such `var` reference now
				 */
            CtVariableReference<?> shadowedVar = target.map(new SiblingsFunction().includingSelf(true).mode(Mode.NEXT)).map(new VariableReferenceFunction(var)).first();
            if (shadowedVar != null) {
                // found variable reference, which would be shadowed by variable after rename.
                createNameConflictIssue(var, shadowedVar);
            } else {
            /*
					 * there is no local variable reference, which would be shadowed by variable after rename.
					 * OK
					 */
            }
        } else {
            /*
				 * the found variable is in conflict with target variable with newName
				 */
            createNameConflictIssue(var);
        }
    }
    /*
		 * 2) target variable is shadowed by later declared variable
		 * ---------------------------------------------------------
		 */
    final QueryDriver queryDriver = new QueryDriver();
    getTarget().map(new LocalVariableScopeFunction(queryDriver)).select(new Filter<CtElement>() {

        /**
         * return true for all CtVariables, which are in conflict
         */
        @Override
        public boolean matches(CtElement element) {
            if (element instanceof CtType<?>) {
                CtType<?> localClass = (CtType<?>) element;
                // TODO use faster hasField, implemented using map(new AllFieldsFunction()).select(new NameFilter(newName)).first()!=null
                Collection<CtFieldReference<?>> fields = localClass.getAllFields();
                for (CtFieldReference<?> fieldRef : fields) {
                    if (newName.equals(fieldRef.getSimpleName())) {
                        /*
							 * we have found a local class field, which will shadow input local variable if it's reference is in visibility scope of that field.
							 * Search for target variable reference in visibility scope of this field.
							 * If found than we cannot rename target variable to newName, because that reference would be shadowed
							 */
                        queryDriver.ignoreChildrenOf(element);
                        CtLocalVariableReference<?> shadowedVar = element.map(new LocalVariableReferenceFunction(target)).first();
                        if (shadowedVar != null) {
                            createNameConflictIssue(fieldRef.getFieldDeclaration(), shadowedVar);
                            return true;
                        }
                        return false;
                    }
                }
                return false;
            }
            if (element instanceof CtVariable<?>) {
                CtVariable<?> variable = (CtVariable<?>) element;
                if (newName.equals(variable.getSimpleName()) == false) {
                    // the variable with different name. Ignore it
                    return false;
                }
                // we have found a variable with new name
                if (variable instanceof CtField) {
                    throw new SpoonException("This should not happen. The children of local class which contains a field with new name should be skipped!");
                }
                if (variable instanceof CtCatchVariable || variable instanceof CtLocalVariable || variable instanceof CtParameter) {
                    /*
						 * we have found a catch variable or local variable or parameter with new name.
						 */
                    if (queryDriver.isInContextOfLocalClass()) {
                        /*
							 * We are in context of local class.
							 * This variable would shadow input local variable after rename
							 * so we cannot rename if there exist a local variable reference in variable visibility scope.
							 */
                        queryDriver.ignoreChildrenOf(variable.getParent());
                        CtQueryable searchScope;
                        if (variable instanceof CtLocalVariable) {
                            searchScope = variable.map(new SiblingsFunction().includingSelf(true).mode(Mode.NEXT));
                        } else {
                            searchScope = variable.getParent();
                        }
                        CtLocalVariableReference<?> shadowedVar = searchScope.map(new LocalVariableReferenceFunction(target)).first();
                        if (shadowedVar != null) {
                            // found local variable reference, which would be shadowed by variable after rename.
                            createNameConflictIssue(variable, shadowedVar);
                            return true;
                        }
                        // there is no local variable reference, which would be shadowed by variable after rename.
                        return false;
                    } else {
                        /*
							 * We are not in context of local class.
							 * So this variable is in conflict. Return it
							 */
                        createNameConflictIssue(variable);
                        return true;
                    }
                } else {
                    // Any new variable type???
                    throw new SpoonException("Unexpected variable " + variable.getClass().getName());
                }
            }
            return false;
        }
    }).first();
}
Also used : CtVariableReference(spoon.reflect.reference.CtVariableReference) SiblingsFunction(spoon.reflect.visitor.filter.SiblingsFunction) SpoonException(spoon.SpoonException) CtElement(spoon.reflect.declaration.CtElement) VariableReferenceFunction(spoon.reflect.visitor.filter.VariableReferenceFunction) LocalVariableReferenceFunction(spoon.reflect.visitor.filter.LocalVariableReferenceFunction) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtParameter(spoon.reflect.declaration.CtParameter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtType(spoon.reflect.declaration.CtType) Filter(spoon.reflect.visitor.Filter) CtField(spoon.reflect.declaration.CtField) CtQueryable(spoon.reflect.visitor.chain.CtQueryable) CtVariable(spoon.reflect.declaration.CtVariable) PotentialVariableDeclarationFunction(spoon.reflect.visitor.filter.PotentialVariableDeclarationFunction) CtCatchVariable(spoon.reflect.code.CtCatchVariable) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) LocalVariableReferenceFunction(spoon.reflect.visitor.filter.LocalVariableReferenceFunction)

Example 5 with CtCatchVariable

use of spoon.reflect.code.CtCatchVariable 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);
}
Also used : CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableScopeFunction(spoon.reflect.visitor.filter.VariableScopeFunction) ParameterScopeFunction(spoon.reflect.visitor.filter.ParameterScopeFunction) CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) CtField(spoon.reflect.declaration.CtField) CtElement(spoon.reflect.declaration.CtElement) List(java.util.List) CtParameter(spoon.reflect.declaration.CtParameter) CtCatchVariable(spoon.reflect.code.CtCatchVariable) CtLocalVariable(spoon.reflect.code.CtLocalVariable) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableReferencesModelTest(spoon.test.query_function.testclasses.VariableReferencesModelTest) Test(org.junit.Test)

Aggregations

CtCatchVariable (spoon.reflect.code.CtCatchVariable)7 CtElement (spoon.reflect.declaration.CtElement)4 CtCatch (spoon.reflect.code.CtCatch)3 Test (org.junit.Test)2 CtLocalVariable (spoon.reflect.code.CtLocalVariable)2 CtField (spoon.reflect.declaration.CtField)2 CtParameter (spoon.reflect.declaration.CtParameter)2 LocalVariableScopeFunction (spoon.reflect.visitor.filter.LocalVariableScopeFunction)2 List (java.util.List)1 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)1 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)1 UnionTypeReference (org.eclipse.jdt.internal.compiler.ast.UnionTypeReference)1 Launcher (spoon.Launcher)1 SpoonException (spoon.SpoonException)1 CtConstructorCall (spoon.reflect.code.CtConstructorCall)1 CtExpression (spoon.reflect.code.CtExpression)1 CtStatement (spoon.reflect.code.CtStatement)1 CtThrow (spoon.reflect.code.CtThrow)1 CtPackage (spoon.reflect.declaration.CtPackage)1 CtType (spoon.reflect.declaration.CtType)1