Exemplo n.º 1
0
  /**
   * Adds a cast check to compareTo methods. This helps Comparable types behave well in sorted
   * collections which rely on Java's runtime type checking.
   */
  @Override
  public void endVisit(MethodDeclaration node) {
    IMethodBinding binding = node.getMethodBinding();
    if (!binding.getName().equals("compareTo") || node.getBody() == null) {
      return;
    }
    ITypeBinding comparableType =
        BindingUtil.findInterface(binding.getDeclaringClass(), "java.lang.Comparable");
    if (comparableType == null) {
      return;
    }
    ITypeBinding[] typeArguments = comparableType.getTypeArguments();
    ITypeBinding[] parameterTypes = binding.getParameterTypes();
    if (typeArguments.length != 1
        || parameterTypes.length != 1
        || !typeArguments[0].isEqualTo(parameterTypes[0])) {
      return;
    }

    IVariableBinding param = node.getParameters().get(0).getVariableBinding();

    FunctionInvocation castCheck = createCastCheck(typeArguments[0], new SimpleName(param));
    if (castCheck != null) {
      node.getBody().getStatements().add(0, new ExpressionStatement(castCheck));
    }
  }
Exemplo n.º 2
0
 public void testVariableDeclarationsInSwitchStatement2() throws IOException {
   CompilationUnit unit =
       translateType(
           "A",
           "public class A { public void doSomething(int i) { switch (i) { "
               + "case 1: int j = i * 2; log(j); break; "
               + "case 2: log(i); break; "
               + "case 3: log(i); int k = i, l = 42; break; }}"
               + "private void log(int i) {}}");
   TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
   MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(0);
   List<Statement> stmts = method.getBody().getStatements();
   assertEquals(1, stmts.size());
   Block block = (Block) stmts.get(0);
   stmts = block.getStatements();
   assertEquals(3, stmts.size());
   assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
   assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
   assertTrue(stmts.get(2) instanceof SwitchStatement);
 }
Exemplo n.º 3
0
  /**
   * Verify that interface methods declaring methods implemented by super-class have a forwarding
   * method.
   */
  public void testInterfaceOfSuperclassMethodInAnonymousInner() {
    String source =
        "interface Equateable { boolean equals(Object o); }"
            + "public class Test { public void foo() { Equateable e = new Equateable() { }; } } ";
    CompilationUnit unit = translateType("Test", source);
    assertEquals(3, unit.getTypes().size());
    TypeDeclaration innerType = (TypeDeclaration) unit.getTypes().get(2);
    assertEquals("$1", innerType.getName().toString());

    List<MethodDeclaration> methods = TreeUtil.getMethodDeclarationsList(innerType);
    assertEquals(2, methods.size()); // isEqual, init
    MethodDeclaration equalsMethod = methods.get(0);
    assertEquals("isEqual", equalsMethod.getName().getIdentifier());
    assertEquals(1, equalsMethod.getParameters().size());
    assertTrue(equalsMethod.getParameters().get(0) instanceof SingleVariableDeclaration);
    List<Statement> stmts = equalsMethod.getBody().getStatements();
    assertEquals(1, stmts.size());
    Statement stmt = stmts.get(0);
    assertTrue(stmt instanceof ReturnStatement);
    assertTrue(((ReturnStatement) stmt).getExpression() instanceof SuperMethodInvocation);
  }