use of jadx.api.data.annotations.InsnCodeOffset in project jadx by skylot.
the class CommentAction method getCommentRef.
/**
* Check if possible insert comment at current line.
*
* @return blank code comment object (comment string empty)
*/
@Nullable
private ICodeComment getCommentRef(int line) {
if (line == -1 || this.topCls == null) {
return null;
}
try {
// TODO: cache and update on class refresh
CodeLinesInfo linesInfo = new CodeLinesInfo(topCls, true);
// add comment if node definition at this line
JavaNode nodeAtLine = linesInfo.getDefAtLine(line);
if (nodeAtLine != null) {
// at node definition -> add comment for it
JadxNodeRef nodeRef = JadxNodeRef.forJavaNode(nodeAtLine);
return new JadxCodeComment(nodeRef, "");
}
Object ann = topCls.getAnnotationAt(new CodePosition(line));
if (ann == null) {
// check if line with comment above node definition
try {
JavaNode defNode = linesInfo.getJavaNodeBelowLine(line);
if (defNode != null) {
String lineStr = codeArea.getLineText(line).trim();
if (lineStr.startsWith("//")) {
return new JadxCodeComment(JadxNodeRef.forJavaNode(defNode), "");
}
}
} catch (Exception e) {
LOG.error("Failed to check comment line: " + line, e);
}
return null;
}
// try to add method line comment
JavaNode node = linesInfo.getJavaNodeByLine(line);
if (node instanceof JavaMethod) {
JadxNodeRef nodeRef = JadxNodeRef.forMth((JavaMethod) node);
if (ann instanceof InsnCodeOffset) {
int rawOffset = ((InsnCodeOffset) ann).getOffset();
return new JadxCodeComment(nodeRef, JadxCodeRef.forInsn(rawOffset), "");
}
}
} catch (Exception e) {
LOG.error("Failed to add comment at line: " + line, e);
}
return null;
}
use of jadx.api.data.annotations.InsnCodeOffset in project jadx by skylot.
the class JsonCodeGen method fillMthCode.
private List<JsonCodeLine> fillMthCode(MethodNode mth, MethodGen mthGen) {
if (mth.isNoCode()) {
return Collections.emptyList();
}
ICodeWriter cw = mth.root().makeCodeWriter();
try {
mthGen.addInstructions(cw);
} catch (Exception e) {
throw new JadxRuntimeException("Method generation error", e);
}
ICodeInfo code = cw.finish();
String codeStr = code.getCodeStr();
if (codeStr.isEmpty()) {
return Collections.emptyList();
}
String[] lines = codeStr.split(ICodeWriter.NL);
Map<Integer, Integer> lineMapping = code.getLineMapping();
Map<CodePosition, Object> annotations = code.getAnnotations();
long mthCodeOffset = mth.getMethodCodeOffset() + 16;
int linesCount = lines.length;
List<JsonCodeLine> codeLines = new ArrayList<>(linesCount);
for (int i = 0; i < linesCount; i++) {
String codeLine = lines[i];
int line = i + 2;
JsonCodeLine jsonCodeLine = new JsonCodeLine();
jsonCodeLine.setCode(codeLine);
jsonCodeLine.setSourceLine(lineMapping.get(line));
Object obj = annotations.get(new CodePosition(line));
if (obj instanceof InsnCodeOffset) {
long offset = ((InsnCodeOffset) obj).getOffset();
jsonCodeLine.setOffset("0x" + Long.toHexString(mthCodeOffset + offset * 2));
}
codeLines.add(jsonCodeLine);
}
return codeLines;
}
use of jadx.api.data.annotations.InsnCodeOffset in project jadx by skylot.
the class IntegrationTest method printCodeWithOffsets.
private void printCodeWithOffsets(ICodeInfo code) {
String codeStr = code.getCodeStr();
Map<CodePosition, Object> annotations = code.getAnnotations();
String[] lines = codeStr.split(ICodeWriter.NL);
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
int curLine = i + 1;
Object ann = annotations.get(new CodePosition(curLine, 0));
String offsetStr = "";
if (ann instanceof InsnCodeOffset) {
int offset = ((InsnCodeOffset) ann).getOffset();
offsetStr = "/* " + leftPad(String.valueOf(offset), 5) + " */";
}
System.out.println(rightPad(offsetStr, 12) + line);
}
}
Aggregations