Ejemplo n.º 1
0
  public void test_visitVariableDeclaration_noInitializer() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String variableName = "v";
    VariableDeclaration variableDeclaration = variableDeclaration(variableName, null);
    variableDeclarationList(null, variableDeclaration);
    variableDeclaration.accept(builder);
    VariableElement[] variables = holder.getVariables();
    assertLength(1, variables);

    VariableElement variable = variables[0];
    assertNotNull(variable);
    assertNull(variable.getInitializer());
    assertEquals(variableName, variable.getName());
    assertFalse(variable.isConst());
    assertFalse(variable.isFinal());
    assertFalse(variable.isSynthetic());
  }
Ejemplo n.º 2
0
  @Override
  public Void visitVariableDeclaration(VariableDeclaration node) {
    VariableElementImpl element;
    if (inFieldContext) {
      SimpleIdentifier fieldName = node.getName();
      element = new FieldElementImpl(fieldName);

      currentHolder.addField((FieldElementImpl) element);
      fieldName.setElement(element);
    } else {
      SimpleIdentifier variableName = node.getName();
      element = new VariableElementImpl(variableName);

      currentHolder.addVariable(element);
      variableName.setElement(element);
    }

    Token keyword = ((VariableDeclarationList) node.getParent()).getKeyword();
    boolean isFinal = matches(keyword, Keyword.FINAL);
    element.setConst(matches(keyword, Keyword.CONST));
    element.setFinal(isFinal);
    if (node.getInitializer() != null) {
      ElementHolder holder = new ElementHolder();
      boolean wasInFieldContext = inFieldContext;
      inFieldContext = false;
      try {
        visitChildren(holder, node.getInitializer());
      } finally {
        inFieldContext = wasInFieldContext;
      }
      FunctionElementImpl initializer = new FunctionElementImpl();
      initializer.setFunctions(holder.getFunctions());
      initializer.setLabels(holder.getLabels());
      initializer.setLocalVariables(holder.getVariables());
      initializer.setSynthetic(true);
      element.setInitializer(initializer);
    }
    if (inFieldContext) {
      FieldElementImpl field = (FieldElementImpl) element;
      PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(field);
      getter.setGetter(true);
      field.setGetter(getter);

      if (!isFinal) {
        PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(field);
        setter.setSetter(true);
        field.setSetter(setter);
      }

      field.setStatic(
          matches(((FieldDeclaration) node.getParent().getParent()).getKeyword(), Keyword.STATIC));
    }
    return null;
  }
Ejemplo n.º 3
0
 @Override
 public Void visitVariableDeclaration(VariableDeclaration node) {
   visit(node.getName());
   visit(" = ", node.getInitializer());
   return null;
 }
 @Override
 public R visitVariableDeclaration(VariableDeclaration node) {
   node.visitChildren(this);
   return null;
 }
Ejemplo n.º 5
0
  @Override
  public Void visitVariableDeclaration(VariableDeclaration node) {
    Token keyword = ((VariableDeclarationList) node.getParent()).getKeyword();
    boolean isConst = matches(keyword, Keyword.CONST);
    boolean isFinal = matches(keyword, Keyword.FINAL);
    boolean hasInitializer = node.getInitializer() != null;

    VariableElementImpl element;
    if (inFieldContext) {
      SimpleIdentifier fieldName = node.getName();
      FieldElementImpl field;
      if (isConst && hasInitializer) {
        field = new ConstFieldElementImpl(fieldName);
      } else {
        field = new FieldElementImpl(fieldName);
      }
      element = field;

      currentHolder.addField(field);
      fieldName.setStaticElement(field);
    } else if (inFunction) {
      SimpleIdentifier variableName = node.getName();
      LocalVariableElementImpl variable;
      if (isConst && hasInitializer) {
        variable = new ConstLocalVariableElementImpl(variableName);
      } else {
        variable = new LocalVariableElementImpl(variableName);
      }
      element = variable;
      Block enclosingBlock = node.getAncestor(Block.class);
      int functionEnd = node.getOffset() + node.getLength();
      int blockEnd = enclosingBlock.getOffset() + enclosingBlock.getLength();
      // TODO(brianwilkerson) This isn't right for variables declared in a for loop.
      variable.setVisibleRange(functionEnd, blockEnd - functionEnd - 1);

      currentHolder.addLocalVariable(variable);
      variableName.setStaticElement(element);
    } else {
      SimpleIdentifier variableName = node.getName();
      TopLevelVariableElementImpl variable;
      if (isConst && hasInitializer) {
        variable = new ConstTopLevelVariableElementImpl(variableName);
      } else {
        variable = new TopLevelVariableElementImpl(variableName);
      }
      element = variable;

      currentHolder.addTopLevelVariable(variable);
      variableName.setStaticElement(element);
    }

    element.setConst(isConst);
    element.setFinal(isFinal);
    if (hasInitializer) {
      ElementHolder holder = new ElementHolder();
      boolean wasInFieldContext = inFieldContext;
      inFieldContext = false;
      try {
        visit(holder, node.getInitializer());
      } finally {
        inFieldContext = wasInFieldContext;
      }
      FunctionElementImpl initializer =
          new FunctionElementImpl(node.getInitializer().getBeginToken().getOffset());
      initializer.setFunctions(holder.getFunctions());
      initializer.setLabels(holder.getLabels());
      initializer.setLocalVariables(holder.getLocalVariables());
      initializer.setSynthetic(true);
      element.setInitializer(initializer);
      holder.validate();
    }
    if (element instanceof PropertyInducingElementImpl) {
      PropertyInducingElementImpl variable = (PropertyInducingElementImpl) element;

      if (inFieldContext) {
        ((FieldElementImpl) variable)
            .setStatic(
                matches(
                    ((FieldDeclaration) node.getParent().getParent()).getStaticKeyword(),
                    Keyword.STATIC));
      }

      PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(variable);
      getter.setGetter(true);
      getter.setStatic(variable.isStatic());

      currentHolder.addAccessor(getter);
      variable.setGetter(getter);

      if (!isFinal) {
        PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(variable);
        setter.setSetter(true);
        setter.setStatic(variable.isStatic());
        ParameterElementImpl parameter =
            new ParameterElementImpl("_" + variable.getName(), variable.getNameOffset());
        parameter.setSynthetic(true);
        parameter.setParameterKind(ParameterKind.REQUIRED);
        setter.setParameters(new ParameterElement[] {parameter});

        currentHolder.addAccessor(setter);
        variable.setSetter(setter);
      }
    }
    return null;
  }