示例#1
0
  public void test_visitCatchClause() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String exceptionParameterName = "e";
    String stackParameterName = "s";
    CatchClause clause = catchClause(exceptionParameterName, stackParameterName);
    clause.accept(builder);
    VariableElement[] variables = holder.getVariables();
    assertLength(2, variables);

    VariableElement exceptionVariable = variables[0];
    assertNotNull(exceptionVariable);
    assertEquals(exceptionParameterName, exceptionVariable.getName());
    assertFalse(exceptionVariable.isSynthetic());
    assertFalse(exceptionVariable.isConst());
    assertFalse(exceptionVariable.isFinal());
    assertNull(exceptionVariable.getInitializer());

    VariableElement stackVariable = variables[1];
    assertNotNull(stackVariable);
    assertEquals(stackParameterName, stackVariable.getName());
    assertFalse(stackVariable.isSynthetic());
    assertFalse(stackVariable.isConst());
    assertFalse(stackVariable.isFinal());
    assertNull(stackVariable.getInitializer());
  }
 @Override
 public Void visitFormalParameterList(FormalParameterList node) {
   ElementHolder holder = new ElementHolder();
   visitChildren(holder, node);
   currentHolder.setParameters(holder.getVariables());
   return null;
 }
示例#3
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;
  }
示例#4
0
  @Override
  public Void visitMethodDeclaration(MethodDeclaration node) {
    ElementHolder holder = new ElementHolder();
    visitChildren(holder, node);

    Token property = node.getPropertyKeyword();
    if (property == null) {
      Identifier methodName = node.getName();
      MethodElementImpl element = new MethodElementImpl(methodName);
      Token keyword = node.getModifierKeyword();
      element.setAbstract(matches(keyword, Keyword.ABSTRACT));
      element.setFunctions(holder.getFunctions());
      element.setLabels(holder.getLabels());
      element.setLocalVariables(holder.getVariables());
      element.setParameters(holder.getParameters());
      element.setStatic(matches(keyword, Keyword.STATIC));

      currentHolder.addMethod(element);
      methodName.setElement(element);
    } else {
      Identifier propertyNameNode = node.getName();
      String propertyName = propertyNameNode.getName();
      FieldElementImpl field = (FieldElementImpl) currentHolder.getField(propertyName);
      if (field == null) {
        field = new FieldElementImpl(node.getName().getName());
        field.setFinal(true);
        field.setStatic(matches(node.getModifierKeyword(), Keyword.STATIC));

        currentHolder.addField(field);
      }
      if (matches(property, Keyword.GET)) {
        PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(propertyNameNode);
        getter.setField(field);
        getter.setGetter(true);
        field.setGetter(getter);

        currentHolder.addAccessor(getter);
        propertyNameNode.setElement(getter);
      } else {
        PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(propertyNameNode);
        setter.setField(field);
        setter.setSetter(true);
        field.setSetter(setter);
        field.setFinal(false);

        currentHolder.addAccessor(setter);
        propertyNameNode.setElement(setter);
      }
    }
    return null;
  }
  @Override
  public Void visitFunctionExpression(FunctionExpression node) {
    ElementHolder holder = new ElementHolder();
    visitChildren(holder, node);

    FunctionElementImpl element = new FunctionElementImpl(node.getName());
    element.setFunctions(holder.getFunctions());
    element.setLabels(holder.getLabels());
    element.setLocalVariables(holder.getVariables());
    if (holder.getParameters() != null) {
      element.setParameters(holder.getParameters());
    }
    currentHolder.addFunction(element);
    return null;
  }
示例#6
0
  @Override
  public Void visitFunctionExpression(FunctionExpression node) {
    ElementHolder holder = new ElementHolder();
    visitChildren(holder, node);

    SimpleIdentifier functionName = null;
    FunctionElementImpl element = new FunctionElementImpl(functionName);
    element.setFunctions(holder.getFunctions());
    element.setLabels(holder.getLabels());
    element.setLocalVariables(holder.getVariables());
    element.setParameters(holder.getParameters());

    currentHolder.addFunction(element);
    node.setElement(element);
    return null;
  }
  @Override
  public Void visitNamedFormalParameter(NamedFormalParameter node) {
    ElementHolder holder = new ElementHolder();
    visitChildren(holder, node.getDefaultValue());

    FunctionElementImpl initializer = new FunctionElementImpl();
    initializer.setFunctions(holder.getFunctions());
    initializer.setLabels(holder.getLabels());
    initializer.setLocalVariables(holder.getVariables());
    if (holder.getParameters() != null) {
      initializer.setParameters(holder.getParameters());
    }

    VariableElementImpl parameter = new VariableElementImpl(node.getParameter().getIdentifier());
    parameter.setInitializer(initializer);
    currentHolder.addVariable(parameter);
    return null;
  }
示例#8
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());
  }
  @Override
  public Void visitConstructorDeclaration(ConstructorDeclaration node) {
    ElementHolder holder = new ElementHolder();
    visitChildren(holder, node);

    ConstructorElementImpl element = new ConstructorElementImpl(node.getName());
    Token keyword = node.getKeyword();
    if (keyword instanceof KeywordToken
        && ((KeywordToken) keyword).getKeyword() == Keyword.FACTORY) {
      element.setFactory(true);
    }
    element.setFunctions(holder.getFunctions());
    element.setLabels(holder.getLabels());
    element.setLocalVariables(holder.getVariables());
    if (holder.getParameters() != null) {
      element.setParameters(holder.getParameters());
    }
    currentHolder.addConstructor(element);
    return null;
  }
示例#10
0
  @Override
  public Void visitDefaultFormalParameter(DefaultFormalParameter node) {
    ElementHolder holder = new ElementHolder();
    visitChildren(holder, node.getDefaultValue());

    FunctionElementImpl initializer = new FunctionElementImpl();
    initializer.setFunctions(holder.getFunctions());
    initializer.setLabels(holder.getLabels());
    initializer.setLocalVariables(holder.getVariables());
    initializer.setParameters(holder.getParameters());

    SimpleIdentifier parameterName = node.getParameter().getIdentifier();
    ParameterElementImpl parameter = new ParameterElementImpl(parameterName);
    parameter.setConst(node.isConst());
    parameter.setFinal(node.isFinal());
    parameter.setInitializer(initializer);
    parameter.setParameterKind(node.getKind());

    currentHolder.addParameter(parameter);
    parameterName.setElement(parameter);
    return null;
  }
示例#11
0
  @Override
  public Void visitConstructorDeclaration(ConstructorDeclaration node) {
    ElementHolder holder = new ElementHolder();
    visitChildren(holder, node);

    SimpleIdentifier constructorName = node.getName();
    ConstructorElementImpl element = new ConstructorElementImpl(constructorName);
    if (node.getFactoryKeyword() != null) {
      element.setFactory(true);
    }
    element.setFunctions(holder.getFunctions());
    element.setLabels(holder.getLabels());
    element.setLocalVariables(holder.getVariables());
    element.setParameters(holder.getParameters());

    currentHolder.addConstructor(element);
    node.setElement(element);
    if (constructorName != null) {
      constructorName.setElement(element);
    }
    return null;
  }