View Javadoc
1   /*
2    * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.codemodel.internal;
27  
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /**
32   * Represents a wildcard type like "? extends Foo".
33   *
34   * <p>
35   * Instances of this class can be obtained from {@link JClass#wildcard()}
36   *
37   * TODO: extend this to cover "? super Integer".
38   *
39   * <p>
40   * Our modeling of types are starting to look really ugly.
41   * ideally it should have been done somewhat like APT,
42   * but it's too late now.
43   *
44   * @author Kohsuke Kawaguchi
45   */
46  final class JTypeWildcard extends JClass {
47  
48      private final JClass bound;
49  
50      JTypeWildcard(JClass bound) {
51          super(bound.owner());
52          this.bound = bound;
53      }
54  
55      public String name() {
56          return "? extends "+bound.name();
57      }
58  
59      public String fullName() {
60          return "? extends "+bound.fullName();
61      }
62  
63      public JPackage _package() {
64          return null;
65      }
66  
67      /**
68       * Returns the class bound of this variable.
69       *
70       * <p>
71       * If no bound is given, this method returns {@link Object}.
72       */
73      public JClass _extends() {
74          if(bound!=null)
75              return bound;
76          else
77              return owner().ref(Object.class);
78      }
79  
80      /**
81       * Returns the interface bounds of this variable.
82       */
83      public Iterator<JClass> _implements() {
84          return bound._implements();
85      }
86  
87      public boolean isInterface() {
88          return false;
89      }
90  
91      public boolean isAbstract() {
92          return false;
93      }
94  
95      protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings) {
96          JClass nb = bound.substituteParams(variables,bindings);
97          if(nb==bound)
98              return this;
99          else
100             return new JTypeWildcard(nb);
101     }
102 
103     public void generate(JFormatter f) {
104         if(bound._extends()==null)
105             f.p("?");   // instead of "? extends Object"
106         else
107             f.p("? extends").g(bound);
108     }
109 }