Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* Copyright (c) 2019, 2026 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -50,6 +50,7 @@
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IExtendedModifier;
import org.eclipse.jdt.core.dom.IMethodBinding;
Expand Down Expand Up @@ -469,6 +470,44 @@ public void getAddMethodModifierProposal(IInvocationContext context, IProblemLoc
return;
}

if (modifier == Modifier.STATIC) {
MethodDeclaration methodDeclaration= (MethodDeclaration)selectedNode;
List<IExtendedModifier> modifiers= methodDeclaration.modifiers();
for (IExtendedModifier declModifier : modifiers) {
if (declModifier instanceof Annotation annotation) {
switch (annotation.getTypeName().getFullyQualifiedName()) {
case "BeforeEach": //$NON-NLS-1$
case "AfterEach": //$NON-NLS-1$
case "BeforeAll": //$NON-NLS-1$
case "AfterAll": //$NON-NLS-1$
case "Before": //$NON-NLS-1$
case "After": //$NON-NLS-1$
case "Test": //$NON-NLS-1$
case "TestTemplate": //$NON-NLS-1$
case "TestFactory": //$NON-NLS-1$
case "ParameterizedTest": //$NON-NLS-1$
case "RepeatedTest": //$NON-NLS-1$
IAnnotationBinding binding= annotation.resolveAnnotationBinding();
if (binding != null) {
ITypeBinding typeBinding= binding.getAnnotationType();
String packageName= typeBinding.getPackage().getName();
if (packageName.equals("org.junit") //$NON-NLS-1$
|| packageName.equals("org.junit.jupiter.api") //$NON-NLS-1$
|| packageName.equals("org.junit.jupiter.params")) { //$NON-NLS-1$
return;
}
}
break;
default:
break;
}
IAnnotationBinding binding= annotation.resolveAnnotationBinding();
if (binding != null) {
System.out.println(annotation.getTypeName().getFullyQualifiedName());
}
}
}
}
IMethodBinding binding= ((MethodDeclaration) selectedNode).resolveBinding();
if (binding != null) {
binding= binding.getMethodDeclaration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
import org.junit.Rule;
import org.junit.Test;

import org.eclipse.jdt.junit.JUnitCore;
import org.eclipse.jdt.testplugin.JavaProjectHelper;
import org.eclipse.jdt.testplugin.TestOptions;

import org.eclipse.jface.preference.IPreferenceStore;

import org.eclipse.jface.text.contentassist.ICompletionProposal;

import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
Expand All @@ -54,6 +56,7 @@
import org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;

public class ModifierCorrectionsQuickFixTest extends QuickFixTest {

Expand Down Expand Up @@ -85,6 +88,9 @@ public void setUp() throws Exception {
StubUtility.setCodeTemplate(CodeTemplateContextType.CONSTRUCTORSTUB_ID, "", null);

fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");

IClasspathEntry cpe= JavaCore.newContainerEntry(JUnitCore.JUNIT5_CONTAINER_PATH);
JavaProjectHelper.addToClasspath(fJProject1, cpe);
}

@After
Expand Down Expand Up @@ -4113,6 +4119,71 @@ private static void foo() {
assertEqualString(preview, str1);
}

@Test
public void testMethodCanBeStatic2() throws Exception {
Hashtable<String, String> hashtable= JavaCore.getOptions();
hashtable.put(JavaCore.COMPILER_PB_MISSING_STATIC_ON_METHOD, JavaCore.ERROR);
hashtable.put(JavaCore.COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD, JavaCore.WARNING);
JavaCore.setOptions(hashtable);

IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String str= """
package test1;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class E {
@BeforeEach
public void setup() {
}
@Test
public void foo() {
System.out.println("doesn't need class");
}
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("E.java", str, false, null);

CompilationUnit astRoot= getASTRoot(cu);
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1);
assertNumberOfProposals(proposals, 2);
assertCorrectLabels(proposals);

assertProposalDoesNotExist(proposals, CorrectionMessages.ModifierCorrectionSubProcessor_addstatic_description);
}

@Test
public void testMethodCanBeStatic3() throws Exception {
Hashtable<String, String> hashtable= JavaCore.getOptions();
hashtable.put(JavaCore.COMPILER_PB_MISSING_STATIC_ON_METHOD, JavaCore.ERROR);
hashtable.put(JavaCore.COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD, JavaCore.WARNING);
JavaCore.setOptions(hashtable);

IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String str= """
package test1;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class E {
@BeforeEach
public void setup() {
System.out.println("setup");
}
@Test
public void foo() {
System.out.println("doesn't need class");
}
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("E.java", str, false, null);

CompilationUnit astRoot= getASTRoot(cu);
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 2);
assertNumberOfProposals(proposals, 3);
assertCorrectLabels(proposals);

assertProposalDoesNotExist(proposals, CorrectionMessages.ModifierCorrectionSubProcessor_addstatic_description);
}

@Test
public void testMethodCanPotentiallyBeStatic() throws Exception {
Hashtable<String, String> hashtable= JavaCore.getOptions();
Expand Down
Loading