001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.codemodel;
021:
022: import java.util.Iterator;
023: import java.util.List;
024:
025: /**
026: * Type variable used to declare generics.
027: *
028: * @see JGenerifiable
029: * @author
030: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
031: */
032: public final class JTypeVar extends JClass implements JDeclaration {
033:
034: private final String name;
035:
036: private JClass bound;
037:
038: JTypeVar(JCodeModel owner, String _name) {
039: super (owner);
040: this .name = _name;
041: }
042:
043: public String name() {
044: return name;
045: }
046:
047: public String fullName() {
048: return name;
049: }
050:
051: public JPackage _package() {
052: return null;
053: }
054:
055: /**
056: * Adds a bound to this variable.
057: *
058: * @return this
059: */
060: public JTypeVar bound(JClass c) {
061: if (bound != null)
062: throw new IllegalArgumentException(
063: "type variable has an existing class bound "
064: + bound);
065: bound = c;
066: return this ;
067: }
068:
069: /**
070: * Returns the class bound of this variable.
071: *
072: * <p>
073: * If no bound is given, this method returns {@link Object}.
074: */
075: public JClass _extends() {
076: if (bound != null)
077: return bound;
078: else
079: return owner().ref(Object.class);
080: }
081:
082: /**
083: * Returns the interface bounds of this variable.
084: */
085: public Iterator<JClass> _implements () {
086: return bound._implements ();
087: }
088:
089: public boolean isInterface() {
090: return false;
091: }
092:
093: public boolean isAbstract() {
094: return false;
095: }
096:
097: /**
098: * Prints out the declaration of the variable.
099: */
100: public void declare(JFormatter f) {
101: f.id(name);
102: if (bound != null)
103: f.p("extends").g(bound);
104: }
105:
106: protected JClass substituteParams(JTypeVar[] variables,
107: List<JClass> bindings) {
108: for (int i = 0; i < variables.length; i++)
109: if (variables[i] == this )
110: return bindings.get(i);
111: return this ;
112: }
113:
114: public void generate(JFormatter f) {
115: f.id(name);
116: }
117: }
|