From 20cc164b3c85598adb0cbe9603b2927530f27528 Mon Sep 17 00:00:00 2001 From: leonardosnt Date: Sun, 10 May 2020 17:05:14 -0300 Subject: [PATCH] some refactoring --- src/App.js | 7 ++-- src/StringSearcher.js | 45 ++---------------------- src/components/StringList/StringEntry.js | 2 +- src/util/{jct-util.js => util.js} | 38 ++++++++++++++++++++ 4 files changed, 47 insertions(+), 45 deletions(-) rename src/util/{jct-util.js => util.js} (74%) diff --git a/src/App.js b/src/App.js index fbf40e8..be52ae2 100644 --- a/src/App.js +++ b/src/App.js @@ -23,10 +23,11 @@ import JSZip from 'jszip'; import { Button, SettingsPanel, FileSelector, StringList } from './components'; import { + getStringContext, extractClassName, extractMethodInfoConstants, getInstructionLineNumber, -} from './util/jct-util'; +} from './util/util'; import { stringContains } from './util/string-util'; import { saveAs } from 'file-saver'; import { translate } from './i18n/i18n'; @@ -154,7 +155,7 @@ class App extends Component { classFile.constant_pool ); - for (const { method, strings } of methods) { + for (const { method, strings, instructions } of methods) { // Extract method info constants only once const methodLocation = extractMethodInfoConstants( method, @@ -173,6 +174,8 @@ class App extends Component { fileName, location, id: stringId++, + context: Settings.sortByContext + && getStringContext(classFile.constant_pool, instructions, string.instructionIndex), }); } } diff --git a/src/StringSearcher.js b/src/StringSearcher.js index d22c8bf..6cd8f8f 100644 --- a/src/StringSearcher.js +++ b/src/StringSearcher.js @@ -19,10 +19,8 @@ import mitt from 'mitt'; import { getAttribute, - extractClassName, - extractMethodInfoConstants, getUtf8String, -} from './util/jct-util'; +} from './util/util'; import { JavaClassFileReader, Modifier, @@ -162,14 +160,14 @@ export default class StringReader { constantIndex, instruction: instructions[i], value: getUtf8String(classFile.constant_pool, constantIndex), - context: this._getStringContext(constantPool, instructions, i), // TODO: remove from here + instructionIndex: i, }); // 'Mark' as found alreadyMappedStrings.add(constantIndex); } - return { method, strings: stringsInThisMethod }; + return { method, instructions, strings: stringsInThisMethod }; }) .filter(m => m && m.strings.length > 0); @@ -182,41 +180,4 @@ export default class StringReader { } } - /** - * TODO: move to another place? - * @param {ConstantPoolInfo[]} constantPool - * @param {Instruction[]} instructions - * @param {number} index - Index in instructions - */ - _getStringContext(constantPool, instructions, index) { - // TODO: Since we only look at the next instruction, - // this won't work with string concatenations. - - const nextInstruction = instructions[index + 1]; - - if (nextInstruction.opcode === Opcode.INVOKEINTERFACE) { - const operands = nextInstruction.operands; - const index = (operands[0] << 8) | operands[1]; - const methodRef = constantPool[index]; - const className = extractClassName(methodRef.class_index, constantPool); - const { name, descriptor } = extractMethodInfoConstants( - methodRef.name_and_type_index, - constantPool - ); - - const fullMethodDesc = `${className}#${name}${descriptor}`; - - switch (fullMethodDesc) { - case 'org/bukkit/command/CommandSender#sendMessage(Ljava/lang/String;)V': - case 'org/bukkit/entity/Player#sendMessage(Ljava/lang/String;)V': - return 'SendMessage'; - - case 'org/bukkit/inventory/meta/ItemMeta#setDisplayName(Ljava/lang/String;)V': - return 'ItemDisplayName'; - - default: - return undefined; - } - } - } } diff --git a/src/components/StringList/StringEntry.js b/src/components/StringList/StringEntry.js index 2f3650f..08cf58b 100644 --- a/src/components/StringList/StringEntry.js +++ b/src/components/StringList/StringEntry.js @@ -18,7 +18,7 @@ import React, { Component } from 'react'; import HighlightWords from 'react-highlight-words'; -import { prettyMethodInfo } from '../../util/jct-util.js'; +import { prettyMethodInfo } from '../../util/util.js'; import InfoIcon from '../../icons/info'; diff --git a/src/util/jct-util.js b/src/util/util.js similarity index 74% rename from src/util/jct-util.js rename to src/util/util.js index 678f010..ab3eb06 100644 --- a/src/util/jct-util.js +++ b/src/util/util.js @@ -18,6 +18,7 @@ import { utf8ByteArrayToString } from 'utf8-string-bytes'; import { parseMethodDescriptor } from './descriptor-parser'; +import { Opcode } from 'java-class-tools'; /** * Get an attribute by its name. @@ -109,3 +110,40 @@ export function getUtf8String(constant_pool, index) { return utf8ByteArrayToString(poolEntry.bytes); } + +/** + * @param {ConstantPoolInfo[]} constantPool + * @param {Instruction[]} instructions + * @param {number} index - Index in instructions + */ +export function getStringContext(constantPool, instructions, index) { + // TODO: Since we only look at the next instruction, + // this won't work with string concatenations. + + const nextInstruction = instructions[index + 1]; + + if (nextInstruction.opcode === Opcode.INVOKEINTERFACE) { + const operands = nextInstruction.operands; + const index = (operands[0] << 8) | operands[1]; + const methodRef = constantPool[index]; + const className = extractClassName(methodRef.class_index, constantPool); + const { name, descriptor } = extractMethodInfoConstants( + methodRef.name_and_type_index, + constantPool + ); + + const fullMethodDesc = `${className}#${name}${descriptor}`; + + switch (fullMethodDesc) { + case 'org/bukkit/command/CommandSender#sendMessage(Ljava/lang/String;)V': + case 'org/bukkit/entity/Player#sendMessage(Ljava/lang/String;)V': + return 'SendMessage'; + + case 'org/bukkit/inventory/meta/ItemMeta#setDisplayName(Ljava/lang/String;)V': + return 'ItemDisplayName'; + + default: + return undefined; + } + } +}; \ No newline at end of file