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