/**
   * 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);
      }
    }
  }
Exemplo n.º 2
0
  @Override
  public Void visitClassDeclaration(ClassDeclaration node) {
    ElementHolder holder = new ElementHolder();
    visitChildren(holder, node);

    SimpleIdentifier className = node.getName();
    ClassElementImpl element = new ClassElementImpl(className);
    MethodElement[] methods = holder.getMethods();
    element.setAbstract(node.getAbstractKeyword() != null);
    element.setAccessors(holder.getAccessors());
    element.setConstructors(holder.getConstructors());
    element.setFields(holder.getFields());
    element.setMethods(methods);
    TypeVariableElement[] typeVariables = holder.getTypeVariables();
    element.setTypeVariables(typeVariables);

    InterfaceTypeImpl interfaceType = new InterfaceTypeImpl(element);
    int typeVariableCount = typeVariables.length;
    Type[] typeArguments = new Type[typeVariableCount];
    for (int i = 0; i < typeVariableCount; i++) {
      TypeVariableElementImpl typeVariable = (TypeVariableElementImpl) typeVariables[i];
      TypeVariableTypeImpl typeArgument = new TypeVariableTypeImpl(typeVariable);
      typeVariable.setType(typeArgument);
      typeArguments[i] = typeArgument;
    }
    interfaceType.setTypeArguments(typeArguments);
    element.setType(interfaceType);

    currentHolder.addType(element);
    className.setElement(element);
    return null;
  }
Exemplo n.º 3
0
  public void test_visitClassDeclaration_parameterized() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String className = "C";
    String firstVariableName = "E";
    String secondVariableName = "F";
    ClassDeclaration classDeclaration =
        classDeclaration(
            null,
            className,
            typeParameterList(firstVariableName, secondVariableName),
            null,
            null,
            null);
    classDeclaration.accept(builder);
    ClassElement[] types = holder.getTypes();
    assertLength(1, types);

    ClassElement type = types[0];
    assertNotNull(type);
    assertEquals(className, type.getName());
    TypeVariableElement[] typeVariables = type.getTypeVariables();
    assertLength(2, typeVariables);
    assertEquals(firstVariableName, typeVariables[0].getName());
    assertEquals(secondVariableName, typeVariables[1].getName());
    assertFalse(type.isAbstract());
    assertFalse(type.isSynthetic());
  }
Exemplo n.º 4
0
  public void test_visitClassDeclaration_withMembers() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String className = "C";
    String typeVariableName = "E";
    String fieldName = "f";
    String methodName = "m";
    ClassDeclaration classDeclaration =
        classDeclaration(
            null,
            className,
            typeParameterList(typeVariableName),
            null,
            null,
            null,
            fieldDeclaration(false, null, variableDeclaration(fieldName)),
            methodDeclaration(
                null,
                null,
                null,
                null,
                identifier(methodName),
                formalParameterList(),
                blockFunctionBody()));
    classDeclaration.accept(builder);
    ClassElement[] types = holder.getTypes();
    assertLength(1, types);

    ClassElement type = types[0];
    assertNotNull(type);
    assertEquals(className, type.getName());
    assertFalse(type.isAbstract());
    assertFalse(type.isSynthetic());

    TypeVariableElement[] typeVariables = type.getTypeVariables();
    assertLength(1, typeVariables);
    TypeVariableElement typeVariable = typeVariables[0];
    assertNotNull(typeVariable);
    assertEquals(typeVariableName, typeVariable.getName());

    FieldElement[] fields = type.getFields();
    assertLength(1, fields);
    FieldElement field = fields[0];
    assertNotNull(field);
    assertEquals(fieldName, field.getName());

    MethodElement[] methods = type.getMethods();
    assertLength(1, methods);
    MethodElement method = methods[0];
    assertNotNull(method);
    assertEquals(methodName, method.getName());
  }
Exemplo n.º 5
0
  @Override
  public Void visitClassDeclaration(ClassDeclaration node) {
    ElementHolder holder = new ElementHolder();
    visitChildren(holder, node);

    TypeElementImpl element = new TypeElementImpl(node.getName());
    MethodElement[] methods = holder.getMethods();
    element.setAbstract(node.getAbstractKeyword() != null || hasAbstractMethod(methods));
    element.setAccessors(holder.getAccessors());
    element.setConstructors(holder.getConstructors());
    element.setFields(holder.getFields());
    element.setMethods(methods);
    element.setTypeVariables(holder.getTypeVariables());
    currentHolder.addType(element);
    return null;
  }
Exemplo n.º 6
0
  public void test_visitClassDeclaration_minimal() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String className = "C";
    ClassDeclaration classDeclaration = classDeclaration(null, className, null, null, null, null);
    classDeclaration.accept(builder);
    ClassElement[] types = holder.getTypes();
    assertLength(1, types);

    ClassElement type = types[0];
    assertNotNull(type);
    assertEquals(className, type.getName());
    TypeVariableElement[] typeVariables = type.getTypeVariables();
    assertLength(0, typeVariables);
    assertFalse(type.isAbstract());
    assertFalse(type.isSynthetic());
  }
Exemplo n.º 7
0
  @Override
  public Void visitClassDeclaration(ClassDeclaration node) {
    ElementHolder holder = new ElementHolder();
    isValidMixin = true;
    functionTypesToFix = new ArrayList<FunctionTypeImpl>();
    visitChildren(holder, node);

    SimpleIdentifier className = node.getName();
    ClassElementImpl element = new ClassElementImpl(className);
    TypeParameterElement[] typeParameters = holder.getTypeParameters();

    Type[] typeArguments = createTypeParameterTypes(typeParameters);
    InterfaceTypeImpl interfaceType = new InterfaceTypeImpl(element);
    interfaceType.setTypeArguments(typeArguments);
    element.setType(interfaceType);

    ConstructorElement[] constructors = holder.getConstructors();
    if (constructors.length == 0) {
      //
      // Create the default constructor.
      //
      constructors = createDefaultConstructors(interfaceType);
    }
    element.setAbstract(node.getAbstractKeyword() != null);
    element.setAccessors(holder.getAccessors());
    element.setConstructors(constructors);
    element.setFields(holder.getFields());
    element.setMethods(holder.getMethods());
    element.setTypeParameters(typeParameters);
    element.setValidMixin(isValidMixin);

    int functionTypeCount = functionTypesToFix.size();
    for (int i = 0; i < functionTypeCount; i++) {
      functionTypesToFix.get(i).setTypeArguments(typeArguments);
    }
    functionTypesToFix = null;
    currentHolder.addType(element);
    className.setStaticElement(element);
    holder.validate();
    return null;
  }
Exemplo n.º 8
0
 @Override
 public Void visitClassDeclaration(ClassDeclaration node) {
   visit(node.getAbstractKeyword(), " ");
   writer.print("class ");
   visit(node.getName());
   visit(node.getTypeParameters());
   visit(" ", node.getExtendsClause());
   visit(" ", node.getWithClause());
   visit(" ", node.getImplementsClause());
   writer.print(" {");
   visitList(node.getMembers(), " ");
   writer.print("}");
   return null;
 }
Exemplo n.º 9
0
  @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);
          }
        });
  }
 @Override
 public R visitClassDeclaration(ClassDeclaration node) {
   node.visitChildren(this);
   return null;
 }