Skip to content

Commit 73e9631

Browse files
committed
Add one more field and method parsing cases
- Added 'field1.field2' where field2 is parsed also - Added '::method' calls
1 parent bc67379 commit 73e9631

2 files changed

Lines changed: 62 additions & 6 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import com.github.javaparser.ast.body.CallableDeclaration;
55
import com.github.javaparser.ast.expr.*;
66
import com.github.javaparser.resolution.UnsolvedSymbolException;
7+
import com.github.javaparser.resolution.types.ResolvedType;
78
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
9+
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.ClassFieldLocation;
10+
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.ClassReferenceLocation;
811

912
import java.util.Objects;
1013

@@ -81,6 +84,36 @@ else if (scope instanceof EnclosedExpr)
8184
printException(expr, e);
8285
}
8386
}
87+
else
88+
{
89+
try
90+
{
91+
// If the scope is something like 'this.field1' and similar
92+
ResolvedType resolvedType = expr.getScope().calculateResolvedType();
93+
if (!resolvedType.isReferenceType())
94+
return;
95+
96+
String qualifiedName = resolvedType.asReferenceType().getQualifiedName();
97+
String className = qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1);
98+
// If the class is not registered as a reference yet
99+
if (container.getClassReferenceLocationsFor(className) == null)
100+
{
101+
String packageName = "";
102+
if (qualifiedName.contains("."))
103+
packageName = qualifiedName.substring(0, qualifiedName.lastIndexOf('.')).replace('.', '/');
104+
105+
// For this purpose, we do not care about its line, columnStart or columnEnd
106+
container.putClassReference(className, new ClassReferenceLocation(getOwner(container), packageName,
107+
fieldValue.name, "reference", -1, -1, -1));
108+
}
109+
110+
container.putField(fieldValue.name, new ClassFieldLocation(className, "reference", fieldValue.line, fieldValue.columnStart, fieldValue.columnEnd + 1));
111+
}
112+
catch (Exception e)
113+
{
114+
printException(expr, e);
115+
}
116+
}
84117
}
85118

86119
static void parseStatic(ClassFileContainer container, FieldAccessExpr expr)

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ public void visit(ClassOrInterfaceDeclaration n, Object arg)
294294

295295
ResolvedReferenceTypeDeclaration resolve = n.resolve();
296296
this.classFileContainer.putClassReference(resolve.getName(),
297-
new ClassReferenceLocation(getOwner(classFileContainer),
298-
resolve.getPackageName(), "", "declaration", value.line, value.columnStart, value.columnEnd + 1));
297+
new ClassReferenceLocation(getOwner(classFileContainer),
298+
resolve.getPackageName(), "", "declaration", value.line, value.columnStart, value.columnEnd + 1));
299299
}
300300
catch (Exception e)
301301
{
@@ -344,8 +344,8 @@ public void visit(ClassOrInterfaceType n, Object arg)
344344
packagePath = qualifiedName.substring(0, qualifiedName.lastIndexOf('.')).replace('.', '/');
345345

346346
this.classFileContainer.putClassReference(classValue.name,
347-
new ClassReferenceLocation(getOwner(classFileContainer),
348-
packagePath, "", "reference", classValue.line, classValue.columnStart, classValue.columnEnd + 1));
347+
new ClassReferenceLocation(getOwner(classFileContainer),
348+
packagePath, "", "reference", classValue.line, classValue.columnStart, classValue.columnEnd + 1));
349349
}
350350
catch (Exception e)
351351
{
@@ -470,7 +470,8 @@ public void visit(EnumDeclaration n, Object arg)
470470
int line = range.begin.line;
471471
int columnStart = range.begin.column;
472472
int columnEnd = range.end.column;
473-
this.classFileContainer.putField(name, new ClassFieldLocation(getOwner(classFileContainer), "declaration", line, columnStart, columnEnd + 1));
473+
this.classFileContainer.putField(name, new ClassFieldLocation(getOwner(classFileContainer), "declaration"
474+
, line, columnStart, columnEnd + 1));
474475
});
475476
}
476477

@@ -899,7 +900,29 @@ public void visit(MethodDeclaration n, Object arg)
899900
public void visit(MethodReferenceExpr n, Object arg)
900901
{
901902
super.visit(n, arg);
902-
if (DEBUG) System.err.println("MethodReferenceExpr");
903+
try
904+
{
905+
ResolvedMethodDeclaration resolve = n.resolve();
906+
String signature = resolve.getQualifiedSignature();
907+
String parameters = "";
908+
if (resolve.getNumberOfParams() != 0)
909+
parameters = signature.substring(signature.indexOf('(') + 1, signature.lastIndexOf(')'));
910+
911+
Range methodRange =
912+
Objects.requireNonNull(n.getTokenRange().orElse(null)).getEnd().getRange().orElse(null);
913+
if (methodRange == null)
914+
return;
915+
916+
String methodName = n.getIdentifier();
917+
918+
String className = resolve.getClassName();
919+
classFileContainer.putMethod(methodName, new ClassMethodLocation(className, signature, parameters,
920+
"reference", methodRange.begin.line, methodRange.begin.column, methodRange.end.column + 1));
921+
}
922+
catch (Exception e)
923+
{
924+
printException(n, e);
925+
}
903926
}
904927

905928

0 commit comments

Comments
 (0)