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
Expand Up @@ -159,6 +159,7 @@ private MultiFixMessages() {
public static String RedundantModifiersCleanup_description;
public static String SubstringCleanUp_description;
public static String InlineDeprecatedMethodCleanUp_description;
public static String ReplaceDeprecatedFieldsCleanUp_description;
public static String JoinCleanup_description;
public static String ArraysFillCleanUp_description;
public static String EvaluateNullableCleanUp_description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ HashCleanup_description=Use Objects.hash()
RedundantModifiersCleanup_description=Remove redundant modifiers
SubstringCleanUp_description=Remove redundant String.substring() parameter
InlineDeprecatedMethodCleanUp_description=Replace deprecated calls with inlined content where possible
ReplaceDeprecatedFieldsCleanUp_description=Replace deprecated fields where possible
JoinCleanup_description=Use String.join()
ArraysFillCleanUp_description=Use Arrays.fill() when possible
EvaluateNullableCleanUp_description=Evaluate without null check
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*******************************************************************************
* Copyright (c) 2025 Red Hat Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.fix;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SuperFieldAccess;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.fix.ReplaceDeprecatedFieldFixCore;

import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
import org.eclipse.jdt.ui.text.java.IProblemLocation;

import org.eclipse.jdt.internal.ui.text.correction.QuickAssistProcessorUtil;

public class ReplaceDeprecatedFieldCleanUpCore extends AbstractMultiFix {

public ReplaceDeprecatedFieldCleanUpCore() {
this(Collections.emptyMap());
}

public ReplaceDeprecatedFieldCleanUpCore(final Map<String, String> options) {
super(options);
}

@Override
public CleanUpRequirements getRequirements() {
boolean requireAST= isEnabled(CleanUpConstants.REPLACE_DEPRECATED_FIELDS);
return new CleanUpRequirements(requireAST, false, false, null);
}

@Override
public String[] getStepDescriptions() {
if (isEnabled(CleanUpConstants.REPLACE_DEPRECATED_FIELDS)) {
return new String[] { MultiFixMessages.ReplaceDeprecatedFieldsCleanUp_description };
}
return new String[0];
}

@Override
public String getPreview() {
StringBuilder bld= new StringBuilder();
bld.append("Class E {\n"); //$NON-NLS-1$
bld.append(" /**\n"); //$NON-NLS-1$
bld.append(" * @deprecated use {@link K#field2} instead\n"); //$NON-NLS-1$
bld.append(" */\n"); //$NON-NLS-1$
bld.append(" @Deprecated\n"); //$NON-NLS-1$
bld.append(" public int field1;\n"); //$NON-NLS-1$
bld.append("}\n\n"); //$NON-NLS-1$
if (isEnabled(CleanUpConstants.REPLACE_DEPRECATED_FIELDS)) {
bld.append("return K.field2;\n"); //$NON-NLS-1$
} else {
bld.append("return E.field1;\n"); //$NON-NLS-1$
}
return bld.toString();
}

@Override
public boolean canFix(ICompilationUnit compilationUnit, IProblemLocation problem) {
return false;
}

@Override
protected ICleanUpFix createFix(CompilationUnit compilationUnit) throws CoreException {
if (!isEnabled(CleanUpConstants.REPLACE_DEPRECATED_FIELDS)) {
return null;
}
if (compilationUnit == null)
return null;

final List<ReplaceDeprecatedFieldFixCore.ReplaceDeprecatedFieldProposalOperation> deprecatedFields= new ArrayList<>();
ASTVisitor visitor= new ASTVisitor() {
@Override
public boolean visit(QualifiedName node) {
String replacement= QuickAssistProcessorUtil.getDeprecatedFieldReplacement(node);
if (replacement != null) {
deprecatedFields.add(new ReplaceDeprecatedFieldFixCore.ReplaceDeprecatedFieldProposalOperation(node, replacement));
}
return false;
}
@Override
public boolean visit(SimpleName node) {
if (node.getLocationInParent() == VariableDeclarationFragment.NAME_PROPERTY) {
return true;
}
String replacement= QuickAssistProcessorUtil.getDeprecatedFieldReplacement(node);
if (replacement != null) {
deprecatedFields.add(new ReplaceDeprecatedFieldFixCore.ReplaceDeprecatedFieldProposalOperation(node, replacement));
}
return false;
}
@Override
public boolean visit(FieldAccess node) {
String replacement= QuickAssistProcessorUtil.getDeprecatedFieldReplacement(node);
if (replacement != null) {
deprecatedFields.add(new ReplaceDeprecatedFieldFixCore.ReplaceDeprecatedFieldProposalOperation(node, replacement));
}
return false;
}
@Override
public boolean visit(SuperFieldAccess node) {
String replacement= QuickAssistProcessorUtil.getDeprecatedFieldReplacement(node);
if (replacement != null) {
deprecatedFields.add(new ReplaceDeprecatedFieldFixCore.ReplaceDeprecatedFieldProposalOperation(node, replacement));
}
return false;
}
};
compilationUnit.accept(visitor);
if (deprecatedFields.isEmpty()) {
return null;
}
return new ReplaceDeprecatedFieldFixCore(getPreview(), compilationUnit, deprecatedFields.toArray(new CompilationUnitRewriteOperation[0]));
}

@Override
public int computeNumberOfFixes(CompilationUnit compilationUnit) {
if (!isEnabled(CleanUpConstants.REPLACE_DEPRECATED_FIELDS)) {
return 0;
}
if (compilationUnit == null)
return 0;

final List<ASTNode> deprecatedFields= new ArrayList<>();
ASTVisitor visitor= new ASTVisitor() {
@Override
public boolean visit(QualifiedName node) {
if (QuickAssistProcessorUtil.getDeprecatedFieldReplacement(node) != null) {
deprecatedFields.add(node);
}
return true;
}
@Override
public boolean visit(FieldAccess node) {
if (QuickAssistProcessorUtil.getDeprecatedFieldReplacement(node) != null) {
deprecatedFields.add(node);
}
return true;
}
@Override
public boolean visit(SuperFieldAccess node) {
if (QuickAssistProcessorUtil.getDeprecatedFieldReplacement(node) != null) {
deprecatedFields.add(node);
}
return true;
}
};
compilationUnit.accept(visitor);
return deprecatedFields.size();
}

@Override
protected ICleanUpFix createFix(CompilationUnit unit, IProblemLocation[] problems) throws CoreException {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2024 IBM Corporation and others.
* Copyright (c) 2012, 2025 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 @@ -58,6 +58,7 @@ public interface IProposalRelevance {
int CREATE_OPTIONAL= 9;
int CREATE_OPTIONAL_OF_NULLABLE= 9;
int INLINE_DEPRECATED_METHOD= 9;
int REPLACE_DEPRECATED_FIELD= 9;

int ADD_ABSTRACT_MODIFIER= 8;
int ADD_STATIC_MODIFIER= 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
Expand All @@ -41,6 +42,8 @@
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ExpressionMethodReference;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IDocElement;
Expand All @@ -49,16 +52,19 @@
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.LambdaExpression;
import org.eclipse.jdt.core.dom.MemberRef;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.MethodRef;
import org.eclipse.jdt.core.dom.MethodReference;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.SuperFieldAccess;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.SuperMethodReference;
import org.eclipse.jdt.core.dom.TagElement;
Expand Down Expand Up @@ -743,6 +749,108 @@ public static CompilationUnit findCUForMethod(CompilationUnit compilationUnit, I
return compilationUnit;
}

public static String getDeprecatedFieldReplacement(ASTNode node) {
IBinding binding= null;
switch (node) {
case QualifiedName q:
binding= q.resolveBinding();
break;
case SimpleName s:
if (s.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
node= s.getParent();
binding= ((QualifiedName)node).resolveBinding();
} else {
binding= s.resolveBinding();
}
break;
case FieldAccess f:
// if (f.getExpression() instanceof MethodInvocation) {
// return null;
// }
binding= f.resolveFieldBinding();
break;
case SuperFieldAccess sf:
binding= sf.resolveFieldBinding();
break;
default:
return null;
}
if (binding instanceof IVariableBinding varBinding && varBinding.isField()) {
IField field= (IField)varBinding.getJavaElement();
if (field == null) {
return null;
}
IAnnotationBinding[] annotations= varBinding.getAnnotations();
for (IAnnotationBinding annotation : annotations) {
if (annotation.getAnnotationType().getQualifiedName().equals("java.lang.Deprecated")) { //$NON-NLS-1$
CompilationUnit sourceCu= (CompilationUnit)node.getRoot();
CompilationUnit cu= findCUForField(sourceCu, (ICompilationUnit)sourceCu.getJavaElement(), varBinding);
if (cu == null) {
return null;
}
try {
FieldDeclaration fieldDeclaration= ASTNodeSearchUtil.getFieldDeclarationNode(field, cu);
Javadoc javadoc= fieldDeclaration.getJavadoc();
if (javadoc == null) {
return null;
}
List<TagElement> tags= javadoc.tags();
for (TagElement tag : tags) {
if ("@deprecated".equals(tag.getTagName())) { //$NON-NLS-1$
List<IDocElement> fragments= tag.fragments();
if (fragments.size() < 2) {
return null;
}
if (fragments.get(0) instanceof TextElement textElement) {
String text= textElement.getText().toLowerCase().trim();
if (text.endsWith("use") || text.endsWith("replace by")) { //$NON-NLS-1$ //$NON-NLS-2$
if (fragments.get(1) instanceof TagElement tagElement) {
if ("@link".equals(tagElement.getTagName())) { //$NON-NLS-1$
List<IDocElement> linkFragments= tagElement.fragments();
if (linkFragments.size() == 1) {
IDocElement linkFragment= linkFragments.get(0);
if (linkFragment instanceof MemberRef methodRef) {
IBinding refBinding= methodRef.resolveBinding();
if (refBinding instanceof IVariableBinding replaceBinding && replaceBinding.isField()) {
return replaceBinding.getDeclaringClass().getQualifiedName() + "." + replaceBinding.getName(); //$NON-NLS-1$
}
}
}
}
}
}
}
}
}
} catch (JavaModelException e) {
// ignore
}
}
}
}
return null;
}

public static CompilationUnit findCUForField(CompilationUnit compilationUnit, ICompilationUnit cu, IVariableBinding fieldBinding) {
ASTNode fieldDecl= compilationUnit.findDeclaringNode(fieldBinding.getVariableDeclaration());
if (fieldDecl == null) {
// is field defined in another CU?
ITypeBinding declaringTypeDecl= fieldBinding.getDeclaringClass().getTypeDeclaration();
if (declaringTypeDecl.isFromSource()) {
ICompilationUnit targetCU= null;
try {
targetCU= ASTResolving.findCompilationUnitForBinding(cu, compilationUnit, declaringTypeDecl);
} catch (JavaModelException e) { /* can't do better */
}
if (targetCU != null) {
return ASTResolving.createQuickFixAST(targetCU, null);
}
}
return null;
}
return compilationUnit;
}

public static ASTNode getCopyOfInner(ASTRewrite rewrite, ASTNode statement, boolean toControlStatementBody) {
if (statement.getNodeType() == ASTNode.BLOCK) {
Block block= (Block) statement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2025 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 @@ -1301,10 +1301,21 @@ public class CleanUpConstants {
*
* @see CleanUpOptions#TRUE
* @see CleanUpOptions#FALSE
* @since 4.30
* @since 4.37
*/
public static final String REPLACE_DEPRECATED_CALLS= "cleanup.replace_deprecated_calls"; //$NON-NLS-1$

/**
* Replace deprecated fields with specified replacement fields if possible.
* <p>
* Possible values: {TRUE, FALSE}
*
* @see CleanUpOptions#TRUE
* @see CleanUpOptions#FALSE
* @since 4.30
*/
public static final String REPLACE_DEPRECATED_FIELDS= "cleanup.replace_deprecated_fields"; //$NON-NLS-1$

/**
* Replaces {@code String.replaceAll()} by {@code String.replace()}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ private FixMessages() {
public static String StringConcatToTextBlockFix_convert_msg;
public static String LambdaExpressionAndMethodRefFix_clean_up_expression_msg;
public static String InlineDeprecatedMethod_msg;
public static String ReplaceDeprecatedField_msg;

static {
// initialize resource bundle
Expand Down
Loading
Loading