Ejemplo n.º 1
0
  public void test_visitMethodDeclaration_withMembers() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String methodName = "m";
    String parameterName = "p";
    String localVariableName = "v";
    String labelName = "l";
    String exceptionParameterName = "e";
    MethodDeclaration methodDeclaration =
        methodDeclaration(
            null,
            null,
            null,
            null,
            identifier(methodName),
            formalParameterList(simpleFormalParameter(parameterName)),
            blockFunctionBody(
                variableDeclarationStatement(Keyword.VAR, variableDeclaration(localVariableName)),
                tryStatement(
                    block(labeledStatement(list(label(labelName)), returnStatement())),
                    catchClause(exceptionParameterName))));
    methodDeclaration.accept(builder);
    MethodElement[] methods = holder.getMethods();
    assertLength(1, methods);

    MethodElement method = methods[0];
    assertNotNull(method);
    assertEquals(methodName, method.getName());
    assertFalse(method.isAbstract());
    assertFalse(method.isStatic());
    assertFalse(method.isSynthetic());

    VariableElement[] parameters = method.getParameters();
    assertLength(1, parameters);
    VariableElement parameter = parameters[0];
    assertNotNull(parameter);
    assertEquals(parameterName, parameter.getName());

    VariableElement[] localVariables = method.getLocalVariables();
    assertLength(2, localVariables);
    VariableElement firstVariable = localVariables[0];
    VariableElement secondVariable = localVariables[1];
    assertNotNull(firstVariable);
    assertNotNull(secondVariable);
    assertTrue(
        (firstVariable.getName().equals(localVariableName)
                && secondVariable.getName().equals(exceptionParameterName))
            || (firstVariable.getName().equals(exceptionParameterName)
                && secondVariable.getName().equals(localVariableName)));

    LabelElement[] labels = method.getLabels();
    assertLength(1, labels);
    LabelElement label = labels[0];
    assertNotNull(label);
    assertEquals(labelName, label.getName());
  }
Ejemplo n.º 2
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.º 3
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());
  }