/** * 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); }
/** List has a toArray() method that uses array types. */ public void testAbstractMethodsAddedWithArrayType() { String source = "import java.util.List; public abstract class Test implements List<Object> { " + "public boolean isEmpty() { 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(26, 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("isEmpty")) { 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)); ITypeBinding returnType = m.getReturnType().getTypeBinding(); if (name.equals("toArray")) { assertEquals("IOSObjectArray", returnType.getName()); if (!m.getParameters().isEmpty()) { assertEquals(1, m.getParameters().size()); SingleVariableDeclaration param = m.getParameters().get(0); IVariableBinding paramBinding = param.getVariableBinding(); assertTrue(paramBinding.getType().isArray()); } } } } }
/** 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); } } } }