Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosnt committed May 10, 2020
1 parent 5ca994a commit 20cc164
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 45 deletions.
7 changes: 5 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -173,6 +174,8 @@ class App extends Component {
fileName,
location,
id: stringId++,
context: Settings.sortByContext
&& getStringContext(classFile.constant_pool, instructions, string.instructionIndex),
});
}
}
Expand Down
45 changes: 3 additions & 42 deletions src/StringSearcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import mitt from 'mitt';
import {
getAttribute,
extractClassName,
extractMethodInfoConstants,
getUtf8String,
} from './util/jct-util';
} from './util/util';
import {
JavaClassFileReader,
Modifier,
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
}
}
}
2 changes: 1 addition & 1 deletion src/components/StringList/StringEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
38 changes: 38 additions & 0 deletions src/util/jct-util.js → src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
};

0 comments on commit 20cc164

Please sign in to comment.