1 | /*
|
---|
2 | * Copyright 2001-2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.sitraka.bytecode;
|
---|
18 |
|
---|
19 | import java.io.DataInputStream;
|
---|
20 | import java.io.IOException;
|
---|
21 | import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool;
|
---|
22 | import org.apache.tools.ant.taskdefs.optional.sitraka.bytecode.attributes.AttributeInfo;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Method info structure.
|
---|
26 | * @todo give a more appropriate name to methods.
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public final class MethodInfo {
|
---|
30 | private int access_flags;
|
---|
31 | private int loc = -1;
|
---|
32 | private String name;
|
---|
33 | private String descriptor;
|
---|
34 |
|
---|
35 | public MethodInfo() {
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void read(ConstantPool constantPool, DataInputStream dis) throws IOException {
|
---|
39 | access_flags = dis.readShort();
|
---|
40 |
|
---|
41 | int name_index = dis.readShort();
|
---|
42 | name = Utils.getUTF8Value(constantPool, name_index);
|
---|
43 |
|
---|
44 | int descriptor_index = dis.readShort();
|
---|
45 | descriptor = Utils.getUTF8Value(constantPool, descriptor_index);
|
---|
46 |
|
---|
47 | int attributes_count = dis.readUnsignedShort();
|
---|
48 | for (int i = 0; i < attributes_count; i++) {
|
---|
49 | int attr_id = dis.readShort();
|
---|
50 | String attr_name = Utils.getUTF8Value(constantPool, attr_id);
|
---|
51 | int len = dis.readInt();
|
---|
52 | if (AttributeInfo.CODE.equals(attr_name)) {
|
---|
53 | readCode(constantPool, dis);
|
---|
54 | } else {
|
---|
55 | dis.skipBytes(len);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected void readCode(ConstantPool constantPool, DataInputStream dis) throws IOException {
|
---|
62 | // skip max_stack (short), max_local (short)
|
---|
63 | dis.skipBytes(2 * 2);
|
---|
64 |
|
---|
65 | // skip bytecode...
|
---|
66 | int bytecode_len = dis.readInt();
|
---|
67 | dis.skip(bytecode_len);
|
---|
68 |
|
---|
69 | // skip exceptions... 1 exception = 4 short.
|
---|
70 | int exception_count = dis.readShort();
|
---|
71 | dis.skipBytes(exception_count * 4 * 2);
|
---|
72 |
|
---|
73 | // read attributes...
|
---|
74 | int attributes_count = dis.readUnsignedShort();
|
---|
75 | for (int i = 0; i < attributes_count; i++) {
|
---|
76 | int attr_id = dis.readShort();
|
---|
77 | String attr_name = Utils.getUTF8Value(constantPool, attr_id);
|
---|
78 | int len = dis.readInt();
|
---|
79 | if (AttributeInfo.LINE_NUMBER_TABLE.equals(attr_name)) {
|
---|
80 | // we're only interested in lines of code...
|
---|
81 | loc = dis.readShort();
|
---|
82 | // skip the table which is 2*loc*short
|
---|
83 | dis.skip(loc * 2 * 2);
|
---|
84 | } else {
|
---|
85 | dis.skipBytes(len);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public int getAccessFlags() {
|
---|
91 | return access_flags;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public String getName() {
|
---|
95 | return name;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public String getDescriptor() {
|
---|
99 | return descriptor;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public String getFullSignature() {
|
---|
103 | return getReturnType() + " " + getShortSignature();
|
---|
104 | }
|
---|
105 |
|
---|
106 | public String getShortSignature() {
|
---|
107 | StringBuffer buf = new StringBuffer(getName());
|
---|
108 | buf.append("(");
|
---|
109 | String[] params = getParametersType();
|
---|
110 | for (int i = 0; i < params.length; i++) {
|
---|
111 | buf.append(params[i]);
|
---|
112 | if (i != params.length - 1) {
|
---|
113 | buf.append(", ");
|
---|
114 | }
|
---|
115 | }
|
---|
116 | buf.append(")");
|
---|
117 | return buf.toString();
|
---|
118 | }
|
---|
119 |
|
---|
120 | public String getReturnType() {
|
---|
121 | return Utils.getMethodReturnType(getDescriptor());
|
---|
122 | }
|
---|
123 |
|
---|
124 | public String[] getParametersType() {
|
---|
125 | return Utils.getMethodParams(getDescriptor());
|
---|
126 | }
|
---|
127 |
|
---|
128 | public int getNumberOfLines() {
|
---|
129 | return loc;
|
---|
130 | }
|
---|
131 |
|
---|
132 | public String getAccess() {
|
---|
133 | return Utils.getMethodAccess(access_flags);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public String toString() {
|
---|
137 | StringBuffer sb = new StringBuffer();
|
---|
138 | sb.append("Method: ").append(getAccess()).append(" ");
|
---|
139 | sb.append(getFullSignature());
|
---|
140 | return sb.toString();
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|