Skip to content

Commit

Permalink
Remove Java 7 checks
Browse files Browse the repository at this point in the history
ECJ support 1.8 as lowest version and JDT UI converts projects to that
as a min level thus this code is non reachable.
  • Loading branch information
akurtakov committed Jan 1, 2025
1 parent b9233aa commit 9c3b475
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 273 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<CatchClause> catchClauses= surroundingTry.catchClauses();
List<ITypeBinding> 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<Type> 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<CatchClause> catchClauses= surroundingTry.catchClauses();
List<ITypeBinding> filteredExceptions= SurroundWithTryCatchRefactoring.filterSubtypeExceptions(uncaughtExceptions);
ASTRewrite rewrite= ASTRewrite.create(ast);

if (!isReplaced) {
listRewrite.insertLast(addedType, null);
}
}
} else {
Type firstType= null;
List<Type> 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<Type> 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<Type> types= newUnionType.types();
if (!isReplaced) {
listRewrite.insertLast(addedType, null);
}
}
} else {
Type firstType= null;
List<Type> 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<Type> 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<Type> 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<Type> 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));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -64,9 +63,6 @@ public void createAddSafeVarargsProposals(IInvocationContext context, IProblemLo
}

public void createAddSafeVarargsToDeclarationProposals(IInvocationContext context, IProblemLocation problem, Collection<T> proposals) {
if (!JavaModelUtil.is1d7OrHigher(context.getCompilationUnit().getJavaProject()))
return;

ASTNode coveringNode= problem.getCoveringNode(context.getASTRoot());
IMethodBinding methodBinding;
if (coveringNode instanceof MethodInvocation) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 9c3b475

Please sign in to comment.