Bug: Fixed imports with '.*'.

- Also added a resolver to help resolve classes within the jar.
This commit is contained in:
Cody 2024-09-18 22:28:13 -06:00
parent 00319e9537
commit 7b7f8976ea

AI 샘플 코드 생성 중입니다

Loading...
3 changed files with 29 additions and 17 deletions

View File

@ -116,7 +116,7 @@ public class BytecodeViewPanelUpdater implements Runnable
//perform decompiling inside of this thread
final String decompiledSource = decompiler.getDecompiler().decompileClassNode(viewer.resource.getResourceClassNode(), classBytes);
ClassFileContainer container = new ClassFileContainer(viewer.resource.workingName + "-" + decompiler.getDecompilerName(), decompiledSource, viewer.resource.container.name);
ClassFileContainer container = new ClassFileContainer(viewer.resource.workingName + "-" + decompiler.getDecompilerName(), decompiledSource, viewer.resource.container);
if (!container.hasBeenParsed)
{
container.parse();

View File

@ -3,14 +3,19 @@ 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.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.resources.ResourceContainer;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.ClassFieldLocation;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.ClassLocalVariableLocation;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.ClassMethodLocation;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.ClassParameterLocation;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.MyVoidVisitor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
@ -34,12 +39,14 @@ public class ClassFileContainer
public final String className;
private final String content;
private final String parentContainer;
private final String path;
public ClassFileContainer(String className, String content, String parentContainer)
public ClassFileContainer(String className, String content, ResourceContainer resourceContainer)
{
this.className = className;
this.content = content;
this.parentContainer = parentContainer;
this.parentContainer = resourceContainer.name;
this.path = resourceContainer.file.getAbsolutePath();
}
/**
@ -49,12 +56,16 @@ public class ClassFileContainer
{
try
{
StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(path));
StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
CompilationUnit compilationUnit = StaticJavaParser.parse(this.content);
compilationUnit.accept(new MyVoidVisitor(this, compilationUnit), null);
} catch (ParseProblemException e)
{
System.err.println("Parsing error!");
} catch (IOException e)
{
throw new RuntimeException(e);
}
}

View File

@ -6,10 +6,10 @@ import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.*;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.resolution.types.ResolvedType;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.ClassFileContainer;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.ClassFieldLocation;
import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.ClassLocalVariableLocation;
@ -75,11 +75,14 @@ public class MyVoidVisitor extends VoidVisitorAdapter<Object>
public void visit(ImportDeclaration n, Object arg)
{
super.visit(n, arg);
Name class_ = n.getName();
String className = class_.getIdentifier();
String package_ = Objects.requireNonNull(class_.getQualifier().orElse(null)).asString();
package_ = package_.replace('.', '/');
this.classFileContainer.putImport(className, package_);
if (!n.isAsterisk())
{
Name class_ = n.getName();
String className = class_.getIdentifier();
String package_ = Objects.requireNonNull(class_.getQualifier().orElse(null)).asString();
package_ = package_.replace('.', '/');
this.classFileContainer.putImport(className, package_);
}
}
/**
@ -137,6 +140,11 @@ public class MyVoidVisitor extends VoidVisitorAdapter<Object>
}
} catch (UnsolvedSymbolException ignore)
{
ResolvedType resolvedType = n.getSymbolResolver().calculateType(nameExpr);
String qualifiedName = resolvedType.asReferenceType().getQualifiedName();
String className = qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1);
String packageName = qualifiedName.substring(0, qualifiedName.lastIndexOf('.'));
this.classFileContainer.putImport(className, packageName.replace('.', '/'));
this.classFileContainer.putField(fieldName, new ClassFieldLocation(name1, "reference", line, columnStart, columnEnd + 1));
}
}
@ -144,13 +152,6 @@ public class MyVoidVisitor extends VoidVisitorAdapter<Object>
}
}
@Override
public void visit(ClassOrInterfaceType n, Object arg)
{
super.visit(n, arg);
// System.err.println(n.getName().getIdentifier());
}
/**
* Visit all {@link ConstructorDeclaration}s.
* <p>