Exemple #1
0
 @Override
 public Void visitFormalParameterList(FormalParameterList node) {
   String groupEnd = null;
   writer.print('(');
   NodeList<FormalParameter> parameters = node.getParameters();
   int size = parameters.size();
   for (int i = 0; i < size; i++) {
     FormalParameter parameter = parameters.get(i);
     if (i > 0) {
       writer.print(", ");
     }
     if (groupEnd == null && parameter instanceof DefaultFormalParameter) {
       if (parameter.getKind() == ParameterKind.NAMED) {
         groupEnd = "}";
         writer.print('{');
       } else {
         groupEnd = "]";
         writer.print('[');
       }
     }
     parameter.accept(this);
   }
   if (groupEnd != null) {
     writer.print(groupEnd);
   }
   writer.print(')');
   return null;
 }
 public void test_metadata_typedef() throws Exception {
   Source source =
       addSource(
           createSource( //
               "const A = null;", "@A typedef F<A>();"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnitElement unitElement = library.getDefiningCompilationUnit();
   assertNotNull(unitElement);
   FunctionTypeAliasElement[] aliases = unitElement.getFunctionTypeAliases();
   assertLength(1, aliases);
   ElementAnnotation[] annotations = aliases[0].getMetadata();
   assertLength(1, annotations);
   assertNoErrors(source);
   verify(source);
   CompilationUnit unit = resolveCompilationUnit(source, library);
   NodeList<CompilationUnitMember> declarations = unit.getDeclarations();
   assertSizeOfList(2, declarations);
   Element expectedElement =
       ((TopLevelVariableDeclaration) declarations.get(0))
           .getVariables()
           .getVariables()
           .get(0)
           .getName()
           .getStaticElement();
   assertInstanceOf(PropertyInducingElement.class, expectedElement);
   expectedElement = ((PropertyInducingElement) expectedElement).getGetter();
   Element actualElement =
       ((FunctionTypeAlias) declarations.get(1)).getMetadata().get(0).getName().getStaticElement();
   assertSame(expectedElement, actualElement);
 }
  /**
   * 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);
      }
    }
  }
Exemple #4
0
 /**
  * Print a list of nodes, separated by the given separator.
  *
  * @param nodes the nodes to be printed
  * @param separator the separator to be printed between adjacent nodes
  */
 private void visitList(NodeList<? extends ASTNode> nodes, String separator) {
   if (nodes != null) {
     int size = nodes.size();
     for (int i = 0; i < size; i++) {
       if (i > 0) {
         writer.print(separator);
       }
       nodes.get(i).accept(this);
     }
   }
 }
Exemple #5
0
 @Override
 public Void visitCompilationUnit(CompilationUnit node) {
   ScriptTag scriptTag = node.getScriptTag();
   NodeList<Directive> directives = node.getDirectives();
   visit(scriptTag);
   String prefix = scriptTag == null ? "" : " ";
   visitList(prefix, directives, " ");
   prefix = scriptTag == null && directives.isEmpty() ? "" : " ";
   visitList(prefix, node.getDeclarations(), " ");
   return null;
 }
  @Override
  public void process(final Context context, final CompilationUnit unit) {
    NodeList<CompilationUnitMember> declarations = unit.getDeclarations();
    // remove NodeList, it is declared in enginelib.dart
    for (Iterator<CompilationUnitMember> iter = declarations.iterator(); iter.hasNext(); ) {
      CompilationUnitMember member = iter.next();
      if (member instanceof ClassDeclaration) {
        ClassDeclaration classDeclaration = (ClassDeclaration) member;
        String name = classDeclaration.getName().getName();
        if (name.equals("NodeList")
            || name.equals("NodeLocator")
            || name.equals("NodeFoundException")) {
          iter.remove();
        }
      }
    }
    // process nodes
    unit.accept(
        new GeneralizingASTVisitor<Void>() {
          @Override
          public Void visitMethodDeclaration(MethodDeclaration node) {
            String name = node.getName().getName();
            if ("accept".equals(name) && node.getParameters().getParameters().size() == 1) {
              node.setReturnType(null);
              FormalParameter formalParameter = node.getParameters().getParameters().get(0);
              ((SimpleFormalParameter) formalParameter).getType().setTypeArguments(null);
            }
            return super.visitMethodDeclaration(node);
          }

          @Override
          public Void visitMethodInvocation(MethodInvocation node) {
            if (isMethodInClass(
                node, "toArray", "com.google.dart.engine.utilities.collection.IntList")) {
              replaceNode(node, node.getTarget());
              return null;
            }
            return super.visitMethodInvocation(node);
          }

          @Override
          public Void visitTypeName(TypeName node) {
            if (node.getName() instanceof SimpleIdentifier) {
              SimpleIdentifier nameNode = (SimpleIdentifier) node.getName();
              String name = nameNode.getName();
              if ("IntList".equals(name)) {
                replaceNode(node, typeName("List", typeName("int")));
                return null;
              }
            }
            return super.visitTypeName(node);
          }

          private boolean isMethodInClass(
              MethodInvocation node, String reqName, String reqClassName) {
            String name = node.getMethodName().getName();
            return Objects.equal(name, reqName)
                && JavaUtils.isMethodInClass(context.getNodeBinding(node), reqClassName);
          }
        });
  }