jaxws/src/share/classes/com/sun/codemodel/internal/JNarrowedClass.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 8 474761f14bca
child 2678 57cf2a1c1a05
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
474761f14bca Initial load
duke
parents:
diff changeset
     1
/*
474761f14bca Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
474761f14bca Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
474761f14bca Initial load
duke
parents:
diff changeset
     4
 *
474761f14bca Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
474761f14bca Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
474761f14bca Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
474761f14bca Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
474761f14bca Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
474761f14bca Initial load
duke
parents:
diff changeset
    10
 *
474761f14bca Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
474761f14bca Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
474761f14bca Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
474761f14bca Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
474761f14bca Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
474761f14bca Initial load
duke
parents:
diff changeset
    16
 *
474761f14bca Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
474761f14bca Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
474761f14bca Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
474761f14bca Initial load
duke
parents:
diff changeset
    20
 *
474761f14bca Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
474761f14bca Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
474761f14bca Initial load
duke
parents:
diff changeset
    23
 * have any questions.
474761f14bca Initial load
duke
parents:
diff changeset
    24
 */
474761f14bca Initial load
duke
parents:
diff changeset
    25
474761f14bca Initial load
duke
parents:
diff changeset
    26
package com.sun.codemodel.internal;
474761f14bca Initial load
duke
parents:
diff changeset
    27
474761f14bca Initial load
duke
parents:
diff changeset
    28
import java.util.Iterator;
474761f14bca Initial load
duke
parents:
diff changeset
    29
import java.util.List;
474761f14bca Initial load
duke
parents:
diff changeset
    30
import java.util.Collections;
474761f14bca Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
474761f14bca Initial load
duke
parents:
diff changeset
    32
474761f14bca Initial load
duke
parents:
diff changeset
    33
/**
474761f14bca Initial load
duke
parents:
diff changeset
    34
 * Represents X<Y>.
474761f14bca Initial load
duke
parents:
diff changeset
    35
 *
474761f14bca Initial load
duke
parents:
diff changeset
    36
 * TODO: consider separating the decl and the use.
474761f14bca Initial load
duke
parents:
diff changeset
    37
 *
474761f14bca Initial load
duke
parents:
diff changeset
    38
 * @author
474761f14bca Initial load
duke
parents:
diff changeset
    39
 *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
474761f14bca Initial load
duke
parents:
diff changeset
    40
 */
474761f14bca Initial load
duke
parents:
diff changeset
    41
class JNarrowedClass extends JClass {
474761f14bca Initial load
duke
parents:
diff changeset
    42
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    43
     * A generic class with type parameters.
474761f14bca Initial load
duke
parents:
diff changeset
    44
     */
474761f14bca Initial load
duke
parents:
diff changeset
    45
    final JClass basis;
474761f14bca Initial load
duke
parents:
diff changeset
    46
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    47
     * Arguments to those parameters.
474761f14bca Initial load
duke
parents:
diff changeset
    48
     */
474761f14bca Initial load
duke
parents:
diff changeset
    49
    private final List<JClass> args;
474761f14bca Initial load
duke
parents:
diff changeset
    50
474761f14bca Initial load
duke
parents:
diff changeset
    51
    JNarrowedClass(JClass basis, JClass arg) {
474761f14bca Initial load
duke
parents:
diff changeset
    52
        this(basis,Collections.singletonList(arg));
474761f14bca Initial load
duke
parents:
diff changeset
    53
    }
474761f14bca Initial load
duke
parents:
diff changeset
    54
474761f14bca Initial load
duke
parents:
diff changeset
    55
    JNarrowedClass(JClass basis, List<JClass> args) {
474761f14bca Initial load
duke
parents:
diff changeset
    56
        super(basis.owner());
474761f14bca Initial load
duke
parents:
diff changeset
    57
        this.basis = basis;
474761f14bca Initial load
duke
parents:
diff changeset
    58
        assert !(basis instanceof JNarrowedClass);
474761f14bca Initial load
duke
parents:
diff changeset
    59
        this.args = args;
474761f14bca Initial load
duke
parents:
diff changeset
    60
    }
474761f14bca Initial load
duke
parents:
diff changeset
    61
474761f14bca Initial load
duke
parents:
diff changeset
    62
    public JClass narrow( JClass clazz ) {
474761f14bca Initial load
duke
parents:
diff changeset
    63
        List<JClass> newArgs = new ArrayList<JClass>(args);
474761f14bca Initial load
duke
parents:
diff changeset
    64
        newArgs.add(clazz);
474761f14bca Initial load
duke
parents:
diff changeset
    65
        return new JNarrowedClass(basis,newArgs);
474761f14bca Initial load
duke
parents:
diff changeset
    66
    }
474761f14bca Initial load
duke
parents:
diff changeset
    67
474761f14bca Initial load
duke
parents:
diff changeset
    68
    public JClass narrow( JClass... clazz ) {
474761f14bca Initial load
duke
parents:
diff changeset
    69
        List<JClass> newArgs = new ArrayList<JClass>(args);
474761f14bca Initial load
duke
parents:
diff changeset
    70
        for (JClass c : clazz)
474761f14bca Initial load
duke
parents:
diff changeset
    71
            newArgs.add(c);
474761f14bca Initial load
duke
parents:
diff changeset
    72
        return new JNarrowedClass(basis,newArgs);
474761f14bca Initial load
duke
parents:
diff changeset
    73
    }
474761f14bca Initial load
duke
parents:
diff changeset
    74
474761f14bca Initial load
duke
parents:
diff changeset
    75
    public String name() {
474761f14bca Initial load
duke
parents:
diff changeset
    76
        StringBuffer buf = new StringBuffer();
474761f14bca Initial load
duke
parents:
diff changeset
    77
        buf.append(basis.name());
474761f14bca Initial load
duke
parents:
diff changeset
    78
        buf.append('<');
474761f14bca Initial load
duke
parents:
diff changeset
    79
        boolean first = true;
474761f14bca Initial load
duke
parents:
diff changeset
    80
        for (JClass c : args) {
474761f14bca Initial load
duke
parents:
diff changeset
    81
            if(first)
474761f14bca Initial load
duke
parents:
diff changeset
    82
                first = false;
474761f14bca Initial load
duke
parents:
diff changeset
    83
            else
474761f14bca Initial load
duke
parents:
diff changeset
    84
                buf.append(',');
474761f14bca Initial load
duke
parents:
diff changeset
    85
            buf.append(c.name());
474761f14bca Initial load
duke
parents:
diff changeset
    86
        }
474761f14bca Initial load
duke
parents:
diff changeset
    87
        buf.append('>');
474761f14bca Initial load
duke
parents:
diff changeset
    88
        return buf.toString();
474761f14bca Initial load
duke
parents:
diff changeset
    89
    }
474761f14bca Initial load
duke
parents:
diff changeset
    90
474761f14bca Initial load
duke
parents:
diff changeset
    91
    public String fullName() {
474761f14bca Initial load
duke
parents:
diff changeset
    92
        StringBuilder buf = new StringBuilder();
474761f14bca Initial load
duke
parents:
diff changeset
    93
        buf.append(basis.fullName());
474761f14bca Initial load
duke
parents:
diff changeset
    94
        buf.append('<');
474761f14bca Initial load
duke
parents:
diff changeset
    95
        boolean first = true;
474761f14bca Initial load
duke
parents:
diff changeset
    96
        for (JClass c : args) {
474761f14bca Initial load
duke
parents:
diff changeset
    97
            if(first)
474761f14bca Initial load
duke
parents:
diff changeset
    98
                first = false;
474761f14bca Initial load
duke
parents:
diff changeset
    99
            else
474761f14bca Initial load
duke
parents:
diff changeset
   100
                buf.append(',');
474761f14bca Initial load
duke
parents:
diff changeset
   101
            buf.append(c.fullName());
474761f14bca Initial load
duke
parents:
diff changeset
   102
        }
474761f14bca Initial load
duke
parents:
diff changeset
   103
        buf.append('>');
474761f14bca Initial load
duke
parents:
diff changeset
   104
        return buf.toString();
474761f14bca Initial load
duke
parents:
diff changeset
   105
    }
474761f14bca Initial load
duke
parents:
diff changeset
   106
474761f14bca Initial load
duke
parents:
diff changeset
   107
    public String binaryName() {
474761f14bca Initial load
duke
parents:
diff changeset
   108
        StringBuilder buf = new StringBuilder();
474761f14bca Initial load
duke
parents:
diff changeset
   109
        buf.append(basis.binaryName());
474761f14bca Initial load
duke
parents:
diff changeset
   110
        buf.append('<');
474761f14bca Initial load
duke
parents:
diff changeset
   111
        boolean first = true;
474761f14bca Initial load
duke
parents:
diff changeset
   112
        for (JClass c : args) {
474761f14bca Initial load
duke
parents:
diff changeset
   113
            if(first)
474761f14bca Initial load
duke
parents:
diff changeset
   114
                first = false;
474761f14bca Initial load
duke
parents:
diff changeset
   115
            else
474761f14bca Initial load
duke
parents:
diff changeset
   116
                buf.append(',');
474761f14bca Initial load
duke
parents:
diff changeset
   117
            buf.append(c.binaryName());
474761f14bca Initial load
duke
parents:
diff changeset
   118
        }
474761f14bca Initial load
duke
parents:
diff changeset
   119
        buf.append('>');
474761f14bca Initial load
duke
parents:
diff changeset
   120
        return buf.toString();
474761f14bca Initial load
duke
parents:
diff changeset
   121
    }
474761f14bca Initial load
duke
parents:
diff changeset
   122
474761f14bca Initial load
duke
parents:
diff changeset
   123
    public void generate(JFormatter f) {
474761f14bca Initial load
duke
parents:
diff changeset
   124
        f.t(basis).p('<').g(args).p(JFormatter.CLOSE_TYPE_ARGS);
474761f14bca Initial load
duke
parents:
diff changeset
   125
    }
474761f14bca Initial load
duke
parents:
diff changeset
   126
474761f14bca Initial load
duke
parents:
diff changeset
   127
    @Override
474761f14bca Initial load
duke
parents:
diff changeset
   128
    void printLink(JFormatter f) {
474761f14bca Initial load
duke
parents:
diff changeset
   129
        basis.printLink(f);
474761f14bca Initial load
duke
parents:
diff changeset
   130
        f.p("{@code <}");
474761f14bca Initial load
duke
parents:
diff changeset
   131
        boolean first = true;
474761f14bca Initial load
duke
parents:
diff changeset
   132
        for( JClass c : args ) {
474761f14bca Initial load
duke
parents:
diff changeset
   133
            if(first)
474761f14bca Initial load
duke
parents:
diff changeset
   134
                first = false;
474761f14bca Initial load
duke
parents:
diff changeset
   135
            else
474761f14bca Initial load
duke
parents:
diff changeset
   136
                f.p(',');
474761f14bca Initial load
duke
parents:
diff changeset
   137
            c.printLink(f);
474761f14bca Initial load
duke
parents:
diff changeset
   138
        }
474761f14bca Initial load
duke
parents:
diff changeset
   139
        f.p("{@code >}");
474761f14bca Initial load
duke
parents:
diff changeset
   140
    }
474761f14bca Initial load
duke
parents:
diff changeset
   141
474761f14bca Initial load
duke
parents:
diff changeset
   142
    public JPackage _package() {
474761f14bca Initial load
duke
parents:
diff changeset
   143
        return basis._package();
474761f14bca Initial load
duke
parents:
diff changeset
   144
    }
474761f14bca Initial load
duke
parents:
diff changeset
   145
474761f14bca Initial load
duke
parents:
diff changeset
   146
    public JClass _extends() {
474761f14bca Initial load
duke
parents:
diff changeset
   147
        JClass base = basis._extends();
474761f14bca Initial load
duke
parents:
diff changeset
   148
        if(base==null)  return base;
474761f14bca Initial load
duke
parents:
diff changeset
   149
        return base.substituteParams(basis.typeParams(),args);
474761f14bca Initial load
duke
parents:
diff changeset
   150
    }
474761f14bca Initial load
duke
parents:
diff changeset
   151
474761f14bca Initial load
duke
parents:
diff changeset
   152
    public Iterator<JClass> _implements() {
474761f14bca Initial load
duke
parents:
diff changeset
   153
        return new Iterator<JClass>() {
474761f14bca Initial load
duke
parents:
diff changeset
   154
            private final Iterator<JClass> core = basis._implements();
474761f14bca Initial load
duke
parents:
diff changeset
   155
            public void remove() {
474761f14bca Initial load
duke
parents:
diff changeset
   156
                core.remove();
474761f14bca Initial load
duke
parents:
diff changeset
   157
            }
474761f14bca Initial load
duke
parents:
diff changeset
   158
            public JClass next() {
474761f14bca Initial load
duke
parents:
diff changeset
   159
                return core.next().substituteParams(basis.typeParams(),args);
474761f14bca Initial load
duke
parents:
diff changeset
   160
            }
474761f14bca Initial load
duke
parents:
diff changeset
   161
            public boolean hasNext() {
474761f14bca Initial load
duke
parents:
diff changeset
   162
                return core.hasNext();
474761f14bca Initial load
duke
parents:
diff changeset
   163
            }
474761f14bca Initial load
duke
parents:
diff changeset
   164
        };
474761f14bca Initial load
duke
parents:
diff changeset
   165
    }
474761f14bca Initial load
duke
parents:
diff changeset
   166
474761f14bca Initial load
duke
parents:
diff changeset
   167
    public JClass erasure() {
474761f14bca Initial load
duke
parents:
diff changeset
   168
        return basis;
474761f14bca Initial load
duke
parents:
diff changeset
   169
    }
474761f14bca Initial load
duke
parents:
diff changeset
   170
474761f14bca Initial load
duke
parents:
diff changeset
   171
    public boolean isInterface() {
474761f14bca Initial load
duke
parents:
diff changeset
   172
        return basis.isInterface();
474761f14bca Initial load
duke
parents:
diff changeset
   173
    }
474761f14bca Initial load
duke
parents:
diff changeset
   174
474761f14bca Initial load
duke
parents:
diff changeset
   175
    public boolean isAbstract() {
474761f14bca Initial load
duke
parents:
diff changeset
   176
        return basis.isAbstract();
474761f14bca Initial load
duke
parents:
diff changeset
   177
    }
474761f14bca Initial load
duke
parents:
diff changeset
   178
474761f14bca Initial load
duke
parents:
diff changeset
   179
    public boolean isArray() {
474761f14bca Initial load
duke
parents:
diff changeset
   180
        return false;
474761f14bca Initial load
duke
parents:
diff changeset
   181
    }
474761f14bca Initial load
duke
parents:
diff changeset
   182
474761f14bca Initial load
duke
parents:
diff changeset
   183
474761f14bca Initial load
duke
parents:
diff changeset
   184
    //
474761f14bca Initial load
duke
parents:
diff changeset
   185
    // Equality is based on value
474761f14bca Initial load
duke
parents:
diff changeset
   186
    //
474761f14bca Initial load
duke
parents:
diff changeset
   187
474761f14bca Initial load
duke
parents:
diff changeset
   188
    public boolean equals(Object obj) {
474761f14bca Initial load
duke
parents:
diff changeset
   189
        if(!(obj instanceof JNarrowedClass))   return false;
474761f14bca Initial load
duke
parents:
diff changeset
   190
        return fullName().equals(((JClass)obj).fullName());
474761f14bca Initial load
duke
parents:
diff changeset
   191
    }
474761f14bca Initial load
duke
parents:
diff changeset
   192
474761f14bca Initial load
duke
parents:
diff changeset
   193
    public int hashCode() {
474761f14bca Initial load
duke
parents:
diff changeset
   194
        return fullName().hashCode();
474761f14bca Initial load
duke
parents:
diff changeset
   195
    }
474761f14bca Initial load
duke
parents:
diff changeset
   196
474761f14bca Initial load
duke
parents:
diff changeset
   197
    protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings) {
474761f14bca Initial load
duke
parents:
diff changeset
   198
        JClass b = basis.substituteParams(variables,bindings);
474761f14bca Initial load
duke
parents:
diff changeset
   199
        boolean different = b!=basis;
474761f14bca Initial load
duke
parents:
diff changeset
   200
474761f14bca Initial load
duke
parents:
diff changeset
   201
        List<JClass> clazz = new ArrayList<JClass>(args.size());
474761f14bca Initial load
duke
parents:
diff changeset
   202
        for( int i=0; i<clazz.size(); i++ ) {
474761f14bca Initial load
duke
parents:
diff changeset
   203
            JClass c = args.get(i).substituteParams(variables,bindings);
474761f14bca Initial load
duke
parents:
diff changeset
   204
            clazz.set(i,c);
474761f14bca Initial load
duke
parents:
diff changeset
   205
            different |= c != args.get(i);
474761f14bca Initial load
duke
parents:
diff changeset
   206
        }
474761f14bca Initial load
duke
parents:
diff changeset
   207
474761f14bca Initial load
duke
parents:
diff changeset
   208
        if(different)
474761f14bca Initial load
duke
parents:
diff changeset
   209
            return new JNarrowedClass(b,clazz);
474761f14bca Initial load
duke
parents:
diff changeset
   210
        else
474761f14bca Initial load
duke
parents:
diff changeset
   211
            return this;
474761f14bca Initial load
duke
parents:
diff changeset
   212
    }
474761f14bca Initial load
duke
parents:
diff changeset
   213
474761f14bca Initial load
duke
parents:
diff changeset
   214
    public List<JClass> getTypeParameters() {
474761f14bca Initial load
duke
parents:
diff changeset
   215
        return args;
474761f14bca Initial load
duke
parents:
diff changeset
   216
    }
474761f14bca Initial load
duke
parents:
diff changeset
   217
}