use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project che by eclipse.
the class SurroundWithTryCatchRefactoring method createTryCatchStatement.
private void createTryCatchStatement(org.eclipse.jdt.core.IBuffer buffer, String lineDelimiter) throws CoreException {
List<Statement> result = new ArrayList<Statement>(1);
TryStatement tryStatement = getAST().newTryStatement();
ITypeBinding[] exceptions = fAnalyzer.getExceptions();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fAnalyzer.getEnclosingBodyDeclaration(), fImportRewrite);
if (!fIsMultiCatch) {
for (int i = 0; i < exceptions.length; i++) {
ITypeBinding exception = exceptions[i];
CatchClause catchClause = getAST().newCatchClause();
tryStatement.catchClauses().add(catchClause);
SingleVariableDeclaration decl = getAST().newSingleVariableDeclaration();
String varName = StubUtility.getExceptionVariableName(fCUnit.getJavaProject());
String name = fScope.createName(varName, false);
decl.setName(getAST().newSimpleName(name));
Type type = fImportRewrite.addImport(exception, getAST(), context);
decl.setType(type);
catchClause.setException(decl);
Statement st = getCatchBody(ASTNodes.getQualifiedTypeName(type), name, lineDelimiter);
if (st != null) {
catchClause.getBody().statements().add(st);
}
fLinkedProposalModel.getPositionGroup(GROUP_EXC_TYPE + i, true).addPosition(fRewriter.track(decl.getType()), i == 0);
fLinkedProposalModel.getPositionGroup(GROUP_EXC_NAME + i, true).addPosition(fRewriter.track(decl.getName()), false);
}
} else {
List<ITypeBinding> filteredExceptions = filterSubtypeExceptions(exceptions);
CatchClause catchClause = getAST().newCatchClause();
SingleVariableDeclaration decl = getAST().newSingleVariableDeclaration();
String varName = StubUtility.getExceptionVariableName(fCUnit.getJavaProject());
String name = fScope.createName(varName, false);
decl.setName(getAST().newSimpleName(name));
UnionType unionType = getAST().newUnionType();
List<Type> types = unionType.types();
int i = 0;
for (ITypeBinding exception : filteredExceptions) {
Type type = fImportRewrite.addImport(exception, getAST(), context);
types.add(type);
fLinkedProposalModel.getPositionGroup(GROUP_EXC_TYPE + i, true).addPosition(fRewriter.track(type), i == 0);
i++;
}
decl.setType(unionType);
catchClause.setException(decl);
fLinkedProposalModel.getPositionGroup(GROUP_EXC_NAME + 0, true).addPosition(fRewriter.track(decl.getName()), false);
//$NON-NLS-1$
Statement st = getCatchBody("Exception", name, lineDelimiter);
if (st != null) {
catchClause.getBody().statements().add(st);
}
tryStatement.catchClauses().add(catchClause);
}
List<ASTNode> variableDeclarations = getSpecialVariableDeclarationStatements();
ListRewrite statements = fRewriter.getListRewrite(tryStatement.getBody(), Block.STATEMENTS_PROPERTY);
boolean selectedNodeRemoved = false;
ASTNode expressionStatement = null;
for (int i = 0; i < fSelectedNodes.length; i++) {
ASTNode node = fSelectedNodes[i];
if (node instanceof VariableDeclarationStatement && variableDeclarations.contains(node)) {
AST ast = getAST();
VariableDeclarationStatement statement = (VariableDeclarationStatement) node;
// Create a copy and remove the initializer
VariableDeclarationStatement copy = (VariableDeclarationStatement) ASTNode.copySubtree(ast, statement);
List<IExtendedModifier> modifiers = copy.modifiers();
for (Iterator<IExtendedModifier> iter = modifiers.iterator(); iter.hasNext(); ) {
IExtendedModifier modifier = iter.next();
if (modifier.isModifier() && Modifier.isFinal(((Modifier) modifier).getKeyword().toFlagValue())) {
iter.remove();
}
}
List<VariableDeclarationFragment> fragments = copy.fragments();
for (Iterator<VariableDeclarationFragment> iter = fragments.iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
fragment.setInitializer(null);
}
CompilationUnit root = (CompilationUnit) statement.getRoot();
int extendedStart = root.getExtendedStartPosition(statement);
// we have a leading comment and the comment is covered by the selection
if (extendedStart != statement.getStartPosition() && extendedStart >= fSelection.getOffset()) {
String commentToken = buffer.getText(extendedStart, statement.getStartPosition() - extendedStart);
commentToken = Strings.trimTrailingTabsAndSpaces(commentToken);
Type type = statement.getType();
String typeName = buffer.getText(type.getStartPosition(), type.getLength());
copy.setType((Type) fRewriter.createStringPlaceholder(commentToken + typeName, type.getNodeType()));
}
result.add(copy);
// convert the fragments into expression statements
fragments = statement.fragments();
if (!fragments.isEmpty()) {
List<ExpressionStatement> newExpressionStatements = new ArrayList<ExpressionStatement>();
for (Iterator<VariableDeclarationFragment> iter = fragments.iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
Expression initializer = fragment.getInitializer();
if (initializer != null) {
Assignment assignment = ast.newAssignment();
assignment.setLeftHandSide((Expression) fRewriter.createCopyTarget(fragment.getName()));
assignment.setRightHandSide((Expression) fRewriter.createCopyTarget(initializer));
newExpressionStatements.add(ast.newExpressionStatement(assignment));
}
}
if (!newExpressionStatements.isEmpty()) {
if (fSelectedNodes.length == 1) {
expressionStatement = fRewriter.createGroupNode(newExpressionStatements.toArray(new ASTNode[newExpressionStatements.size()]));
} else {
fRewriter.replace(statement, fRewriter.createGroupNode(newExpressionStatements.toArray(new ASTNode[newExpressionStatements.size()])), null);
}
} else {
fRewriter.remove(statement, null);
selectedNodeRemoved = true;
}
} else {
fRewriter.remove(statement, null);
selectedNodeRemoved = true;
}
}
}
result.add(tryStatement);
ASTNode replacementNode;
if (result.size() == 1) {
replacementNode = result.get(0);
} else {
replacementNode = fRewriter.createGroupNode(result.toArray(new ASTNode[result.size()]));
}
if (fSelectedNodes.length == 1) {
if (expressionStatement != null) {
statements.insertLast(expressionStatement, null);
} else {
if (!selectedNodeRemoved)
statements.insertLast(fRewriter.createMoveTarget(fSelectedNodes[0]), null);
}
fRewriter.replace(fSelectedNodes[0], replacementNode, null);
} else {
ListRewrite source = fRewriter.getListRewrite(fSelectedNodes[0].getParent(), (ChildListPropertyDescriptor) fSelectedNodes[0].getLocationInParent());
ASTNode toMove = source.createMoveTarget(fSelectedNodes[0], fSelectedNodes[fSelectedNodes.length - 1], replacementNode, null);
statements.insertLast(toMove, null);
}
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project eclipse.jdt.ls by eclipse.
the class AddUnimplementedMethodsOperation method rewriteAST.
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
IMethodBinding[] unimplementedMethods = getUnimplementedMethods(fTypeNode);
if (unimplementedMethods.length == 0) {
return;
}
ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fTypeNode.getRoot(), fTypeNode.getStartPosition(), cuRewrite.getImportRewrite());
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ICompilationUnit unit = cuRewrite.getCu();
CodeGenerationSettings settings = PreferenceManager.getCodeGenerationSettings(unit.getJavaProject().getProject());
ListRewrite listRewrite;
ITypeBinding currentType = null;
if (fTypeNode instanceof AnonymousClassDeclaration) {
AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
currentType = decl.resolveBinding();
} else if (fTypeNode instanceof AbstractTypeDeclaration) {
AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, decl.getBodyDeclarationsProperty());
currentType = decl.resolveBinding();
} else if (fTypeNode instanceof EnumConstantDeclaration) {
EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) fTypeNode;
AnonymousClassDeclaration anonymousClassDeclaration = enumConstantDeclaration.getAnonymousClassDeclaration();
if (anonymousClassDeclaration == null) {
anonymousClassDeclaration = rewrite.getAST().newAnonymousClassDeclaration();
rewrite.set(enumConstantDeclaration, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, anonymousClassDeclaration, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
listRewrite = rewrite.getListRewrite(anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
} else {
// $NON-NLS-1$
Assert.isTrue(false, "Unknown type node");
return;
}
ImportRewrite imports = cuRewrite.getImportRewrite();
for (int i = 0; i < unimplementedMethods.length; i++) {
IMethodBinding curr = unimplementedMethods[i];
MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(unit, rewrite, imports, context, curr, curr.getDeclaringClass(), settings, false, currentType, false);
listRewrite.insertLast(newMethodDecl, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project eclipse.jdt.ls by eclipse.
the class TypeMismatchSubProcessor method addTypeMismatchProposals.
public static void addTypeMismatchProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
AST ast = astRoot.getAST();
ASTNode selectedNode = problem.getCoveredNode(astRoot);
if (!(selectedNode instanceof Expression)) {
return;
}
Expression nodeToCast = (Expression) selectedNode;
Name receiverNode = null;
ITypeBinding castTypeBinding = null;
int parentNodeType = selectedNode.getParent().getNodeType();
if (parentNodeType == ASTNode.ASSIGNMENT) {
Assignment assign = (Assignment) selectedNode.getParent();
Expression leftHandSide = assign.getLeftHandSide();
if (selectedNode.equals(leftHandSide)) {
nodeToCast = assign.getRightHandSide();
}
castTypeBinding = assign.getLeftHandSide().resolveTypeBinding();
if (leftHandSide instanceof Name) {
receiverNode = (Name) leftHandSide;
} else if (leftHandSide instanceof FieldAccess) {
receiverNode = ((FieldAccess) leftHandSide).getName();
}
} else if (parentNodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment frag = (VariableDeclarationFragment) selectedNode.getParent();
if (selectedNode.equals(frag.getName()) || selectedNode.equals(frag.getInitializer())) {
nodeToCast = frag.getInitializer();
castTypeBinding = ASTNodes.getType(frag).resolveBinding();
receiverNode = frag.getName();
}
} else if (parentNodeType == ASTNode.MEMBER_VALUE_PAIR) {
receiverNode = ((MemberValuePair) selectedNode.getParent()).getName();
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
} else if (parentNodeType == ASTNode.SINGLE_MEMBER_ANNOTATION) {
// use the type name
receiverNode = ((SingleMemberAnnotation) selectedNode.getParent()).getTypeName();
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
} else {
// try to find the binding corresponding to 'castTypeName'
castTypeBinding = ASTResolving.guessBindingForReference(nodeToCast);
}
if (castTypeBinding == null) {
return;
}
ITypeBinding currBinding = nodeToCast.resolveTypeBinding();
if (currBinding == null && nodeToCast instanceof MethodInvocation) {
IMethodBinding methodBinding = ((MethodInvocation) nodeToCast).resolveMethodBinding();
if (methodBinding != null) {
currBinding = methodBinding.getReturnType();
}
}
if (!(nodeToCast instanceof ArrayInitializer)) {
ITypeBinding castFixType = null;
if (currBinding == null || castTypeBinding.isCastCompatible(currBinding) || nodeToCast instanceof CastExpression) {
castFixType = castTypeBinding;
} else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
ITypeBinding boxUnboxedTypeBinding = boxUnboxPrimitives(castTypeBinding, currBinding, ast);
if (boxUnboxedTypeBinding != castTypeBinding && boxUnboxedTypeBinding.isCastCompatible(currBinding)) {
castFixType = boxUnboxedTypeBinding;
}
}
if (castFixType != null) {
proposals.add(createCastProposal(context, castFixType, nodeToCast, IProposalRelevance.CREATE_CAST));
}
}
// $NON-NLS-1$
boolean nullOrVoid = currBinding == null || "void".equals(currBinding.getName());
// change method return statement to actual type
if (!nullOrVoid && parentNodeType == ASTNode.RETURN_STATEMENT) {
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
currBinding = Bindings.normalizeTypeBinding(currBinding);
if (currBinding == null) {
// $NON-NLS-1$
currBinding = ast.resolveWellKnownType("java.lang.Object");
}
if (currBinding.isWildcardType()) {
currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
}
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changereturntype_description, BasicElementLabels.getJavaElementName(currBinding.getName()));
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_METHOD_RETURN_TYPE);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
Type newReturnType = imports.addImport(currBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);
rewrite.replace(methodDeclaration.getReturnType2(), newReturnType, null);
proposals.add(proposal);
}
}
if (!nullOrVoid && receiverNode != null) {
currBinding = Bindings.normalizeTypeBinding(currBinding);
if (currBinding == null) {
// $NON-NLS-1$
currBinding = ast.resolveWellKnownType("java.lang.Object");
}
if (currBinding.isWildcardType()) {
currBinding = ASTResolving.normalizeWildcardType(currBinding, true, ast);
}
addChangeSenderTypeProposals(context, receiverNode, currBinding, true, IProposalRelevance.CHANGE_TYPE_OF_RECEIVER_NODE, proposals);
}
addChangeSenderTypeProposals(context, nodeToCast, castTypeBinding, false, IProposalRelevance.CHANGE_TYPE_OF_NODE_TO_CAST, proposals);
if (castTypeBinding == ast.resolveWellKnownType("boolean") && currBinding != null && !currBinding.isPrimitive() && !Bindings.isVoidType(currBinding)) {
// $NON-NLS-1$
String label = CorrectionMessages.TypeMismatchSubProcessor_insertnullcheck_description;
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
InfixExpression expression = ast.newInfixExpression();
expression.setLeftOperand((Expression) rewrite.createMoveTarget(nodeToCast));
expression.setRightOperand(ast.newNullLiteral());
expression.setOperator(InfixExpression.Operator.NOT_EQUALS);
rewrite.replace(nodeToCast, expression, null);
proposals.add(new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_NULL_CHECK));
}
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method addRemoveIncludingConditionProposal.
private static void addRemoveIncludingConditionProposal(IInvocationContext context, ASTNode toRemove, ASTNode replacement, Collection<CUCorrectionProposal> proposals) {
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
AST ast = toRemove.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_UNREACHABLE_CODE_INCLUDING_CONDITION);
if (replacement == null || replacement instanceof EmptyStatement || replacement instanceof Block && ((Block) replacement).statements().size() == 0) {
if (ASTNodes.isControlStatementBody(toRemove.getLocationInParent())) {
rewrite.replace(toRemove, toRemove.getAST().newBlock(), null);
} else {
rewrite.remove(toRemove, null);
}
} else if (toRemove instanceof Expression && replacement instanceof Expression) {
Expression moved = (Expression) rewrite.createMoveTarget(replacement);
Expression toRemoveExpression = (Expression) toRemove;
Expression replacementExpression = (Expression) replacement;
ITypeBinding explicitCast = ASTNodes.getExplicitCast(replacementExpression, toRemoveExpression);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(replacementExpression, cast, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(moved);
moved = parenthesized;
}
cast.setExpression(moved);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(toRemove, imports);
cast.setType(imports.addImport(explicitCast, ast, importRewriteContext, TypeLocation.CAST));
moved = cast;
}
rewrite.replace(toRemove, moved, null);
} else {
ASTNode parent = toRemove.getParent();
ASTNode moveTarget;
if ((parent instanceof Block || parent instanceof SwitchStatement) && replacement instanceof Block) {
ListRewrite listRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
List<Statement> list = ((Block) replacement).statements();
int lastIndex = list.size() - 1;
moveTarget = listRewrite.createMoveTarget(list.get(0), list.get(lastIndex));
} else {
moveTarget = rewrite.createMoveTarget(replacement);
}
rewrite.replace(toRemove, moveTarget, null);
}
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method addUncaughtExceptionProposals.
public static void addUncaughtExceptionProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return;
}
while (selectedNode != null && !(selectedNode instanceof Statement) && !(selectedNode instanceof VariableDeclarationExpression) && !(selectedNode.getLocationInParent() == LambdaExpression.BODY_PROPERTY) && !(selectedNode instanceof MethodReference)) {
selectedNode = selectedNode.getParent();
}
if (selectedNode == null) {
return;
}
int offset = selectedNode.getStartPosition();
int length = selectedNode.getLength();
int selectionEnd = context.getSelectionOffset() + context.getSelectionLength();
if (selectionEnd > offset + length) {
// extend the selection if more than one statement is selected (bug 72149)
length = selectionEnd - offset;
}
// Surround with proposals
SurroundWithTryCatchRefactoring refactoring = SurroundWithTryCatchRefactoring.create(cu, offset, length);
if (refactoring == null) {
return;
}
refactoring.setLeaveDirty(true);
if (refactoring.checkActivationBasics(astRoot).isOK()) {
String label = CorrectionMessages.LocalCorrectionsSubProcessor_surroundwith_trycatch_description;
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, refactoring, IProposalRelevance.SURROUND_WITH_TRY_CATCH);
proposal.setLinkedProposalModel(refactoring.getLinkedProposalModel());
proposals.add(proposal);
}
if (JavaModelUtil.is17OrHigher(cu.getJavaProject())) {
refactoring = SurroundWithTryCatchRefactoring.create(cu, offset, length, true);
if (refactoring == null) {
return;
}
refactoring.setLeaveDirty(true);
if (refactoring.checkActivationBasics(astRoot).isOK()) {
String label = CorrectionMessages.LocalCorrectionsSubProcessor_surroundwith_trymulticatch_description;
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, refactoring, IProposalRelevance.SURROUND_WITH_TRY_MULTICATCH);
proposal.setLinkedProposalModel(refactoring.getLinkedProposalModel());
proposals.add(proposal);
}
}
// Catch exception
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl == null) {
return;
}
ASTNode enclosingNode = SurroundWithAnalyzer.getEnclosingNode(selectedNode);
if (enclosingNode == null) {
return;
}
ITypeBinding[] uncaughtExceptions = ExceptionAnalyzer.perform(enclosingNode, Selection.createFromStartLength(offset, length));
if (uncaughtExceptions.length == 0) {
return;
}
TryStatement surroundingTry = ASTResolving.findParentTryStatement(selectedNode);
AST ast = astRoot.getAST();
if (surroundingTry != null && (ASTNodes.isParent(selectedNode, surroundingTry.getBody()) || selectedNode.getLocationInParent() == TryStatement.RESOURCES_PROPERTY)) {
{
ASTRewrite rewrite = ASTRewrite.create(surroundingTry.getAST());
String label = CorrectionMessages.LocalCorrectionsSubProcessor_addadditionalcatch_description;
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_ADDITIONAL_CATCH);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
CodeScopeBuilder.Scope scope = CodeScopeBuilder.perform(decl, Selection.createFromStartLength(offset, length)).findScope(offset, length);
scope.setCursor(offset);
ListRewrite clausesRewrite = rewrite.getListRewrite(surroundingTry, TryStatement.CATCH_CLAUSES_PROPERTY);
for (int i = 0; i < uncaughtExceptions.length; i++) {
ITypeBinding excBinding = uncaughtExceptions[i];
String varName = StubUtility.getExceptionVariableName(cu.getJavaProject());
String name = scope.createName(varName, false);
SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
var.setName(ast.newSimpleName(name));
var.setType(imports.addImport(excBinding, ast, importRewriteContext, TypeLocation.EXCEPTION));
CatchClause newClause = ast.newCatchClause();
newClause.setException(var);
String catchBody = StubUtility.getCatchBodyContent(cu, excBinding.getName(), name, selectedNode, String.valueOf('\n'));
if (catchBody != null) {
ASTNode node = rewrite.createStringPlaceholder(catchBody, ASTNode.RETURN_STATEMENT);
newClause.getBody().statements().add(node);
}
clausesRewrite.insertLast(newClause, null);
// $NON-NLS-1$
String typeKey = "type" + i;
// $NON-NLS-1$
String nameKey = "name" + i;
proposal.addLinkedPosition(rewrite.track(var.getType()), false, typeKey);
proposal.addLinkedPosition(rewrite.track(var.getName()), false, nameKey);
addExceptionTypeLinkProposals(proposal, excBinding, typeKey);
}
proposals.add(proposal);
}
if (JavaModelUtil.is17OrHigher(cu.getJavaProject())) {
List<CatchClause> catchClauses = surroundingTry.catchClauses();
if (catchClauses != null && catchClauses.size() == 1) {
List<ITypeBinding> filteredExceptions = SurroundWithTryCatchRefactoring.filterSubtypeExceptions(uncaughtExceptions);
String label = filteredExceptions.size() > 1 ? CorrectionMessages.LocalCorrectionsSubProcessor_addexceptionstoexistingcatch_description : CorrectionMessages.LocalCorrectionsSubProcessor_addexceptiontoexistingcatch_description;
ASTRewrite rewrite = ASTRewrite.create(ast);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_EXCEPTIONS_TO_EXISTING_CATCH);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
CatchClause catchClause = catchClauses.get(0);
Type type = catchClause.getException().getType();
if (type instanceof UnionType) {
UnionType unionType = (UnionType) type;
ListRewrite listRewrite = rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY);
for (int i = 0; i < filteredExceptions.size(); i++) {
ITypeBinding excBinding = filteredExceptions.get(i);
Type type2 = imports.addImport(excBinding, ast, importRewriteContext, TypeLocation.EXCEPTION);
listRewrite.insertLast(type2, null);
// $NON-NLS-1$
String typeKey = "type" + i;
proposal.addLinkedPosition(rewrite.track(type2), false, typeKey);
addExceptionTypeLinkProposals(proposal, excBinding, typeKey);
}
} else {
UnionType newUnionType = ast.newUnionType();
List<Type> types = newUnionType.types();
types.add((Type) rewrite.createCopyTarget(type));
for (int i = 0; i < filteredExceptions.size(); i++) {
ITypeBinding excBinding = filteredExceptions.get(i);
Type type2 = imports.addImport(excBinding, ast, importRewriteContext, TypeLocation.EXCEPTION);
types.add(type2);
// $NON-NLS-1$
String typeKey = "type" + i;
proposal.addLinkedPosition(rewrite.track(type2), false, typeKey);
addExceptionTypeLinkProposals(proposal, excBinding, typeKey);
}
rewrite.replace(type, newUnionType, null);
}
proposals.add(proposal);
} else if (catchClauses != null && catchClauses.size() == 0) {
List<ITypeBinding> filteredExceptions = SurroundWithTryCatchRefactoring.filterSubtypeExceptions(uncaughtExceptions);
if (filteredExceptions.size() > 1) {
String label = CorrectionMessages.LocalCorrectionsSubProcessor_addadditionalmulticatch_description;
ASTRewrite rewrite = ASTRewrite.create(ast);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_ADDITIONAL_MULTI_CATCH);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
CodeScopeBuilder.Scope scope = CodeScopeBuilder.perform(decl, Selection.createFromStartLength(offset, length)).findScope(offset, length);
scope.setCursor(offset);
CatchClause newCatchClause = ast.newCatchClause();
String varName = StubUtility.getExceptionVariableName(cu.getJavaProject());
String name = scope.createName(varName, false);
SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
var.setName(ast.newSimpleName(name));
UnionType newUnionType = ast.newUnionType();
List<Type> types = newUnionType.types();
for (int i = 0; i < filteredExceptions.size(); i++) {
ITypeBinding excBinding = filteredExceptions.get(i);
Type type2 = imports.addImport(excBinding, ast, importRewriteContext, TypeLocation.EXCEPTION);
types.add(type2);
// $NON-NLS-1$
String typeKey = "type" + i;
proposal.addLinkedPosition(rewrite.track(type2), false, typeKey);
addExceptionTypeLinkProposals(proposal, excBinding, typeKey);
}
// $NON-NLS-1$
String nameKey = "name";
proposal.addLinkedPosition(rewrite.track(var.getName()), false, nameKey);
var.setType(newUnionType);
newCatchClause.setException(var);
// $NON-NLS-1$
String catchBody = StubUtility.getCatchBodyContent(cu, "Exception", name, selectedNode, String.valueOf('\n'));
if (catchBody != null) {
ASTNode node = rewrite.createStringPlaceholder(catchBody, ASTNode.RETURN_STATEMENT);
newCatchClause.getBody().statements().add(node);
}
ListRewrite listRewrite = rewrite.getListRewrite(surroundingTry, TryStatement.CATCH_CLAUSES_PROPERTY);
listRewrite.insertFirst(newCatchClause, null);
proposals.add(proposal);
}
}
}
}
// Add throws declaration
if (enclosingNode instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) enclosingNode;
IMethodBinding binding = methodDecl.resolveBinding();
boolean isApplicable = (binding != null);
if (isApplicable) {
IMethodBinding overriddenMethod = Bindings.findOverriddenMethod(binding, true);
if (overriddenMethod != null) {
isApplicable = overriddenMethod.getDeclaringClass().isFromSource();
if (!isApplicable) {
// bug 349051
ITypeBinding[] exceptionTypes = overriddenMethod.getExceptionTypes();
ArrayList<ITypeBinding> unhandledExceptions = new ArrayList<>(uncaughtExceptions.length);
for (int i = 0; i < uncaughtExceptions.length; i++) {
ITypeBinding curr = uncaughtExceptions[i];
if (isSubtype(curr, exceptionTypes)) {
unhandledExceptions.add(curr);
}
}
uncaughtExceptions = unhandledExceptions.toArray(new ITypeBinding[unhandledExceptions.size()]);
isApplicable |= uncaughtExceptions.length > 0;
}
}
}
if (isApplicable) {
ITypeBinding[] methodExceptions = binding.getExceptionTypes();
ArrayList<ITypeBinding> unhandledExceptions = new ArrayList<>(uncaughtExceptions.length);
for (int i = 0; i < uncaughtExceptions.length; i++) {
ITypeBinding curr = uncaughtExceptions[i];
if (!isSubtype(curr, methodExceptions)) {
unhandledExceptions.add(curr);
}
}
uncaughtExceptions = unhandledExceptions.toArray(new ITypeBinding[unhandledExceptions.size()]);
List<Type> exceptions = methodDecl.thrownExceptionTypes();
int nExistingExceptions = exceptions.size();
ChangeDescription[] desc = new ChangeDescription[nExistingExceptions + uncaughtExceptions.length];
for (int i = 0; i < exceptions.size(); i++) {
Type elem = exceptions.get(i);
if (isSubtype(elem.resolveBinding(), uncaughtExceptions)) {
desc[i] = new RemoveDescription();
}
}
for (int i = 0; i < uncaughtExceptions.length; i++) {
// $NON-NLS-1$
desc[i + nExistingExceptions] = new InsertDescription(uncaughtExceptions[i], "");
}
String label = CorrectionMessages.LocalCorrectionsSubProcessor_addthrows_description;
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, cu, astRoot, binding, null, desc, IProposalRelevance.ADD_THROWS_DECLARATION);
for (int i = 0; i < uncaughtExceptions.length; i++) {
addExceptionTypeLinkProposals(proposal, uncaughtExceptions[i], proposal.getExceptionTypeGroupId(i + nExistingExceptions));
}
proposals.add(proposal);
}
}
}
Aggregations