Skip to content

Commit

Permalink
Add new feature to convert pattern instanceof to switch (#1972)
Browse files Browse the repository at this point in the history
* Add new feature to convert pattern instanceof to switch

- add new PatternInstanceofToSwitchCleanUpCore and
  PatternInstanceofToSwitchFixCore classes to convert
  a nesting if statement using pattern instanceof expressions
  into either a switch statement or switch expression if possible
- add new cleanup to JavaFeatureTabPage, plugin, and CleanUpConstants
- add rtstubs_21.jar for testing
- add new CleanUpTest21 class and add tests for new feature
- fixes #1919
  • Loading branch information
jjohnstn authored Jan 24, 2025
1 parent c3f2219 commit 8c58ba0
Show file tree
Hide file tree
Showing 16 changed files with 1,316 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 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 @@ -78,6 +78,7 @@ private MultiFixMessages() {
public static String CodeStyleCleanUp_numberSuffix_description;
public static String CodeStyleCleanUp_QualifyNonStaticMethod_description;
public static String CodeStyleCleanUp_QualifyStaticMethod_description;
public static String CodeStyleCleanUp_PatternInstanceOfToSwitch_description;
public static String CodeStyleCleanUp_removeFieldThis_description;
public static String CodeStyleCleanUp_removeMethodThis_description;
public static String CodeStyleCleanUp_Switch_description;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2005, 2024 IBM Corporation and others.
# Copyright (c) 2005, 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 @@ -53,6 +53,7 @@ CodeStyleCleanUp_removeFieldThis_description=Remove 'this' qualifier for non sta
CodeStyleCleanUp_removeMethodThis_description=Remove 'this' qualifier for non static method accesses
CodeStyleMultiFix_ConvertSingleStatementInControlBodyToBlock_description=Convert control statement bodies to block
CodeStyleCleanUp_Switch_description=Convert if/else if/else chain to switch
CodeStyleCleanUp_PatternInstanceOfToSwitch_description=Convert pattern instanceOf if/else if/else to switch

Java50MultiFix_AddMissingDeprecated_description=Add missing '@Deprecated' annotations
Java50CleanUp_ConvertToEnhancedForLoop_description=Convert to enhanced 'for' loops
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*******************************************************************************
* 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.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.jdt.core.dom.CompilationUnit;

import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.PatternInstanceofToSwitchFixCore;

import org.eclipse.jdt.ui.cleanup.CleanUpContext;
import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;

public class PatternInstanceofToSwitchCleanUpCore extends AbstractCleanUp {
public PatternInstanceofToSwitchCleanUpCore(final Map<String, String> options) {
super(options);
}

public PatternInstanceofToSwitchCleanUpCore() {
}

@Override
public CleanUpRequirements getRequirements() {
return new CleanUpRequirements(requireAST(), false, false, null);
}

public boolean requireAST() {
return isEnabled(CleanUpConstants.USE_SWITCH_FOR_INSTANCEOF_PATTERN);
}

@Override
public ICleanUpFix createFix(final CleanUpContext context) throws CoreException {
CompilationUnit compilationUnit= context.getAST();

if (compilationUnit == null) {
return null;
}

boolean convertIfElseInstanceOfChainToSwitch= isEnabled(CleanUpConstants.USE_SWITCH_FOR_INSTANCEOF_PATTERN);

if (!convertIfElseInstanceOfChainToSwitch) {
return null;
}

return PatternInstanceofToSwitchFixCore.createCleanUp(compilationUnit);
}

@Override
public String[] getStepDescriptions() {
List<String> result= new ArrayList<>();

if (isEnabled(CleanUpConstants.USE_SWITCH_FOR_INSTANCEOF_PATTERN)) {
result.add(MultiFixMessages.CodeStyleCleanUp_PatternInstanceOfToSwitch_description);
}

return result.toArray(new String[0]);
}

@Override
public String getPreview() {
if (isEnabled(CleanUpConstants.USE_SWITCH_FOR_INSTANCEOF_PATTERN)) {
return """
switch (x) {
case Integer xInt -> i = xInt.intValue();
case Double xDouble -> d = xDouble.doubleValue():
case Boolean xBoolean -> b = xBoolean.booleanValue();
case null, default -> {
i = 0;
d = 0.0D;
b = false;
}
}
"""; //$NON-NLS-1$
}

return """
if (x instanceof Integer xInt) {
i = xInt.intValue();
} else if (x instanceof Double xDouble) {
d = xDouble.doubleValue();
} else if (x instancoef Boolean) {
b = xBoolean.booleanValue();
} else {
i = 0;
d = 0.0D;
b = false;
}
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,17 @@ public class CleanUpConstants {
*/
public static final String USE_SWITCH= "cleanup.switch"; //$NON-NLS-1$

/**
* Replaces {@code if x instanceof Type t1}/{@code else if x instanceof Type t2}/{@code else} blocks to use {@code switch} where possible.
* <p>
* Possible values: {TRUE, FALSE}
*
* @see CleanUpOptions#TRUE
* @see CleanUpOptions#FALSE
* @since 4.35
*/
public static final String USE_SWITCH_FOR_INSTANCEOF_PATTERN= "cleanup.switch_for_instanceof_pattern"; //$NON-NLS-1$

/**
* Convert switch statements to switch expressions.<br>
* <p>
Expand Down
Loading

0 comments on commit 8c58ba0

Please sign in to comment.