[fixes #3529] Set real field initializer source

Eclipse copies the field initializer source code if it is a constant.
For generated nodes we have to read it from the ast node and not from
the source file.
This commit is contained in:
Rawi01 committed 2024-01-15 23:06:21 +01:00
1 parent fd55c7aa93
commit f277791cf0
7 files changed
+193 -3

No files matched your search

@@ -0,0 +1,5 @@
package pkg;
public @interface Annotation {
String value();
}
@@ -0,0 +1,8 @@
package pkg;
import lombok.experimental.FieldNameConstants;
@FieldNameConstants
public class Constants {
String test;
}
@@ -0,0 +1,5 @@
package pkg;
@Annotation(Constants.Fields.test)
public class Usage {
}
@@ -5,6 +5,7 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import lombok.eclipse.cleanup.CleanupTest;
import lombok.eclipse.compile.NoErrorsTest;
import lombok.eclipse.edit.SelectTest;
import lombok.eclipse.refactoring.ExtractInterfaceTest;
import lombok.eclipse.refactoring.InlineTest;
@@ -12,7 +13,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})
@SuiteClasses({ExtractInterfaceTest.class, RenameTest.class, SelectTest.class, CleanupTest.class, FindReferencesTest.class, InlineTest.class, NoErrorsTest.class})
public class EclipseTests {
}
@@ -0,0 +1,68 @@
package lombok.eclipse.compile;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IProblemRequestor;
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.compiler.IProblem;
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 NoErrorsTest {
@Rule
public SetupSingleFileTest setup = new SetupSingleFileTest();
@Test
public void fieldNameConstantsInAnnotation() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("Usage.java");
final List<IProblem> problems = new ArrayList<IProblem>();
final IProblemRequestor requestor = new IProblemRequestor() {
@Override
public void acceptProblem(IProblem problem) {
problems.add(problem);
}
@Override
public void beginReporting() {
problems.clear();
}
@Override
public void endReporting() {
}
@Override
public boolean isActive() {
return true;
}
};
WorkingCopyOwner workingCopyOwner = new WorkingCopyOwner() {
@Override
public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
return requestor;
}
};
ICompilationUnit workingCopy = cu.getWorkingCopy(workingCopyOwner, null);
try {
workingCopy.reconcile(ICompilationUnit.NO_AST, true, true, workingCopy.getOwner(), null);
} finally {
workingCopy.discardWorkingCopy();
}
System.out.println(problems);
assertTrue(problems.isEmpty());
}
}