1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package signature.converter; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotSame; 21 import static org.junit.Assert.assertSame; 22 import static org.junit.Assert.assertTrue; 23 24 import java.io.IOException; 25 import java.util.HashMap; 26 import java.util.List; 27 import java.util.Map; 28 import java.util.Set; 29 30 import org.junit.Test; 31 32 import signature.converter.util.AbstractConvertTest; 33 import signature.converter.util.CompilationUnit; 34 import signature.model.IAnnotation; 35 import signature.model.IApi; 36 import signature.model.IArrayType; 37 import signature.model.IClassDefinition; 38 import signature.model.IClassReference; 39 import signature.model.IConstructor; 40 import signature.model.IField; 41 import signature.model.IMethod; 42 import signature.model.IPackage; 43 import signature.model.IParameter; 44 import signature.model.IParameterizedType; 45 import signature.model.ITypeReference; 46 import signature.model.ITypeVariableDefinition; 47 import signature.model.ITypeVariableReference; 48 import signature.model.Kind; 49 import signature.model.Modifier; 50 import signature.model.impl.SigPrimitiveType; 51 import signature.model.util.ModelUtil; 52 53 public abstract class ConvertClassTest extends AbstractConvertTest { 54 55 @Test 56 public void convertPackageClassTest() throws IOException { 57 IApi api = convert(new CompilationUnit("a.b.A", "package a.b; public class A{}")); 58 IPackage sigPackage = ModelUtil.getPackage(api, "a.b"); 59 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 60 assertEquals("a.b.A", sigClass.getQualifiedName()); 61 assertEquals("A", sigClass.getName()); 62 assertEquals("a.b", sigPackage.getName()); 63 assertEquals(1, sigPackage.getClasses().size()); 64 } 65 66 @Test 67 public void convertClassClassTest() throws IOException { 68 IApi api = convert(new CompilationUnit("a.A", "package a; public class A{}")); 69 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 70 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 71 assertEquals("a.A", sigClass.getQualifiedName()); 72 assertEquals("A", sigClass.getName()); 73 assertEquals(Kind.CLASS, sigClass.getKind()); 74 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 75 } 76 77 @Test 78 public void convertClassInterfaceTest() throws IOException { 79 IApi api = convert(new CompilationUnit("a.A", "package a; public interface A{}")); 80 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 81 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 82 assertEquals("a.A", sigClass.getQualifiedName()); 83 assertEquals("A", sigClass.getName()); 84 assertEquals(Kind.INTERFACE, sigClass.getKind()); 85 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 86 } 87 88 @Test 89 public void convertClassEnumTest() throws IOException { 90 IApi api = convert(new CompilationUnit("a.A", "package a; public enum A{}")); 91 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 92 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 93 assertEquals("a.A", sigClass.getQualifiedName()); 94 assertEquals("A", sigClass.getName()); 95 assertEquals(Kind.ENUM, sigClass.getKind()); 96 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 97 } 98 99 @Test 100 public void convertClassAnnotationTest() throws IOException { 101 IApi api = convert(new CompilationUnit("a.A", "package a; public @interface A{}")); 102 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 103 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 104 assertEquals("a.A", sigClass.getQualifiedName()); 105 assertEquals("A", sigClass.getName()); 106 assertEquals(Kind.ANNOTATION, sigClass.getKind()); 107 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 108 } 109 110 @Test 111 public void convertAnnotationTest() throws IOException { 112 IApi api = convert(new CompilationUnit("a.A", "package a; @Deprecated public class A{}")); 113 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 114 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 115 assertEquals("a.A", sigClass.getQualifiedName()); 116 assertEquals("A", sigClass.getName()); 117 assertEquals(Kind.CLASS, sigClass.getKind()); 118 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 119 assertEquals(1, sigClass.getAnnotations().size()); 120 IAnnotation annotation = sigClass.getAnnotations().iterator().next(); 121 assertEquals("java.lang.Deprecated", annotation.getType().getClassDefinition().getQualifiedName()); 122 } 123 124 @Test 125 public void convertAnnotationOnFieldTest() throws IOException { 126 IApi api = convert(new CompilationUnit("a.A", 127 "package a; " + 128 "public class A{" + 129 " @Deprecated" + 130 " public int f;" + 131 "}")); 132 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 133 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 134 assertEquals("a.A", sigClass.getQualifiedName()); 135 assertEquals("A", sigClass.getName()); 136 assertEquals(Kind.CLASS, sigClass.getKind()); 137 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 138 assertEquals(1, sigClass.getFields().size()); 139 IField field = sigClass.getFields().iterator().next(); 140 assertEquals("f", field.getName()); 141 assertEquals(1, field.getAnnotations().size()); 142 IAnnotation annotation = field.getAnnotations().iterator().next(); 143 assertEquals("java.lang.Deprecated", annotation.getType().getClassDefinition().getQualifiedName()); 144 } 145 146 @Test 147 public void convertGenericClass0() throws IOException { 148 IApi api = convert(new CompilationUnit("a.A", "package a; public class A<T> {}")); 149 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 150 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 151 assertEquals("a.A", sigClass.getQualifiedName()); 152 assertEquals("A", sigClass.getName()); 153 assertEquals(Kind.CLASS, sigClass.getKind()); 154 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 155 //test type variable 156 assertEquals(1, sigClass.getTypeParameters().size()); 157 ITypeVariableDefinition variable = sigClass.getTypeParameters().get(0); 158 assertEquals("T", variable.getName()); 159 //test type variable bound 160 assertEquals(1, variable.getUpperBounds().size()); 161 IClassReference bound = (IClassReference) variable.getUpperBounds().get(0); 162 assertEquals("java.lang.Object", bound.getClassDefinition().getQualifiedName()); 163 } 164 165 @Test 166 public void convertGenericClass00() throws IOException { 167 IApi api = convert(new CompilationUnit("a.A", "package a; public class A<T extends Integer> {}")); 168 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 169 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 170 assertEquals("a.A", sigClass.getQualifiedName()); 171 assertEquals("A", sigClass.getName()); 172 assertEquals(Kind.CLASS, sigClass.getKind()); 173 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 174 //test type variable 175 assertEquals(1, sigClass.getTypeParameters().size()); 176 ITypeVariableDefinition variable = sigClass.getTypeParameters().get(0); 177 assertEquals("T", variable.getName()); 178 //test type variable bound 179 assertEquals(1, variable.getUpperBounds().size()); 180 IClassReference bound = (IClassReference) variable.getUpperBounds().get(0); 181 assertEquals("java.lang.Integer", bound.getClassDefinition().getQualifiedName()); 182 } 183 184 @Test 185 public void convertGenericClass1() throws IOException { 186 IApi api = convert(new CompilationUnit("a.A", "package a; public class A<S,T> {}")); 187 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 188 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 189 assertEquals("a.A", sigClass.getQualifiedName()); 190 assertEquals("A", sigClass.getName()); 191 assertEquals(Kind.CLASS, sigClass.getKind()); 192 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 193 //test type variable 194 assertEquals(2, sigClass.getTypeParameters().size()); 195 ITypeVariableDefinition variableS = sigClass.getTypeParameters().get(0); 196 assertEquals("S", variableS.getName()); 197 //test type variable bound 198 assertEquals(1, variableS.getUpperBounds().size()); 199 IClassReference boundS = (IClassReference) variableS.getUpperBounds().get(0); 200 assertEquals("java.lang.Object", boundS.getClassDefinition().getQualifiedName()); 201 202 ITypeVariableDefinition variableT = sigClass.getTypeParameters().get(1); 203 assertEquals("T", variableT.getName()); 204 //test type variable bound 205 assertEquals(1, variableT.getUpperBounds().size()); 206 IClassReference boundT = (IClassReference) variableT.getUpperBounds().get(0); 207 assertEquals("java.lang.Object", boundT.getClassDefinition().getQualifiedName()); 208 } 209 210 @Test 211 public void convertGenericClass2() throws IOException { 212 IApi api = convert(new CompilationUnit("a.A", "package a; public class A<S,T extends S> {}")); 213 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 214 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 215 assertEquals("a.A", sigClass.getQualifiedName()); 216 assertEquals("A", sigClass.getName()); 217 assertEquals(Kind.CLASS, sigClass.getKind()); 218 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 219 //test type variable 220 assertEquals(2, sigClass.getTypeParameters().size()); 221 ITypeVariableDefinition variableS = sigClass.getTypeParameters().get(0); 222 assertEquals("S", variableS.getName()); 223 //test type variable bound 224 assertEquals(1, variableS.getUpperBounds().size()); 225 IClassReference boundS = (IClassReference) variableS.getUpperBounds().get(0); 226 assertEquals("java.lang.Object", boundS.getClassDefinition().getQualifiedName()); 227 228 ITypeVariableDefinition variableT = sigClass.getTypeParameters().get(1); 229 assertEquals("T", variableT.getName()); 230 //test type variable bound 231 assertEquals(1, variableT.getUpperBounds().size()); 232 ITypeVariableReference boundT = (ITypeVariableReference) variableT.getUpperBounds().get(0); 233 assertSame(variableS, boundT.getTypeVariableDefinition()); 234 } 235 236 @Test 237 public void convertClassImplementsInterface0() throws IOException { 238 IApi api = convert(new CompilationUnit("a.A", "package a; public class A implements Cloneable {}")); 239 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 240 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 241 assertEquals("a.A", sigClass.getQualifiedName()); 242 assertEquals("A", sigClass.getName()); 243 assertEquals(Kind.CLASS, sigClass.getKind()); 244 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 245 246 assertEquals(1, sigClass.getInterfaces().size()); 247 IClassDefinition clonable = ((IClassReference) sigClass.getInterfaces().iterator().next()).getClassDefinition(); 248 assertEquals("java.lang.Cloneable", clonable.getQualifiedName()); 249 assertEquals(Kind.INTERFACE, clonable.getKind()); 250 } 251 252 @Test 253 public void convertClassImplementsInterface1() throws IOException { 254 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A implements I {}"); 255 CompilationUnit interfaceSrc = new CompilationUnit("a.I", "package a; public interface I {}"); 256 IApi api = convert(classSrc, interfaceSrc); 257 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 258 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 259 assertEquals("a.A", sigClass.getQualifiedName()); 260 assertEquals("A", sigClass.getName()); 261 assertEquals(Kind.CLASS, sigClass.getKind()); 262 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 263 264 IClassDefinition sigInterface = ModelUtil.getClass(sigPackage, "I"); 265 assertEquals("a.I", sigInterface.getQualifiedName()); 266 assertEquals("I", sigInterface.getName()); 267 assertEquals(Kind.INTERFACE, sigInterface.getKind()); 268 assertTrue(sigInterface.getModifiers().contains(Modifier.PUBLIC)); 269 270 assertEquals(1, sigClass.getInterfaces().size()); 271 IClassReference interfaze = (IClassReference) sigClass.getInterfaces().iterator().next(); 272 assertEquals(Kind.INTERFACE, interfaze.getClassDefinition().getKind()); 273 assertSame(sigInterface, interfaze.getClassDefinition()); 274 } 275 276 @Test 277 public void convertClassImplementsGenericInterface0() throws IOException { 278 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A implements I<String> {}"); 279 CompilationUnit interfaceSrc = new CompilationUnit("a.I", "package a; public interface I<T> {}"); 280 IApi api = convert(classSrc, interfaceSrc); 281 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 282 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 283 assertEquals("a.A", sigClass.getQualifiedName()); 284 assertEquals("A", sigClass.getName()); 285 assertEquals(Kind.CLASS, sigClass.getKind()); 286 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 287 288 IClassDefinition sigInterface = ModelUtil.getClass(sigPackage, "I"); 289 assertEquals("a.I", sigInterface.getQualifiedName()); 290 assertEquals("I", sigInterface.getName()); 291 assertEquals(Kind.INTERFACE, sigInterface.getKind()); 292 assertTrue(sigInterface.getModifiers().contains(Modifier.PUBLIC)); 293 294 assertEquals(1, sigClass.getInterfaces().size()); 295 IParameterizedType interfaze = (IParameterizedType) sigClass.getInterfaces().iterator().next(); 296 //compare raw type "I" 297 assertSame(sigInterface, interfaze.getRawType().getClassDefinition()); 298 //test type argument string 299 assertEquals(1, interfaze.getTypeArguments().size()); 300 IClassReference string = (IClassReference) interfaze.getTypeArguments().get(0); 301 assertEquals("java.lang.String", string.getClassDefinition().getQualifiedName()); 302 } 303 304 @Test 305 public void convertGenericClassImplementsGenericInterface() throws IOException { 306 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A<T> implements I<T> {}"); 307 CompilationUnit interfaceSrc = new CompilationUnit("a.I", "package a; public interface I<T> {}"); 308 IApi api = convert(classSrc, interfaceSrc); 309 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 310 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 311 assertEquals("a.A", sigClass.getQualifiedName()); 312 assertEquals("A", sigClass.getName()); 313 assertEquals(Kind.CLASS, sigClass.getKind()); 314 assertTrue(sigClass.getModifiers().contains(Modifier.PUBLIC)); 315 316 //get type variable from class 317 assertEquals(1, sigClass.getTypeParameters().size()); 318 ITypeVariableDefinition classTypeVar = sigClass.getTypeParameters().get(0); 319 assertEquals("T", classTypeVar.getName()); 320 assertSame(sigClass, classTypeVar.getGenericDeclaration()); 321 322 //inspect interface 323 IClassDefinition sigInterface = ModelUtil.getClass(sigPackage, "I"); 324 assertEquals("a.I", sigInterface.getQualifiedName()); 325 assertEquals("I", sigInterface.getName()); 326 assertEquals(Kind.INTERFACE, sigInterface.getKind()); 327 assertTrue(sigInterface.getModifiers().contains(Modifier.PUBLIC)); 328 329 //get type variable from interface 330 assertEquals(1, sigInterface.getTypeParameters().size()); 331 ITypeVariableDefinition interfaceTypeVar = sigInterface.getTypeParameters().get(0); 332 333 //get implemented interface from A 334 assertEquals(1, sigClass.getInterfaces().size()); 335 IParameterizedType interfaze = (IParameterizedType) sigClass.getInterfaces().iterator().next(); 336 337 //compare raw type "I" 338 assertSame(sigInterface, interfaze.getRawType().getClassDefinition()); 339 340 //test if type variable of A is type argument of I 341 assertEquals(1, interfaze.getTypeArguments().size()); 342 ITypeVariableReference argument = (ITypeVariableReference) interfaze.getTypeArguments().get(0); 343 assertSame(sigClass, argument.getTypeVariableDefinition().getGenericDeclaration()); 344 assertEquals(classTypeVar, argument.getTypeVariableDefinition()); 345 346 //test that the two type parameters are not equal 347 assertNotSame(classTypeVar, interfaceTypeVar); 348 assertEquals(classTypeVar.getName(), interfaceTypeVar.getName()); 349 assertNotSame(classTypeVar.getGenericDeclaration(), interfaceTypeVar.getGenericDeclaration()); 350 } 351 352 @Test 353 public void convertConstructor0() throws IOException { 354 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A { public A(){} }"); 355 IApi api = convert(classSrc); 356 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 357 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 358 assertEquals(1, sigClass.getConstructors().size()); 359 IConstructor constructor = sigClass.getConstructors().iterator().next(); 360 assertTrue(constructor.getModifiers().contains(Modifier.PUBLIC)); 361 } 362 363 @Test 364 public void convertConstructor1a() throws IOException { 365 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A { public class B{ } }"); 366 IApi api = convert(classSrc); 367 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 368 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A.B"); 369 assertEquals(1, sigClass.getConstructors().size()); 370 IConstructor constructor = sigClass.getConstructors().iterator().next(); 371 assertTrue(constructor.getModifiers().contains(Modifier.PUBLIC)); 372 } 373 374 @Test 375 public void convertConstructor1b() throws IOException { 376 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A { protected class B{ } }"); 377 IApi api = convert(classSrc); 378 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 379 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A.B"); 380 assertEquals(1, sigClass.getConstructors().size()); 381 IConstructor constructor = sigClass.getConstructors().iterator().next(); 382 assertTrue(constructor.getModifiers().contains(Modifier.PROTECTED)); 383 } 384 385 @Test 386 public void convertConstructor2() throws IOException { 387 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A {}"); 388 IApi api = convert(classSrc); 389 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 390 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 391 assertEquals(1, sigClass.getConstructors().size()); 392 } 393 394 @Test 395 public void convertConstructor3() throws IOException { 396 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A { public class B { public B(){}} }"); 397 IApi api = convert(classSrc); 398 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 399 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A.B"); 400 assertEquals(1, sigClass.getConstructors().size()); 401 } 402 403 @Test 404 public void convertConstructorWithException() throws IOException { 405 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A { public A() throws NullPointerException{} }"); 406 IApi api = convert(classSrc); 407 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 408 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 409 assertEquals(1, sigClass.getConstructors().size()); 410 IConstructor constructor = sigClass.getConstructors().iterator().next(); 411 assertTrue(constructor.getModifiers().contains(Modifier.PUBLIC)); 412 assertEquals(1, constructor.getExceptions().size()); 413 IClassReference exception = (IClassReference) constructor.getExceptions().iterator().next(); 414 assertEquals("java.lang.NullPointerException", exception.getClassDefinition().getQualifiedName()); 415 } 416 417 @Test 418 public void convertConstructorWithParameter() throws IOException { 419 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A { public A(String param){} }"); 420 IApi api = convert(classSrc); 421 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 422 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 423 assertEquals(1, sigClass.getConstructors().size()); 424 IConstructor constructor = sigClass.getConstructors().iterator().next(); 425 assertTrue(constructor.getModifiers().contains(Modifier.PUBLIC)); 426 assertEquals(1, constructor.getParameters().size()); 427 IParameter param = constructor.getParameters().get(0); 428 assertEquals("java.lang.String", ((IClassReference)param.getType()).getClassDefinition().getQualifiedName()); 429 } 430 431 @Test 432 public void convertConstructorWithGenericParameter() throws IOException { 433 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A<T> { public A(T param){} }"); 434 IApi api = convert(classSrc); 435 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 436 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 437 assertEquals(1, sigClass.getConstructors().size()); 438 IConstructor constructor = sigClass.getConstructors().iterator().next(); 439 assertTrue(constructor.getModifiers().contains(Modifier.PUBLIC)); 440 assertEquals(1, constructor.getParameters().size()); 441 IParameter param = constructor.getParameters().get(0); 442 ITypeVariableDefinition paramType = ((ITypeVariableReference)param.getType()).getTypeVariableDefinition(); 443 assertEquals("T", paramType.getName()); 444 assertSame(sigClass, paramType.getGenericDeclaration()); 445 } 446 447 @Test 448 public void convertVoidMethod() throws IOException { 449 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A { public void m(){} }"); 450 IApi api = convert(classSrc); 451 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 452 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 453 assertEquals(1, sigClass.getMethods().size()); 454 IMethod method = sigClass.getMethods().iterator().next(); 455 assertTrue(method.getModifiers().contains(Modifier.PUBLIC)); 456 assertEquals("m", method.getName()); 457 assertSame(SigPrimitiveType.VOID_TYPE, method.getReturnType()); 458 } 459 460 @Test 461 public void convertArrayMethod() throws IOException { 462 CompilationUnit classSrc = new CompilationUnit("a.A", "package a; public class A { public String[] m(){ return null; } }"); 463 IApi api = convert(classSrc); 464 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 465 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 466 assertEquals(1, sigClass.getMethods().size()); 467 IMethod method = sigClass.getMethods().iterator().next(); 468 assertTrue(method.getModifiers().contains(Modifier.PUBLIC)); 469 assertEquals("m", method.getName()); 470 IArrayType arrayType = (IArrayType) method.getReturnType(); 471 IClassDefinition clazz = ((IClassReference) arrayType.getComponentType()).getClassDefinition(); 472 assertEquals("java.lang.String", clazz.getQualifiedName()); 473 } 474 475 @Test 476 public void testParameterConformance1() throws IOException { 477 IApi api = convert(new CompilationUnit("test.A", 478 "package test; " + 479 "import java.util.List;" + 480 "public class A<T> {" + 481 " public String[] a;" + 482 " public String[] b;" + 483 " public List<String> c;" + 484 " public List<String> d;" + 485 " public List<T> e;" + 486 " public List<T> f;" + 487 "}" 488 )); 489 IPackage p = ModelUtil.getPackage(api, "test"); 490 assertEquals("test", p.getName()); 491 IClassDefinition c = ModelUtil.getClass(p, "A"); 492 assertEquals("A", c.getName()); 493 assertEquals("test.A", c.getQualifiedName()); 494 495 Map<String, IField> fields = new HashMap<String, IField>(); 496 for(IField f : c.getFields()){ 497 fields.put(f.getName(), f); 498 } 499 assertSame(((IClassReference)((IArrayType)fields.get("a").getType()).getComponentType()).getClassDefinition(), ((IClassReference)((IArrayType)fields.get("b").getType()).getComponentType()).getClassDefinition()); 500 assertSame(((IParameterizedType)fields.get("c").getType()).getRawType().getClassDefinition(), ((IParameterizedType)fields.get("d").getType()).getRawType().getClassDefinition()); 501 assertSame(((IParameterizedType)fields.get("e").getType()).getRawType().getClassDefinition(), ((IParameterizedType)fields.get("f").getType()).getRawType().getClassDefinition()); 502 503 ITypeReference type = fields.get("f").getType(); 504 assertTrue(type instanceof IParameterizedType); 505 ITypeReference typeArgument = ((IParameterizedType)type).getTypeArguments().get(0); 506 ITypeVariableDefinition typeParameter = c.getTypeParameters().get(0); 507 assertSame(((ITypeVariableReference)typeArgument).getTypeVariableDefinition(), typeParameter); 508 } 509 510 @Test 511 public void testParameterConformance2() throws IOException { 512 IApi api = convert(new CompilationUnit("test.A", 513 "package test; " + 514 "public class A<T> {" + 515 " public <T> T foo(T t){return null;}" + 516 "}" 517 )); 518 IPackage p = ModelUtil.getPackage(api, "test"); 519 assertEquals("test", p.getName()); 520 521 IPackage sigPackage = ModelUtil.getPackage(api, "test"); 522 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 523 ITypeVariableDefinition t = sigClass.getTypeParameters().get(0); 524 525 IMethod sigMethod = sigClass.getMethods().iterator().next(); 526 ITypeVariableDefinition t1 = sigMethod.getTypeParameters().get(0); 527 IParameter param = sigMethod.getParameters().get(0); 528 ITypeReference t2 = param.getType(); 529 ITypeReference t3 = sigMethod.getReturnType(); 530 531 assertSame(t1, ((ITypeVariableReference)t2).getTypeVariableDefinition()); 532 assertSame(((ITypeVariableReference)t2).getTypeVariableDefinition(), ((ITypeVariableReference)t3).getTypeVariableDefinition()); 533 534 assertNotSame(t, t1); 535 } 536 537 @Test 538 public void testInnerReferenceToOuterTypeVariable() throws IOException { 539 IApi api = convert(new CompilationUnit("test.A", 540 "package test; " + 541 "public class A<T> {" + 542 " public class B {" + 543 " public T f;" + 544 " } " + 545 "}" 546 )); 547 IPackage p = ModelUtil.getPackage(api, "test"); 548 assertEquals("test", p.getName()); 549 550 IPackage sigPackage = ModelUtil.getPackage(api, "test"); 551 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 552 ITypeVariableDefinition t = sigClass.getTypeParameters().get(0); 553 554 IClassDefinition innerClass = ModelUtil.getClass(sigPackage, "A.B"); 555 IField field = ModelUtil.getField(innerClass, "f"); 556 557 assertSame(t, ((ITypeVariableReference)field.getType()).getTypeVariableDefinition()); 558 } 559 560 561 @Test 562 public void convertTypeVariableMultipleUpperBound() throws IOException { 563 String source = 564 "package a; " + 565 "public class A<T extends Comparable<T> & java.io.Serializable> {}"; 566 IApi api = convert(new CompilationUnit("a.A", source)); 567 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 568 IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A"); 569 570 List<ITypeVariableDefinition> typeParameters = sigClass.getTypeParameters(); 571 assertEquals(1, typeParameters.size()); 572 573 ITypeVariableDefinition typeVariable = typeParameters.get(0); 574 List<ITypeReference> upperBounds = typeVariable.getUpperBounds(); 575 assertEquals(2, upperBounds.size()); 576 ITypeReference firstBound = upperBounds.get(0); 577 ITypeReference secondBound = upperBounds.get(1); 578 579 assertTrue(firstBound instanceof IParameterizedType); 580 581 IParameterizedType parametrizedType = (IParameterizedType)firstBound; 582 583 IClassReference rawType = parametrizedType.getRawType(); 584 assertEquals("Comparable", rawType.getClassDefinition().getName()); 585 assertEquals(1, parametrizedType.getTypeArguments().size()); 586 ITypeVariableReference variable = (ITypeVariableReference) parametrizedType.getTypeArguments().get(0); 587 assertSame(typeVariable, variable.getTypeVariableDefinition()); 588 589 assertTrue(secondBound instanceof IClassReference); 590 IClassReference secondBoundClass = (IClassReference) secondBound; 591 assertEquals("Serializable", secondBoundClass.getClassDefinition().getName()); 592 } 593 594 @Test 595 public void convertPrivateStaticInnerClass0() throws IOException { 596 String source = 597 "package a; " + 598 "public class A { " + 599 " private static class I{}" + 600 " private transient Object i = new I(); " + 601 "}"; 602 IApi api = convert(Visibility.PRIVATE, new CompilationUnit("a.A", source)); 603 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 604 IClassDefinition innerClass = ModelUtil.getClass(sigPackage, "A.I"); 605 assertEquals("A.I", innerClass.getName()); 606 Set<IConstructor> constructors = innerClass.getConstructors(); 607 assertEquals(1, constructors.size()); 608 } 609 610 @Test 611 public void convertPrivateStaticInnerClass1() throws IOException { 612 String source = 613 "package a; " + 614 "public class A {" + 615 " private static class B {" + 616 " public static class C {}" + 617 " }" + 618 "}"; 619 IApi api = convert(new CompilationUnit("a.A", source)); 620 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 621 assertEquals(1, sigPackage.getClasses().size()); 622 } 623 624 /** 625 * Tests whether the first constructor argument is removed. 626 */ 627 @Test 628 public void convertNonStaticInnerClassConstructor0() throws IOException { 629 String source = 630 "package a; " + 631 "public class A {" + 632 " public class B {" + 633 " public B(){}" + 634 " }" + 635 "}"; 636 IApi api = convert(new CompilationUnit("a.A", source)); 637 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 638 IClassDefinition innerClass = ModelUtil.getClass(sigPackage, "A.B"); 639 assertEquals(1, innerClass.getConstructors().size()); 640 Set<IConstructor> constructors = innerClass.getConstructors(); 641 IConstructor first = constructors.iterator().next(); 642 //implicit A.this reference must be removed 643 assertEquals(0, first.getParameters().size()); 644 } 645 646 /** 647 * Tests whether the first constructor argument is removed. 648 */ 649 @Test 650 public void convertNonStaticInnerClassConstructor1() throws IOException { 651 String source = 652 "package a; " + 653 "public class A {" + 654 " public class B {}" + 655 "}"; 656 IApi api = convert(new CompilationUnit("a.A", source)); 657 IPackage sigPackage = ModelUtil.getPackage(api, "a"); 658 IClassDefinition innerClass = ModelUtil.getClass(sigPackage, "A.B"); 659 assertEquals(1, innerClass.getConstructors().size()); 660 Set<IConstructor> constructors = innerClass.getConstructors(); 661 IConstructor first = constructors.iterator().next(); 662 //implicit A.this reference must be removed 663 assertEquals(0, first.getParameters().size()); 664 } 665 666 667 668 } 669