1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.services.bytecode.BCMethodDescriptor |
4 | |
5 | Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.services.bytecode; |
22 | |
23 | import org.apache.derby.iapi.services.classfile.VMDescriptor; |
24 | |
25 | /** |
26 | A method descriptor. Ie. something that describes the |
27 | type of a method, parameter types and return types. |
28 | It is not an instance of a method. |
29 | <BR> |
30 | This has no generated class specific state. |
31 | */ |
32 | class BCMethodDescriptor { |
33 | |
34 | static final String[] EMPTY = new String[0]; |
35 | |
36 | private final String[] vmParameterTypes; |
37 | private final String vmReturnType; |
38 | |
39 | private final String vmDescriptor; |
40 | |
41 | BCMethodDescriptor(String[] vmParameterTypes, String vmReturnType, BCJava factory) { |
42 | |
43 | this.vmParameterTypes = vmParameterTypes; |
44 | this.vmReturnType = vmReturnType; |
45 | |
46 | vmDescriptor = factory.vmType(this); |
47 | } |
48 | /* |
49 | static String get(Expression[] vmParameters, String vmReturnType, BCJava factory) { |
50 | |
51 | int count = vmParameters.length; |
52 | String[] vmParameterTypes; |
53 | if (count == 0) { |
54 | vmParameterTypes = BCMethodDescriptor.EMPTY; |
55 | } else { |
56 | vmParameterTypes = new String[count]; |
57 | for (int i =0; i < count; i++) { |
58 | vmParameterTypes[i] = ((BCExpr) vmParameters[i]).vmType(); |
59 | } |
60 | } |
61 | |
62 | return new BCMethodDescriptor(vmParameterTypes, vmReturnType, factory).toString(); |
63 | } |
64 | */ |
65 | static String get(String[] vmParameterTypes, String vmReturnType, BCJava factory) { |
66 | |
67 | return new BCMethodDescriptor(vmParameterTypes, vmReturnType, factory).toString(); |
68 | } |
69 | |
70 | /** |
71 | * builds the JVM method descriptor for this method as |
72 | * defined in JVM Spec 4.3.3, Method Descriptors. |
73 | */ |
74 | String buildMethodDescriptor() { |
75 | |
76 | int paramCount = vmParameterTypes.length; |
77 | |
78 | int approxLength = (30 * (paramCount + 1)); |
79 | |
80 | StringBuffer methDesc = new StringBuffer(approxLength); |
81 | |
82 | methDesc.append(VMDescriptor.C_METHOD); |
83 | |
84 | for (int i = 0; i < paramCount; i++) { |
85 | methDesc.append(vmParameterTypes[i]); |
86 | } |
87 | |
88 | methDesc.append(VMDescriptor.C_ENDMETHOD); |
89 | methDesc.append(vmReturnType); |
90 | |
91 | return methDesc.toString(); |
92 | } |
93 | |
94 | public String toString() { |
95 | return vmDescriptor; |
96 | } |
97 | |
98 | |
99 | public int hashCode() { |
100 | return vmParameterTypes.length | (vmReturnType.hashCode() & 0xFFFFFF00); |
101 | } |
102 | |
103 | public boolean equals(Object other) { |
104 | if (!(other instanceof BCMethodDescriptor)) |
105 | return false; |
106 | |
107 | BCMethodDescriptor o = (BCMethodDescriptor) other; |
108 | |
109 | |
110 | if (o.vmParameterTypes.length != vmParameterTypes.length) |
111 | return false; |
112 | |
113 | for (int i = 0; i < vmParameterTypes.length; i++) { |
114 | if (!vmParameterTypes[i].equals(o.vmParameterTypes[i])) |
115 | return false; |
116 | } |
117 | |
118 | return vmReturnType.equals(o.vmReturnType); |
119 | } |
120 | } |