|
36 | 36 | import org.eclipse.jdt.core.dom.ASTParser; |
37 | 37 | import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; |
38 | 38 | import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
| 39 | +import org.eclipse.jdt.internal.compiler.env.IElementInfo; |
39 | 40 | import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
40 | 41 | import org.eclipse.jdt.internal.core.Buffer; |
41 | 42 | import org.eclipse.jdt.internal.core.CompilationUnit; |
| 43 | +import org.eclipse.jdt.internal.core.JavaModelManager; |
42 | 44 | import org.eclipse.jdt.internal.core.util.Util; |
43 | 45 | import org.eclipse.text.edits.ReplaceEdit; |
44 | 46 | import org.eclipse.text.edits.TextEdit; |
@@ -2882,4 +2884,52 @@ public void java15SwitchExpr(int x){ |
2882 | 2884 | assertTrue(methodStatements.size() == 1); |
2883 | 2885 | assertTrue("Should have at least 1 problem", problems.length > 0); |
2884 | 2886 | } |
| 2887 | + |
| 2888 | +/** |
| 2889 | + * Test that calling getOptions(true) on a closed ICompilationUnit does NOT |
| 2890 | + * cause the element info to be loaded (i.e., does not trigger parsing/opening). |
| 2891 | + * |
| 2892 | + * Regression test for https://github.com/eclipse-jdt/eclipse.jdt.core/pull/4779 |
| 2893 | + * |
| 2894 | + * Before the fix, getCustomOptions() unconditionally called |
| 2895 | + * getCompilationUnitElementInfo(), which triggered openWhenClosed() -> |
| 2896 | + * buildStructure() -> parse, even when no setOptions() had ever been called |
| 2897 | + * on the CU. After the fix it uses JavaModelManager.getInfo(this) which |
| 2898 | + * returns null without opening when the element is not yet cached. |
| 2899 | + */ |
| 2900 | +public void testGetOptionsDoesNotLoadElementInfo() throws CoreException { |
| 2901 | + // Use a CU that is definitely not open (force-close it first). |
| 2902 | + ICompilationUnit cu2 = getCompilationUnit("P", "src", "p", "X.java"); |
| 2903 | + cu2.close(); // ensure info is evicted from the model cache |
| 2904 | + |
| 2905 | + // Precondition: info must be null before we call getOptions. |
| 2906 | + IElementInfo infoBefore = JavaModelManager.getJavaModelManager().getInfo(cu2); |
| 2907 | + assertNull("Precondition failed: element info should be null before getOptions()", infoBefore); |
| 2908 | + |
| 2909 | + // Call the API under test. |
| 2910 | + cu2.getOptions(true); |
| 2911 | + |
| 2912 | + // The fix: info must STILL be null — getOptions must not have opened the CU. |
| 2913 | + IElementInfo infoAfter = JavaModelManager.getJavaModelManager().getInfo(cu2); |
| 2914 | + assertNull( |
| 2915 | + "getOptions(true) should not load the IElementInfo when setOptions() " + |
| 2916 | + "has never been called on this CU", |
| 2917 | + infoAfter); |
| 2918 | +} |
| 2919 | + |
| 2920 | +public void testGetOptionsReturnsCustomOptionsWhenSet() throws CoreException { |
| 2921 | + ICompilationUnit cu2 = getCompilationUnit("P", "src", "p", "X.java"); |
| 2922 | + ICompilationUnit wc = null; |
| 2923 | + try { |
| 2924 | + wc = cu2.getWorkingCopy(null); |
| 2925 | + Map<String, String> opts = new HashMap<>(); |
| 2926 | + opts.put(JavaCore.COMPILER_SOURCE, "11"); |
| 2927 | + wc.setOptions(opts); |
| 2928 | + |
| 2929 | + Map<String, String> result = wc.getOptions(false); |
| 2930 | + assertEquals("11", result.get(JavaCore.COMPILER_SOURCE)); |
| 2931 | + } finally { |
| 2932 | + if (wc != null) wc.discardWorkingCopy(); |
| 2933 | + } |
| 2934 | +} |
2885 | 2935 | } |
0 commit comments