Example #1
0
  @Override
  public void endVisit(TypeDeclaration node) {
    List<Statement> adjustments = getFieldAdjustments(node);
    if (adjustments.isEmpty()) {
      return;
    }
    ITypeBinding type = node.getTypeBinding();

    ITypeBinding voidType = typeEnv.resolveJavaType("void");
    int modifiers = Modifier.PUBLIC | BindingUtil.ACC_SYNTHETIC;
    IOSMethodBinding methodBinding =
        IOSMethodBinding.newMethod(JAVA_CLONE_METHOD, modifiers, voidType, type);

    MethodDeclaration declaration = new MethodDeclaration(methodBinding);
    node.getBodyDeclarations().add(declaration);

    Block body = new Block();
    declaration.setBody(body);
    List<Statement> statements = body.getStatements();

    ITypeBinding nsObjectType = typeEnv.resolveIOSType("NSObject");
    IOSMethodBinding cloneMethod =
        IOSMethodBinding.newMethod(JAVA_CLONE_METHOD, Modifier.PUBLIC, voidType, nsObjectType);
    SuperMethodInvocation superCall = new SuperMethodInvocation(cloneMethod);
    statements.add(new ExpressionStatement(superCall));

    statements.addAll(adjustments);
  }
Example #2
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);
  }
Example #3
0
  /** Verify that super-interface methods are also added. */
  public void testAbstractClassGrandfatherInterface() {
    String source =
        "public class Test {"
            + "  public interface I1 { void foo(); } "
            + "  public interface I2 extends I1 { void bar(); } "
            + "  public abstract class Inner implements I2 { } }";
    CompilationUnit unit = translateType("Test", source);
    List<AbstractTypeDeclaration> types = unit.getTypes();
    assertEquals(4, types.size());
    assertTrue(types.get(3) instanceof TypeDeclaration);
    TypeDeclaration innerType = (TypeDeclaration) types.get(3);
    assertEquals("Inner", innerType.getName().toString());

    List<MethodDeclaration> methods = TreeUtil.getMethodDeclarationsList(innerType);
    assertEquals(3, methods.size());
    String name0 = methods.get(0).getName().getIdentifier();
    assertTrue(name0.matches("foo|bar"));
    String name1 = methods.get(1).getName().getIdentifier();
    assertTrue(name1.matches("foo|bar"));
    assertNotSame(name0, name1);
  }
Example #4
0
  /** Verifies that abstract methods to implement an interface are added to an abstract class. */
  public void testAbstractMethodsAdded() {
    String source =
        "import java.util.Iterator; public abstract class Test implements Iterator<Test> { "
            + "public boolean hasNext() { return true; } }";
    CompilationUnit unit = translateType("Test", source);
    List<AbstractTypeDeclaration> types = unit.getTypes();
    assertEquals(1, types.size());
    assertTrue(types.get(0) instanceof TypeDeclaration);
    TypeDeclaration testType = (TypeDeclaration) types.get(0);
    List<MethodDeclaration> methods = TreeUtil.getMethodDeclarationsList(testType);
    assertEquals(4, methods.size());

    // verify added methods are abstract, and that existing method wasn't changed
    for (MethodDeclaration m : methods) {
      int modifiers = m.getModifiers();
      String name = m.getName().getIdentifier();
      if (name.equals("hasNext")) {
        assertFalse(Modifier.isAbstract(modifiers));
      } else if (name.equals(NameTable.FINALIZE_METHOD)
          || name.equals(NameTable.DEALLOC_METHOD)
          || name.equals(NameTable.INIT_NAME)) {
        // it's ok.
      } else {
        // it's an added method
        assertTrue(Modifier.isAbstract(modifiers));
        assertEquals(0, m.getParameters().size());
        if (name.equals("next")) {
          assertEquals(testType.getTypeBinding(), m.getReturnType().getTypeBinding());
        } else if (name.equals("remove")) {
          ITypeBinding voidType = Types.resolveJavaType("void");
          assertEquals(voidType, m.getReturnType().getTypeBinding());
        } else {
          fail("unknown method added: " + name);
        }
      }
    }
  }
 @Override
 public void endVisit(TypeDeclaration node) {
   typeMap.put(node.getTypeBinding(), node);
 }