jaxws/src/share/classes/com/sun/codemodel/internal/JNarrowedClass.java
changeset 8 474761f14bca
child 2678 57cf2a1c1a05
equal deleted inserted replaced
0:fd16c54261b3 8:474761f14bca
       
     1 /*
       
     2  * Copyright 2006 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.codemodel.internal;
       
    27 
       
    28 import java.util.Iterator;
       
    29 import java.util.List;
       
    30 import java.util.Collections;
       
    31 import java.util.ArrayList;
       
    32 
       
    33 /**
       
    34  * Represents X<Y>.
       
    35  *
       
    36  * TODO: consider separating the decl and the use.
       
    37  *
       
    38  * @author
       
    39  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
       
    40  */
       
    41 class JNarrowedClass extends JClass {
       
    42     /**
       
    43      * A generic class with type parameters.
       
    44      */
       
    45     final JClass basis;
       
    46     /**
       
    47      * Arguments to those parameters.
       
    48      */
       
    49     private final List<JClass> args;
       
    50 
       
    51     JNarrowedClass(JClass basis, JClass arg) {
       
    52         this(basis,Collections.singletonList(arg));
       
    53     }
       
    54 
       
    55     JNarrowedClass(JClass basis, List<JClass> args) {
       
    56         super(basis.owner());
       
    57         this.basis = basis;
       
    58         assert !(basis instanceof JNarrowedClass);
       
    59         this.args = args;
       
    60     }
       
    61 
       
    62     public JClass narrow( JClass clazz ) {
       
    63         List<JClass> newArgs = new ArrayList<JClass>(args);
       
    64         newArgs.add(clazz);
       
    65         return new JNarrowedClass(basis,newArgs);
       
    66     }
       
    67 
       
    68     public JClass narrow( JClass... clazz ) {
       
    69         List<JClass> newArgs = new ArrayList<JClass>(args);
       
    70         for (JClass c : clazz)
       
    71             newArgs.add(c);
       
    72         return new JNarrowedClass(basis,newArgs);
       
    73     }
       
    74 
       
    75     public String name() {
       
    76         StringBuffer buf = new StringBuffer();
       
    77         buf.append(basis.name());
       
    78         buf.append('<');
       
    79         boolean first = true;
       
    80         for (JClass c : args) {
       
    81             if(first)
       
    82                 first = false;
       
    83             else
       
    84                 buf.append(',');
       
    85             buf.append(c.name());
       
    86         }
       
    87         buf.append('>');
       
    88         return buf.toString();
       
    89     }
       
    90 
       
    91     public String fullName() {
       
    92         StringBuilder buf = new StringBuilder();
       
    93         buf.append(basis.fullName());
       
    94         buf.append('<');
       
    95         boolean first = true;
       
    96         for (JClass c : args) {
       
    97             if(first)
       
    98                 first = false;
       
    99             else
       
   100                 buf.append(',');
       
   101             buf.append(c.fullName());
       
   102         }
       
   103         buf.append('>');
       
   104         return buf.toString();
       
   105     }
       
   106 
       
   107     public String binaryName() {
       
   108         StringBuilder buf = new StringBuilder();
       
   109         buf.append(basis.binaryName());
       
   110         buf.append('<');
       
   111         boolean first = true;
       
   112         for (JClass c : args) {
       
   113             if(first)
       
   114                 first = false;
       
   115             else
       
   116                 buf.append(',');
       
   117             buf.append(c.binaryName());
       
   118         }
       
   119         buf.append('>');
       
   120         return buf.toString();
       
   121     }
       
   122 
       
   123     public void generate(JFormatter f) {
       
   124         f.t(basis).p('<').g(args).p(JFormatter.CLOSE_TYPE_ARGS);
       
   125     }
       
   126 
       
   127     @Override
       
   128     void printLink(JFormatter f) {
       
   129         basis.printLink(f);
       
   130         f.p("{@code <}");
       
   131         boolean first = true;
       
   132         for( JClass c : args ) {
       
   133             if(first)
       
   134                 first = false;
       
   135             else
       
   136                 f.p(',');
       
   137             c.printLink(f);
       
   138         }
       
   139         f.p("{@code >}");
       
   140     }
       
   141 
       
   142     public JPackage _package() {
       
   143         return basis._package();
       
   144     }
       
   145 
       
   146     public JClass _extends() {
       
   147         JClass base = basis._extends();
       
   148         if(base==null)  return base;
       
   149         return base.substituteParams(basis.typeParams(),args);
       
   150     }
       
   151 
       
   152     public Iterator<JClass> _implements() {
       
   153         return new Iterator<JClass>() {
       
   154             private final Iterator<JClass> core = basis._implements();
       
   155             public void remove() {
       
   156                 core.remove();
       
   157             }
       
   158             public JClass next() {
       
   159                 return core.next().substituteParams(basis.typeParams(),args);
       
   160             }
       
   161             public boolean hasNext() {
       
   162                 return core.hasNext();
       
   163             }
       
   164         };
       
   165     }
       
   166 
       
   167     public JClass erasure() {
       
   168         return basis;
       
   169     }
       
   170 
       
   171     public boolean isInterface() {
       
   172         return basis.isInterface();
       
   173     }
       
   174 
       
   175     public boolean isAbstract() {
       
   176         return basis.isAbstract();
       
   177     }
       
   178 
       
   179     public boolean isArray() {
       
   180         return false;
       
   181     }
       
   182 
       
   183 
       
   184     //
       
   185     // Equality is based on value
       
   186     //
       
   187 
       
   188     public boolean equals(Object obj) {
       
   189         if(!(obj instanceof JNarrowedClass))   return false;
       
   190         return fullName().equals(((JClass)obj).fullName());
       
   191     }
       
   192 
       
   193     public int hashCode() {
       
   194         return fullName().hashCode();
       
   195     }
       
   196 
       
   197     protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings) {
       
   198         JClass b = basis.substituteParams(variables,bindings);
       
   199         boolean different = b!=basis;
       
   200 
       
   201         List<JClass> clazz = new ArrayList<JClass>(args.size());
       
   202         for( int i=0; i<clazz.size(); i++ ) {
       
   203             JClass c = args.get(i).substituteParams(variables,bindings);
       
   204             clazz.set(i,c);
       
   205             different |= c != args.get(i);
       
   206         }
       
   207 
       
   208         if(different)
       
   209             return new JNarrowedClass(b,clazz);
       
   210         else
       
   211             return this;
       
   212     }
       
   213 
       
   214     public List<JClass> getTypeParameters() {
       
   215         return args;
       
   216     }
       
   217 }