|
10 | 10 | import me.coley.bmf.ClassNode; |
11 | 11 | import me.coley.bmf.JarReader; |
12 | 12 | import me.coley.bmf.MemberNode; |
| 13 | +import me.coley.bmf.MethodNode; |
13 | 14 | import me.coley.bmf.consts.*; |
14 | 15 | import me.coley.bmf.mapping.ClassMapping; |
15 | 16 | import me.coley.bmf.mapping.MemberMapping; |
| 17 | +import me.coley.bmf.opcode.AbstractFieldOpcode; |
| 18 | +import me.coley.bmf.opcode.AbstractMethodOpcode; |
| 19 | +import me.coley.bmf.opcode.Opcode; |
16 | 20 | import me.coley.bmf.type.Type; |
17 | 21 | import me.coley.bmf.util.ConstUtil; |
18 | 22 | import me.coley.bmf.util.StreamUtil; |
@@ -102,8 +106,7 @@ public DefaultMutableTreeNode searchClass(int mode, String text) { |
102 | 106 | boolean meth = mode == CLASS_REFERENCE_METHODS; |
103 | 107 | for (int i = 0; i < cn.constants.size(); i++) { |
104 | 108 | Constant c = cn.constants.get(i); |
105 | | - if (c == null || c.type != (meth ? ConstantType.METHOD : ConstantType.FIELD)) |
106 | | - continue; |
| 109 | + if (c == null || c.type != (meth ? ConstantType.METHOD : ConstantType.FIELD)) continue; |
107 | 110 | if (c instanceof AbstractMemberConstant) { |
108 | 111 | AbstractMemberConstant amc = (AbstractMemberConstant) c; |
109 | 112 | String memberOwner = ConstUtil.getClassName(cn, amc.getClassIndex()); |
@@ -158,42 +161,59 @@ public DefaultMutableTreeNode searchMember(int mode, boolean methods, String tex |
158 | 161 | return root; |
159 | 162 | } |
160 | 163 |
|
161 | | - public DefaultMutableTreeNode searchMember(MemberMapping mm) { |
| 164 | + public DefaultMutableTreeNode searchMember(ClassMapping memberOwner, MemberMapping mm) { |
162 | 165 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(mm.toString()); |
163 | 166 | JarReader jar = callback.getJarReader(); |
164 | 167 | boolean isMethod = mm.desc.original.startsWith("("); |
165 | 168 | // Sort at this stage rather than sorting the root later. |
166 | 169 | // Its not worth the trouble later on. |
167 | | - // @formatter:off |
168 | 170 | for (String name : StreamUtil.listOfSortedJavaNames(jar.getClassEntries().keySet())) { |
169 | 171 | ClassNode cn = jar.getClassEntries().get(name); |
170 | 172 | ClassMapping cm = jar.getMapping().getMapping(name); |
171 | 173 | MappingTreeNode mtn = new MappingTreeNode(cm.name.getValue(), cm); |
172 | | - String space = isMethod ? "" : " "; |
173 | | - cn.constants.stream() |
174 | | - .filter(c -> c instanceof AbstractMemberConstant) |
175 | | - .map(c -> ((AbstractMemberConstant)c)) |
176 | | - .map(c -> ((ConstNameType) cn.getConst(c.getNameTypeIndex()))) |
177 | | - .filter(c -> matchesNameDesc(cn, c, mm)) |
178 | | - .forEach( |
179 | | - c -> mtn.add(new SearchResultTreeNode(mtn, |
180 | | - ConstUtil.getUTF8(cn, c.getNameIndex()) + space + ConstUtil.getUTF8(cn, c.getDescIndex()))) |
181 | | - ); |
| 174 | + for (MethodNode mn : cn.methods) { |
| 175 | + if (mn.code != null && mn.code.opcodes != null) { |
| 176 | + for (Opcode op : mn.code.opcodes.opcodes) { |
| 177 | + AbstractMemberConstant amc = getMemberConstantFromOpcode(cn, op, isMethod); |
| 178 | + if (amc == null) { |
| 179 | + continue; |
| 180 | + } |
| 181 | + // Check for proper owner |
| 182 | + String className = ConstUtil.getClassName(cn, amc.getClassIndex()); |
| 183 | + if (className.equals(memberOwner.name.getValue())) { |
| 184 | + ConstNameType cnt = (ConstNameType) cn.getConst(amc.getNameTypeIndex()); |
| 185 | + if (matchesNameDesc(cn, cnt, mm)) { |
| 186 | + mtn.add(new SearchResultTreeNode(mtn, |
| 187 | + ConstUtil.getUTF8(cn, mn.name) + " " + ConstUtil.getUTF8(cn, mn.desc))); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
182 | 193 | if (!mtn.isLeaf()) { |
183 | 194 | root.add(mtn); |
184 | 195 | } |
185 | 196 | } |
186 | | - // @formatter:on |
187 | 197 | return root; |
188 | 198 | } |
189 | 199 |
|
190 | | - private boolean matchesNameDesc(ClassNode cn, ConstNameType c, MemberMapping mm) { |
191 | | - ConstUTF8 utfName = (ConstUTF8) cn.getConst(c.getNameIndex()); |
192 | | - ConstUTF8 utfDesc = (ConstUTF8) cn.getConst(c.getDescIndex()); |
193 | | - return mm.name.getValue().equals(utfName.getValue()) && mm.desc.toDesc().equals(utfDesc.getValue()); |
| 200 | + private static AbstractMemberConstant getMemberConstantFromOpcode(ClassNode cn, Opcode op, boolean isMethod) { |
| 201 | + if (isMethod && op instanceof AbstractMethodOpcode) { |
| 202 | + AbstractMethodOpcode amo = (AbstractMethodOpcode) op; |
| 203 | + return (AbstractMemberConstant) cn.getConst(amo.methodIndex); |
| 204 | + } else if (!isMethod && op instanceof AbstractFieldOpcode) { |
| 205 | + AbstractFieldOpcode afo = (AbstractFieldOpcode) op; |
| 206 | + return (AbstractMemberConstant) cn.getConst(afo.fieldIndex); |
| 207 | + } |
| 208 | + return null; |
| 209 | + } |
| 210 | + |
| 211 | + private static boolean matchesNameDesc(ClassNode cn, ConstNameType c, MemberMapping mm) { |
| 212 | + return mm.name.getValue().equals(ConstUtil.getUTF8(cn, c.getNameIndex())) |
| 213 | + && mm.desc.toDesc().equals(ConstUtil.getUTF8(cn, c.getDescIndex())); |
194 | 214 | } |
195 | 215 |
|
196 | | - private boolean isPrim(String search, boolean methods) { |
| 216 | + private static boolean isPrim(String search, boolean methods) { |
197 | 217 | int l = search.length(); |
198 | 218 | if (methods) { |
199 | 219 | return l == 3 && Type.readPrim(search.charAt(2)) != null; |
|
0 commit comments