Skip to content

Commit 340bec4

Browse files
committed
Attempt at making the code more understandable and a few other things.
- Added examples on what visitor visits - Moved away from the line, columnStart and columnEnd variables for each visitor, instead we use the Value class. - Added a few more values we parse.
1 parent 82789aa commit 340bec4

9 files changed

Lines changed: 2260 additions & 1719 deletions

File tree

src/main/java/the/bytecode/club/bytecodeviewer/resources/classcontainer/parser/MyVoidVisitor.java

Lines changed: 0 additions & 1719 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors;
2+
3+
import com.github.javaparser.Range;
4+
import com.github.javaparser.ast.CompilationUnit;
5+
import com.github.javaparser.ast.body.CallableDeclaration;
6+
import com.github.javaparser.ast.expr.*;
7+
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
8+
import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.Value;
9+
10+
import static the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.*;
11+
12+
/**
13+
* Created by Bl3nd.
14+
* Date: 10/1/2024
15+
*/
16+
class ArrayParser
17+
{
18+
19+
static void parseAccess(CompilationUnit compilationUnit, ArrayAccessExpr expr, ClassFileContainer container)
20+
{
21+
Expression valueExp = expr.getName();
22+
if (valueExp instanceof NameExpr)
23+
{
24+
NameExpr nameExpr = (NameExpr) valueExp;
25+
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
26+
if (method == null)
27+
{
28+
method = findConstructorForExpression(expr, compilationUnit);
29+
}
30+
31+
if (method == null)
32+
{
33+
System.err.println("ArrayAccess1 - Method not found");
34+
return;
35+
}
36+
37+
Range range = nameExpr.getName().getRange().orElse(null);
38+
if (range == null)
39+
return;
40+
41+
Value nameValue = new Value(nameExpr.getName(), range);
42+
putResolvedValues(container, "reference", method, nameExpr, nameValue);
43+
}
44+
45+
Expression indexExp = expr.getIndex();
46+
if (indexExp instanceof NameExpr)
47+
{
48+
NameExpr nameExpr = (NameExpr) indexExp;
49+
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
50+
if (method == null)
51+
method = findConstructorForExpression(expr, compilationUnit);
52+
53+
if (method == null)
54+
{
55+
System.err.println("ArrayAccess2 - Method not found");
56+
return;
57+
}
58+
59+
Range range = nameExpr.getName().getRange().orElse(null);
60+
if (range == null)
61+
return;
62+
63+
Value indexValue = new Value(nameExpr.getName(), range);
64+
putResolvedValues(container, "reference", method, nameExpr, indexValue);
65+
}
66+
}
67+
68+
static void parseCreation(CompilationUnit compilationUnit, ArrayCreationExpr expr,
69+
ClassFileContainer container)
70+
{
71+
expr.getLevels().forEach(level -> {
72+
Expression dimensionExpr = level.getDimension().orElse(null);
73+
if (dimensionExpr instanceof NameExpr)
74+
{
75+
NameExpr nameExpr = (NameExpr) dimensionExpr;
76+
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
77+
if (method == null)
78+
method = findConstructorForExpression(expr, compilationUnit);
79+
80+
if (method == null)
81+
{
82+
System.err.println("ArrayCreation - Method not found");
83+
return;
84+
}
85+
86+
Range range = nameExpr.getName().getRange().orElse(null);
87+
if (range == null)
88+
return;
89+
90+
Value dimensionValue = new Value(nameExpr.getName(), range);
91+
putResolvedValues(container, "reference", method, nameExpr, dimensionValue);
92+
}
93+
});
94+
}
95+
96+
static void parseInitializer(CompilationUnit compilationUnit, ArrayInitializerExpr expr,
97+
ClassFileContainer container)
98+
{
99+
expr.getValues().forEach(value -> {
100+
if (value instanceof NameExpr)
101+
{
102+
NameExpr nameExpr = (NameExpr) value;
103+
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
104+
if (method == null)
105+
method = findConstructorForExpression(expr, compilationUnit);
106+
107+
if (method == null)
108+
{
109+
System.err.println("ArrayInitializer - Method not found");
110+
return;
111+
}
112+
113+
Range range = nameExpr.getName().getRange().orElse(null);
114+
if (range == null)
115+
return;
116+
117+
Value valueValue = new Value(nameExpr.getName(), range);
118+
putResolvedValues(container, "reference", method, nameExpr, valueValue);
119+
}
120+
});
121+
}
122+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors;
2+
3+
import com.github.javaparser.Range;
4+
import com.github.javaparser.ast.body.CallableDeclaration;
5+
import com.github.javaparser.ast.expr.AssignExpr;
6+
import com.github.javaparser.ast.expr.NameExpr;
7+
import com.github.javaparser.ast.expr.SimpleName;
8+
import com.github.javaparser.resolution.UnsolvedSymbolException;
9+
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
10+
import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.Value;
11+
12+
import static the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.printException;
13+
import static the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.putResolvedValues;
14+
15+
/**
16+
* Created by Bl3nd.
17+
* Date: 9/29/2024
18+
*/
19+
class AssignParser
20+
{
21+
22+
static void parse(ClassFileContainer container, AssignExpr expr, CallableDeclaration<?> method)
23+
{
24+
if (expr.getValue() instanceof NameExpr)
25+
{
26+
NameExpr nameExpr = (NameExpr) expr.getValue();
27+
Range range = nameExpr.getName().getRange().orElse(null);
28+
if (range == null)
29+
return;
30+
31+
Value value = new Value(nameExpr.getName(), range);
32+
try
33+
{
34+
putResolvedValues(container, "reference", method, nameExpr, value);
35+
}
36+
catch (UnsolvedSymbolException e)
37+
{
38+
printException(expr, e);
39+
}
40+
}
41+
42+
if (expr.getTarget() instanceof NameExpr)
43+
{
44+
NameExpr nameExpr = (NameExpr) expr.getTarget();
45+
try
46+
{
47+
SimpleName simpleName = nameExpr.getName();
48+
Range range = simpleName.getRange().orElse(null);
49+
if (range == null)
50+
return;
51+
52+
Value target = new Value(nameExpr.getName(), range);
53+
putResolvedValues(container, "reference", method, nameExpr, target);
54+
}
55+
catch (UnsolvedSymbolException e)
56+
{
57+
printException(expr, e);
58+
}
59+
}
60+
}
61+
62+
static void parseStatic(ClassFileContainer container, AssignExpr expr)
63+
{
64+
if (expr.getValue() instanceof NameExpr)
65+
{
66+
NameExpr nameExpr = (NameExpr) expr.getValue();
67+
Range range = nameExpr.getName().getRange().orElse(null);
68+
if (range == null)
69+
return;
70+
71+
Value value = new Value(nameExpr.getName(), range);
72+
try
73+
{
74+
putResolvedValues(container, "reference", nameExpr, value);
75+
}
76+
catch (UnsolvedSymbolException e)
77+
{
78+
printException(expr, e);
79+
}
80+
}
81+
82+
if (expr.getTarget() instanceof NameExpr)
83+
{
84+
NameExpr nameExpr = (NameExpr) expr.getTarget();
85+
try
86+
{
87+
Range range = nameExpr.getName().getRange().orElse(null);
88+
if (range == null)
89+
return;
90+
91+
Value target = new Value(nameExpr.getName(), range);
92+
putResolvedValues(container, "reference", nameExpr, target);
93+
}
94+
catch (UnsolvedSymbolException e)
95+
{
96+
printException(expr, e);
97+
}
98+
}
99+
}
100+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors;
2+
3+
import com.github.javaparser.Range;
4+
import com.github.javaparser.ast.CompilationUnit;
5+
import com.github.javaparser.ast.body.CallableDeclaration;
6+
import com.github.javaparser.ast.expr.ConditionalExpr;
7+
import com.github.javaparser.ast.expr.Expression;
8+
import com.github.javaparser.ast.expr.NameExpr;
9+
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
10+
import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.Value;
11+
12+
import static the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.*;
13+
14+
/**
15+
* Created by Bl3nd.
16+
* Date: 10/1/2024
17+
*/
18+
class ConditionalParser
19+
{
20+
21+
static void parse(CompilationUnit compilationUnit, ConditionalExpr expr, ClassFileContainer container)
22+
{
23+
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
24+
if (method == null)
25+
method = findConstructorForExpression(expr, compilationUnit);
26+
27+
if (method == null)
28+
return;
29+
30+
Expression elseExpr = expr.getElseExpr();
31+
if (elseExpr instanceof NameExpr)
32+
{
33+
NameExpr nameExpr = (NameExpr) elseExpr;
34+
Range range = nameExpr.getName().getRange().orElse(null);
35+
if (range == null)
36+
return;
37+
38+
Value elseValue = new Value(nameExpr.getName(), range);
39+
putResolvedValues(container, "reference", method, nameExpr, elseValue);
40+
}
41+
42+
Expression thenExpr = expr.getThenExpr();
43+
if (thenExpr instanceof NameExpr)
44+
{
45+
NameExpr nameExpr = (NameExpr) thenExpr;
46+
Range range = nameExpr.getName().getRange().orElse(null);
47+
if (range == null)
48+
return;
49+
50+
Value thenValue = new Value(nameExpr.getName(), range);
51+
putResolvedValues(container, "reference", method, nameExpr, thenValue);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)