|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 IBM Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * IBM Corporation - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.jdt.ls.core.internal.correction; |
| 14 | + |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 18 | +import org.eclipse.jdt.core.IJavaProject; |
| 19 | +import org.eclipse.jdt.core.IPackageFragment; |
| 20 | +import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 21 | +import org.eclipse.jdt.core.JavaCore; |
| 22 | +import org.eclipse.jdt.internal.corext.fix.FixMessages; |
| 23 | +import org.eclipse.jdt.ls.core.internal.CodeActionUtil; |
| 24 | +import org.eclipse.lsp4j.Position; |
| 25 | +import org.eclipse.lsp4j.Range; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +public class DeprecatedMethodQuickFixTest extends AbstractQuickFixTest { |
| 30 | + |
| 31 | + private IJavaProject fJProject; |
| 32 | + private IPackageFragmentRoot fSourceFolder; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setup() throws Exception { |
| 36 | + fJProject = newEmptyProject(); |
| 37 | + Map<String, String> options = TestOptions.getDefaultOptions(); |
| 38 | + options.put(JavaCore.COMPILER_PB_DEPRECATION, JavaCore.WARNING); |
| 39 | + fJProject.setOptions(options); |
| 40 | + fSourceFolder = fJProject.getPackageFragmentRoot(fJProject.getProject().getFolder("src")); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testSelectionOnMethod() throws Exception { |
| 45 | + ICompilationUnit cu = createDeprecatedMethodTestUnits(); |
| 46 | + String expected = """ |
| 47 | + package test; |
| 48 | +
|
| 49 | + public class E { |
| 50 | +
|
| 51 | + public void test() { |
| 52 | + K.newMethod(); |
| 53 | + } |
| 54 | + } |
| 55 | + """; |
| 56 | + Range selection = CodeActionUtil.getRange(cu, "deprecatedMethod"); |
| 57 | + Expected e = new Expected(FixMessages.InlineDeprecatedMethod_msg, expected); |
| 58 | + assertCodeActions(cu, selection, e); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testCaretInsideMethod() throws Exception { |
| 63 | + ICompilationUnit cu = createDeprecatedMethodTestUnits(); |
| 64 | + Position start = CodeActionUtil.getRange(cu, "deprecatedMethod").getStart(); |
| 65 | + Range selection = new Range(new Position(start.getLine(), start.getCharacter() + 1), new Position(start.getLine(), start.getCharacter() + 1)); |
| 66 | + |
| 67 | + String expected = """ |
| 68 | + package test; |
| 69 | +
|
| 70 | + public class E { |
| 71 | +
|
| 72 | + public void test() { |
| 73 | + K.newMethod(); |
| 74 | + } |
| 75 | + } |
| 76 | + """; |
| 77 | + Expected e = new Expected(FixMessages.InlineDeprecatedMethod_msg, expected); |
| 78 | + assertCodeActions(cu, selection, e); |
| 79 | + } |
| 80 | + |
| 81 | + private ICompilationUnit createDeprecatedMethodTestUnits() throws Exception { |
| 82 | + IPackageFragment pack = fSourceFolder.createPackageFragment("test", false, null); |
| 83 | + |
| 84 | + String str2 = """ |
| 85 | + package test; |
| 86 | +
|
| 87 | + public class K { |
| 88 | + public static void newMethod() { |
| 89 | + System.out.println("Calling newMethod()"); |
| 90 | + } |
| 91 | + } |
| 92 | + """; |
| 93 | + pack.createCompilationUnit("K.java", str2, false, null); |
| 94 | + |
| 95 | + String str1 = """ |
| 96 | + package test; |
| 97 | +
|
| 98 | + public class B { |
| 99 | +
|
| 100 | + /** |
| 101 | + * @deprecated use {@link K#newMethod()} instead |
| 102 | + */ |
| 103 | + @Deprecated |
| 104 | + public static void deprecatedMethod() { |
| 105 | + K.newMethod(); |
| 106 | + } |
| 107 | + } |
| 108 | + """; |
| 109 | + pack.createCompilationUnit("B.java", str1, false, null); |
| 110 | + |
| 111 | + String str = """ |
| 112 | + package test; |
| 113 | +
|
| 114 | + public class E { |
| 115 | +
|
| 116 | + public void test() { |
| 117 | + B.deprecatedMethod(); |
| 118 | + } |
| 119 | + } |
| 120 | + """; |
| 121 | + return pack.createCompilationUnit("E.java", str, false, null); |
| 122 | + } |
| 123 | +} |
0 commit comments