View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: MethodType.java,v 1.2.4.1 2005/09/05 11:18:05 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
25  
26  import java.util.Vector;
27  
28  /**
29   * @author Jacek Ambroziak
30   * @author Santiago Pericas-Geertsen
31   */
32  public final class MethodType extends Type {
33      private final Type _resultType;
34      private final Vector _argsType;
35  
36      public MethodType(Type resultType) {
37          _argsType = null;
38          _resultType = resultType;
39      }
40  
41      public MethodType(Type resultType, Type arg1) {
42          if (arg1 != Type.Void) {
43              _argsType = new Vector();
44              _argsType.addElement(arg1);
45          }
46          else {
47              _argsType = null;
48          }
49          _resultType = resultType;
50      }
51  
52      public MethodType(Type resultType, Type arg1, Type arg2) {
53          _argsType = new Vector(2);
54          _argsType.addElement(arg1);
55          _argsType.addElement(arg2);
56          _resultType = resultType;
57      }
58  
59      public MethodType(Type resultType, Type arg1, Type arg2, Type arg3) {
60          _argsType = new Vector(3);
61          _argsType.addElement(arg1);
62          _argsType.addElement(arg2);
63          _argsType.addElement(arg3);
64          _resultType = resultType;
65      }
66  
67      public MethodType(Type resultType, Vector argsType) {
68          _resultType = resultType;
69          _argsType = argsType.size() > 0 ? argsType : null;
70      }
71  
72      public String toString() {
73          StringBuffer result = new StringBuffer("method{");
74          if (_argsType != null) {
75              final int count = _argsType.size();
76              for (int i=0; i<count; i++) {
77                  result.append(_argsType.elementAt(i));
78                  if (i != (count-1)) result.append(',');
79              }
80          }
81          else {
82              result.append("void");
83          }
84          result.append('}');
85          return result.toString();
86      }
87  
88      public String toSignature() {
89          return toSignature("");
90      }
91  
92      /**
93       * Returns the signature of this method that results by adding
94       * <code>lastArgSig</code> to the end of the argument list.
95       */
96      public String toSignature(String lastArgSig) {
97          final StringBuffer buffer = new StringBuffer();
98          buffer.append('(');
99          if (_argsType != null) {
100             final int n = _argsType.size();
101             for (int i = 0; i < n; i++) {
102                 buffer.append(((Type)_argsType.elementAt(i)).toSignature());
103             }
104         }
105         return buffer
106             .append(lastArgSig)
107             .append(')')
108             .append(_resultType.toSignature())
109             .toString();
110     }
111 
112     public com.sun.org.apache.bcel.internal.generic.Type toJCType() {
113         return null;    // should never be called
114     }
115 
116     public boolean identicalTo(Type other) {
117         boolean result = false;
118         if (other instanceof MethodType) {
119             final MethodType temp = (MethodType) other;
120             if (_resultType.identicalTo(temp._resultType)) {
121                 final int len = argsCount();
122                 result = len == temp.argsCount();
123                 for (int i = 0; i < len && result; i++) {
124                     final Type arg1 = (Type)_argsType.elementAt(i);
125                     final Type arg2 = (Type)temp._argsType.elementAt(i);
126                     result = arg1.identicalTo(arg2);
127                 }
128             }
129         }
130         return result;
131     }
132 
133     public int distanceTo(Type other) {
134         int result = Integer.MAX_VALUE;
135         if (other instanceof MethodType) {
136             final MethodType mtype = (MethodType) other;
137             if (_argsType != null) {
138                 final int len = _argsType.size();
139                 if (len == mtype._argsType.size()) {
140                     result = 0;
141                     for (int i = 0; i < len; i++) {
142                         Type arg1 = (Type) _argsType.elementAt(i);
143                         Type arg2 = (Type) mtype._argsType.elementAt(i);
144                         final int temp = arg1.distanceTo(arg2);
145                         if (temp == Integer.MAX_VALUE) {
146                             result = temp;  // return MAX_VALUE
147                             break;
148                         }
149                         else {
150                             result += arg1.distanceTo(arg2);
151                         }
152                     }
153                 }
154             }
155             else if (mtype._argsType == null) {
156                 result = 0;   // both methods have no args
157             }
158         }
159         return result;
160     }
161 
162     public Type resultType() {
163         return _resultType;
164     }
165 
166     public Vector argsType() {
167         return _argsType;
168     }
169 
170     public int argsCount() {
171         return _argsType == null ? 0 : _argsType.size();
172     }
173 }