001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.codemodel.internal;
027:
028: import java.util.Iterator;
029: import java.util.List;
030:
031: /**
032: * Represents a wildcard type like "? extends Foo".
033: *
034: * <p>
035: * Instances of this class can be obtained from {@link JClass#wildcard()}
036: *
037: * TODO: extend this to cover "? super Integer".
038: *
039: * <p>
040: * Our modeling of types are starting to look really ugly.
041: * ideally it should have been done somewhat like APT,
042: * but it's too late now.
043: *
044: * @author Kohsuke Kawaguchi
045: */
046: final class JTypeWildcard extends JClass {
047:
048: private final JClass bound;
049:
050: JTypeWildcard(JClass bound) {
051: super (bound.owner());
052: this .bound = bound;
053: }
054:
055: public String name() {
056: return "? extends " + bound.name();
057: }
058:
059: public String fullName() {
060: return "? extends " + bound.fullName();
061: }
062:
063: public JPackage _package() {
064: return null;
065: }
066:
067: /**
068: * Returns the class bound of this variable.
069: *
070: * <p>
071: * If no bound is given, this method returns {@link Object}.
072: */
073: public JClass _extends() {
074: if (bound != null)
075: return bound;
076: else
077: return owner().ref(Object.class);
078: }
079:
080: /**
081: * Returns the interface bounds of this variable.
082: */
083: public Iterator<JClass> _implements () {
084: return bound._implements ();
085: }
086:
087: public boolean isInterface() {
088: return false;
089: }
090:
091: public boolean isAbstract() {
092: return false;
093: }
094:
095: protected JClass substituteParams(JTypeVar[] variables,
096: List<JClass> bindings) {
097: JClass nb = bound.substituteParams(variables, bindings);
098: if (nb == bound)
099: return this ;
100: else
101: return new JTypeWildcard(nb);
102: }
103:
104: public void generate(JFormatter f) {
105: if (bound._extends() == null)
106: f.p("?"); // instead of "? extends Object"
107: else
108: f.p("? extends").g(bound);
109: }
110: }
|