Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few fixes for parsing #516

Merged
merged 4 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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