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_visitLabeledStatement() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String labelName = "l";
    LabeledStatement statement = labeledStatement(list(label(labelName)), breakStatement());
    statement.accept(builder);
    LabelElement[] labels = holder.getLabels();
    assertLength(1, labels);

    LabelElement label = labels[0];
    assertNotNull(label);
    assertEquals(labelName, label.getName());
    assertFalse(label.isSynthetic());
  }