001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gbean;
017:
018: import java.lang.reflect.Method;
019: import java.util.List;
020:
021: /**
022: * This is a key class based on a MBean operation name and parameters.
023: *
024: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
025: */
026: public final class GOperationSignature {
027: private final static String[] NO_TYPES = new String[0];
028: private final String name;
029: private final String[] argumentTypes;
030:
031: public GOperationSignature(Method method) {
032: name = method.getName();
033: Class[] parameters = method.getParameterTypes();
034: argumentTypes = new String[parameters.length];
035: for (int i = 0; i < parameters.length; i++) {
036: argumentTypes[i] = parameters[i].getName();
037: }
038: }
039:
040: public GOperationSignature(String name, String[] argumentTypes) {
041: this .name = name;
042: if (argumentTypes != null) {
043: this .argumentTypes = argumentTypes;
044: } else {
045: this .argumentTypes = NO_TYPES;
046: }
047: }
048:
049: public GOperationSignature(String name, List argumentTypes) {
050: this .name = name;
051: if (argumentTypes != null) {
052: this .argumentTypes = new String[argumentTypes.size()];
053: for (int i = 0; i < argumentTypes.size(); i++) {
054: this .argumentTypes[i] = (String) argumentTypes.get(i);
055: }
056: } else {
057: this .argumentTypes = NO_TYPES;
058: }
059: }
060:
061: public boolean equals(Object object) {
062: if (!(object instanceof GOperationSignature)) {
063: return false;
064: }
065:
066: // match names
067: GOperationSignature methodKey = (GOperationSignature) object;
068: if (!methodKey.name.equals(name)) {
069: return false;
070: }
071:
072: // match arg length
073: int length = methodKey.argumentTypes.length;
074: if (length != argumentTypes.length) {
075: return false;
076: }
077:
078: // match each arg
079: for (int i = 0; i < length; i++) {
080: if (!methodKey.argumentTypes[i].equals(argumentTypes[i])) {
081: return false;
082: }
083: }
084: return true;
085: }
086:
087: public int hashCode() {
088: int result = 17;
089: result = 37 * result + name.hashCode();
090: for (int i = 0; i < argumentTypes.length; i++) {
091: result = 37 * result + argumentTypes[i].hashCode();
092: }
093: return result;
094: }
095:
096: public String toString() {
097: StringBuffer buffer = new StringBuffer(name).append("(");
098: for (int i = 0; i < argumentTypes.length; i++) {
099: if (i > 0) {
100: buffer.append(", ");
101: }
102: buffer.append(argumentTypes[i]);
103: }
104: return buffer.append(")").toString();
105: }
106: }
|