| /******************************************************************************* |
| * Copyright (c) 2019 Fabrice TIERCELIN 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: |
| * Fabrice TIERCELIN - 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.text.edits.TextEditGroup; |
| |
| import org.eclipse.jdt.core.ICompilationUnit; |
| import org.eclipse.jdt.core.dom.AST; |
| 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.NumberLiteral; |
| import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; |
| |
| import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; |
| import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFix; |
| import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFix.CompilationUnitRewriteOperation; |
| import org.eclipse.jdt.internal.corext.fix.LinkedProposalModel; |
| import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; |
| |
| import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; |
| import org.eclipse.jdt.ui.cleanup.ICleanUpFix; |
| import org.eclipse.jdt.ui.text.java.IProblemLocation; |
| |
| /** |
| * A fix that puts the long literal suffixes into uppercase: |
| * <ul> |
| * <li>Lowercase suffix is ambiguous. 'l' can be seen as '1'.</li> |
| * </ul> |
| */ |
| public class NumberSuffixCleanUp extends AbstractMultiFix { |
| public NumberSuffixCleanUp() { |
| this(Collections.emptyMap()); |
| } |
| |
| public NumberSuffixCleanUp(Map<String, String> options) { |
| super(options); |
| } |
| |
| @Override |
| public CleanUpRequirements getRequirements() { |
| boolean requireAST= isEnabled(CleanUpConstants.NUMBER_SUFFIX); |
| Map<String, String> requiredOptions= null; |
| return new CleanUpRequirements(requireAST, false, false, requiredOptions); |
| } |
| |
| @Override |
| public String[] getStepDescriptions() { |
| if (isEnabled(CleanUpConstants.NUMBER_SUFFIX)) { |
| return new String[] { MultiFixMessages.CodeStyleCleanUp_numberSuffix_description }; |
| } |
| return new String[0]; |
| } |
| |
| @Override |
| public String getPreview() { |
| StringBuilder bld= new StringBuilder(); |
| |
| if (isEnabled(CleanUpConstants.NUMBER_SUFFIX)) { |
| bld.append("long number1 = 10L;\n"); //$NON-NLS-1$ |
| } else { |
| bld.append("long number1 = 10l;\n"); //$NON-NLS-1$ |
| } |
| |
| return bld.toString(); |
| } |
| |
| @Override |
| protected ICleanUpFix createFix(CompilationUnit unit) throws CoreException { |
| if (!isEnabled(CleanUpConstants.NUMBER_SUFFIX)) { |
| return null; |
| } |
| |
| final List<CompilationUnitRewriteOperation> rewriteOperations= new ArrayList<>(); |
| |
| unit.accept(new ASTVisitor() { |
| @Override |
| public boolean visit(NumberLiteral node) { |
| final String token= node.getToken(); |
| |
| if (token.endsWith("l")) { //$NON-NLS-1$ |
| rewriteOperations.add(new NumberSuffixOperation(node, token)); |
| return false; |
| } |
| |
| return true; |
| } |
| }); |
| |
| if (rewriteOperations.isEmpty()) { |
| return null; |
| } |
| |
| return new CompilationUnitRewriteOperationsFix(MultiFixMessages.CodeStyleCleanUp_numberSuffix_description, unit, |
| rewriteOperations.toArray(new CompilationUnitRewriteOperation[rewriteOperations.size()])); |
| } |
| |
| @Override |
| public boolean canFix(ICompilationUnit compilationUnit, IProblemLocation problem) { |
| return false; |
| } |
| |
| @Override |
| protected ICleanUpFix createFix(CompilationUnit unit, IProblemLocation[] problems) throws CoreException { |
| return null; |
| } |
| |
| private static class NumberSuffixOperation extends CompilationUnitRewriteOperation { |
| private final ASTNode node; |
| |
| private final String token; |
| |
| public NumberSuffixOperation(NumberLiteral node, String token) { |
| this.node= node; |
| this.token= token; |
| } |
| |
| @Override |
| public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel linkedModel) throws CoreException { |
| ASTRewrite rewrite= cuRewrite.getASTRewrite(); |
| AST ast= cuRewrite.getRoot().getAST(); |
| TextEditGroup group= createTextEditGroup(MultiFixMessages.CodeStyleCleanUp_numberSuffix_description, cuRewrite); |
| |
| final String newToken= token.substring(0, token.length() - 1) + token.substring(token.length() - 1).toUpperCase(); |
| final NumberLiteral replacement= ast.newNumberLiteral(newToken); |
| |
| rewrite.replace(node, replacement, group); |
| } |
| } |
| } |