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