mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 10:15:53 +00:00
Support @Delegate in eclipse 2024-03
This commit is contained in:
1 parent
98cdf67ed6
commit
3b20b70363
6 files changed
+112
-6
No files matched your search
@@ -771,6 +771,13 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
|
||||
.request(StackRequest.RETURN_VALUE, StackRequest.THIS)
|
||||
.wrapMethod(new Hook("lombok.launch.PatchFixesHider$Delegate", "addGeneratedDelegateMethods", "java.lang.Object[]", "java.lang.Object", "java.lang.Object"))
|
||||
.build());
|
||||
|
||||
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.exitEarly()
|
||||
.target(new MethodTarget("org.eclipse.jdt.internal.core.JavaElement", "getElementInfo", "org.eclipse.jdt.internal.compiler.env.IElementInfo"))
|
||||
.request(StackRequest.THIS)
|
||||
.decisionMethod(new Hook("lombok.launch.PatchFixesHider$Delegate", "isDelegateSourceMethod", "boolean", "java.lang.Object"))
|
||||
.valueMethod(new Hook("lombok.launch.PatchFixesHider$Delegate", "returnElementInfo", "java.lang.Object", "java.lang.Object"))
|
||||
.build());
|
||||
}
|
||||
|
||||
private static void addPatchesForValEclipse(ScriptManager sm) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2021 The Project Lombok Authors.
|
||||
* Copyright (C) 2010-2024 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
|
||||
@@ -21,10 +21,11 @@
|
||||
*/
|
||||
package lombok.eclipse.agent;
|
||||
|
||||
import static lombok.eclipse.Eclipse.*;
|
||||
import static lombok.eclipse.EcjAugments.*;
|
||||
import static lombok.eclipse.Eclipse.*;
|
||||
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -726,6 +727,15 @@ public class PatchDelegate {
|
||||
return EclipseOnlyMethods.addGeneratedDelegateMethodsToChildren(returnValue, javaElement);
|
||||
}
|
||||
|
||||
public static Object returnElementInfo(Object delegateSourceMethod) {
|
||||
Field field = Permit.permissiveGetField(delegateSourceMethod.getClass(), "sourceMethodInfo");
|
||||
return Permit.permissiveReadField(Object.class, field, delegateSourceMethod);
|
||||
}
|
||||
|
||||
public static boolean isDelegateSourceMethod(Object sourceMethod) {
|
||||
return sourceMethod.getClass().getName().equals("lombok.eclipse.agent.PatchDelegate$EclipseOnlyMethods$DelegateSourceMethod");
|
||||
}
|
||||
|
||||
public static class EclipseOnlyMethods {
|
||||
private static void cleanupDelegateMethods(CompilationUnitDeclaration cud) {
|
||||
CompilationUnit compilationUnit = getCompilationUnit(cud);
|
||||
|
||||
@@ -277,11 +277,16 @@ final class PatchFixesHider {
|
||||
public static final class Delegate {
|
||||
private static final Method HANDLE_DELEGATE_FOR_TYPE;
|
||||
private static final Method ADD_GENERATED_DELEGATE_METHODS;
|
||||
public static final Method IS_DELEGATE_SOURCE_METHOD;
|
||||
public static final Method RETURN_ELEMENT_INFO;
|
||||
|
||||
static {
|
||||
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchDelegatePortal");
|
||||
HANDLE_DELEGATE_FOR_TYPE = Util.findMethod(shadowed, "handleDelegateForType", Object.class);
|
||||
ADD_GENERATED_DELEGATE_METHODS = Util.findMethod(shadowed, "addGeneratedDelegateMethods", Object.class, Object.class);
|
||||
Class<?> shadowedPortal = Util.shadowLoadClass("lombok.eclipse.agent.PatchDelegatePortal");
|
||||
HANDLE_DELEGATE_FOR_TYPE = Util.findMethod(shadowedPortal, "handleDelegateForType", Object.class);
|
||||
ADD_GENERATED_DELEGATE_METHODS = Util.findMethod(shadowedPortal, "addGeneratedDelegateMethods", Object.class, Object.class);
|
||||
Class<?> shadowed = Util.shadowLoadClass("lombok.eclipse.agent.PatchDelegate");
|
||||
IS_DELEGATE_SOURCE_METHOD = Util.findMethod(shadowed, "isDelegateSourceMethod", Object.class);
|
||||
RETURN_ELEMENT_INFO = Util.findMethod(shadowed, "returnElementInfo", Object.class);
|
||||
}
|
||||
|
||||
public static boolean handleDelegateForType(Object classScope) {
|
||||
@@ -291,6 +296,14 @@ final class PatchFixesHider {
|
||||
public static Object[] addGeneratedDelegateMethods(Object returnValue, Object javaElement) {
|
||||
return (Object[]) Util.invokeMethod(ADD_GENERATED_DELEGATE_METHODS, returnValue, javaElement);
|
||||
}
|
||||
|
||||
public static boolean isDelegateSourceMethod(Object sourceMethod) {
|
||||
return (Boolean) Util.invokeMethod(IS_DELEGATE_SOURCE_METHOD, sourceMethod);
|
||||
}
|
||||
|
||||
public static Object returnElementInfo(Object delegateSourceMethod) {
|
||||
return Util.invokeMethod(RETURN_ELEMENT_INFO, delegateSourceMethod);
|
||||
}
|
||||
}
|
||||
|
||||
/** Contains patch code to support {@code val} (eclipse specific) */
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package pkg;
|
||||
|
||||
import lombok.experimental.Delegate;
|
||||
|
||||
public class DelegateModel {
|
||||
|
||||
@Delegate
|
||||
private Runnable run;
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import org.junit.runners.Suite.SuiteClasses;
|
||||
import lombok.eclipse.cleanup.CleanupTest;
|
||||
import lombok.eclipse.compile.NoErrorsTest;
|
||||
import lombok.eclipse.edit.SelectTest;
|
||||
import lombok.eclipse.misc.DelegateTest;
|
||||
import lombok.eclipse.misc.JavadocTest;
|
||||
import lombok.eclipse.refactoring.ExtractInterfaceTest;
|
||||
import lombok.eclipse.refactoring.InlineTest;
|
||||
@@ -35,7 +36,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})
|
||||
@SuiteClasses({ExtractInterfaceTest.class, RenameTest.class, SelectTest.class, CleanupTest.class, FindReferencesTest.class, InlineTest.class, NoErrorsTest.class, JavadocTest.class, DelegateTest.class})
|
||||
public class EclipseTests {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2024 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.misc;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.eclipse.jdt.core.ICompilationUnit;
|
||||
import org.eclipse.jdt.core.IMethod;
|
||||
import org.eclipse.jdt.core.ISourceRange;
|
||||
import org.eclipse.jdt.core.WorkingCopyOwner;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import lombok.eclipse.EclipseRunner;
|
||||
import lombok.eclipse.SetupSingleFileTest;
|
||||
|
||||
@RunWith(EclipseRunner.class)
|
||||
public class DelegateTest {
|
||||
|
||||
@Rule
|
||||
public SetupSingleFileTest setup = new SetupSingleFileTest();
|
||||
|
||||
@Test
|
||||
public void model() throws Exception {
|
||||
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("DelegateModel.java");
|
||||
|
||||
WorkingCopyOwner workingCopyOwner = new WorkingCopyOwner() {};
|
||||
ICompilationUnit workingCopy = cu.getWorkingCopy(workingCopyOwner, null);
|
||||
try {
|
||||
workingCopy.reconcile(ICompilationUnit.NO_AST, true, true, workingCopy.getOwner(), null);
|
||||
|
||||
assertThat(workingCopy.findPrimaryType().getMethods().length, is(not(0)));
|
||||
|
||||
IMethod runMethod = workingCopy.findPrimaryType().getMethods()[0];
|
||||
assertEquals(runMethod.getElementName(), "run");
|
||||
|
||||
ISourceRange sourceRange = runMethod.getSourceRange();
|
||||
assertNotNull(sourceRange);
|
||||
assertEquals(sourceRange.getOffset(), 84);
|
||||
assertEquals(sourceRange.getLength(), 9);
|
||||
} finally {
|
||||
workingCopy.discardWorkingCopy();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user