KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > bytecode > BCMethodDescriptor


1 /*
2
3    Derby - Class org.apache.derby.impl.services.bytecode.BCMethodDescriptor
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.bytecode;
23
24 import org.apache.derby.iapi.services.classfile.VMDescriptor;
25
26 /**
27     A method descriptor. Ie. something that describes the
28     type of a method, parameter types and return types.
29     It is not an instance of a method.
30     <BR>
31     This has no generated class specific state.
32  */

33 class BCMethodDescriptor {
34
35     static final String JavaDoc[] EMPTY = new String JavaDoc[0];
36
37     private final String JavaDoc[] vmParameterTypes;
38     private final String JavaDoc vmReturnType;
39
40     private final String JavaDoc vmDescriptor;
41
42      BCMethodDescriptor(String JavaDoc[] vmParameterTypes, String JavaDoc vmReturnType, BCJava factory) {
43
44         this.vmParameterTypes = vmParameterTypes;
45         this.vmReturnType = vmReturnType;
46
47         vmDescriptor = factory.vmType(this);
48     }
49 /*
50     static String get(Expression[] vmParameters, String vmReturnType, BCJava factory) {
51
52         int count = vmParameters.length;
53         String[] vmParameterTypes;
54         if (count == 0) {
55             vmParameterTypes = BCMethodDescriptor.EMPTY;
56         } else {
57             vmParameterTypes = new String[count];
58             for (int i =0; i < count; i++) {
59                 vmParameterTypes[i] = ((BCExpr) vmParameters[i]).vmType();
60             }
61         }
62
63         return new BCMethodDescriptor(vmParameterTypes, vmReturnType, factory).toString();
64     }
65 */

66     static String JavaDoc get(String JavaDoc[] vmParameterTypes, String JavaDoc vmReturnType, BCJava factory) {
67
68         return new BCMethodDescriptor(vmParameterTypes, vmReturnType, factory).toString();
69     }
70
71     /**
72      * builds the JVM method descriptor for this method as
73      * defined in JVM Spec 4.3.3, Method Descriptors.
74      */

75     String JavaDoc buildMethodDescriptor() {
76
77         int paramCount = vmParameterTypes.length;
78
79         int approxLength = (30 * (paramCount + 1));
80
81         StringBuffer JavaDoc methDesc = new StringBuffer JavaDoc(approxLength);
82
83         methDesc.append(VMDescriptor.C_METHOD);
84
85         for (int i = 0; i < paramCount; i++) {
86             methDesc.append(vmParameterTypes[i]);
87         }
88
89         methDesc.append(VMDescriptor.C_ENDMETHOD);
90         methDesc.append(vmReturnType);
91
92         return methDesc.toString();
93     }
94
95     public String JavaDoc toString() {
96         return vmDescriptor;
97     }
98         
99     
100     public int hashCode() {
101         return vmParameterTypes.length | (vmReturnType.hashCode() & 0xFFFFFF00);
102     }
103
104     public boolean equals(Object JavaDoc other) {
105         if (!(other instanceof BCMethodDescriptor))
106             return false;
107
108         BCMethodDescriptor o = (BCMethodDescriptor) other;
109
110
111         if (o.vmParameterTypes.length != vmParameterTypes.length)
112             return false;
113
114         for (int i = 0; i < vmParameterTypes.length; i++) {
115             if (!vmParameterTypes[i].equals(o.vmParameterTypes[i]))
116                 return false;
117         }
118
119         return vmReturnType.equals(o.vmReturnType);
120     }
121 }
122
Popular Tags