From c2ea72d739c8dda8bcd5c867b07a3a6e4620c76d Mon Sep 17 00:00:00 2001 From: Rob Stryker Date: Tue, 21 May 2024 10:17:17 -0400 Subject: [PATCH] partial fix to testBug497719_0001 - modifiers missing on try with resources Signed-off-by: Rob Stryker --- .../eclipse/jdt/core/dom/JavacConverter.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java index d8e2ddad539..1fc394b6f8a 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacConverter.java @@ -1735,7 +1735,7 @@ private Statement convertStatement(JCStatement javac, ASTNode parent) { return res; } if (javac instanceof JCTry tryStatement) { - return convertTryStatement(tryStatement); + return convertTryStatement(tryStatement, parent); } if (javac instanceof JCSynchronized jcSynchronized) { SynchronizedStatement res = this.ast.newSynchronizedStatement(); @@ -2003,7 +2003,7 @@ private Block convertBlock(JCBlock javac) { return res; } - private TryStatement convertTryStatement(JCTry javac) { + private TryStatement convertTryStatement(JCTry javac, ASTNode parent) { TryStatement res = this.ast.newTryStatement(); commonSettings(res, javac); res.setBody(convertBlock(javac.getBlock())); @@ -2012,15 +2012,19 @@ private TryStatement convertTryStatement(JCTry javac) { } if( this.ast.apiLevel >= AST.JLS4_INTERNAL) { - javac.getResources().stream().map(this::convertTryResource) - .filter(Objects::nonNull) - .forEach(res.resources()::add); + Iterator it = javac.getResources().iterator(); + while(it.hasNext()) { + ASTNode working = convertTryResource(it.next(), parent); + if( working != null ) { + res.resources().add(working); + } + } } javac.getCatches().stream().map(this::convertCatcher).forEach(res.catchClauses()::add); return res; } - private ASTNode /*VariableDeclarationExpression or Name*/ convertTryResource(JCTree javac) { + private ASTNode /*VariableDeclarationExpression or Name*/ convertTryResource(JCTree javac, ASTNode parent) { if (javac instanceof JCFieldAccess || javac instanceof JCIdent) { return toName(javac); } @@ -2050,8 +2054,16 @@ private TryStatement convertTryStatement(JCTry javac) { fragment = this.ast.newVariableDeclarationFragment(); } VariableDeclarationExpression res = this.ast.newVariableDeclarationExpression(fragment); - res.setType(convertToType(decl.getType())); commonSettings(res, javac); + res.setType(convertToType(decl.getType())); + if( this.ast.apiLevel > AST.JLS2_INTERNAL) { + res.modifiers().addAll(convert(decl.getModifiers(), res)); + } else { + JCModifiers mods = decl.getModifiers(); + int[] total = new int[] {0}; + mods.getFlags().forEach(x -> {total[0] += modifierToFlagVal(x);}); + res.internalSetModifiers(total[0]); + } return res; } if (javac instanceof JCErroneous error && error.getErrorTrees().isEmpty()) { @@ -2472,10 +2484,12 @@ private Modifier convert(javax.lang.model.element.Modifier javac, int startPos, Modifier res = modifierToDom(javac); if (startPos >= 0) { // This needs work... It's not a great solution. - String sub = this.rawText.substring(startPos, endPos); - int indOf = sub.indexOf(res.getKeyword().toString()); - if( indOf != -1 ) { - res.setSourceRange(startPos+indOf, res.getKeyword().toString().length()); + if( endPos >= startPos && endPos >= 0 && endPos < this.rawText.length()) { + String sub = this.rawText.substring(startPos, endPos); + int indOf = sub.indexOf(res.getKeyword().toString()); + if( indOf != -1 ) { + res.setSourceRange(startPos+indOf, res.getKeyword().toString().length()); + } } } return res;