Skip to content

Commit 05fa036

Browse files
committed
Add/Re-add field parsing when scope is from a parameter or variable.
- Added 0.1s wait between opening class and registering "Go-To" caret event, hopefully fixes text area not scrolling to caret. - Added/Re-added ability to open a field declaration in its owner class if the scope was a parameter or variable.
1 parent f560fa5 commit 05fa036

4 files changed

Lines changed: 77 additions & 25 deletions

File tree

src/main/java/the/bytecode/club/bytecodeviewer/gui/components/actions/GoToAction.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,34 @@ private ClassFileContainer openClass(String lexeme, boolean field, boolean metho
185185
if (field)
186186
{
187187
String className = container.getClassForField(lexeme);
188-
BytecodeViewer.viewer.workPane.addClassResource(resourceContainer, className + ".class");
189-
ClassViewer activeResource = (ClassViewer) BytecodeViewer.viewer.workPane.getActiveResource();
190-
HashMap<String, ClassFileContainer> classFiles = BytecodeViewer.viewer.workPane.classFiles;
191-
return wait(classFiles, activeResource);
188+
189+
// If the field we want to go to wasn't an expression like Class.field. For example param.field or
190+
// variable.field
191+
if (className.isEmpty())
192+
{
193+
ClassFieldLocation classFieldLocation = container.getFieldLocationsFor(lexeme).get(0);
194+
className = classFieldLocation.owner;
195+
ClassReferenceLocation classReferenceLocation =
196+
container.getClassReferenceLocationsFor(className).get(0);
197+
if (classReferenceLocation == null)
198+
return null;
199+
200+
String packagePath = classReferenceLocation.packagePath;
201+
202+
if (packagePath.startsWith("java") || packagePath.startsWith("javax") || packagePath.startsWith("com.sun"))
203+
return null;
204+
205+
if (!packagePath.isEmpty())
206+
className = packagePath + "/" + className;
207+
}
208+
209+
if (resourceContainer.resourceClasses.containsKey(className))
210+
{
211+
BytecodeViewer.viewer.workPane.addClassResource(resourceContainer, className + ".class");
212+
ClassViewer activeResource = (ClassViewer) BytecodeViewer.viewer.workPane.getActiveResource();
213+
HashMap<String, ClassFileContainer> classFiles = BytecodeViewer.viewer.workPane.classFiles;
214+
return wait(classFiles, activeResource);
215+
}
192216
}
193217
else if (method)
194218
{
@@ -354,6 +378,16 @@ private ClassFileContainer wait(HashMap<String, ClassFileContainer> classFiles,
354378

355379
private void moveCursor(int line, int columnStart)
356380
{
381+
// Wait for 100ms so we make sure there is enough time between loading the class and registering cursor movement
382+
try
383+
{
384+
Thread.sleep(100);
385+
}
386+
catch (InterruptedException e)
387+
{
388+
throw new RuntimeException(e);
389+
}
390+
357391
for (int i = 0; i < 3; i++)
358392
{
359393
BytecodeViewPanel panel = ((ClassViewer) BytecodeViewer.viewer.workPane.getActiveResource()).getPanel(i);

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors;
22

33
import com.github.javaparser.Range;
4+
import com.github.javaparser.ast.body.CallableDeclaration;
45
import com.github.javaparser.ast.expr.*;
56
import com.github.javaparser.resolution.UnsolvedSymbolException;
67
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
@@ -16,7 +17,7 @@
1617
class FieldAccessParser
1718
{
1819

19-
static void parse(ClassFileContainer container, FieldAccessExpr expr)
20+
static void parse(ClassFileContainer container, FieldAccessExpr expr, CallableDeclaration<?> method)
2021
{
2122
Range fieldRange = Objects.requireNonNull(expr.getTokenRange().orElse(null)).getEnd().getRange().orElse(null);
2223
if (fieldRange == null)
@@ -26,7 +27,7 @@ static void parse(ClassFileContainer container, FieldAccessExpr expr)
2627

2728
Expression scope = expr.getScope();
2829

29-
// Ex. Clazz.field -> Clazz
30+
// Ex. Clazz.field -> Clazz or c.field -> c
3031
if (scope instanceof NameExpr)
3132
{
3233
NameExpr nameExpr = (NameExpr) scope;
@@ -38,7 +39,11 @@ static void parse(ClassFileContainer container, FieldAccessExpr expr)
3839

3940
try
4041
{
41-
putClassResolvedValues(container, expr, nameExpr, scopeValue, fieldValue);
42+
// Scope
43+
putResolvedValues(container, "reference", method, nameExpr, scopeValue);
44+
45+
// Field
46+
putFieldResolvedValues(container, expr, nameExpr, fieldValue);
4247
}
4348
catch (UnsolvedSymbolException ignore)
4449
{
@@ -58,7 +63,8 @@ else if (scope instanceof ThisExpr)
5863
try
5964
{
6065
putFieldResolvedValues(container, expr, thisExpr, fieldValue);
61-
} catch (UnsolvedSymbolException e)
66+
}
67+
catch (UnsolvedSymbolException e)
6268
{
6369
printException(expr, e);
6470
}
@@ -69,7 +75,8 @@ else if (scope instanceof EnclosedExpr)
6975
try
7076
{
7177
putFieldResolvedValues(container, expr, enclosedExpr, fieldValue);
72-
} catch (UnsolvedSymbolException e)
78+
}
79+
catch (UnsolvedSymbolException e)
7380
{
7481
printException(expr, e);
7582
}
@@ -104,17 +111,22 @@ static void parseStatic(ClassFileContainer container, FieldAccessExpr expr)
104111
try
105112
{
106113
putClassResolvedValues(container, expr, nameExpr, scopeValue, fieldValue);
107-
} catch (UnsolvedSymbolException e) {
114+
}
115+
catch (UnsolvedSymbolException e)
116+
{
108117
printException(expr, e);
109118
}
110119
}
111120
}
112121
else if (scope instanceof ThisExpr)
113122
{
114123
ThisExpr thisExpr = (ThisExpr) scope;
115-
try {
124+
try
125+
{
116126
putFieldResolvedValues(container, expr, thisExpr, fieldValue);
117-
} catch (UnsolvedSymbolException e) {
127+
}
128+
catch (UnsolvedSymbolException e)
129+
{
118130
printException(expr, e);
119131
}
120132
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,13 @@ public void visit(FieldAccessExpr n, Object arg)
532532
try
533533
{
534534
InitializerDeclaration initializer = findInitializerForExpression(n, this.compilationUnit);
535+
CallableDeclaration<?> method = findMethodForExpression(n, this.compilationUnit);
536+
if (method == null)
537+
method = findConstructorForExpression(n, this.compilationUnit);
535538

536-
if (initializer == null)
537-
FieldAccessParser.parse(classFileContainer, n);
538-
else
539+
if (method != null)
540+
FieldAccessParser.parse(classFileContainer, n, method);
541+
else if (initializer != null)
539542
FieldAccessParser.parseStatic(classFileContainer, n);
540543
}
541544
catch (Exception e)

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,30 @@ public Value(SimpleName simpleName, Range range)
5656
* @param resolveExpr The {@code NameExpr}
5757
* @param value The value
5858
*/
59-
static void putResolvedValues(ClassFileContainer container, String decRef, CallableDeclaration<?> method,
60-
NameExpr resolveExpr, Value value)
59+
static boolean putResolvedValues(ClassFileContainer container, String decRef, CallableDeclaration<?> method,
60+
NameExpr resolveExpr, Value value)
6161
{
6262
ResolvedValueDeclaration vd = resolveExpr.resolve();
6363
if (vd.isField())
6464
{
6565
container.putField(value.name, new ClassFieldLocation(getOwner(container), decRef,
6666
value.line, value.columnStart, value.columnEnd + 1));
67+
return true;
6768
}
6869
else if (vd.isVariable())
6970
{
70-
container.putLocalVariable(value.name, new ClassLocalVariableLocation(getOwner(container)
71-
, getMethod(method), decRef, value.line, value.columnStart,
72-
value.columnEnd + 1));
71+
container.putLocalVariable(value.name, new ClassLocalVariableLocation(getOwner(container),
72+
getMethod(method), decRef, value.line, value.columnStart, value.columnEnd + 1));
73+
return true;
7374
}
7475
else if (vd.isParameter())
7576
{
76-
container.putParameter(value.name, new ClassParameterLocation(getOwner(container),
77-
getMethod(method), decRef, value.line, value.columnStart,
78-
value.columnEnd + 1));
77+
container.putParameter(value.name, new ClassParameterLocation(getOwner(container), getMethod(method),
78+
decRef, value.line, value.columnStart, value.columnEnd + 1));
79+
return true;
7980
}
81+
82+
return false;
8083
}
8184

8285
/**
@@ -96,8 +99,8 @@ static void putResolvedValues(ClassFileContainer container, String decRef, NameE
9699
}
97100
else if (vd.isVariable())
98101
{
99-
container.putLocalVariable(value.name, new ClassLocalVariableLocation(getOwner(container)
100-
, "static", decRef, value.line, value.columnStart, value.columnEnd + 1));
102+
container.putLocalVariable(value.name, new ClassLocalVariableLocation(getOwner(container), "static",
103+
decRef, value.line, value.columnStart, value.columnEnd + 1));
101104
}
102105
else if (vd.isParameter())
103106
{

0 commit comments

Comments
 (0)