001: /*
002:
003: Derby - Class org.apache.derby.impl.services.bytecode.BCMethodDescriptor
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.services.bytecode;
023:
024: import org.apache.derby.iapi.services.classfile.VMDescriptor;
025:
026: /**
027: A method descriptor. Ie. something that describes the
028: type of a method, parameter types and return types.
029: It is not an instance of a method.
030: <BR>
031: This has no generated class specific state.
032: */
033: class BCMethodDescriptor {
034:
035: static final String[] EMPTY = new String[0];
036:
037: private final String[] vmParameterTypes;
038: private final String vmReturnType;
039:
040: private final String vmDescriptor;
041:
042: BCMethodDescriptor(String[] vmParameterTypes, String vmReturnType,
043: BCJava factory) {
044:
045: this .vmParameterTypes = vmParameterTypes;
046: this .vmReturnType = vmReturnType;
047:
048: vmDescriptor = factory.vmType(this );
049: }
050:
051: /*
052: static String get(Expression[] vmParameters, String vmReturnType, BCJava factory) {
053:
054: int count = vmParameters.length;
055: String[] vmParameterTypes;
056: if (count == 0) {
057: vmParameterTypes = BCMethodDescriptor.EMPTY;
058: } else {
059: vmParameterTypes = new String[count];
060: for (int i =0; i < count; i++) {
061: vmParameterTypes[i] = ((BCExpr) vmParameters[i]).vmType();
062: }
063: }
064:
065: return new BCMethodDescriptor(vmParameterTypes, vmReturnType, factory).toString();
066: }
067: */
068: static String get(String[] vmParameterTypes, String vmReturnType,
069: BCJava factory) {
070:
071: return new BCMethodDescriptor(vmParameterTypes, vmReturnType,
072: factory).toString();
073: }
074:
075: /**
076: * builds the JVM method descriptor for this method as
077: * defined in JVM Spec 4.3.3, Method Descriptors.
078: */
079: String buildMethodDescriptor() {
080:
081: int paramCount = vmParameterTypes.length;
082:
083: int approxLength = (30 * (paramCount + 1));
084:
085: StringBuffer methDesc = new StringBuffer(approxLength);
086:
087: methDesc.append(VMDescriptor.C_METHOD);
088:
089: for (int i = 0; i < paramCount; i++) {
090: methDesc.append(vmParameterTypes[i]);
091: }
092:
093: methDesc.append(VMDescriptor.C_ENDMETHOD);
094: methDesc.append(vmReturnType);
095:
096: return methDesc.toString();
097: }
098:
099: public String toString() {
100: return vmDescriptor;
101: }
102:
103: public int hashCode() {
104: return vmParameterTypes.length
105: | (vmReturnType.hashCode() & 0xFFFFFF00);
106: }
107:
108: public boolean equals(Object other) {
109: if (!(other instanceof BCMethodDescriptor))
110: return false;
111:
112: BCMethodDescriptor o = (BCMethodDescriptor) other;
113:
114: if (o.vmParameterTypes.length != vmParameterTypes.length)
115: return false;
116:
117: for (int i = 0; i < vmParameterTypes.length; i++) {
118: if (!vmParameterTypes[i].equals(o.vmParameterTypes[i]))
119: return false;
120: }
121:
122: return vmReturnType.equals(o.vmReturnType);
123: }
124: }
|