diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/MultiCatchCleanUpCore.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/MultiCatchCleanUpCore.java index bcb7a7bbb24..778060a6d12 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/MultiCatchCleanUpCore.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/MultiCatchCleanUpCore.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020, 2024 Fabrice TIERCELIN and others. + * Copyright (c) 2020, 2025 Fabrice TIERCELIN and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -57,7 +57,6 @@ import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; import org.eclipse.jdt.ui.cleanup.ICleanUpFix; @@ -122,7 +121,7 @@ public String getPreview() { @Override protected ICleanUpFix createFix(final CompilationUnit unit) throws CoreException { - if (!isEnabled(CleanUpConstants.MULTI_CATCH) || !JavaModelUtil.is1d7OrHigher(unit.getJavaElement().getJavaProject())) { + if (!isEnabled(CleanUpConstants.MULTI_CATCH)) { return null; } diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsBaseSubProcessor.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsBaseSubProcessor.java index 39826592851..3815cf2b80c 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsBaseSubProcessor.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsBaseSubProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2024 IBM Corporation and others. + * Copyright (c) 2024, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -282,23 +282,21 @@ public void getUncaughtExceptionProposals(IInvocationContext context, IProblemLo proposals.add(refactoringCorrectionProposalToT(proposal, SURROUND_WITH_TRY_CATCH)); } - if (JavaModelUtil.is1d7OrHigher(cu.getJavaProject())) { - refactoring= SurroundWithTryCatchRefactoring.create(cu, offset, length, true); - if (refactoring == null) - return; + refactoring= SurroundWithTryCatchRefactoring.create(cu, offset, length, true); + if (refactoring == null) + return; - refactoring.setLeaveDirty(true); - if (refactoring.checkActivationBasics(astRoot).isOK()) { - String label; - if ((vType != null) && (vName != null) && ASTNodes.isVarType(selectedNode, astRoot) && affectedLocals.contains(vName.getIdentifier())) { - label= Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_surroundwith_trymulticatch_var_description, new Object[] { vName.getIdentifier(), vType.getName() }); - } else { - label= CorrectionMessages.LocalCorrectionsSubProcessor_surroundwith_trymulticatch_description; - } - RefactoringCorrectionProposalCore proposal= new RefactoringCorrectionProposalCore(label, cu, refactoring, IProposalRelevance.SURROUND_WITH_TRY_MULTICATCH); - proposal.setLinkedProposalModel(refactoring.getLinkedProposalModel()); - proposals.add(refactoringCorrectionProposalToT(proposal, SURROUND_WITH_TRY_MULTI_CATCH)); + refactoring.setLeaveDirty(true); + if (refactoring.checkActivationBasics(astRoot).isOK()) { + String label; + if ((vType != null) && (vName != null) && ASTNodes.isVarType(selectedNode, astRoot) && affectedLocals.contains(vName.getIdentifier())) { + label= Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_surroundwith_trymulticatch_var_description, new Object[] { vName.getIdentifier(), vType.getName() }); + } else { + label= CorrectionMessages.LocalCorrectionsSubProcessor_surroundwith_trymulticatch_description; } + RefactoringCorrectionProposalCore proposal= new RefactoringCorrectionProposalCore(label, cu, refactoring, IProposalRelevance.SURROUND_WITH_TRY_MULTICATCH); + proposal.setLinkedProposalModel(refactoring.getLinkedProposalModel()); + proposals.add(refactoringCorrectionProposalToT(proposal, SURROUND_WITH_TRY_MULTI_CATCH)); } //Catch exception @@ -322,113 +320,111 @@ public void getUncaughtExceptionProposals(IInvocationContext context, IProblemLo if (surroundingTry != null && (ASTNodes.isParent(selectedNode, surroundingTry.getBody()) || selectedNode.getLocationInParent() == TryStatement.RESOURCES2_PROPERTY)) { addAdditionalCatchProposal(context, proposals, cu, selectedNode, offset, length, decl, uncaughtExceptions, surroundingTry, ast); - if (JavaModelUtil.is1d7OrHigher(cu.getJavaProject())) { - List catchClauses= surroundingTry.catchClauses(); - List filteredExceptions= SurroundWithTryCatchRefactoring.filterSubtypeExceptions(uncaughtExceptions); - ASTRewrite rewrite= ASTRewrite.create(ast); - - if (catchClauses != null && catchClauses.size() == 1) { - String label= filteredExceptions.size() > 1 - ? CorrectionMessages.LocalCorrectionsSubProcessor_addexceptionstoexistingcatch_description - : CorrectionMessages.LocalCorrectionsSubProcessor_addexceptiontoexistingcatch_description; - LinkedCorrectionProposalCore proposal= new LinkedCorrectionProposalCore(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 originalType= catchClause.getException().getType(); - - if (originalType instanceof UnionType) { - UnionType unionType= (UnionType) originalType; - ListRewrite listRewrite= rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY); - @SuppressWarnings("unchecked") - List existingTypes= new ArrayList<>(unionType.types()); - - for (int i= 0; i < filteredExceptions.size(); i++) { - Type addedType= addNewException(ast, filteredExceptions, rewrite, proposal, imports, importRewriteContext, i); - boolean isReplaced= false; - - for (Type existingType : existingTypes) { - if (existingType.resolveBinding().isSubTypeCompatible(filteredExceptions.get(i))) { - listRewrite.replace(existingType, addedType, null); - isReplaced= true; - break; - } - } + List catchClauses= surroundingTry.catchClauses(); + List filteredExceptions= SurroundWithTryCatchRefactoring.filterSubtypeExceptions(uncaughtExceptions); + ASTRewrite rewrite= ASTRewrite.create(ast); - if (!isReplaced) { - listRewrite.insertLast(addedType, null); - } - } - } else { - Type firstType= null; - List typesToAdd= new ArrayList<>(); + if (catchClauses != null && catchClauses.size() == 1) { + String label= filteredExceptions.size() > 1 + ? CorrectionMessages.LocalCorrectionsSubProcessor_addexceptionstoexistingcatch_description + : CorrectionMessages.LocalCorrectionsSubProcessor_addexceptiontoexistingcatch_description; + LinkedCorrectionProposalCore proposal= new LinkedCorrectionProposalCore(label, cu, rewrite, IProposalRelevance.ADD_EXCEPTIONS_TO_EXISTING_CATCH); + ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot()); + ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports); - for (int i= 0; i < filteredExceptions.size(); i++) { - Type addedType= addNewException(ast, filteredExceptions, rewrite, proposal, imports, importRewriteContext, i); + CatchClause catchClause= catchClauses.get(0); + Type originalType= catchClause.getException().getType(); - if (originalType.resolveBinding().isSubTypeCompatible(filteredExceptions.get(i))) { - firstType= addedType; - } else { - typesToAdd.add(addedType); + if (originalType instanceof UnionType) { + UnionType unionType= (UnionType) originalType; + ListRewrite listRewrite= rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY); + @SuppressWarnings("unchecked") + List existingTypes= new ArrayList<>(unionType.types()); + + for (int i= 0; i < filteredExceptions.size(); i++) { + Type addedType= addNewException(ast, filteredExceptions, rewrite, proposal, imports, importRewriteContext, i); + boolean isReplaced= false; + + for (Type existingType : existingTypes) { + if (existingType.resolveBinding().isSubTypeCompatible(filteredExceptions.get(i))) { + listRewrite.replace(existingType, addedType, null); + isReplaced= true; + break; } } - if (!typesToAdd.isEmpty()) { - UnionType newUnionType= ast.newUnionType(); - List types= newUnionType.types(); + if (!isReplaced) { + listRewrite.insertLast(addedType, null); + } + } + } else { + Type firstType= null; + List typesToAdd= new ArrayList<>(); - if (firstType == null) { - types.add(ASTNodes.createMoveTarget(rewrite, originalType)); - } else { - types.add(firstType); - } - types.addAll(typesToAdd); + for (int i= 0; i < filteredExceptions.size(); i++) { + Type addedType= addNewException(ast, filteredExceptions, rewrite, proposal, imports, importRewriteContext, i); - rewrite.replace(originalType, newUnionType, null); - } else if (firstType != null) { - rewrite.replace(originalType, firstType, null); + if (originalType.resolveBinding().isSubTypeCompatible(filteredExceptions.get(i))) { + firstType= addedType; + } else { + typesToAdd.add(addedType); } } - proposals.add(linkedCorrectionProposalToT(proposal, ADD_EXCEPTION_TO_CATCH)); - } else if (catchClauses != null && catchClauses.isEmpty() && filteredExceptions.size() > 1) { - String label= CorrectionMessages.LocalCorrectionsSubProcessor_addadditionalmulticatch_description; - LinkedCorrectionProposalCore proposal= new LinkedCorrectionProposalCore(label, cu, rewrite, IProposalRelevance.ADD_EXCEPTIONS_TO_EXISTING_CATCH); - ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot()); - ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports); + if (!typesToAdd.isEmpty()) { + UnionType newUnionType= ast.newUnionType(); + List types= newUnionType.types(); + + if (firstType == null) { + types.add(ASTNodes.createMoveTarget(rewrite, originalType)); + } else { + types.add(firstType); + } + types.addAll(typesToAdd); + + rewrite.replace(originalType, newUnionType, null); + } else if (firstType != null) { + rewrite.replace(originalType, firstType, null); + } + } - CodeScopeBuilder.Scope scope= CodeScopeBuilder.perform(decl, Selection.createFromStartLength(offset, length)).findScope(offset, length); - scope.setCursor(offset); + proposals.add(linkedCorrectionProposalToT(proposal, ADD_EXCEPTION_TO_CATCH)); + } else if (catchClauses != null && catchClauses.isEmpty() && filteredExceptions.size() > 1) { + String label= CorrectionMessages.LocalCorrectionsSubProcessor_addadditionalmulticatch_description; + LinkedCorrectionProposalCore proposal= new LinkedCorrectionProposalCore(label, cu, rewrite, IProposalRelevance.ADD_EXCEPTIONS_TO_EXISTING_CATCH); + ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot()); + ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(decl, imports); - 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)); + CodeScopeBuilder.Scope scope= CodeScopeBuilder.perform(decl, Selection.createFromStartLength(offset, length)).findScope(offset, length); + scope.setCursor(offset); - UnionType newUnionType= ast.newUnionType(); - List types= newUnionType.types(); + 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)); - for (int i= 0; i < filteredExceptions.size(); i++) { - types.add(addNewException(ast, filteredExceptions, rewrite, proposal, imports, importRewriteContext, i)); - } + UnionType newUnionType= ast.newUnionType(); + List types= newUnionType.types(); - String nameKey= "name"; //$NON-NLS-1$ - proposal.addLinkedPosition(rewrite.track(var.getName()), false, nameKey); - var.setType(newUnionType); - newCatchClause.setException(var); - String catchBody= StubUtility.getCatchBodyContent(cu, "Exception", name, selectedNode, String.valueOf('\n')); //$NON-NLS-1$ + for (int i= 0; i < filteredExceptions.size(); i++) { + types.add(addNewException(ast, filteredExceptions, rewrite, proposal, imports, importRewriteContext, i)); + } - if (catchBody != null) { - ASTNode node= rewrite.createStringPlaceholder(catchBody, ASTNode.RETURN_STATEMENT); - newCatchClause.getBody().statements().add(node); - } + String nameKey= "name"; //$NON-NLS-1$ + proposal.addLinkedPosition(rewrite.track(var.getName()), false, nameKey); + var.setType(newUnionType); + newCatchClause.setException(var); + String catchBody= StubUtility.getCatchBodyContent(cu, "Exception", name, selectedNode, String.valueOf('\n')); //$NON-NLS-1$ - ListRewrite listRewrite= rewrite.getListRewrite(surroundingTry, TryStatement.CATCH_CLAUSES_PROPERTY); - listRewrite.insertFirst(newCatchClause, null); - proposals.add(linkedCorrectionProposalToT(proposal, ADD_EXCEPTION_TO_MULTI_CATCH)); + 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(linkedCorrectionProposalToT(proposal, ADD_EXCEPTION_TO_MULTI_CATCH)); } } diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/VarargsWarningsBaseSubProcessor.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/VarargsWarningsBaseSubProcessor.java index f66b2160eef..34ffb9083be 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/VarargsWarningsBaseSubProcessor.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/VarargsWarningsBaseSubProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2024 IBM Corporation and others. + * Copyright (c) 2024, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -32,7 +32,6 @@ import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.internal.core.manipulation.dom.ASTResolving; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.ui.text.java.IInvocationContext; @@ -64,9 +63,6 @@ public void createAddSafeVarargsProposals(IInvocationContext context, IProblemLo } public void createAddSafeVarargsToDeclarationProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) { - if (!JavaModelUtil.is1d7OrHigher(context.getCompilationUnit().getJavaProject())) - return; - ASTNode coveringNode= problem.getCoveringNode(context.getASTRoot()); IMethodBinding methodBinding; if (coveringNode instanceof MethodInvocation) { diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/JoinVariableFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/JoinVariableFixCore.java index ba339f08fd5..4b90f19d8bd 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/JoinVariableFixCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/JoinVariableFixCore.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2023, 2024 Gayan Perera and others. + * Copyright (c) 2023, 2025 Gayan Perera and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -17,7 +17,6 @@ import org.eclipse.core.runtime.CoreException; -import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTMatcher; import org.eclipse.jdt.core.dom.ASTNode; @@ -52,7 +51,6 @@ import org.eclipse.jdt.internal.corext.refactoring.code.Invocations; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages; @@ -220,9 +218,7 @@ public JoinVariableProposalOperation(VariableDeclarationStatement statement, IfS @Override public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore linkedModel) throws CoreException { final ASTRewrite rewrite= cuRewrite.getASTRewrite(); - final CompilationUnit cup= (CompilationUnit) statement.getRoot(); final AST ast= cuRewrite.getAST(); - final IJavaProject project= cup.getTypeRoot().getJavaProject(); TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer(); sourceRangeComputer.addTightSourceNode(ifStatement != null ? ifStatement : assignParent); @@ -236,10 +232,8 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore Expression thenCopy= (Expression) rewrite.createCopyTarget(thenExpression); Expression elseCopy= (Expression) rewrite.createCopyTarget(elseExpression); - if (JavaModelUtil.is1d7OrHigher(project)) { - addExplicitTypeArgumentsIfNecessary(rewrite, cuRewrite, thenExpression); - addExplicitTypeArgumentsIfNecessary(rewrite, cuRewrite, elseExpression); - } + addExplicitTypeArgumentsIfNecessary(rewrite, cuRewrite, thenExpression); + addExplicitTypeArgumentsIfNecessary(rewrite, cuRewrite, elseExpression); conditionalExpression.setThenExpression(thenCopy); conditionalExpression.setElseExpression(elseCopy); rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, conditionalExpression, null); diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UpdateProperty.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UpdateProperty.java index 973932f310a..2ebb7f3aee8 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UpdateProperty.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/UpdateProperty.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2021, 2024 IBM Corporation and others. + * Copyright (c) 2021, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -59,7 +59,6 @@ import org.eclipse.jdt.internal.corext.dom.ASTNodes; import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.internal.corext.util.Messages; public enum UpdateProperty { @@ -452,55 +451,29 @@ private static void defaultRewrite(UpdateProperty upp, final MethodInvocation vi ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); ASTNode replace_with_Call; - if (JavaModelUtil.is1d7OrHigher(cuRewrite.getCu().getJavaProject())) { - /** - * Add import - */ - ImportRewrite importRewrite= cuRewrite.getImportRewrite(); - importRewrite.addImport(upp.cl.getCanonicalName()); - /** - * Add first method call - */ - MethodInvocation firstCall= ast.newMethodInvocation(); - firstCall.setExpression(ASTNodeFactory.newName(ast, upp.cl.getSimpleName())); - firstCall.setName(ast.newSimpleName(upp.simplename)); + /** + * Add import + */ + ImportRewrite importRewrite= cuRewrite.getImportRewrite(); + importRewrite.addImport(upp.cl.getCanonicalName()); + /** + * Add first method call + */ + MethodInvocation firstCall= ast.newMethodInvocation(); + firstCall.setExpression(ASTNodeFactory.newName(ast, upp.cl.getSimpleName())); + firstCall.setName(ast.newSimpleName(upp.simplename)); - if(upp.simplename2==null) { - replace_with_Call= firstCall; - } else { - /** - * Add second method call - */ - MethodInvocation secondCall= ast.newMethodInvocation(); - secondCall.setExpression(firstCall); - secondCall.setName(ast.newSimpleName(upp.simplename2)); - replace_with_Call= secondCall; - } + if(upp.simplename2==null) { + replace_with_Call= firstCall; } else { - if(upp.alternativecl==null) { - /** - * can be null for System.getProperty("file.encoding") on Java < 7 - */ - return; - } else { - /** - * fallback to File.pathSeparator for java < 7.0 - */ - - /** - * Add import - */ - ImportRewrite importRewrite= cuRewrite.getImportRewrite(); - importRewrite.addImport(upp.alternativecl.getCanonicalName()); - /** - * Add field access - */ - FieldAccess fieldaccess= ast.newFieldAccess(); - fieldaccess.setExpression(ASTNodeFactory.newName(ast, upp.alternativecl.getSimpleName())); - fieldaccess.setName(ast.newSimpleName(upp.constant)); - replace_with_Call= fieldaccess; - } + /** + * Add second method call + */ + MethodInvocation secondCall= ast.newMethodInvocation(); + secondCall.setExpression(firstCall); + secondCall.setName(ast.newSimpleName(upp.simplename2)); + replace_with_Call= secondCall; } ASTNodes.replaceAndRemoveNLS(rewrite, visited, replace_with_Call, group, cuRewrite); } diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/util/JavaModelUtil.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/util/JavaModelUtil.java index 9ff831d3c28..20bbda68c04 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/util/JavaModelUtil.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/util/JavaModelUtil.java @@ -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 @@ -855,16 +855,6 @@ public static boolean is23OrHigher(String compliance) { return !isVersionLessThan(compliance, JavaCore.VERSION_23); } - /** - * Checks if the given project or workspace has source compliance 1.7 or greater. - * - * @param project the project to test or null to test the workspace settings - * @return true if the given project or workspace has source compliance 1.7 or greater. - */ - public static boolean is1d7OrHigher(IJavaProject project) { - return is1d7OrHigher(getSourceCompliance(project)); - } - /** * Checks if the given project or workspace has source compliance 1.8 or greater. * diff --git a/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/RefactoringMessages.java b/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/RefactoringMessages.java index 6c662459ea2..8b0ca0b85cf 100644 --- a/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/RefactoringMessages.java +++ b/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/RefactoringMessages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2021 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 @@ -998,8 +998,6 @@ public final class RefactoringMessages extends NLS { public static String SurroundWithTryMultiCatchAction_label; - public static String SurroundWithTryMultiCatchAction_not17; - public static String SurroundWithTryResourcesAction_label; public static String SurroundWithTryResourcesAction_exception; diff --git a/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/refactoringui.properties b/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/refactoringui.properties index 88903dad63e..8b9ed6eb246 100644 --- a/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/refactoringui.properties +++ b/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/refactoringui.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2000, 2021 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 @@ -37,7 +37,6 @@ SurroundWithTryCatchAction_label=Surround with tr&y/catch Block SurroundWithTryMultiCatchAction_label=Surround with t&ry/multi-catch Block SurroundWithTryCatchAction_dialog_title=Surround with try/catch SurroundWithTryMultiCatchAction_dialog_title=Surround with try/multi-catch -SurroundWithTryMultiCatchAction_not17=Project ''{0}'' cannot be processed since its compiler source level is below 1.7. SurroundWithTryCatchAction_exception=An unexpected exception occurred. See the error log for more details. SurroundWithTryResourcesAction_label=Surround with try-with-resources Block SurroundWithTryResourcesAction_dialog_title=Surround with try-with-resources diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/dialogs/GenerateHashCodeEqualsDialog.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/dialogs/GenerateHashCodeEqualsDialog.java index 2b8a59fe37f..04cbebafb0c 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/dialogs/GenerateHashCodeEqualsDialog.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/dialogs/GenerateHashCodeEqualsDialog.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2021 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 @@ -32,12 +32,10 @@ import org.eclipse.ui.PlatformUI; import org.eclipse.ui.dialogs.ISelectionStatusValidator; -import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.IVariableBinding; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; @@ -130,13 +128,11 @@ public IStatus validate(Object[] selection) { private boolean fUseInstanceOf; private boolean fUseBlocks; private boolean fUseJ7HashEquals; - private IJavaProject fProject; private boolean fNoFields; public GenerateHashCodeEqualsDialog(Shell shell, CompilationUnitEditor editor, IType type, IVariableBinding[] allFields, IVariableBinding[] selectedFields) throws JavaModelException { super(shell, new BindingLabelProvider(), new GenerateHashCodeEqualsContentProvider(allFields), editor, type, false); - this.fProject = type.getJavaProject(); setEmptyListMessage(JavaUIMessages.GenerateHashCodeEqualsDialog_no_entries); setInitialSelections((Object[]) selectedFields); @@ -150,7 +146,7 @@ public GenerateHashCodeEqualsDialog(Shell shell, CompilationUnitEditor editor, I fUseInstanceOf= asBoolean(getDialogSettings().get(SETTINGS_INSTANCEOF), false); fUseBlocks= asBoolean(getDialogSettings().get(SETTINGS_BLOCKS), false); - fUseJ7HashEquals= asBoolean(getDialogSettings().get(SETTINGS_J7_HASH_EQUALS), JavaModelUtil.is1d7OrHigher(this.fProject)); + fUseJ7HashEquals= asBoolean(getDialogSettings().get(SETTINGS_J7_HASH_EQUALS), true); } @@ -158,8 +154,7 @@ public GenerateHashCodeEqualsDialog(Shell shell, CompilationUnitEditor editor, I public boolean close() { getDialogSettings().put(SETTINGS_INSTANCEOF, fUseInstanceOf); getDialogSettings().put(SETTINGS_BLOCKS, fUseBlocks); - if (JavaModelUtil.is1d7OrHigher(this.fProject)) - getDialogSettings().put(SETTINGS_J7_HASH_EQUALS, fUseJ7HashEquals); + getDialogSettings().put(SETTINGS_J7_HASH_EQUALS, fUseJ7HashEquals); return super.close(); } @@ -202,20 +197,14 @@ public void widgetSelected(SelectionEvent event) { button= new Button(composite, SWT.CHECK); button.setText(JavaUIMessages.GenerateHashCodeEqualsDialog_j7hashequals_button); - if (JavaModelUtil.is1d7OrHigher(this.fProject)) { - button.addSelectionListener(new SelectionAdapter() { + button.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent event) { - setUseJ7HashEquals((((Button) event.widget).getSelection())); - } - }); - button.setSelection(isUseJ7HashEquals()); - } else { - button.setEnabled(false); - button.setSelection(false); - setUseJ7HashEquals(false); - } + @Override + public void widgetSelected(SelectionEvent event) { + setUseJ7HashEquals((((Button) event.widget).getSelection())); + } + }); + button.setSelection(isUseJ7HashEquals()); data= new GridData(GridData.HORIZONTAL_ALIGN_FILL); data.horizontalSpan= 2; button.setLayoutData(data); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/HashCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/HashCleanUp.java index 81954b84269..fb32761a096 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/HashCleanUp.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/HashCleanUp.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020 Fabrice TIERCELIN and others. + * Copyright (c) 2020, 2025 Fabrice TIERCELIN and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -63,7 +63,6 @@ import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperationWithSourceRange; import org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; import org.eclipse.jdt.ui.cleanup.ICleanUpFix; @@ -128,7 +127,7 @@ public String getPreview() { @Override protected ICleanUpFix createFix(CompilationUnit unit) throws CoreException { - if (!isEnabled(CleanUpConstants.MODERNIZE_HASH) || !JavaModelUtil.is1d7OrHigher(unit.getJavaElement().getJavaProject())) { + if (!isEnabled(CleanUpConstants.MODERNIZE_HASH)) { return null; } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/ObjectsEqualsCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/ObjectsEqualsCleanUp.java index 875ad6ee8fa..cce8afc473e 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/ObjectsEqualsCleanUp.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/ObjectsEqualsCleanUp.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020 Fabrice TIERCELIN and others. + * Copyright (c) 2020, 2025 Fabrice TIERCELIN and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -51,7 +51,6 @@ import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperationWithSourceRange; import org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; import org.eclipse.jdt.ui.cleanup.ICleanUpFix; @@ -93,10 +92,10 @@ public String getPreview() { if (!Objects.equals(aText, other.aText)) { return false; } - - - - + + + + """; //$NON-NLS-1$ } @@ -113,7 +112,7 @@ public String getPreview() { @Override protected ICleanUpFix createFix(CompilationUnit unit) throws CoreException { - if (!isEnabled(CleanUpConstants.USE_OBJECTS_EQUALS) || !JavaModelUtil.is1d7OrHigher(unit.getJavaElement().getJavaProject())) { + if (!isEnabled(CleanUpConstants.USE_OBJECTS_EQUALS)) { return null; } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/TryWithResourceCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/TryWithResourceCleanUp.java index a40e53cfca6..254bb4396fc 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/TryWithResourceCleanUp.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/TryWithResourceCleanUp.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2020 Fabrice TIERCELIN and others. + * Copyright (c) 2020, 2025 Fabrice TIERCELIN and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -25,7 +25,6 @@ import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; import org.eclipse.jdt.internal.corext.fix.TryWithResourceFixCore; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; import org.eclipse.jdt.ui.cleanup.ICleanUpFix; @@ -66,8 +65,8 @@ public String getPreview() { try (inputStream) { System.out.println(inputStream.read()); } - - + + """; //$NON-NLS-1$ } @@ -83,7 +82,7 @@ public String getPreview() { @Override protected ICleanUpFix createFix(final CompilationUnit unit) throws CoreException { - if (!isEnabled(CleanUpConstants.TRY_WITH_RESOURCE) || !JavaModelUtil.is1d7OrHigher(unit.getJavaElement().getJavaProject())) { + if (!isEnabled(CleanUpConstants.TRY_WITH_RESOURCE)) { return null; } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java index ad23bb83577..0570399cbb2 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java @@ -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 @@ -34,7 +34,6 @@ import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IField; import org.eclipse.jdt.core.IJavaElement; -import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; @@ -115,7 +114,6 @@ import org.eclipse.jdt.internal.corext.fix.IProposableFix; import org.eclipse.jdt.internal.corext.refactoring.util.NoCommentSourceRangeComputer; import org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.ui.cleanup.CleanUpOptions; @@ -1898,12 +1896,8 @@ private static boolean getReplaceIfElseWithConditionalProposals(IInvocationConte Expression thenCopy= (Expression) rewrite.createCopyTarget(thenExpression); Expression elseCopy= (Expression) rewrite.createCopyTarget(elseExpression); - - IJavaProject project= context.getCompilationUnit().getJavaProject(); - if (JavaModelUtil.is1d7OrHigher(project)) { - addExplicitTypeArgumentsIfNecessary(rewrite, proposal, thenExpression); - addExplicitTypeArgumentsIfNecessary(rewrite, proposal, elseExpression); - } + addExplicitTypeArgumentsIfNecessary(rewrite, proposal, thenExpression); + addExplicitTypeArgumentsIfNecessary(rewrite, proposal, elseExpression); conditionalExpression.setThenExpression(thenCopy); conditionalExpression.setElseExpression(elseCopy); @@ -2949,8 +2943,6 @@ private static boolean getConvertIfElseToSwitchProposals(final IInvocationContex if (leftBinding != null) { if ("java.lang.String".equals(leftBinding.getQualifiedName())) { //$NON-NLS-1$ - if (!JavaModelUtil.is1d7OrHigher(context.getCompilationUnit().getJavaProject())) - return false; } else if (!leftBinding.isEnum()) { return false; } @@ -2967,8 +2959,6 @@ private static boolean getConvertIfElseToSwitchProposals(final IInvocationContex if (rightBinding != null) { if ("java.lang.String".equals(rightBinding.getQualifiedName())) { //$NON-NLS-1$ - if (!JavaModelUtil.is1d7OrHigher(context.getCompilationUnit().getJavaProject())) - return false; } else if (!rightBinding.isEnum()) { return false; } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java index 7ebbf24627a..7ffc571bcf7 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java @@ -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 @@ -1584,9 +1584,6 @@ private static boolean getPickoutTypeFromMulticatchProposals(IInvocationContext } private static boolean getConvertToMultiCatchProposals(IInvocationContext context, ASTNode covering, Collection resultingCollections) { - if (!JavaModelUtil.is1d7OrHigher(context.getCompilationUnit().getJavaProject())) - return false; - CatchClause catchClause= (CatchClause) ASTResolving.findAncestor(covering, ASTNode.CATCH_CLAUSE); if (catchClause == null) { return false; @@ -1673,9 +1670,6 @@ private static boolean getConvertToMultiCatchProposals(IInvocationContext contex } private static boolean getUnrollMultiCatchProposals(IInvocationContext context, ASTNode covering, Collection resultingCollections) { - if (!JavaModelUtil.is1d7OrHigher(context.getCompilationUnit().getJavaProject())) - return false; - CatchClause catchClause= (CatchClause) ASTResolving.findAncestor(covering, ASTNode.CATCH_CLAUSE); if (catchClause == null) { return false; diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/SurroundWithTryMultiCatchAction.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/SurroundWithTryMultiCatchAction.java index 30cb3ed975f..44a5cd21790 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/SurroundWithTryMultiCatchAction.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/actions/SurroundWithTryMultiCatchAction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2016 IBM Corporation and others. + * Copyright (c) 2011, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -13,20 +13,12 @@ *******************************************************************************/ package org.eclipse.jdt.ui.actions; -import org.eclipse.jface.dialogs.MessageDialog; - import org.eclipse.jface.text.ITextSelection; import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.IJavaProject; -import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels; import org.eclipse.jdt.internal.corext.refactoring.surround.SurroundWithTryCatchRefactoring; -import org.eclipse.jdt.internal.corext.util.JavaModelUtil; -import org.eclipse.jdt.internal.corext.util.Messages; -import org.eclipse.jdt.internal.ui.JavaPlugin; -import org.eclipse.jdt.internal.ui.actions.SelectionConverter; import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; @@ -54,18 +46,6 @@ public SurroundWithTryMultiCatchAction(CompilationUnitEditor editor) { setText(RefactoringMessages.SurroundWithTryMultiCatchAction_label); } - @Override - public void run(ITextSelection selection) { - ICompilationUnit compilationUnit= SelectionConverter.getInputAsCompilationUnit(fEditor); - IJavaProject javaProject= compilationUnit.getJavaProject(); - if (!JavaModelUtil.is1d7OrHigher(javaProject)) { - String message= Messages.format(RefactoringMessages.SurroundWithTryMultiCatchAction_not17, BasicElementLabels.getJavaElementName(javaProject.getElementName())); - MessageDialog.openInformation(JavaPlugin.getActiveWorkbenchShell(), getDialogTitle(), message); - return; - } - super.run(selection); - } - @Override SurroundWithTryCatchRefactoring createRefactoring(ITextSelection selection, ICompilationUnit cu) { return SurroundWithTryCatchRefactoring.create(cu, selection.getOffset(), selection.getLength(), true); @@ -75,15 +55,4 @@ SurroundWithTryCatchRefactoring createRefactoring(ITextSelection selection, ICom String getDialogTitle() { return RefactoringMessages.SurroundWithTryMultiCatchAction_dialog_title; } - - @Override - boolean isApplicable() { - if (!super.isApplicable()) - return false; - - ICompilationUnit compilationUnit= SelectionConverter.getInputAsCompilationUnit(fEditor); - IJavaProject javaProject= compilationUnit.getJavaProject(); - return JavaModelUtil.is1d7OrHigher(javaProject); - } - }