Hide generated nodes from FoldingVisitor

This commit is contained in:
Rawi01 authored and Reinier Zwitserloot committed 2026-04-21 23:00:57 +02:00
1 parent 21e0ebf082
commit 4a0f85674e
9 files changed
+275 -2

No files matched your search

@@ -220,6 +220,7 @@ public final class HandleLockedUtil {
tryStatement.tryBlock = block;
tryStatement.finallyBlock = new Block(0);
tryStatement.finallyBlock.statements = new Statement[] { unLock };
setGeneratedBy(tryStatement, source);
method.statements = new Statement[] { acquireLock, tryStatement };
@@ -103,6 +103,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
patchASTConverterLiterals(sm);
patchASTNodeSearchUtil(sm);
patchFieldInitializer(sm);
patchFoldingStructure(sm);
patchPostCompileHookEcj(sm);
@@ -1073,6 +1074,30 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
.build());
}
private static void patchFoldingStructure(ScriptManager sm) {
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.exitEarly()
.target(new MethodTarget("org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$FoldingVisitor", "createFoldingRegionForTryBlock", "void", "org.eclipse.jdt.core.dom.TryStatement"))
.target(new MethodTarget("org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$FoldingVisitor", "createFoldingRegionForFinallyBlock", "void", "org.eclipse.jdt.core.dom.TryStatement"))
.target(new MethodTarget("org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$FoldingVisitor", "createFoldingRegionForStatement", "void", "org.eclipse.jdt.core.dom.ASTNode"))
.target(new MethodTarget("org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$FoldingVisitor", "createFoldingRegion", "void", "org.eclipse.jdt.core.dom.ASTNode", "boolean"))
.decisionMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "isGenerated", "boolean", "org.eclipse.jdt.core.dom.ASTNode"))
.request(StackRequest.PARAM1)
.transplant()
.build());
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.exitEarly()
.target(new MethodTarget("org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$FoldingVisitor", "visit", "boolean", "org.eclipse.jdt.core.dom.TypeDeclaration"))
.target(new MethodTarget("org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$FoldingVisitor", "visit", "boolean", "org.eclipse.jdt.core.dom.MethodDeclaration"))
.target(new MethodTarget("org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$FoldingVisitor", "visit", "boolean", "org.eclipse.jdt.core.dom.IfStatement"))
.target(new MethodTarget("org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$FoldingVisitor", "visit", "boolean", "org.eclipse.jdt.core.dom.IfStatement"))
.decisionMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "isGenerated", "boolean", "org.eclipse.jdt.core.dom.ASTNode"))
.request(StackRequest.PARAM1)
.valueMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "returnFalse", "boolean", "java.lang.Object"))
.transplant()
.build());
}
private static void patchFieldInitializer(ScriptManager sm) {
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.addField()
.targetClass("org.eclipse.jdt.internal.core.CompilationUnitStructureRequestor")
@@ -0,0 +1,25 @@
package pkg;
import lombok.Cleanup;
import java.util.List;
public class CleanupFolding {
public void method() {
@Cleanup FileInputStream inStream = new FileInputStream("test");
List<String> a = null;
if (a.isBlank()) {
return;
}
try {
} catch (Exception e) {
} finally {
}
}
}
@@ -0,0 +1,24 @@
package pkg;
import lombok.Locked;
import java.util.List;
public class LockedFolding {
@Locked
public void method() {
List<String> a = null;
if (a.isBlank()) {
return;
}
try {
} catch (Exception e) {
} finally {
}
}
}
@@ -0,0 +1,21 @@
package pkg;
import java.util.List;
import lombok.NonNull;
public class NonNullFolding {
public void method(@NonNull List<String> a) {
if (a.isEmpty()) {
return;
}
try {
} catch (Exception e) {
} finally {
}
}
}
@@ -0,0 +1,24 @@
package pkg;
import lombok.Synchronized;
import java.util.List;
public class LockedFolding {
@Synchronized
public void method() {
List<String> a = null;
if (a.isBlank()) {
return;
}
try {
} catch (Exception e) {
} finally {
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 The Project Lombok Authors.
* Copyright (C) 2022-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -27,6 +27,7 @@ import org.junit.runners.Suite.SuiteClasses;
import lombok.eclipse.cleanup.CleanupTest;
import lombok.eclipse.compile.NoErrorsTest;
import lombok.eclipse.edit.FoldingTest;
import lombok.eclipse.edit.SelectTest;
import lombok.eclipse.misc.DelegateTest;
import lombok.eclipse.misc.JavadocTest;
@@ -37,7 +38,7 @@ import lombok.eclipse.refactoring.RenameTest;
import lombok.eclipse.references.FindReferencesTest;
@RunWith(Suite.class)
@SuiteClasses({ExtractInterfaceTest.class, RenameTest.class, SelectTest.class, CleanupTest.class, FindReferencesTest.class, InlineTest.class, NoErrorsTest.class, JavadocTest.class, DelegateTest.class, ExtractVariableTest.class})
@SuiteClasses({ExtractInterfaceTest.class, RenameTest.class, SelectTest.class, CleanupTest.class, FindReferencesTest.class, InlineTest.class, NoErrorsTest.class, JavadocTest.class, DelegateTest.class, ExtractVariableTest.class, FoldingTest.class})
public class EclipseTests {
}
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.eclipse;
import static org.junit.Assume.assumeTrue;
import org.eclipse.core.runtime.adaptor.EclipseStarter;
import org.osgi.framework.Bundle;
public class TestUtils {
public static void assumeMinJdtVersion(int version) {
Bundle coreBundle = getBundle("org.eclipse.jdt.core");
assumeTrue(coreBundle.getVersion().getMinor() >= version);
}
public static Bundle getBundle(String name) {
for (Bundle bundle : EclipseStarter.getSystemBundleContext().getBundles()) {
if (bundle.getSymbolicName().equals(name)) {
return bundle;
}
}
return null;
}
}
@@ -0,0 +1,108 @@
/*
* Copyright (C) 2022-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.eclipse.edit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.compiler.IScanner;
import org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import lombok.eclipse.EclipseRunner;
import lombok.eclipse.SetupSingleFileTest;
import lombok.eclipse.TestUtils;
import lombok.permit.Permit;
@RunWith(EclipseRunner.class)
public class FoldingTest {
@Rule
public SetupSingleFileTest setup = new SetupSingleFileTest();
@BeforeClass
public static void versionCheck() {
TestUtils.assumeMinJdtVersion(43);
}
@Test
public void locked() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("LockedFolding.java");
Map<?, ?> foldingMap = getFoldingMap(cu);
assertThat(foldingMap.size(), is(6));
}
@Test
public void nonNull() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("NonNullFolding.java");
Map<?, ?> foldingMap = getFoldingMap(cu);
assertThat(foldingMap.size(), is(6));
}
@Test
public void cleanup() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("CleanupFolding.java");
Map<?, ?> foldingMap = getFoldingMap(cu);
assertThat(foldingMap.size(), is(6));
}
@Test
public void synch() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("SynchronizedFolding.java");
Map<?, ?> foldingMap = getFoldingMap(cu);
assertThat(foldingMap.size(), is(6));
}
private Map<?, ?> getFoldingMap(ICompilationUnit cu) throws Exception {
Class<?> contextClass = Class.forName("org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$FoldingStructureComputationContext");
Constructor<?> contextConstructor = Permit.getConstructor(contextClass, DefaultJavaFoldingStructureProvider.class, IDocument.class, ProjectionAnnotationModel.class, boolean.class, IScanner.class);
Method processMethod = Permit.getMethod(DefaultJavaFoldingStructureProvider.class, "processCompilationUnit", ICompilationUnit.class, contextClass);
Field foldingMapField = Permit.getField(contextClass, "fMap");
DefaultJavaFoldingStructureProvider defaultJavaFoldingStructureProvider = new DefaultJavaFoldingStructureProvider();
Object context = Permit.newInstance(contextConstructor, defaultJavaFoldingStructureProvider, new Document(cu.getSource()), new ProjectionAnnotationModel(), true, null);
Permit.invoke(processMethod, defaultJavaFoldingStructureProvider, cu, context);
return (Map<?, ?>) Permit.get(foldingMapField, context);
}
}