FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jeb-samplecode/scripts/JavaASTTags.py at master · pnfsoftware/jeb-samplecode · GitHub
pnfsoftware
jeb-samplecode
Repository navigation
Code
Pull requests
3
(3)
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
jeb-samplecode
/
scripts
/
JavaASTTags.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
84 lines (71 loc) · 3 KB
Breadcrumbs
jeb-samplecode
/
scripts
/
JavaASTTags.py
Copy path
File metadata and controls
84 lines (71 loc) · 3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#?description=Show how to "tag" elements of an AST tree, and later on retrieve those tags from the Java text document output (referred to as "marks").
#?shortcut=
from
com
.
pnfsoftware
.
jeb
.
client
.
api
import
IScript
,
IconType
,
ButtonGroupType
from
com
.
pnfsoftware
.
jeb
.
core
import
RuntimeProjectUtil
from
com
.
pnfsoftware
.
jeb
.
core
.
units
.
code
.
java
import
IJavaSourceUnit
from
com
.
pnfsoftware
.
jeb
.
core
.
units
.
code
import
ICodeUnit
,
ICodeItem
from
com
.
pnfsoftware
.
jeb
.
core
.
output
.
text
import
ITextDocument
from
com
.
pnfsoftware
.
jeb
.
core
.
units
.
code
.
java
import
IJavaConstant
,
IJavaClass
"""
Sample script for JEB Decompiler.
This script shows how to "tag" elements of an AST tree, and later on retrieve
those tags from the Java text document output (referred to as "marks").
This code looks for Java units, and tags String contants containing the word
'html'. Output example:
...
<Java code >
...
=> Marks:
17:59 - htmlTag (Potential HTML code found)
Tags are persisted in JDB2 database files.
Note: tags are specific to Java units. However, marks are not (they are
specific to text documents). The Java plugin simply renders tags as text
marks. This example demonstrates usage of tags in that context.
Marks are not displayed by the desktop GUI client.
It is up to third-party code (clients, plugins, or scripts) to display them.
Revision note: Script updated on April 12 2022.
"""
class
JavaASTTags
(
IScript
):
def
run
(
self
,
ctx
):
prj
=
ctx
.
getMainProject
()
assert
prj
,
'Need a project'
for
unit
in
prj
.
findUnits
(
IJavaSourceUnit
):
elt
=
unit
.
getASTElement
()
if
isinstance
(
elt
,
IJavaClass
):
self
.
processClassTree
(
elt
)
doc
=
unit
.
getSourceDocument
()
javaCode
,
formattedMarks
=
self
.
formatTextDocument
(
doc
)
#print(javaCode)
if
(
formattedMarks
):
print
(
'=> Marks:'
)
print
(
formattedMarks
)
def
processClassTree
(
self
,
e_class
):
for
e
in
e_class
.
getMethods
():
self
.
processEltTree
(
e
)
def
processEltTree
(
self
,
e
):
if
e
:
self
.
analyzeNode
(
e
)
elts
=
e
.
getSubElements
()
for
e
in
elts
:
self
.
processEltTree
(
e
)
# demo
def
analyzeNode
(
self
,
e
):
if
isinstance
(
e
,
IJavaConstant
):
if
e
.
isString
():
if
e
.
getString
().
find
(
'html'
)
>=
0
:
print
(
'TAGGING: %s'
%
e
)
e
.
addTag
(
'htmlTag'
,
'Potential HTML code found'
)
def
formatTextDocument
(
self
,
doc
):
javaCode
,
formattedMarks
=
''
,
''
# retrieve the entire document -it's a source file,
# no need to buffer individual parts. 10 MLoC is enough
alldoc
=
doc
.
getDocumentPart
(
0
,
10000000
)
for
lineIndex
,
line
in
enumerate
(
alldoc
.
getLines
()):
javaCode
+=
line
.
getText
().
toString
()
+
'
\n
'
for
mark
in
line
.
getMarks
():
# the mark name is the tag attribute's key
if
mark
.
getName
()
==
'htmlTag'
:
# 0-based line and column indexes
# the mark object is the tag attribute's value
formattedMarks
+=
'%d:%d %s
\n
'
%
(
lineIndex
,
mark
.
getOffset
(),
mark
.
getObject
())
return
javaCode
,
formattedMarks
Back
|
FazBrowse Home
|
New Git URL