From ccd92d1a8bff7eb75a82575e2df8bf63764745d4 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Mon, 23 Sep 2024 12:05:01 +0200 Subject: [PATCH] Attempt to cleanup context upon usage --- org.eclipse.jdt.core.javac/.classpath | 2 +- org.eclipse.jdt.core.javac/META-INF/p2.inf | 2 ++ org.eclipse.jdt.core.javac/pom.xml | 2 ++ .../jdt/core/dom/JavacBindingResolver.java | 1 + .../dom/JavacCompilationUnitResolver.java | 29 ++++++++++++++++++- org.eclipse.jdt.core.tests.javac/pom.xml | 2 +- 6 files changed, 35 insertions(+), 3 deletions(-) diff --git a/org.eclipse.jdt.core.javac/.classpath b/org.eclipse.jdt.core.javac/.classpath index bdfae34cced..d4b342fe609 100644 --- a/org.eclipse.jdt.core.javac/.classpath +++ b/org.eclipse.jdt.core.javac/.classpath @@ -5,7 +5,7 @@ - + diff --git a/org.eclipse.jdt.core.javac/META-INF/p2.inf b/org.eclipse.jdt.core.javac/META-INF/p2.inf index 83eda3a114a..a02283e8011 100644 --- a/org.eclipse.jdt.core.javac/META-INF/p2.inf +++ b/org.eclipse.jdt.core.javac/META-INF/p2.inf @@ -26,6 +26,8 @@ jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED\n\ jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED\n\ --add-opens\n\ jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED\n\ +--add-opens\n\ +jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED\n\ -DICompilationUnitResolver=org.eclipse.jdt.core.dom.JavacCompilationUnitResolver\n\ -DAbstractImageBuilder.compilerFactory=org.eclipse.jdt.internal.javac.JavacCompilerFactory\n\ -DCompilationUnit.DOM_BASED_OPERATIONS=true\n\ diff --git a/org.eclipse.jdt.core.javac/pom.xml b/org.eclipse.jdt.core.javac/pom.xml index 5f93050c744..317573bdce2 100644 --- a/org.eclipse.jdt.core.javac/pom.xml +++ b/org.eclipse.jdt.core.javac/pom.xml @@ -45,6 +45,8 @@ --add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports + jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED + --add-exports jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets.snippet=ALL-UNNAMED --add-exports jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets=ALL-UNNAMED diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java index e1470d503b0..50ea5f4eea6 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java @@ -415,6 +415,7 @@ private void resolve() { // symbols not already present: analyze try { this.javac.analyze(); + JavacCompilationUnitResolver.cleanUp(this.context); } catch (IOException e) { ILog.get().error(e.getMessage(), e); } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java index 719d3c3b22a..5a0298cb3cb 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java @@ -93,6 +93,7 @@ import com.sun.tools.javac.parser.ScannerFactory; import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle; import com.sun.tools.javac.parser.Tokens.TokenKind; +import com.sun.tools.javac.platform.PlatformDescription; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; @@ -628,6 +629,7 @@ public Void visitClass(ClassTree node, Void p) { if ((flags & ICompilationUnit.FORCE_PROBLEM_DETECTION) != 0 || (aptPath != null && aptPath.iterator().hasNext())) { task.analyze(); + cleanUp(context); } Throwable cachedThrown = null; @@ -718,6 +720,8 @@ public void postVisit(ASTNode node) { JavacBindingResolver resolver = new JavacBindingResolver(javaProject, task, context, converter, workingCopyOwner); resolver.isRecoveringBindings = (flags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0; ast.setBindingResolver(resolver); + } else { + cleanUp(context); } // ast.setOriginalModificationCount(ast.modificationCount()); // "un-dirty" AST so Rewrite can process it @@ -1012,7 +1016,30 @@ private boolean configureAPIfNecessary(JavacFileManager fileManager) { } } } - return false; } + + /** + * Cleanup a context from elements that become useless upon analysis. + * Elements that can still be useful (eg names, types) for binding resolution must + * be retained. + * @param context the context to clean up + */ + public static void cleanUp(Context context) { + // Some ZipFileSystem instances are still retained, while they don't seem + // useful at that stage. They're references by classSymbol->classfile (which + // is a ZipPath) -> ZipFileSystem -> cen (the bytes we don't care about anymore, + // but that we cannot dispose/free). + // Maybe this can be fixed in JDK cleaning the "cen" on ZipFileSystem#close ? + // Maybe we can try to get rid of all classfile field values on Symbols, or replace them by empty variants? + // + try { + PlatformDescription platform = context.get(PlatformDescription.class); + if (platform != null) platform.close(); + JavaFileManager fileManager = context.get(JavaFileManager.class); + if (fileManager != null) fileManager.close(); + } catch (IOException ex) { + ILog.get().error(ex.getMessage(), ex); + } + } } diff --git a/org.eclipse.jdt.core.tests.javac/pom.xml b/org.eclipse.jdt.core.tests.javac/pom.xml index 91577e93ad2..9e03bb0fa2f 100644 --- a/org.eclipse.jdt.core.tests.javac/pom.xml +++ b/org.eclipse.jdt.core.tests.javac/pom.xml @@ -50,7 +50,7 @@ - --add-modules ALL-SYSTEM -Dcompliance=21 -DCompilationUnit.DOM_BASED_OPERATIONS=true -DSourceIndexer.DOM_BASED_INDEXER=true -DICompilationUnitResolver=org.eclipse.jdt.core.dom.JavacCompilationUnitResolver -DAbstractImageBuilder.compiler=org.eclipse.jdt.internal.javac.JavacCompiler --add-opens jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets.snippet=ALL-UNNAMED --add-opens jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets=ALL-UNNAMED + --add-modules ALL-SYSTEM -Dcompliance=21 -DCompilationUnit.DOM_BASED_OPERATIONS=true -DSourceIndexer.DOM_BASED_INDEXER=true -DICompilationUnitResolver=org.eclipse.jdt.core.dom.JavacCompilationUnitResolver -DAbstractImageBuilder.compiler=org.eclipse.jdt.internal.javac.JavacCompiler --add-opens jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED --add-opens jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets.snippet=ALL-UNNAMED --add-opens jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets=ALL-UNNAMED