Example #1
0
  public void test_visitMethodDeclaration_static() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String methodName = "m";
    MethodDeclaration methodDeclaration =
        methodDeclaration(
            Keyword.STATIC,
            null,
            null,
            null,
            identifier(methodName),
            formalParameterList(),
            blockFunctionBody());
    methodDeclaration.accept(builder);
    MethodElement[] methods = holder.getMethods();
    assertLength(1, methods);

    MethodElement method = methods[0];
    assertNotNull(method);
    assertEquals(methodName, method.getName());
    assertLength(0, method.getFunctions());
    assertLength(0, method.getLabels());
    assertLength(0, method.getLocalVariables());
    assertLength(0, method.getParameters());
    assertFalse(method.isAbstract());
    assertTrue(method.isStatic());
    assertFalse(method.isSynthetic());
  }
Example #2
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());
  }