Skip to content

Commit

Permalink
Attempt at making the code more understandable and a few other things.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Bl3nd committed Oct 3, 2024
1 parent 82789aa commit 340bec4
Show file tree
Hide file tree
Showing 9 changed files with 2,260 additions and 1,719 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors;

import com.github.javaparser.Range;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.CallableDeclaration;
import com.github.javaparser.ast.expr.*;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.Value;

import static the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.*;

/**
* Created by Bl3nd.
* Date: 10/1/2024
*/
class ArrayParser
{

static void parseAccess(CompilationUnit compilationUnit, ArrayAccessExpr expr, ClassFileContainer container)
{
Expression valueExp = expr.getName();
if (valueExp instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) valueExp;
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
if (method == null)
{
method = findConstructorForExpression(expr, compilationUnit);
}

if (method == null)
{
System.err.println("ArrayAccess1 - Method not found");
return;
}

Range range = nameExpr.getName().getRange().orElse(null);
if (range == null)
return;

Value nameValue = new Value(nameExpr.getName(), range);
putResolvedValues(container, "reference", method, nameExpr, nameValue);
}

Expression indexExp = expr.getIndex();
if (indexExp instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) indexExp;
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
if (method == null)
method = findConstructorForExpression(expr, compilationUnit);

if (method == null)
{
System.err.println("ArrayAccess2 - Method not found");
return;
}

Range range = nameExpr.getName().getRange().orElse(null);
if (range == null)
return;

Value indexValue = new Value(nameExpr.getName(), range);
putResolvedValues(container, "reference", method, nameExpr, indexValue);
}
}

static void parseCreation(CompilationUnit compilationUnit, ArrayCreationExpr expr,
ClassFileContainer container)
{
expr.getLevels().forEach(level -> {
Expression dimensionExpr = level.getDimension().orElse(null);
if (dimensionExpr instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) dimensionExpr;
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
if (method == null)
method = findConstructorForExpression(expr, compilationUnit);

if (method == null)
{
System.err.println("ArrayCreation - Method not found");
return;
}

Range range = nameExpr.getName().getRange().orElse(null);
if (range == null)
return;

Value dimensionValue = new Value(nameExpr.getName(), range);
putResolvedValues(container, "reference", method, nameExpr, dimensionValue);
}
});
}

static void parseInitializer(CompilationUnit compilationUnit, ArrayInitializerExpr expr,
ClassFileContainer container)
{
expr.getValues().forEach(value -> {
if (value instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) value;
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
if (method == null)
method = findConstructorForExpression(expr, compilationUnit);

if (method == null)
{
System.err.println("ArrayInitializer - Method not found");
return;
}

Range range = nameExpr.getName().getRange().orElse(null);
if (range == null)
return;

Value valueValue = new Value(nameExpr.getName(), range);
putResolvedValues(container, "reference", method, nameExpr, valueValue);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors;

import com.github.javaparser.Range;
import com.github.javaparser.ast.body.CallableDeclaration;
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.Value;

import static the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.printException;
import static the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.putResolvedValues;

/**
* Created by Bl3nd.
* Date: 9/29/2024
*/
class AssignParser
{

static void parse(ClassFileContainer container, AssignExpr expr, CallableDeclaration<?> method)
{
if (expr.getValue() instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) expr.getValue();
Range range = nameExpr.getName().getRange().orElse(null);
if (range == null)
return;

Value value = new Value(nameExpr.getName(), range);
try
{
putResolvedValues(container, "reference", method, nameExpr, value);
}
catch (UnsolvedSymbolException e)
{
printException(expr, e);
}
}

if (expr.getTarget() instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) expr.getTarget();
try
{
SimpleName simpleName = nameExpr.getName();
Range range = simpleName.getRange().orElse(null);
if (range == null)
return;

Value target = new Value(nameExpr.getName(), range);
putResolvedValues(container, "reference", method, nameExpr, target);
}
catch (UnsolvedSymbolException e)
{
printException(expr, e);
}
}
}

static void parseStatic(ClassFileContainer container, AssignExpr expr)
{
if (expr.getValue() instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) expr.getValue();
Range range = nameExpr.getName().getRange().orElse(null);
if (range == null)
return;

Value value = new Value(nameExpr.getName(), range);
try
{
putResolvedValues(container, "reference", nameExpr, value);
}
catch (UnsolvedSymbolException e)
{
printException(expr, e);
}
}

if (expr.getTarget() instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) expr.getTarget();
try
{
Range range = nameExpr.getName().getRange().orElse(null);
if (range == null)
return;

Value target = new Value(nameExpr.getName(), range);
putResolvedValues(container, "reference", nameExpr, target);
}
catch (UnsolvedSymbolException e)
{
printException(expr, e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors;

import com.github.javaparser.Range;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.CallableDeclaration;
import com.github.javaparser.ast.expr.ConditionalExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.NameExpr;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.Value;

import static the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.ParserUtil.*;

/**
* Created by Bl3nd.
* Date: 10/1/2024
*/
class ConditionalParser
{

static void parse(CompilationUnit compilationUnit, ConditionalExpr expr, ClassFileContainer container)
{
CallableDeclaration<?> method = findMethodForExpression(expr, compilationUnit);
if (method == null)
method = findConstructorForExpression(expr, compilationUnit);

if (method == null)
return;

Expression elseExpr = expr.getElseExpr();
if (elseExpr instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) elseExpr;
Range range = nameExpr.getName().getRange().orElse(null);
if (range == null)
return;

Value elseValue = new Value(nameExpr.getName(), range);
putResolvedValues(container, "reference", method, nameExpr, elseValue);
}

Expression thenExpr = expr.getThenExpr();
if (thenExpr instanceof NameExpr)
{
NameExpr nameExpr = (NameExpr) thenExpr;
Range range = nameExpr.getName().getRange().orElse(null);
if (range == null)
return;

Value thenValue = new Value(nameExpr.getName(), range);
putResolvedValues(container, "reference", method, nameExpr, thenValue);
}
}
}
Loading

0 comments on commit 340bec4

Please sign in to comment.