KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > util > MethodType


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: MethodType.java,v 1.5 2004/02/16 22:26:45 minchau Exp $
18  */

19
20 package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
21
22 import java.util.Vector JavaDoc;
23
24 /**
25  * @author Jacek Ambroziak
26  * @author Santiago Pericas-Geertsen
27  */

28 public final class MethodType extends Type {
29     private final Type _resultType;
30     private final Vector JavaDoc _argsType;
31     
32     public MethodType(Type resultType) {
33     _argsType = null;
34     _resultType = resultType;
35     }
36
37     public MethodType(Type resultType, Type arg1) {
38     if (arg1 != Type.Void) {
39         _argsType = new Vector JavaDoc();
40         _argsType.addElement(arg1);
41     }
42     else {
43         _argsType = null;
44     }
45     _resultType = resultType;
46     }
47
48     public MethodType(Type resultType, Type arg1, Type arg2) {
49     _argsType = new Vector JavaDoc(2);
50     _argsType.addElement(arg1);
51     _argsType.addElement(arg2);
52     _resultType = resultType;
53     }
54
55     public MethodType(Type resultType, Type arg1, Type arg2, Type arg3) {
56     _argsType = new Vector JavaDoc(3);
57     _argsType.addElement(arg1);
58     _argsType.addElement(arg2);
59     _argsType.addElement(arg3);
60     _resultType = resultType;
61     }
62
63     public MethodType(Type resultType, Vector JavaDoc argsType) {
64     _resultType = resultType;
65     _argsType = argsType.size() > 0 ? argsType : null;
66     }
67
68     public String JavaDoc toString() {
69     StringBuffer JavaDoc result = new StringBuffer JavaDoc("method{");
70     if (_argsType != null) {
71         final int count = _argsType.size();
72         for (int i=0; i<count; i++) {
73         result.append(_argsType.elementAt(i));
74         if (i != (count-1)) result.append(',');
75         }
76     }
77     else {
78         result.append("void");
79     }
80     result.append('}');
81     return result.toString();
82     }
83
84     public String JavaDoc toSignature() {
85     return toSignature("");
86     }
87
88     /**
89      * Returns the signature of this method that results by adding
90      * <code>lastArgSig</code> to the end of the argument list.
91      */

92     public String JavaDoc toSignature(String JavaDoc lastArgSig) {
93     final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
94     buffer.append('(');
95     if (_argsType != null) {
96         final int n = _argsType.size();
97         for (int i = 0; i < n; i++) {
98         buffer.append(((Type)_argsType.elementAt(i)).toSignature());
99         }
100     }
101     return buffer
102         .append(lastArgSig)
103         .append(')')
104         .append(_resultType.toSignature())
105         .toString();
106     }
107
108     public com.sun.org.apache.bcel.internal.generic.Type toJCType() {
109     return null; // should never be called
110
}
111
112     public boolean identicalTo(Type other) {
113     boolean result = false;
114     if (other instanceof MethodType) {
115         final MethodType temp = (MethodType) other;
116         if (_resultType.identicalTo(temp._resultType)) {
117         final int len = argsCount();
118         result = len == temp.argsCount();
119         for (int i = 0; i < len && result; i++) {
120             final Type arg1 = (Type)_argsType.elementAt(i);
121             final Type arg2 = (Type)temp._argsType.elementAt(i);
122             result = arg1.identicalTo(arg2);
123         }
124         }
125     }
126     return result;
127     }
128     
129     public int distanceTo(Type other) {
130     int result = Integer.MAX_VALUE;
131     if (other instanceof MethodType) {
132         final MethodType mtype = (MethodType) other;
133         if (_argsType != null) {
134         final int len = _argsType.size();
135         if (len == mtype._argsType.size()) {
136             result = 0;
137             for (int i = 0; i < len; i++) {
138             Type arg1 = (Type) _argsType.elementAt(i);
139             Type arg2 = (Type) mtype._argsType.elementAt(i);
140             final int temp = arg1.distanceTo(arg2);
141             if (temp == Integer.MAX_VALUE) {
142                 result = temp; // return MAX_VALUE
143
break;
144             }
145             else {
146                 result += arg1.distanceTo(arg2);
147             }
148             }
149         }
150         }
151         else if (mtype._argsType == null) {
152         result = 0; // both methods have no args
153
}
154     }
155     return result;
156     }
157         
158     public Type resultType() {
159     return _resultType;
160     }
161         
162     public Vector JavaDoc argsType() {
163     return _argsType;
164     }
165
166     public int argsCount() {
167     return _argsType == null ? 0 : _argsType.size();
168     }
169 }
170
Popular Tags