• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.*;
20 
21 import java.io.IOException;
22 import java.lang.reflect.Field;
23 import java.lang.reflect.ParameterizedType;
24 import org.junit.Test;
25 
26 import signature.converter.util.AbstractConvertTest;
27 import signature.converter.util.CompilationUnit;
28 import signature.model.IApi;
29 import signature.model.IClassDefinition;
30 import signature.model.IField;
31 import signature.model.IPackage;
32 import signature.model.IParameterizedType;
33 import signature.model.ITypeReference;
34 import signature.model.util.ModelUtil;
35 
36 public abstract class ConvertParameterizedTypeTest extends AbstractConvertTest {
37     /**
38      * [L, a, /, A, $, B, <, L, j, a, v, a, /, l, a, n, g, /, I, n, t, e, g, e, r, ;, >, ;]
39      * '$' - separated
40      * @throws IOException
41      */
42     @Test
convertParameterizedType()43     public void convertParameterizedType() throws IOException {
44         String source =
45         "package a; " +
46         "public class A{" +
47         "  public class B<T> {} " +
48         "  public A.B<Integer> f; "+
49         "}";
50         IApi api = convert(new CompilationUnit("a.A", source));
51         IPackage sigPackage = ModelUtil.getPackage(api, "a");
52         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A");
53         IField field = ModelUtil.getField(sigClass, "f");
54 
55         ITypeReference type = field.getType();
56         assertTrue(type instanceof IParameterizedType);
57 
58         IParameterizedType parametrizedType = (IParameterizedType)type;
59         ITypeReference ownerType = parametrizedType.getOwnerType();
60         assertNotNull(ownerType);
61     }
62 
63     @Test
convertWildcardLowerBound()64     public void convertWildcardLowerBound() throws IOException {
65         String clazz =
66         "package a; " +
67         "public final class A<T> implements I<T>{ " +
68         " abstract class Super{} " +
69         " final class Sub extends Super implements I<T>{} " +
70         "}";
71         String interfaze =
72             "package a; " +
73             "public interface I <T>{}";
74         IApi api = convert(Visibility.PRIVATE, new CompilationUnit("a.A", clazz),new CompilationUnit("a.I", interfaze));
75         IPackage sigPackage = ModelUtil.getPackage(api, "a");
76         IClassDefinition sigClass = ModelUtil.getClass(sigPackage, "A.Sub");
77         System.out.println(sigClass);
78     }
79 
80     public class A{
81       public class B<T> {}
82       public A.B<Integer> f;
83     }
84 
85     @Test
reflectionTest0()86     public void reflectionTest0() throws SecurityException, NoSuchFieldException{
87         Field field = A.class.getDeclaredField("f");
88         ParameterizedType paramType = (ParameterizedType)field.getGenericType();
89         assertNotNull(paramType.getOwnerType());
90     }
91 
92     public static class C<T>{}
93 
94     ConvertParameterizedTypeTest.C<String> f;
95 
96       @Test
reflectionTest1()97       public void reflectionTest1() throws SecurityException, NoSuchFieldException{
98           Field field = ConvertParameterizedTypeTest.class.getDeclaredField("f");
99           ParameterizedType paramType = (ParameterizedType)field.getGenericType();
100           assertNotNull(paramType.getOwnerType());
101       }
102 
103       public static class E<T>{
104           static class F<Q>{}
105           E.F<String> f;
106       }
107 
108 
109      @Test
reflectionTest2()110     public void reflectionTest2() throws SecurityException, NoSuchFieldException {
111         Field field = E.class.getDeclaredField("f");
112         ParameterizedType paramType = (ParameterizedType) field.getGenericType();
113         assertNotNull(paramType.getOwnerType());
114     }
115 }
116