11/*******************************************************************************
2- * Copyright (c) 2024 IBM Corporation and others.
2+ * Copyright (c) 2024, 2025 IBM Corporation and others.
33 *
44 * This program and the accompanying materials
55 * are made available under the terms of the Eclipse Public License 2.0
@@ -282,23 +282,21 @@ public void getUncaughtExceptionProposals(IInvocationContext context, IProblemLo
282282 proposals .add (refactoringCorrectionProposalToT (proposal , SURROUND_WITH_TRY_CATCH ));
283283 }
284284
285- if (JavaModelUtil .is1d7OrHigher (cu .getJavaProject ())) {
286- refactoring = SurroundWithTryCatchRefactoring .create (cu , offset , length , true );
287- if (refactoring == null )
288- return ;
285+ refactoring = SurroundWithTryCatchRefactoring .create (cu , offset , length , true );
286+ if (refactoring == null )
287+ return ;
289288
290- refactoring .setLeaveDirty (true );
291- if (refactoring .checkActivationBasics (astRoot ).isOK ()) {
292- String label ;
293- if ((vType != null ) && (vName != null ) && ASTNodes .isVarType (selectedNode , astRoot ) && affectedLocals .contains (vName .getIdentifier ())) {
294- label = Messages .format (CorrectionMessages .LocalCorrectionsSubProcessor_surroundwith_trymulticatch_var_description , new Object [] { vName .getIdentifier (), vType .getName () });
295- } else {
296- label = CorrectionMessages .LocalCorrectionsSubProcessor_surroundwith_trymulticatch_description ;
297- }
298- RefactoringCorrectionProposalCore proposal = new RefactoringCorrectionProposalCore (label , cu , refactoring , IProposalRelevance .SURROUND_WITH_TRY_MULTICATCH );
299- proposal .setLinkedProposalModel (refactoring .getLinkedProposalModel ());
300- proposals .add (refactoringCorrectionProposalToT (proposal , SURROUND_WITH_TRY_MULTI_CATCH ));
289+ refactoring .setLeaveDirty (true );
290+ if (refactoring .checkActivationBasics (astRoot ).isOK ()) {
291+ String label ;
292+ if ((vType != null ) && (vName != null ) && ASTNodes .isVarType (selectedNode , astRoot ) && affectedLocals .contains (vName .getIdentifier ())) {
293+ label = Messages .format (CorrectionMessages .LocalCorrectionsSubProcessor_surroundwith_trymulticatch_var_description , new Object [] { vName .getIdentifier (), vType .getName () });
294+ } else {
295+ label = CorrectionMessages .LocalCorrectionsSubProcessor_surroundwith_trymulticatch_description ;
301296 }
297+ RefactoringCorrectionProposalCore proposal = new RefactoringCorrectionProposalCore (label , cu , refactoring , IProposalRelevance .SURROUND_WITH_TRY_MULTICATCH );
298+ proposal .setLinkedProposalModel (refactoring .getLinkedProposalModel ());
299+ proposals .add (refactoringCorrectionProposalToT (proposal , SURROUND_WITH_TRY_MULTI_CATCH ));
302300 }
303301
304302 //Catch exception
@@ -322,113 +320,111 @@ public void getUncaughtExceptionProposals(IInvocationContext context, IProblemLo
322320 if (surroundingTry != null && (ASTNodes .isParent (selectedNode , surroundingTry .getBody ()) || selectedNode .getLocationInParent () == TryStatement .RESOURCES2_PROPERTY )) {
323321 addAdditionalCatchProposal (context , proposals , cu , selectedNode , offset , length , decl , uncaughtExceptions , surroundingTry , ast );
324322
325- if (JavaModelUtil .is1d7OrHigher (cu .getJavaProject ())) {
326- List <CatchClause > catchClauses = surroundingTry .catchClauses ();
327- List <ITypeBinding > filteredExceptions = SurroundWithTryCatchRefactoring .filterSubtypeExceptions (uncaughtExceptions );
328- ASTRewrite rewrite = ASTRewrite .create (ast );
329-
330- if (catchClauses != null && catchClauses .size () == 1 ) {
331- String label = filteredExceptions .size () > 1
332- ? CorrectionMessages .LocalCorrectionsSubProcessor_addexceptionstoexistingcatch_description
333- : CorrectionMessages .LocalCorrectionsSubProcessor_addexceptiontoexistingcatch_description ;
334- LinkedCorrectionProposalCore proposal = new LinkedCorrectionProposalCore (label , cu , rewrite , IProposalRelevance .ADD_EXCEPTIONS_TO_EXISTING_CATCH );
335- ImportRewrite imports = proposal .createImportRewrite (context .getASTRoot ());
336- ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext (decl , imports );
337-
338- CatchClause catchClause = catchClauses .get (0 );
339- Type originalType = catchClause .getException ().getType ();
340-
341- if (originalType instanceof UnionType ) {
342- UnionType unionType = (UnionType ) originalType ;
343- ListRewrite listRewrite = rewrite .getListRewrite (unionType , UnionType .TYPES_PROPERTY );
344- @ SuppressWarnings ("unchecked" )
345- List <Type > existingTypes = new ArrayList <>(unionType .types ());
346-
347- for (int i = 0 ; i < filteredExceptions .size (); i ++) {
348- Type addedType = addNewException (ast , filteredExceptions , rewrite , proposal , imports , importRewriteContext , i );
349- boolean isReplaced = false ;
350-
351- for (Type existingType : existingTypes ) {
352- if (existingType .resolveBinding ().isSubTypeCompatible (filteredExceptions .get (i ))) {
353- listRewrite .replace (existingType , addedType , null );
354- isReplaced = true ;
355- break ;
356- }
357- }
323+ List <CatchClause > catchClauses = surroundingTry .catchClauses ();
324+ List <ITypeBinding > filteredExceptions = SurroundWithTryCatchRefactoring .filterSubtypeExceptions (uncaughtExceptions );
325+ ASTRewrite rewrite = ASTRewrite .create (ast );
358326
359- if (! isReplaced ) {
360- listRewrite . insertLast ( addedType , null );
361- }
362- }
363- } else {
364- Type firstType = null ;
365- List < Type > typesToAdd = new ArrayList <>( );
327+ if (catchClauses != null && catchClauses . size () == 1 ) {
328+ String label = filteredExceptions . size () > 1
329+ ? CorrectionMessages . LocalCorrectionsSubProcessor_addexceptionstoexistingcatch_description
330+ : CorrectionMessages . LocalCorrectionsSubProcessor_addexceptiontoexistingcatch_description ;
331+ LinkedCorrectionProposalCore proposal = new LinkedCorrectionProposalCore ( label , cu , rewrite , IProposalRelevance . ADD_EXCEPTIONS_TO_EXISTING_CATCH );
332+ ImportRewrite imports = proposal . createImportRewrite ( context . getASTRoot ()) ;
333+ ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext ( decl , imports );
366334
367- for ( int i = 0 ; i < filteredExceptions . size (); i ++) {
368- Type addedType = addNewException ( ast , filteredExceptions , rewrite , proposal , imports , importRewriteContext , i );
335+ CatchClause catchClause = catchClauses . get ( 0 );
336+ Type originalType = catchClause . getException (). getType ( );
369337
370- if (originalType .resolveBinding ().isSubTypeCompatible (filteredExceptions .get (i ))) {
371- firstType = addedType ;
372- } else {
373- typesToAdd .add (addedType );
338+ if (originalType instanceof UnionType ) {
339+ UnionType unionType = (UnionType ) originalType ;
340+ ListRewrite listRewrite = rewrite .getListRewrite (unionType , UnionType .TYPES_PROPERTY );
341+ @ SuppressWarnings ("unchecked" )
342+ List <Type > existingTypes = new ArrayList <>(unionType .types ());
343+
344+ for (int i = 0 ; i < filteredExceptions .size (); i ++) {
345+ Type addedType = addNewException (ast , filteredExceptions , rewrite , proposal , imports , importRewriteContext , i );
346+ boolean isReplaced = false ;
347+
348+ for (Type existingType : existingTypes ) {
349+ if (existingType .resolveBinding ().isSubTypeCompatible (filteredExceptions .get (i ))) {
350+ listRewrite .replace (existingType , addedType , null );
351+ isReplaced = true ;
352+ break ;
374353 }
375354 }
376355
377- if (!typesToAdd .isEmpty ()) {
378- UnionType newUnionType = ast .newUnionType ();
379- List <Type > types = newUnionType .types ();
356+ if (!isReplaced ) {
357+ listRewrite .insertLast (addedType , null );
358+ }
359+ }
360+ } else {
361+ Type firstType = null ;
362+ List <Type > typesToAdd = new ArrayList <>();
380363
381- if (firstType == null ) {
382- types .add (ASTNodes .createMoveTarget (rewrite , originalType ));
383- } else {
384- types .add (firstType );
385- }
386- types .addAll (typesToAdd );
364+ for (int i = 0 ; i < filteredExceptions .size (); i ++) {
365+ Type addedType = addNewException (ast , filteredExceptions , rewrite , proposal , imports , importRewriteContext , i );
387366
388- rewrite .replace (originalType , newUnionType , null );
389- } else if (firstType != null ) {
390- rewrite .replace (originalType , firstType , null );
367+ if (originalType .resolveBinding ().isSubTypeCompatible (filteredExceptions .get (i ))) {
368+ firstType = addedType ;
369+ } else {
370+ typesToAdd .add (addedType );
391371 }
392372 }
393373
394- proposals .add (linkedCorrectionProposalToT (proposal , ADD_EXCEPTION_TO_CATCH ));
395- } else if (catchClauses != null && catchClauses .isEmpty () && filteredExceptions .size () > 1 ) {
396- String label = CorrectionMessages .LocalCorrectionsSubProcessor_addadditionalmulticatch_description ;
397- LinkedCorrectionProposalCore proposal = new LinkedCorrectionProposalCore (label , cu , rewrite , IProposalRelevance .ADD_EXCEPTIONS_TO_EXISTING_CATCH );
398- ImportRewrite imports = proposal .createImportRewrite (context .getASTRoot ());
399- ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext (decl , imports );
374+ if (!typesToAdd .isEmpty ()) {
375+ UnionType newUnionType = ast .newUnionType ();
376+ List <Type > types = newUnionType .types ();
377+
378+ if (firstType == null ) {
379+ types .add (ASTNodes .createMoveTarget (rewrite , originalType ));
380+ } else {
381+ types .add (firstType );
382+ }
383+ types .addAll (typesToAdd );
384+
385+ rewrite .replace (originalType , newUnionType , null );
386+ } else if (firstType != null ) {
387+ rewrite .replace (originalType , firstType , null );
388+ }
389+ }
400390
401- CodeScopeBuilder .Scope scope = CodeScopeBuilder .perform (decl , Selection .createFromStartLength (offset , length )).findScope (offset , length );
402- scope .setCursor (offset );
391+ proposals .add (linkedCorrectionProposalToT (proposal , ADD_EXCEPTION_TO_CATCH ));
392+ } else if (catchClauses != null && catchClauses .isEmpty () && filteredExceptions .size () > 1 ) {
393+ String label = CorrectionMessages .LocalCorrectionsSubProcessor_addadditionalmulticatch_description ;
394+ LinkedCorrectionProposalCore proposal = new LinkedCorrectionProposalCore (label , cu , rewrite , IProposalRelevance .ADD_EXCEPTIONS_TO_EXISTING_CATCH );
395+ ImportRewrite imports = proposal .createImportRewrite (context .getASTRoot ());
396+ ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext (decl , imports );
403397
404- CatchClause newCatchClause = ast .newCatchClause ();
405- String varName = StubUtility .getExceptionVariableName (cu .getJavaProject ());
406- String name = scope .createName (varName , false );
407- SingleVariableDeclaration var = ast .newSingleVariableDeclaration ();
408- var .setName (ast .newSimpleName (name ));
398+ CodeScopeBuilder .Scope scope = CodeScopeBuilder .perform (decl , Selection .createFromStartLength (offset , length )).findScope (offset , length );
399+ scope .setCursor (offset );
409400
410- UnionType newUnionType = ast .newUnionType ();
411- List <Type > types = newUnionType .types ();
401+ CatchClause newCatchClause = ast .newCatchClause ();
402+ String varName = StubUtility .getExceptionVariableName (cu .getJavaProject ());
403+ String name = scope .createName (varName , false );
404+ SingleVariableDeclaration var = ast .newSingleVariableDeclaration ();
405+ var .setName (ast .newSimpleName (name ));
412406
413- for (int i = 0 ; i < filteredExceptions .size (); i ++) {
414- types .add (addNewException (ast , filteredExceptions , rewrite , proposal , imports , importRewriteContext , i ));
415- }
407+ UnionType newUnionType = ast .newUnionType ();
408+ List <Type > types = newUnionType .types ();
416409
417- String nameKey = "name" ; //$NON-NLS-1$
418- proposal .addLinkedPosition (rewrite .track (var .getName ()), false , nameKey );
419- var .setType (newUnionType );
420- newCatchClause .setException (var );
421- String catchBody = StubUtility .getCatchBodyContent (cu , "Exception" , name , selectedNode , String .valueOf ('\n' )); //$NON-NLS-1$
410+ for (int i = 0 ; i < filteredExceptions .size (); i ++) {
411+ types .add (addNewException (ast , filteredExceptions , rewrite , proposal , imports , importRewriteContext , i ));
412+ }
422413
423- if (catchBody != null ) {
424- ASTNode node = rewrite .createStringPlaceholder (catchBody , ASTNode .RETURN_STATEMENT );
425- newCatchClause .getBody ().statements ().add (node );
426- }
414+ String nameKey = "name" ; //$NON-NLS-1$
415+ proposal .addLinkedPosition (rewrite .track (var .getName ()), false , nameKey );
416+ var .setType (newUnionType );
417+ newCatchClause .setException (var );
418+ String catchBody = StubUtility .getCatchBodyContent (cu , "Exception" , name , selectedNode , String .valueOf ('\n' )); //$NON-NLS-1$
427419
428- ListRewrite listRewrite = rewrite . getListRewrite ( surroundingTry , TryStatement . CATCH_CLAUSES_PROPERTY );
429- listRewrite . insertFirst ( newCatchClause , null );
430- proposals . add ( linkedCorrectionProposalToT ( proposal , ADD_EXCEPTION_TO_MULTI_CATCH ) );
420+ if ( catchBody != null ) {
421+ ASTNode node = rewrite . createStringPlaceholder ( catchBody , ASTNode . RETURN_STATEMENT );
422+ newCatchClause . getBody (). statements (). add ( node );
431423 }
424+
425+ ListRewrite listRewrite = rewrite .getListRewrite (surroundingTry , TryStatement .CATCH_CLAUSES_PROPERTY );
426+ listRewrite .insertFirst (newCatchClause , null );
427+ proposals .add (linkedCorrectionProposalToT (proposal , ADD_EXCEPTION_TO_MULTI_CATCH ));
432428 }
433429 }
434430
0 commit comments