Skip to content

Commit

Permalink
Merge pull request #516 from Bl3nd/go-to-enhancement
Browse files Browse the repository at this point in the history
A few fixes for parsing
  • Loading branch information
Konloch authored Sep 29, 2024
2 parents 1557cd6 + d642610 commit 95eeee0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public void processDisplay()

if (!BytecodeViewer.viewer.workPane.classFiles.containsKey(workingDecompilerName))
{
container.parse();
boolean parsed = container.parse();
BytecodeViewer.viewer.workPane.classFiles.put(workingDecompilerName, container);
container.hasBeenParsed = true;
container.hasBeenParsed = parsed;
}

//set the swing components on the swing thread
Expand Down Expand Up @@ -449,24 +449,27 @@ else if (isPanelEditable && decompiler == Decompiler.KRAKATAU_DISASSEMBLER)
@Override
public void mouseMoved(MouseEvent e)
{
if (e.isControlDown())
if (classFileContainer.hasBeenParsed)
{
RSyntaxTextArea textArea = (RSyntaxTextArea) e.getSource();
Token token = textArea.viewToToken(e.getPoint());
if (token != null)
if (e.isControlDown())
{
String lexeme = token.getLexeme();
if (classFileContainer.fieldMembers.containsKey(lexeme) || classFileContainer.methodMembers.containsKey(lexeme) || classFileContainer.methodLocalMembers.containsKey(lexeme) || classFileContainer.methodParameterMembers.containsKey(lexeme) || classFileContainer.classReferences.containsKey(lexeme))
RSyntaxTextArea textArea = (RSyntaxTextArea) e.getSource();
Token token = textArea.viewToToken(e.getPoint());
if (token != null)
{
textArea.setCursor(new Cursor(Cursor.HAND_CURSOR));
String lexeme = token.getLexeme();
if (classFileContainer.fieldMembers.containsKey(lexeme) || classFileContainer.methodMembers.containsKey(lexeme) || classFileContainer.methodLocalMembers.containsKey(lexeme) || classFileContainer.methodParameterMembers.containsKey(lexeme) || classFileContainer.classReferences.containsKey(lexeme))
{
textArea.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
}
}
}
else
{
if (bytecodeViewPanel.textArea.getCursor().getType() != Cursor.TEXT_CURSOR)
else
{
bytecodeViewPanel.textArea.setCursor(new Cursor(Cursor.TEXT_CURSOR));
if (bytecodeViewPanel.textArea.getCursor().getType() != Cursor.TEXT_CURSOR)
{
bytecodeViewPanel.textArea.setCursor(new Cursor(Cursor.TEXT_CURSOR));
}
}
}
}
Expand All @@ -477,10 +480,13 @@ public void mouseMoved(MouseEvent e)
@Override
public void mouseClicked(MouseEvent e)
{
if (e.isControlDown())
if (classFileContainer.hasBeenParsed)
{
RSyntaxTextArea textArea = (RSyntaxTextArea) e.getSource();
textArea.getActionMap().get("goToAction").actionPerformed(new ActionEvent(textArea, ActionEvent.ACTION_PERFORMED, "goToAction"));
if (e.isControlDown())
{
RSyntaxTextArea textArea = (RSyntaxTextArea) e.getSource();
textArea.getActionMap().get("goToAction").actionPerformed(new ActionEvent(textArea, ActionEvent.ACTION_PERFORMED, "goToAction"));
}
}
}
});
Expand All @@ -504,7 +510,6 @@ private void markOccurrences(RSyntaxTextArea textArea, ClassFileContainer classF
if (token == null)
{
highlighterEx.clearMarkOccurrencesHighlights();
errorStripe.refreshMarkers();
return;
}
}
Expand All @@ -513,7 +518,6 @@ private void markOccurrences(RSyntaxTextArea textArea, ClassFileContainer classF
if (token == null)
{
highlighterEx.clearMarkOccurrencesHighlights();
errorStripe.refreshMarkers();
return;
}

Expand Down Expand Up @@ -614,7 +618,7 @@ private void markMethod(RSyntaxTextArea textArea, ClassFileContainer classFileCo
* @param finalToken the token
* @param highlighterEx the highlighter
*/
private static void markMethodParameter(RSyntaxTextArea textArea, ClassFileContainer classFileContainer, int line, int column, Token finalToken, RSyntaxTextAreaHighlighterEx highlighterEx)
private void markMethodParameter(RSyntaxTextArea textArea, ClassFileContainer classFileContainer, int line, int column, Token finalToken, RSyntaxTextAreaHighlighterEx highlighterEx)
{
classFileContainer.methodParameterMembers.values().forEach(parameters -> parameters.forEach(parameter ->
{
Expand All @@ -631,7 +635,6 @@ private static void markMethodParameter(RSyntaxTextArea textArea, ClassFileConta
{
int startOffset = root.getElement(location.line - 1).getStartOffset() + (location.columnStart - 1);
int endOffset = root.getElement(location.line - 1).getStartOffset() + (location.columnEnd - 1);

highlighterEx.addMarkedOccurrenceHighlight(startOffset, endOffset, new SmartHighlightPainter());
}
}
Expand All @@ -654,7 +657,7 @@ private static void markMethodParameter(RSyntaxTextArea textArea, ClassFileConta
* @param finalToken the token
* @param highlighterEx the highlighter
*/
private static void markMethodLocalVariable(RSyntaxTextArea textArea, ClassFileContainer classFileContainer, int line, int column, Token finalToken, RSyntaxTextAreaHighlighterEx highlighterEx)
private void markMethodLocalVariable(RSyntaxTextArea textArea, ClassFileContainer classFileContainer, int line, int column, Token finalToken, RSyntaxTextAreaHighlighterEx highlighterEx)
{
classFileContainer.methodLocalMembers.values().forEach(localVariables -> localVariables.forEach(localVariable ->
{
Expand All @@ -671,7 +674,6 @@ private static void markMethodLocalVariable(RSyntaxTextArea textArea, ClassFileC
{
int startOffset = root.getElement(location.line - 1).getStartOffset() + (location.columnStart - 1);
int endOffset = root.getElement(location.line - 1).getStartOffset() + (location.columnEnd - 1);

highlighterEx.addMarkedOccurrenceHighlight(startOffset, endOffset, new SmartHighlightPainter());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package the.bytecode.club.bytecodeviewer.resources.classcontainer;

import com.github.javaparser.ParseProblemException;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import the.bytecode.club.bytecodeviewer.decompilers.Decompiler;
import the.bytecode.club.bytecodeviewer.resources.ResourceContainer;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.*;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.MyVoidVisitor;
Expand Down Expand Up @@ -51,14 +50,18 @@ public ClassFileContainer(String className, String content, ResourceContainer re
/**
* Parse the class content with JavaParser.
*/
public void parse()
public boolean parse()
{
try
{
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false), new JarTypeSolver(path));
StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
CompilationUnit compilationUnit = StaticJavaParser.parse(this.content);
compilationUnit.accept(new MyVoidVisitor(this, compilationUnit), null);
if (shouldParse())
{
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false), new JarTypeSolver(path));
StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
CompilationUnit compilationUnit = StaticJavaParser.parse(this.content);
compilationUnit.accept(new MyVoidVisitor(this, compilationUnit), null);
return true;
}
}
catch (IOException e)
{
Expand All @@ -69,6 +72,17 @@ public void parse()
System.err.println("Parsing error: " + className);
e.printStackTrace();
}

return false;
}

public boolean shouldParse()
{
return !getDecompiler().equals(Decompiler.BYTECODE_DISASSEMBLER.getDecompilerName())
&& !getDecompiler().equals(Decompiler.KRAKATAU_DISASSEMBLER.getDecompilerName())
&& !getDecompiler().equals(Decompiler.JAVAP_DISASSEMBLER.getDecompilerName())
&& !getDecompiler().equals(Decompiler.SMALI_DISASSEMBLER.getDecompilerName())
&& !getDecompiler().equals(Decompiler.ASM_TEXTIFY_DISASSEMBLER.getDecompilerName());
}

public String getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.javaparser.Range;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.*;
Expand Down Expand Up @@ -175,8 +176,6 @@ else if (scope instanceof ThisExpr)
ResolvedType resolvedType = n.getSymbolResolver().calculateType(thisExpr);
String qualifiedName = resolvedType.asReferenceType().getQualifiedName();
String className = qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1);
String packageName = qualifiedName.substring(0, qualifiedName.lastIndexOf('.'));
this.classFileContainer.putClassReference(className, new ClassReferenceLocation(getOwner(), packageName.replace('.', '/'), fieldName, "reference", line, columnStart, columnEnd + 1));
this.classFileContainer.putField(fieldName, new ClassFieldLocation(className, "reference", line, columnStart, columnEnd + 1));
}
}
Expand Down Expand Up @@ -207,6 +206,16 @@ public void visit(ConstructorDeclaration n, Object arg)
this.classFileContainer.putParameter(parameterName, new ClassParameterLocation(getOwner(), n.getDeclarationAsString(false, false), "declaration", line, columnStart, columnEnd + 1));
});

if (n.getParentNode().get() instanceof ObjectCreationExpr)
{
ObjectCreationExpr objectCreationExpr = (ObjectCreationExpr) n.getParentNode().get();
NodeList<BodyDeclaration<?>> bodyDeclarations = objectCreationExpr.getAnonymousClassBody().get();
if (bodyDeclarations.getFirst().get().equals(n))
{
return;
}
}

ResolvedConstructorDeclaration resolve = n.resolve();
String signature = resolve.getQualifiedSignature();
String parameters = "";
Expand Down

0 comments on commit 95eeee0

Please sign in to comment.