adamw7 / tools

code generation and data oriented tools
MIT License
5 stars 1 forks source link

Remove unused imports in generated code #40

Open adamw7 opened 1 year ago

adamw7 commented 1 year ago

Try: import org.eclipse.jdt.core.; import org.eclipse.jdt.core.dom.; import org.eclipse.jdt.core.compiler.CategorizedProblem; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jface.text.Document; import org.eclipse.text.edits.TextEdit;

import java.util.ArrayList; import java.util.List;

public class RemoveUnusedImportsExample {

public static void main(String[] args) {
    String filePath = "path_to_your_java_file.java";

    // Initialize the Java project
    IJavaProject javaProject = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject());

    // Create a compilation unit from the source file
    ICompilationUnit compilationUnit = javaProject.findCompilationUnit(filePath);

    // Get compilation problems (warnings and errors)
    IProblem[] problems = compilationUnit.getProblems();

    // Find and remove unused imports based on compilation warnings
    List<ImportDeclaration> unusedImports = findUnusedImportsFromWarnings(problems, compilationUnit);
    removeImports(compilationUnit, unusedImports);

    // Generate the modified source code
    String modifiedSource = generateModifiedSource(compilationUnit);

    // Print the modified source code
    System.out.println(modifiedSource);
}

private static List<ImportDeclaration> findUnusedImportsFromWarnings(IProblem[] problems, ICompilationUnit compilationUnit) {
    List<ImportDeclaration> unusedImports = new ArrayList<>();

    // Find compilation warnings related to unused imports
    for (IProblem problem : problems) {
        if (problem.getID() == IProblem.UnusedImport) {
            int startOffset = problem.getSourceStart();
            int endOffset = problem.getSourceEnd();

            // Convert offsets to line and column numbers
            int startLine = -1;
            int endLine = -1;

            try {
                IRegion startRegion = compilationUnit.getLineInformationOfOffset(startOffset);
                IRegion endRegion = compilationUnit.getLineInformationOfOffset(endOffset);

                startLine = compilationUnit.getLineNumber(startOffset);
                endLine = compilationUnit.getLineNumber(endOffset);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // Create an ImportDeclaration for the warning range
            ImportDeclaration importDeclaration = createImportDeclaration(startLine, endLine);
            unusedImports.add(importDeclaration);
        }
    }

    return unusedImports;
}

private static ImportDeclaration createImportDeclaration(int startLine, int endLine) {
    // Create an ImportDeclaration with the specified line range
    // You'll need to construct the AST nodes properly using org.eclipse.jdt.core.dom
    // based on the line numbers you've extracted from the warnings
    // Return the created ImportDeclaration instance
    return null;
}

private static void removeImports(ICompilationUnit compilationUnit, List<ImportDeclaration> importsToRemove) {
    // Remove the specified import declarations from the compilation unit
    // You can use compilationUnit.applyTextEdit() to apply changes to the source
}

private static String generateModifiedSource(ICompilationUnit compilationUnit) {
    Document document = new Document(compilationUnit.getSource());

    // Apply the changes to the document
    try {
        compilationUnit.applyTextEdit(null, document, null);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    return document.get();
}

}