/** * Resolve the given source and verify that the arguments in a specific method invocation were * correctly resolved. * * <p>The source is expected to be source for a compilation unit, the first declaration is * expected to be a class, the first member of which is expected to be a method with a block body, * and the first statement in the body is expected to be an expression statement whose expression * is a method invocation. It is the arguments to that method invocation that are tested. The * method invocation can contain errors. * * <p>The arguments were resolved correctly if the number of expressions in the list matches the * length of the array of indices and if, for each index in the array of indices, the parameter to * which the argument expression was resolved is the parameter in the invoked method's list of * parameters at that index. Arguments that should not be resolved to a parameter because of an * error can be denoted by including a negative index in the array of indices. * * @param source the source to be resolved * @param indices the array of indices used to associate arguments with parameters * @throws Exception if the source could not be resolved or if the structure of the source is not * valid */ private void validateArgumentResolution(Source source, int... indices) throws Exception { LibraryElement library = resolve(source); assertNotNull(library); ClassElement classElement = library.getDefiningCompilationUnit().getTypes()[0]; ParameterElement[] parameters = classElement.getMethods()[1].getParameters(); CompilationUnit unit = resolveCompilationUnit(source, library); assertNotNull(unit); ClassDeclaration classDeclaration = (ClassDeclaration) unit.getDeclarations().get(0); MethodDeclaration methodDeclaration = ((MethodDeclaration) classDeclaration.getMembers().get(0)); Block block = ((BlockFunctionBody) methodDeclaration.getBody()).getBlock(); ExpressionStatement statement = (ExpressionStatement) block.getStatements().get(0); MethodInvocation invocation = (MethodInvocation) statement.getExpression(); NodeList<Expression> arguments = invocation.getArgumentList().getArguments(); int argumentCount = arguments.size(); assertEquals(indices.length, argumentCount); for (int i = 0; i < argumentCount; i++) { Expression argument = arguments.get(i); ParameterElement element = argument.getStaticParameterElement(); int index = indices[i]; if (index < 0) { assertNull(element); } else { assertSame(parameters[index], element); } } }
public void test_argumentResolution_setter_static_propertyAccess() throws Exception { Source source = addSource( createSource( // "main() {", " A a = new A();", " a.b.sss = 0;", "}", "class A {", " B b = new B();", "}", "class B {", " set sss(x) {}", "}")); LibraryElement library = resolve(source); CompilationUnitElement unit = library.getDefiningCompilationUnit(); // find "a.b.sss = 0" AssignmentExpression assignment; { FunctionElement mainElement = unit.getFunctions()[0]; FunctionBody mainBody = mainElement.getNode().getFunctionExpression().getBody(); Statement statement = ((BlockFunctionBody) mainBody).getBlock().getStatements().get(1); ExpressionStatement expressionStatement = (ExpressionStatement) statement; assignment = (AssignmentExpression) expressionStatement.getExpression(); } // get parameter Expression rhs = assignment.getRightHandSide(); ParameterElement parameter = rhs.getStaticParameterElement(); assertNotNull(parameter); assertEquals("x", parameter.getDisplayName()); // validate ClassElement classB = unit.getTypes()[1]; PropertyAccessorElement setter = classB.getAccessors()[0]; assertSame(parameter, setter.getParameters()[0]); }
@Override public Void visitReturnStatement(ReturnStatement node) { Expression expression = node.getExpression(); if (expression == null) { writer.print("return;"); } else { writer.print("return "); expression.accept(this); writer.print(";"); } return null; }
@Override public Void visitDefaultFormalParameter(DefaultFormalParameter node) { ElementHolder holder = new ElementHolder(); SimpleIdentifier parameterName = node.getParameter().getIdentifier(); ParameterElementImpl parameter; if (node.getParameter() instanceof FieldFormalParameter) { parameter = new DefaultFieldFormalParameterElementImpl(parameterName); } else { parameter = new DefaultParameterElementImpl(parameterName); } parameter.setConst(node.isConst()); parameter.setFinal(node.isFinal()); parameter.setParameterKind(node.getKind()); // set initializer, default value range Expression defaultValue = node.getDefaultValue(); if (defaultValue != null) { visit(holder, defaultValue); FunctionElementImpl initializer = new FunctionElementImpl(defaultValue.getBeginToken().getOffset()); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getLocalVariables()); initializer.setParameters(holder.getParameters()); initializer.setSynthetic(true); parameter.setInitializer(initializer); parameter.setDefaultValueRange(defaultValue.getOffset(), defaultValue.getLength()); } // visible range setParameterVisibleRange(node, parameter); currentHolder.addParameter(parameter); parameterName.setStaticElement(parameter); node.getParameter().accept(this); holder.validate(); return null; }