Home | History | Annotate | Download | only in converter
      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.assertSame;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import java.io.IOException;
     24 import java.util.Set;
     25 
     26 import org.junit.Test;
     27 
     28 import signature.converter.util.AbstractConvertTest;
     29 import signature.converter.util.CompilationUnit;
     30 import signature.model.IAnnotation;
     31 import signature.model.IAnnotationField;
     32 import signature.model.IApi;
     33 import signature.model.IArrayType;
     34 import signature.model.IClassDefinition;
     35 import signature.model.IClassReference;
     36 import signature.model.IEnumConstant;
     37 import signature.model.IPackage;
     38 import signature.model.IPrimitiveType;
     39 import signature.model.ITypeReference;
     40 import signature.model.Kind;
     41 import signature.model.impl.SigClassReference;
     42 import signature.model.impl.SigPrimitiveType;
     43 import signature.model.util.ModelUtil;
     44 
     45 public abstract class ConvertAnnotationTest extends AbstractConvertTest {
     46 
     47     // Tests whether an annotation declaration element may be annotated with
     48     // the declared annotation.
     49     @Test
     50     public void convertAnnotationDefinition1() throws IOException {
     51         CompilationUnit classSrc = new CompilationUnit("a.ToDo",
     52                 "package a; " +
     53                 "public @interface ToDo {" +
     54                 "    @ToDo(name=\"ToDo\")" +
     55                 "   String name() default \"nobody\";" +
     56                 "}");
     57         IApi api = convert(classSrc);
     58         IPackage sigPackage = ModelUtil.getPackage(api, "a");
     59         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "ToDo");
     60         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
     61         assertEquals(0, sigClass.getConstructors().size());
     62         assertEquals(0, sigClass.getMethods().size());
     63         assertEquals(0, sigClass.getFields().size());
     64         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
     65         assertEquals(1, annotationFields.size());
     66         IAnnotationField field = annotationFields.iterator().next();
     67         assertEquals("name", field.getName());
     68         assertEquals("String", ((IClassReference)field.getType()).getClassDefinition().getName());
     69         assertEquals("nobody", field.getDefaultValue());
     70         Set<IAnnotation> annotations = field.getAnnotations();
     71         assertEquals(1, annotations.size());
     72         IAnnotation annotation = annotations.iterator().next();
     73         assertSame(sigClass, annotation.getType().getClassDefinition());
     74         // TODO TEST add additional assert for annotation.getElements()
     75     }
     76 
     77     // Tests whether an annotation declaration may be annotated with
     78     // the declared annotation.
     79     @Test
     80     public void convertAnnotationDefinition2() throws IOException {
     81         CompilationUnit classSrc = new CompilationUnit("a.ToDo",
     82                 "package a; " +
     83                 "@ToDo\n" +
     84                 "public @interface ToDo {" +
     85                 "   String name() default \"nobody\";" +
     86                 "}");
     87         IApi api = convert(classSrc);
     88         IPackage sigPackage = ModelUtil.getPackage(api, "a");
     89         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "ToDo");
     90         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
     91         assertEquals(0, sigClass.getConstructors().size());
     92         assertEquals(0, sigClass.getMethods().size());
     93         assertEquals(0, sigClass.getFields().size());
     94         Set<IAnnotation> annotations = sigClass.getAnnotations();
     95         assertEquals(1, annotations.size());
     96         IAnnotation annotation = annotations.iterator().next();
     97         assertSame(sigClass, ((SigClassReference)annotation.getType()).getClassDefinition());
     98         assertEquals(0, annotation.getElements().size());
     99     }
    100 
    101     @Test
    102     public void convertAnnotationDefinition3() throws IOException {
    103         CompilationUnit classSrc = new CompilationUnit("a.ToDo",
    104                 "package a; " +
    105                 "public @interface ToDo {" +
    106                 "   String name() default \"nobody\";" +
    107                 "   int num() default 3;" +
    108                 "}");
    109         IApi api = convert(classSrc);
    110         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    111         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "ToDo");
    112         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    113         assertEquals(0, sigClass.getConstructors().size());
    114         assertEquals(0, sigClass.getMethods().size());
    115         assertEquals(0, sigClass.getFields().size());
    116         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    117         assertEquals(2, annotationFields.size());
    118         IAnnotationField name = ModelUtil.getAnnotationField(sigClass, "name");
    119         IAnnotationField num = ModelUtil.getAnnotationField(sigClass, "num");
    120 
    121         assertEquals("name", name.getName());
    122         assertEquals("num", num.getName());
    123 
    124         assertEquals("nobody", name.getDefaultValue());
    125         assertEquals(3, num.getDefaultValue());
    126     }
    127 
    128 
    129     // tests whether default int value has the correct type and defaultValue
    130     @Test
    131     public void testAnnotationDefaultsInt1() throws IOException {
    132         CompilationUnit src = new CompilationUnit("a.A",
    133                 "package a; " +
    134                 "public @interface A {" +
    135                 "    int value() default 1;" +
    136                 "}");
    137         IApi api = convert(src);
    138         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    139         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    140         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    141         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    142         assertEquals(1, annotationFields.size());
    143         IAnnotationField annotationField = annotationFields.iterator().next();
    144         assertEquals("value", annotationField.getName());
    145         assertTrue(annotationField.getType() instanceof IPrimitiveType);
    146         IPrimitiveType annotationFieldType = (IPrimitiveType)annotationField.getType();
    147         assertSame(SigPrimitiveType.INT_TYPE, annotationFieldType);
    148 
    149         assertTrue(annotationField.getDefaultValue() instanceof Integer);
    150         Integer defaultValue = (Integer)annotationField.getDefaultValue();
    151         assertEquals(1, defaultValue.intValue());
    152     }
    153 
    154     // tests whether default int[] value has the correct type and defaultValue
    155     @Test
    156     public void testAnnotationDefaultsInt2() throws IOException {
    157         CompilationUnit src = new CompilationUnit("a.A",
    158                 "package a; " +
    159                 "public @interface A {" +
    160                 "    int[] value() default {};" +
    161                 "}");
    162         IApi api = convert(src);
    163         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    164         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    165         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    166         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    167         assertEquals(1, annotationFields.size());
    168         IAnnotationField field = annotationFields.iterator().next();
    169         assertEquals("value", field.getName());
    170 
    171         assertTrue(field.getType() instanceof IArrayType);
    172         assertTrue(field.getDefaultValue() instanceof Object[]);
    173         assertSame(field.getDefaultValue().getClass(), Object[].class);
    174         Object[] defaultValue = (Object[])field.getDefaultValue();
    175         assertEquals(0, defaultValue.length);
    176     }
    177 
    178     // tests whether default int[] value has the correct type and defaultValue
    179     @Test
    180     public void testAnnotationDefaultsInt3() throws IOException {
    181         CompilationUnit src = new CompilationUnit("a.A",
    182                 "package a; " +
    183                 "public @interface A {" +
    184                 "    int[] value() default {1,2};" +
    185                 "}");
    186         IApi api = convert(src);
    187         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    188         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    189         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    190         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    191         assertEquals(1, annotationFields.size());
    192         IAnnotationField field = annotationFields.iterator().next();
    193         assertEquals("value", field.getName());
    194 
    195         assertTrue(field.getType() instanceof IArrayType);
    196         assertTrue(field.getDefaultValue() instanceof Object[]);
    197         assertSame(field.getDefaultValue().getClass(), Object[].class);
    198         Object[] defaultValue = (Object[])field.getDefaultValue();
    199         assertEquals(2, defaultValue.length);
    200 
    201         Object defaultValue0 = defaultValue[0];
    202         assertTrue(defaultValue0 instanceof Integer);
    203         Integer defaultValue0int = (Integer)defaultValue0;
    204         assertEquals(1, defaultValue0int.intValue());
    205 
    206         Object defaultValue1 = defaultValue[1];
    207         assertTrue(defaultValue1 instanceof Integer);
    208         Integer defaultValue1int = (Integer)defaultValue1;
    209         assertEquals(2, defaultValue1int.intValue());
    210     }
    211 
    212 
    213     // tests whether default double value has the correct type and defaultValue
    214     @Test
    215     public void testAnnotationDefaultsDouble1() throws IOException {
    216         CompilationUnit src = new CompilationUnit("a.A",
    217                 "package a; " +
    218                 "public @interface A {" +
    219                 "    double value() default 1;" +
    220                 "}");
    221         IApi api = convert(src);
    222         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    223         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    224         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    225         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    226         assertEquals(1, annotationFields.size());
    227         IAnnotationField annotationField = annotationFields.iterator().next();
    228         assertEquals("value", annotationField.getName());
    229         assertTrue(annotationField.getType() instanceof IPrimitiveType);
    230         IPrimitiveType annotationFieldType = (IPrimitiveType)annotationField.getType();
    231         assertSame(SigPrimitiveType.DOUBLE_TYPE, annotationFieldType);
    232 
    233 
    234         assertTrue(annotationField.getDefaultValue() instanceof Double);
    235         Double defaultValue = (Double)annotationField.getDefaultValue();
    236         assertEquals(1.0, defaultValue.doubleValue());
    237     }
    238 
    239     // tests whether default int[] value has the correct type and defaultValue
    240     @Test
    241     public void testAnnotationDefaultsDouble2() throws IOException {
    242         CompilationUnit src = new CompilationUnit("a.A",
    243                 "package a; " +
    244                 "public @interface A {" +
    245                 "    double[] value() default {};" +
    246                 "}");
    247         IApi api = convert(src);
    248         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    249         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    250         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    251         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    252         assertEquals(1, annotationFields.size());
    253         IAnnotationField field = annotationFields.iterator().next();
    254         assertEquals("value", field.getName());
    255 
    256         assertTrue(field.getType() instanceof IArrayType);
    257         assertTrue(field.getDefaultValue() instanceof Object[]);
    258         assertSame(field.getDefaultValue().getClass(), Object[].class);
    259         Object[] defaultValue = (Object[])field.getDefaultValue();
    260         assertEquals(0, defaultValue.length);
    261     }
    262 
    263     // tests whether default int[] value has the correct type and defaultValue
    264     @Test
    265     public void testAnnotationDefaultsDouble3() throws IOException {
    266         CompilationUnit src = new CompilationUnit("a.A",
    267                 "package a; " +
    268                 "public @interface A {" +
    269                 "    double[] value() default {1,2.5};" +
    270                 "}");
    271         IApi api = convert(src);
    272         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    273         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    274         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    275         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    276         assertEquals(1, annotationFields.size());
    277         IAnnotationField field = annotationFields.iterator().next();
    278         assertEquals("value", field.getName());
    279 
    280         assertTrue(field.getType() instanceof IArrayType);
    281         assertTrue(field.getDefaultValue() instanceof Object[]);
    282         assertSame(field.getDefaultValue().getClass(), Object[].class);
    283         Object[] defaultValue = (Object[])field.getDefaultValue();
    284         assertEquals(2, defaultValue.length);
    285 
    286         Object defaultValue0 = defaultValue[0];
    287         assertTrue(defaultValue0 instanceof Double);
    288         Double defaultValue0int = (Double)defaultValue0;
    289         assertEquals(1, defaultValue0int.doubleValue());
    290 
    291         Object defaultValue1 = defaultValue[1];
    292         assertTrue(defaultValue1 instanceof Double);
    293         Double defaultValue1int = (Double)defaultValue1;
    294         assertEquals(2.5, defaultValue1int.doubleValue());
    295     }
    296 
    297 
    298     // tests whether default enum value has the correct type
    299     @Test
    300     public void testAnnotationDefaultsEnum1() throws IOException {
    301         CompilationUnit src1 = new CompilationUnit("a.A",
    302                 "package a; " +
    303                 "public @interface A {" +
    304                 "    Kind value() default Kind.TWO;" +
    305                 "}");
    306         CompilationUnit src2 = new CompilationUnit("a.Kind",
    307                 "package a; " +
    308                 "public enum Kind {" +
    309                 "    ONE, TWO, THREE" +
    310                 "}");
    311         IApi api = convert(src1, src2);
    312         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    313         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    314         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    315         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    316         assertEquals(1, annotationFields.size());
    317         IAnnotationField field = annotationFields.iterator().next();
    318         assertEquals("value", field.getName());
    319         assertEquals("Kind", ((IClassReference)field.getType()).getClassDefinition().getName());
    320         assertTrue(field.getDefaultValue() instanceof IEnumConstant);
    321         IEnumConstant defaultValue = (IEnumConstant)field.getDefaultValue();
    322 //        assertEquals(1, defaultValue.getOrdinal());
    323         assertEquals("TWO", defaultValue.getName());
    324 
    325         IClassDefinition enumClass = ModelUtil.getClass(sigPackage, "Kind");
    326         assertTrue(enumClass.getKind() == Kind.ENUM);
    327         IEnumConstant enumTWO = null;
    328         Set<IEnumConstant> enumConstants = enumClass.getEnumConstants();
    329         for(IEnumConstant c : enumConstants){
    330             if("TWO".equals(c.getName())) enumTWO = c;
    331         }
    332         assertSame(enumTWO, defaultValue);
    333     }
    334 
    335     // tests whether default enum value has the correct type
    336     @Test
    337     public void testAnnotationDefaultsEnum2() throws IOException {
    338         CompilationUnit src1 = new CompilationUnit("a.A",
    339                 "package a; " +
    340                 "public @interface A {" +
    341                 "    Kind[] value() default {};" +
    342                 "}");
    343         CompilationUnit src2 = new CompilationUnit("a.Kind",
    344                 "package a; " +
    345                 "public enum Kind {" +
    346                 "    ONE, TWO, THREE" +
    347                 "}");
    348         IApi api = convert(src1, src2);
    349         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    350         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    351         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    352         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    353         assertEquals(1, annotationFields.size());
    354         IAnnotationField field = annotationFields.iterator().next();
    355         assertEquals("value", field.getName());
    356         assertTrue(field.getType() instanceof IArrayType);
    357         assertTrue(field.getDefaultValue() instanceof Object[]);
    358         assertSame(field.getDefaultValue().getClass(), Object[].class);
    359         Object[] defaultValue = (Object[])field.getDefaultValue();
    360         assertEquals(0, defaultValue.length);
    361     }
    362 
    363     // tests whether default enum value has the correct type
    364     @Test
    365     public void testAnnotationDefaultsEnum3() throws IOException {
    366         CompilationUnit src1 = new CompilationUnit("a.A",
    367                 "package a; " +
    368                 "public @interface A {" +
    369                 "    Kind[] value() default {Kind.ONE,Kind.TWO};" +
    370                 "}");
    371         CompilationUnit src2 = new CompilationUnit("a.Kind",
    372                 "package a; " +
    373                 "public enum Kind {" +
    374                 "    ONE, TWO, THREE" +
    375                 "}");
    376         IApi api = convert(src1, src2);
    377         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    378         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    379         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    380 
    381         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    382         assertEquals(1, annotationFields.size());
    383         IAnnotationField field = annotationFields.iterator().next();
    384         assertEquals("value", field.getName());
    385 
    386         assertTrue(field.getType() instanceof IArrayType);
    387         assertTrue(field.getDefaultValue() instanceof Object[]);
    388         assertSame(field.getDefaultValue().getClass(), Object[].class);
    389         Object[] defaultValue = (Object[])field.getDefaultValue();
    390         assertEquals(2, defaultValue.length);
    391 
    392         IClassDefinition enumClass = ModelUtil.getClass(sigPackage, "Kind");
    393         assertTrue(enumClass.getKind() == Kind.ENUM);
    394         IEnumConstant enumONE = null;
    395         IEnumConstant enumTWO = null;
    396         Set<IEnumConstant> enumConstants = enumClass.getEnumConstants();
    397         for(IEnumConstant c : enumConstants){
    398             if("ONE".equals(c.getName())) enumONE = c;
    399             if("TWO".equals(c.getName())) enumTWO = c;
    400         }
    401 
    402         Object defaultValue0 = defaultValue[0];
    403         assertTrue(defaultValue0 instanceof IEnumConstant);
    404         IEnumConstant defaultValue0enum = (IEnumConstant)defaultValue0;
    405 //        assertEquals(0, defaultValue0enum.getOrdinal());
    406         assertEquals("ONE", defaultValue0enum.getName());
    407         assertSame(enumONE, defaultValue0enum);
    408 
    409         Object defaultValue1 = defaultValue[1];
    410         assertTrue(defaultValue1 instanceof IEnumConstant);
    411         IEnumConstant defaultValue1enum = (IEnumConstant)defaultValue1;
    412 //        assertEquals(1, defaultValue1enum.getOrdinal());
    413         assertEquals("TWO", defaultValue1enum.getName());
    414         assertSame(enumTWO, defaultValue1enum);
    415     }
    416 
    417 
    418     @Test
    419     public void testAnnotationDefaultsAnnotation1() throws IOException {
    420         CompilationUnit src1 = new CompilationUnit("a.A",
    421                 "package a; " +
    422                 "public @interface A {" +
    423                 "    AA aa() default @AA;" +
    424                 "}");
    425         CompilationUnit src2 = new CompilationUnit("a.AA",
    426                 "package a; " +
    427                 "public @interface AA {}");
    428         IApi api = convert(src1, src2);
    429         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    430         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    431         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    432         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    433         assertEquals(1, annotationFields.size());
    434         IAnnotationField field = annotationFields.iterator().next();
    435         assertEquals("aa", field.getName());
    436         IClassReference fieldType = (IClassReference)field.getType();
    437         assertEquals("AA", fieldType.getClassDefinition().getName());
    438 
    439         IClassDefinition aaType = ModelUtil.getClass(sigPackage, "AA");
    440         assertTrue(aaType.getKind() == Kind.ANNOTATION);
    441         assertSame(aaType, fieldType.getClassDefinition());
    442 
    443         assertTrue(field.getDefaultValue() instanceof IAnnotation);
    444         IAnnotation defaultValue = (IAnnotation)field.getDefaultValue();
    445         assertEquals(0, defaultValue.getElements().size());
    446         assertSame(aaType, defaultValue.getType().getClassDefinition());
    447     }
    448 
    449     @Test
    450     public void testAnnotationDefaultsAnnotation2() throws IOException {
    451         CompilationUnit src1 = new CompilationUnit("a.A",
    452                 "package a; " +
    453                 "public @interface A {" +
    454                 "    AA[] aa() default {};" +
    455                 "}");
    456         CompilationUnit src2 = new CompilationUnit("a.AA",
    457                 "package a; " +
    458                 "public @interface AA {}");
    459 
    460         IApi api = convert(src1, src2);
    461         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    462         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    463         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    464         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    465         assertEquals(1, annotationFields.size());
    466         IAnnotationField field = annotationFields.iterator().next();
    467         assertEquals("aa", field.getName());
    468 
    469         assertTrue(field.getType() instanceof IArrayType);
    470         assertTrue(field.getDefaultValue() instanceof Object[]);
    471         assertSame(field.getDefaultValue().getClass(), Object[].class);
    472         Object[] defaultValue = (Object[])field.getDefaultValue();
    473         assertEquals(0, defaultValue.length);
    474     }
    475 
    476     @Test
    477     public void testAnnotationDefaultsAnnotation3() throws IOException {
    478         CompilationUnit src1 = new CompilationUnit("a.A",
    479                 "package a; " +
    480                 "public @interface A {" +
    481                 "    AA[] aa() default {@AA,@AA};" +
    482                 "}");
    483         CompilationUnit src2 = new CompilationUnit("a.AA",
    484                 "package a; " +
    485                 "public @interface AA {}");
    486 
    487         IApi api = convert(src1, src2);
    488         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    489         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    490         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    491         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    492         assertEquals(1, annotationFields.size());
    493         IAnnotationField field = annotationFields.iterator().next();
    494         assertEquals("aa", field.getName());
    495 
    496         assertTrue(field.getType() instanceof IArrayType);
    497         assertTrue(field.getDefaultValue() instanceof Object[]);
    498         assertSame(field.getDefaultValue().getClass(), Object[].class);
    499         Object[] defaultValue = (Object[])field.getDefaultValue();
    500         assertEquals(2, defaultValue.length);
    501 
    502         IClassDefinition aaType = ModelUtil.getClass(sigPackage, "AA");
    503         assertTrue(aaType.getKind() == Kind.ANNOTATION);
    504 
    505         Object defaultValue0 = defaultValue[0];
    506         assertTrue(defaultValue0 instanceof IAnnotation);
    507         IAnnotation defaultValue0ann = (IAnnotation)defaultValue0;
    508         assertSame(aaType, defaultValue0ann.getType().getClassDefinition());
    509         assertEquals(0, defaultValue0ann.getElements().size());
    510 
    511         Object defaultValue1 = defaultValue[1];
    512         assertTrue(defaultValue1 instanceof IAnnotation);
    513         IAnnotation defaultValue1ann = (IAnnotation)defaultValue1;
    514         assertSame(aaType, defaultValue1ann.getType().getClassDefinition());
    515         assertEquals(0, defaultValue1ann.getElements().size());
    516     }
    517 
    518 
    519 
    520     @Test
    521     public void testAnnotationDefaultsString1() throws IOException {
    522         CompilationUnit src = new CompilationUnit("a.A",
    523                 "package a;" +
    524                 "public @interface A {" +
    525                 "    String str() default \"xxx\";" +
    526                 "}");
    527         IApi api = convert(src);
    528         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    529         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    530         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    531         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    532         assertEquals(1, annotationFields.size());
    533         IAnnotationField field = annotationFields.iterator().next();
    534         assertEquals("str", field.getName());
    535 
    536         assertTrue(field.getType() instanceof IClassReference);
    537         assertEquals("String", ((IClassReference)field.getType()).getClassDefinition().getName());
    538 
    539         assertTrue(field.getDefaultValue() instanceof String);
    540         String defaultValue = (String)field.getDefaultValue();
    541         assertEquals("xxx", defaultValue);
    542     }
    543 
    544     @Test
    545     public void testAnnotationDefaultsString2() throws IOException {
    546         CompilationUnit src = new CompilationUnit("a.A",
    547                 "package a;" +
    548                 "public @interface A {" +
    549                 "    String[] str() default {};" +
    550                 "}");
    551         IApi api = convert(src);
    552         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    553         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    554         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    555         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    556         assertEquals(1, annotationFields.size());
    557         IAnnotationField field = annotationFields.iterator().next();
    558         assertEquals("str", field.getName());
    559 
    560         assertTrue(field.getType() instanceof IArrayType);
    561         Object defaultValue = field.getDefaultValue();
    562         assertTrue(defaultValue instanceof Object[]);
    563         assertSame(defaultValue.getClass(), Object[].class);
    564         assertEquals(0, ((Object[])defaultValue).length);
    565     }
    566 
    567     @Test
    568     public void testAnnotationDefaultsString3() throws IOException {
    569         CompilationUnit src = new CompilationUnit("a.A",
    570                 "package a;" +
    571                 "public @interface A {" +
    572                 "    String[] str() default {\"ONE\", \"TWO\"};" +
    573                 "}");
    574         IApi api = convert(src);
    575         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    576         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
    577         assertTrue(sigClass.getKind() == Kind.ANNOTATION);
    578         Set<IAnnotationField> annotationFields = sigClass.getAnnotationFields();
    579         assertEquals(1, annotationFields.size());
    580         IAnnotationField field = annotationFields.iterator().next();
    581         assertEquals("str", field.getName());
    582 
    583         assertTrue(field.getType() instanceof IArrayType);
    584         assertTrue(field.getDefaultValue() instanceof Object[]);
    585         assertSame(field.getDefaultValue().getClass(), Object[].class);
    586         Object[] defaultValue = (Object[])field.getDefaultValue();
    587         assertEquals(2, defaultValue.length);
    588 
    589         Object defaultValue0 = defaultValue[0];
    590         assertTrue(defaultValue0 instanceof String);
    591         String defaultValue0str = (String)defaultValue0;
    592         assertEquals("ONE", defaultValue0str);
    593 
    594         Object defaultValue1 = defaultValue[1];
    595         assertTrue(defaultValue1 instanceof String);
    596         String defaultValue1str = (String)defaultValue1;
    597         assertEquals("TWO", defaultValue1str);
    598     }
    599 
    600     @Test
    601     public void testAnnotationDefaultsClass1() throws IOException {
    602         CompilationUnit src = new CompilationUnit("a.A",
    603                 "package a;" +
    604                 "public @interface A {" +
    605                 "    Class cc() default A.class;" +
    606                 "}");
    607         IApi api = convert(src);
    608         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    609         IClassDefinition aType = ModelUtil.getClass(sigPackage, "A");
    610         assertTrue(aType.getKind() == Kind.ANNOTATION);
    611         Set<IAnnotationField> annotationFields = aType.getAnnotationFields();
    612         assertEquals(1, annotationFields.size());
    613         IAnnotationField field = annotationFields.iterator().next();
    614         assertEquals("cc", field.getName());
    615 
    616         assertTrue(field.getType() instanceof IClassReference);
    617         assertEquals("Class", ((IClassReference)field.getType()).getClassDefinition().getName());
    618         Object defaultValue = field.getDefaultValue();
    619         assertSame(aType, ((IClassReference)defaultValue).getClassDefinition());
    620     }
    621 
    622     @Test
    623     public void testAnnotationDefaultsClass2() throws IOException {
    624         CompilationUnit src = new CompilationUnit("a.A",
    625                 "package a;" +
    626                 "public @interface A {" +
    627                 "    Class cc() default void.class;" +
    628                 "}");
    629         IApi api = convert(src);
    630         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    631         IClassDefinition aType = ModelUtil.getClass(sigPackage, "A");
    632         assertTrue(aType.getKind() == Kind.ANNOTATION);
    633         Set<IAnnotationField> annotationFields = aType.getAnnotationFields();
    634         assertEquals(1, annotationFields.size());
    635         IAnnotationField field = annotationFields.iterator().next();
    636         assertEquals("cc", field.getName());
    637 
    638         assertTrue(field.getType() instanceof IClassReference);
    639         assertEquals("Class", ((IClassReference)field.getType()).getClassDefinition().getName());
    640 
    641         Object defaultValue = field.getDefaultValue();
    642         assertTrue(defaultValue instanceof IPrimitiveType);
    643         assertEquals("void", ((IPrimitiveType)defaultValue).getName());
    644         assertSame(SigPrimitiveType.VOID_TYPE, defaultValue);
    645     }
    646 
    647     @Test
    648     public void testAnnotationDefaultsClass3() throws IOException {
    649         CompilationUnit src = new CompilationUnit("a.A",
    650                 "package a;" +
    651                 "public @interface A {" +
    652                 "    Class[] cc() default {};" +
    653                 "}");
    654         IApi api = convert(src);
    655         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    656         IClassDefinition aType = ModelUtil.getClass(sigPackage, "A");
    657         assertTrue(aType.getKind() == Kind.ANNOTATION);
    658         Set<IAnnotationField> annotationFields = aType.getAnnotationFields();
    659         assertEquals(1, annotationFields.size());
    660         IAnnotationField field = annotationFields.iterator().next();
    661         assertEquals("cc", field.getName());
    662 
    663         assertTrue(field.getDefaultValue() instanceof Object[]);
    664         assertSame(field.getDefaultValue().getClass(), Object[].class);
    665         Object[] defaultValue = (Object[])field.getDefaultValue();
    666         assertEquals(0, defaultValue.length);
    667     }
    668 
    669     @Test
    670     public void testAnnotationDefaultsClass4() throws IOException {
    671         CompilationUnit src = new CompilationUnit("a.A",
    672                 "package a;" +
    673                 "public @interface A {" +
    674                 "    Class[] cc() default {A.class, void.class};" +
    675                 "}");
    676         IApi api = convert(src);
    677         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    678         IClassDefinition aType = ModelUtil.getClass(sigPackage, "A");
    679         assertTrue(aType.getKind() == Kind.ANNOTATION);
    680         Set<IAnnotationField> annotationFields = aType.getAnnotationFields();
    681         assertEquals(1, annotationFields.size());
    682         IAnnotationField field = annotationFields.iterator().next();
    683         assertEquals("cc", field.getName());
    684 
    685         assertTrue(field.getDefaultValue() instanceof Object[]);
    686         assertSame(field.getDefaultValue().getClass(), Object[].class);
    687         Object[] defaultValue = (Object[])field.getDefaultValue();
    688         assertEquals(2, defaultValue.length);
    689 
    690         Object defaultValue0 = defaultValue[0];
    691         assertTrue(defaultValue0 instanceof ITypeReference);
    692         ITypeReference defaultValue0type = (ITypeReference)defaultValue0;
    693         assertSame(aType, ((IClassReference)defaultValue0type).getClassDefinition());
    694 
    695         Object defaultValue1 = defaultValue[1];
    696         assertTrue(defaultValue1 instanceof ITypeReference);
    697         assertTrue(defaultValue1 instanceof IPrimitiveType);
    698         IPrimitiveType defaultValue1type = (IPrimitiveType)defaultValue1;
    699         assertEquals("void", defaultValue1type.getName());
    700         assertSame(SigPrimitiveType.VOID_TYPE, defaultValue1type);
    701     }
    702 
    703     @Test
    704     public void testAnnotationRetentionRuntime() throws IOException {
    705         CompilationUnit retention = new CompilationUnit("java.lang.annotation.Retention",
    706                 "package java.lang.annotation; " +
    707                 "@Retention(RetentionPolicy.RUNTIME) " +
    708                 //"@Target(ElementType.ANNOTATION_TYPE) " +
    709                 "public @interface Retention { " +
    710                 "   RetentionPolicy value() default RetentionPolicy.CLASS; " +
    711                 "}");
    712         CompilationUnit retentionPolicy = new CompilationUnit("java.lang.annotation.RetentionPolicy",
    713                 "package java.lang.annotation; " +
    714                 "public enum RetentionPolicy { " +
    715                 "    SOURCE," +
    716                 "    CLASS," +
    717                 "    RUNTIME" +
    718                 "}");
    719         CompilationUnit anno = new CompilationUnit("a.A",
    720                 "package a;" +
    721                 "@java.lang.annotation.Retention(value=java.lang.annotation.RetentionPolicy.SOURCE)" +
    722                 "public @interface A{}");
    723         CompilationUnit cla = new CompilationUnit("a.B",
    724                 "package a;" +
    725                 "@A public class B{}");
    726         IApi api = convert(anno, cla, retention, retentionPolicy);
    727         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    728         IClassDefinition aType = ModelUtil.getClass(sigPackage, "B");
    729         assertTrue(aType.getAnnotations().isEmpty());
    730     }
    731 
    732     @Test
    733     public void testAnnotationRetentionClass() throws IOException {
    734         CompilationUnit retention = new CompilationUnit("java.lang.annotation.Retention",
    735                 "package java.lang.annotation; " +
    736                 "@Retention(RetentionPolicy.RUNTIME) " +
    737                 //"@Target(ElementType.ANNOTATION_TYPE) " +
    738                 "public @interface Retention { " +
    739                 "   RetentionPolicy value() default RetentionPolicy.CLASS; " +
    740                 "}");
    741         CompilationUnit retentionPolicy = new CompilationUnit("java.lang.annotation.RetentionPolicy",
    742                 "package java.lang.annotation; " +
    743                 "public enum RetentionPolicy { " +
    744                 "    SOURCE," +
    745                 "    CLASS," +
    746                 "    RUNTIME" +
    747                 "}");
    748         CompilationUnit anno = new CompilationUnit("a.A",
    749                 "package a;" +
    750                 "@java.lang.annotation.Retention(value=java.lang.annotation.RetentionPolicy.CLASS)" +
    751                 "public @interface A{}");
    752         CompilationUnit cla = new CompilationUnit("a.B",
    753                 "package a;" +
    754                 "@A public class B{}");
    755         IApi api = convert(anno, cla, retention, retentionPolicy);
    756         IPackage sigPackage = ModelUtil.getPackage(api, "a");
    757         IClassDefinition aType = ModelUtil.getClass(sigPackage, "B");
    758         assertEquals(1, aType.getAnnotations().size());
    759     }
    760 
    761 }
    762