|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2014, 2017 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 v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Originally copied from org.eclipse.jdt.internal.corext.fix.TypeParametersFix |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * IBM Corporation - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.jdt.ls.core.internal.corext.fix; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Iterator; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.eclipse.core.runtime.CoreException; |
| 20 | +import org.eclipse.jdt.core.compiler.IProblem; |
| 21 | +import org.eclipse.jdt.core.dom.AST; |
| 22 | +import org.eclipse.jdt.core.dom.ASTNode; |
| 23 | +import org.eclipse.jdt.core.dom.ASTVisitor; |
| 24 | +import org.eclipse.jdt.core.dom.CompilationUnit; |
| 25 | +import org.eclipse.jdt.core.dom.ITypeBinding; |
| 26 | +import org.eclipse.jdt.core.dom.ParameterizedType; |
| 27 | +import org.eclipse.jdt.core.dom.Statement; |
| 28 | +import org.eclipse.jdt.core.dom.Type; |
| 29 | +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; |
| 30 | +import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; |
| 31 | +import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.TypeLocation; |
| 32 | +import org.eclipse.jdt.core.dom.rewrite.ListRewrite; |
| 33 | +import org.eclipse.jdt.ls.core.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext; |
| 34 | +import org.eclipse.jdt.ls.core.internal.corext.refactoring.structure.CompilationUnitRewrite; |
| 35 | +import org.eclipse.jdt.ls.core.internal.corext.refactoring.util.NoCommentSourceRangeComputer; |
| 36 | +import org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation; |
| 37 | +import org.eclipse.jdt.ls.core.internal.corrections.ProblemLocation; |
| 38 | +import org.eclipse.text.edits.TextEditGroup; |
| 39 | + |
| 40 | +public class TypeParametersFix extends CompilationUnitRewriteOperationsFix { |
| 41 | + |
| 42 | + public static final class InsertTypeArgumentsVisitor extends ASTVisitor { |
| 43 | + |
| 44 | + private final ArrayList<ASTNode> fNodes; |
| 45 | + |
| 46 | + public InsertTypeArgumentsVisitor(ArrayList<ASTNode> nodes) { |
| 47 | + fNodes= nodes; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean visit(ParameterizedType createdType) { |
| 52 | + if (createdType == null || createdType.typeArguments().size() != 0) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + ITypeBinding binding= createdType.resolveBinding(); |
| 57 | + if (binding == null) { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + ITypeBinding[] typeArguments= binding.getTypeArguments(); |
| 62 | + if (typeArguments.length == 0) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + fNodes.add(createdType); |
| 67 | + return true; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static class InsertTypeArgumentsOperation extends CompilationUnitRewriteOperation { |
| 72 | + |
| 73 | + private final ParameterizedType[] fCreatedTypes; |
| 74 | + |
| 75 | + public InsertTypeArgumentsOperation(ParameterizedType[] parameterizedTypes) { |
| 76 | + fCreatedTypes= parameterizedTypes; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { |
| 81 | + TextEditGroup group= createTextEditGroup(FixMessages.TypeParametersFix_insert_inferred_type_arguments_description, cuRewrite); |
| 82 | + |
| 83 | + ASTRewrite rewrite= cuRewrite.getASTRewrite(); |
| 84 | + ImportRewrite importRewrite= cuRewrite.getImportRewrite(); |
| 85 | + AST ast= cuRewrite.getRoot().getAST(); |
| 86 | + |
| 87 | + for (int i= 0; i < fCreatedTypes.length; i++) { |
| 88 | + ParameterizedType createdType= fCreatedTypes[i]; |
| 89 | + |
| 90 | + ITypeBinding[] typeArguments= createdType.resolveBinding().getTypeArguments(); |
| 91 | + ContextSensitiveImportRewriteContext importContext= new ContextSensitiveImportRewriteContext(cuRewrite.getRoot(), createdType.getStartPosition(), importRewrite); |
| 92 | + |
| 93 | + ListRewrite argumentsRewrite= rewrite.getListRewrite(createdType, ParameterizedType.TYPE_ARGUMENTS_PROPERTY); |
| 94 | + for (int j= 0; j < typeArguments.length; j++) { |
| 95 | + ITypeBinding typeArgument= typeArguments[j]; |
| 96 | + Type argumentNode= importRewrite.addImport(typeArgument, ast, importContext, TypeLocation.TYPE_ARGUMENT); |
| 97 | + argumentsRewrite.insertLast(argumentNode, group); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static class RemoveTypeArgumentsOperation extends CompilationUnitRewriteOperation { |
| 104 | + |
| 105 | + private final ParameterizedType fParameterizedType; |
| 106 | + |
| 107 | + public RemoveTypeArgumentsOperation(ParameterizedType parameterizedType) { |
| 108 | + fParameterizedType= parameterizedType; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException { |
| 113 | + TextEditGroup group= createTextEditGroup(FixMessages.TypeParametersFix_remove_redundant_type_arguments_description, cuRewrite); |
| 114 | + |
| 115 | + ASTRewrite rewrite= cuRewrite.getASTRewrite(); |
| 116 | + rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer()); |
| 117 | + |
| 118 | + ListRewrite listRewrite= rewrite.getListRewrite(fParameterizedType, ParameterizedType.TYPE_ARGUMENTS_PROPERTY); |
| 119 | + |
| 120 | + List<Type> typeArguments= fParameterizedType.typeArguments(); |
| 121 | + for (Iterator<Type> iterator= typeArguments.iterator(); iterator.hasNext();) { |
| 122 | + listRewrite.remove(iterator.next(), group); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static TypeParametersFix createInsertInferredTypeArgumentsFix(CompilationUnit compilationUnit, ParameterizedType node) { |
| 128 | + if (node == null) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + final ArrayList<ASTNode> changedNodes= new ArrayList<>(); |
| 133 | + node.accept(new InsertTypeArgumentsVisitor(changedNodes)); |
| 134 | + |
| 135 | + if (changedNodes.isEmpty()) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + CompilationUnitRewriteOperation op= new InsertTypeArgumentsOperation(new ParameterizedType[] { node }); |
| 140 | + return new TypeParametersFix(FixMessages.TypeParametersFix_insert_inferred_type_arguments_name, compilationUnit, new CompilationUnitRewriteOperation[] { op }); |
| 141 | + } |
| 142 | + |
| 143 | + public static TypeParametersFix createRemoveRedundantTypeArgumentsFix(CompilationUnit compilationUnit, IProblemLocation problem) { |
| 144 | + int id= problem.getProblemId(); |
| 145 | + if (id == IProblem.RedundantSpecificationOfTypeArguments) { |
| 146 | + ParameterizedType parameterizedType= getParameterizedType(compilationUnit, problem); |
| 147 | + if (parameterizedType == null) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + RemoveTypeArgumentsOperation operation= new RemoveTypeArgumentsOperation(parameterizedType); |
| 151 | + return new TypeParametersFix(FixMessages.TypeParametersFix_remove_redundant_type_arguments_name, compilationUnit, new CompilationUnitRewriteOperation[] { operation }); |
| 152 | + } |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean insertInferredTypeArguments, boolean removeRedundantTypeArguments) { |
| 157 | + |
| 158 | + IProblem[] problems= compilationUnit.getProblems(); |
| 159 | + IProblemLocation[] locations= new IProblemLocation[problems.length]; |
| 160 | + for (int i= 0; i < problems.length; i++) { |
| 161 | + locations[i]= new ProblemLocation(problems[i]); |
| 162 | + } |
| 163 | + |
| 164 | + return createCleanUp(compilationUnit, locations, |
| 165 | + insertInferredTypeArguments, |
| 166 | + removeRedundantTypeArguments); |
| 167 | + } |
| 168 | + |
| 169 | + public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean insertInferredTypeArguments, boolean removeRedundantTypeArguments) { |
| 170 | + |
| 171 | + if (insertInferredTypeArguments) { |
| 172 | + final ArrayList<ASTNode> changedNodes= new ArrayList<>(); |
| 173 | + compilationUnit.accept(new InsertTypeArgumentsVisitor(changedNodes)); |
| 174 | + |
| 175 | + if (changedNodes.isEmpty()) { |
| 176 | + return null; |
| 177 | + } |
| 178 | + |
| 179 | + CompilationUnitRewriteOperation op= new InsertTypeArgumentsOperation(changedNodes.toArray(new ParameterizedType[changedNodes.size()])); |
| 180 | + return new TypeParametersFix(FixMessages.TypeParametersFix_insert_inferred_type_arguments_name, compilationUnit, new CompilationUnitRewriteOperation[] { op }); |
| 181 | + |
| 182 | + } else if (removeRedundantTypeArguments) { |
| 183 | + List<CompilationUnitRewriteOperation> result= new ArrayList<>(); |
| 184 | + for (int i= 0; i < problems.length; i++) { |
| 185 | + IProblemLocation problem= problems[i]; |
| 186 | + int id= problem.getProblemId(); |
| 187 | + |
| 188 | + if (id == IProblem.RedundantSpecificationOfTypeArguments) { |
| 189 | + ParameterizedType parameterizedType= getParameterizedType(compilationUnit, problem); |
| 190 | + if (parameterizedType == null) { |
| 191 | + return null; |
| 192 | + } |
| 193 | + result.add(new RemoveTypeArgumentsOperation(parameterizedType)); |
| 194 | + } |
| 195 | + } |
| 196 | + if (!result.isEmpty()) { |
| 197 | + return new TypeParametersFix(FixMessages.TypeParametersFix_remove_redundant_type_arguments_name, compilationUnit, result.toArray(new CompilationUnitRewriteOperation[result.size()])); |
| 198 | + } |
| 199 | + } |
| 200 | + return null; |
| 201 | + } |
| 202 | + |
| 203 | + private static ParameterizedType getParameterizedType(CompilationUnit compilationUnit, IProblemLocation problem) { |
| 204 | + ASTNode selectedNode= problem.getCoveringNode(compilationUnit); |
| 205 | + if (selectedNode == null) { |
| 206 | + return null; |
| 207 | + } |
| 208 | + |
| 209 | + while (!(selectedNode instanceof ParameterizedType) && !(selectedNode instanceof Statement)) { |
| 210 | + selectedNode= selectedNode.getParent(); |
| 211 | + } |
| 212 | + if (selectedNode instanceof ParameterizedType) { |
| 213 | + return (ParameterizedType) selectedNode; |
| 214 | + } |
| 215 | + return null; |
| 216 | + } |
| 217 | + |
| 218 | + protected TypeParametersFix(String name, CompilationUnit compilationUnit, CompilationUnitRewriteOperation[] fixRewriteOperations) { |
| 219 | + super(name, compilationUnit, fixRewriteOperations); |
| 220 | + } |
| 221 | +} |
0 commit comments