Skip to content

Commit

Permalink
Support @DeleGate in eclipse 2024-03
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 authored and rspilker committed Mar 18, 2024
1 parent 98cdf67 commit 3b20b70
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,13 @@ private static void addPatchesForDelegate(ScriptManager sm) {
.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) {
Expand Down
14 changes: 12 additions & 2 deletions src/eclipseAgent/lombok/eclipse/agent/PatchDelegate.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -726,6 +727,15 @@ public static Object[] addGeneratedDelegateMethods(Object[] returnValue, Object
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);
Expand Down
19 changes: 16 additions & 3 deletions src/eclipseAgent/lombok/launch/PatchFixesHider.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,16 @@ public static void transform_swapped(Object ast, Object parser) throws IOExcepti
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) {
Expand All @@ -291,6 +296,14 @@ public static boolean handleDelegateForType(Object classScope) {
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) */
Expand Down
9 changes: 9 additions & 0 deletions test/eclipse/resource/delegate/model/DelegateModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pkg;

import lombok.experimental.Delegate;

public class DelegateModel {

@Delegate
private Runnable run;
}
3 changes: 2 additions & 1 deletion test/eclipse/src/lombok/eclipse/EclipseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
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;
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 {

}
66 changes: 66 additions & 0 deletions test/eclipse/src/lombok/eclipse/misc/DelegateTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit 3b20b70

Please sign in to comment.