View Javadoc

1   /*
2    Copyright (C) 2007 Richard Gomes
3   
4    This source code is release under the BSD License.
5   
6    This file is part of JQuantLib, a free-software/open-source library
7    for financial quantitative analysts and developers - http://jquantlib.org/
8   
9    JQuantLib is free software: you can redistribute it and/or modify it
10   under the terms of the JQuantLib license.  You should have received a
11   copy of the license along with this program; if not, please email
12   <jquant-devel@lists.sourceforge.net>. The license is also available online at
13   <http://www.jquantlib.org/index.php/LICENSE.TXT>.
14  
15   This program is distributed in the hope that it will be useful, but WITHOUT
16   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   FOR A PARTICULAR PURPOSE.  See the license for more details.
18  
19   JQuantLib is based on QuantLib. http://quantlib.org/
20   When applicable, the original copyright notice follows this notice.
21   */
22  
23  package org.jquantlib.testsuite.lang;
24  
25  import static org.junit.Assert.fail;
26  
27  import org.jquantlib.QL;
28  import org.jquantlib.lang.reflect.TypeToken;
29  import org.junit.Test;
30  
31  /**
32   * @author Richard Gomes
33   */
34  public class TypeTokenTest {
35  
36      public TypeTokenTest() {
37          QL.info("::::: "+this.getClass().getSimpleName()+" :::::");
38      }
39  
40      @Test
41      public void testTypeToken() {
42          final C c = new C();
43          if (c.getClazz() != Double.class) {
44              fail("Object 'c' should be java.lang.Double");
45          }
46  
47          final D d = new D();
48          if (d.getClazz() != Integer.class) {
49              fail("Object 'd' should be java.lang.Integer");
50          }
51      }
52  
53  
54      /**
55       * It's very important to notice that we need to create anonymous classes in order to make these tests pass.
56       * <p>
57       * The reason is that TypeToken and TypeReference make use of Class.getGenericSuperClass() which will
58       * retrieve type information from the current caller instance.
59       */
60      @Test
61      public void testTypeToken2() {
62          final K k1 = new K<java.lang.Double>() {}; // ANONYMOUS INSTANCE!
63          if (k1.getClazz() != Double.class) {
64              fail("Object 'k1' should be java.lang.Double");
65          }
66  
67          final K k2 = new K<java.lang.Integer>() {}; // ANONYMOUS INSTANCE!
68          if (k2.getClazz() != Integer.class) {
69              fail("Object 'k2' should be java.lang.Integer");
70          }
71  
72      }
73  
74      //
75      // inner classes
76      //
77  
78      private class B<T extends Number> {
79          public Class<?> getClazz() {
80              return TypeToken.getClazz(this.getClass());
81          }
82      }
83  
84      private class C extends B<java.lang.Double> {  }
85  
86      private class D extends B<java.lang.Integer> {  }
87  
88      private class K<T extends Number> extends B<T> {  }
89  
90  }