From 9887b92cf11f31a77a130065a3fcba9031b7747b Mon Sep 17 00:00:00 2001 From: nettozahler <106603198+nettozahler@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:25:37 +0100 Subject: [PATCH] Bugfix for #1803: [Externalize Strings] IndexOutOfBoundsException. (#1805) Fixes https://github.com/eclipse-jdt/eclipse.jdt.ui/issues/1803 --- .../corext/refactoring/nls/NLSHint.java | 28 ++++++++++--------- .../nls/NLSHintStripQuotesTest.java | 8 ++++++ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/org.eclipse.jdt.core.manipulation/refactoring/org/eclipse/jdt/internal/corext/refactoring/nls/NLSHint.java b/org.eclipse.jdt.core.manipulation/refactoring/org/eclipse/jdt/internal/corext/refactoring/nls/NLSHint.java index d60f27fa92d..e5ab4d6abc4 100644 --- a/org.eclipse.jdt.core.manipulation/refactoring/org/eclipse/jdt/internal/corext/refactoring/nls/NLSHint.java +++ b/org.eclipse.jdt.core.manipulation/refactoring/org/eclipse/jdt/internal/corext/refactoring/nls/NLSHint.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2023 IBM Corporation and others. + * Copyright (c) 2000, 2024 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -18,6 +18,7 @@ import java.util.Properties; import java.util.SortedMap; import java.util.TreeMap; +import java.util.regex.Pattern; import org.eclipse.osgi.util.NLS; @@ -51,7 +52,6 @@ import org.eclipse.jdt.internal.corext.util.JavaModelUtil; - /** * calculates hints for the nls-refactoring out of a compilation unit. * - package fragments of the accessor class and the resource bundle @@ -59,6 +59,13 @@ */ public class NLSHint { + /** + * A text block begins with three double-quote characters followed by zero or more white spaces followed by + * a line terminator. A text block cannot be on a single line, nor can the contents of the text block follow + * the three opening double-quotes (and maybe whitespace) immediately without a line terminator. + */ + private static final Pattern TEXT_BLOCK_START_PATTERN = Pattern.compile("^\"\"\"\\s*\r?\n"); //$NON-NLS-1$ + private String fAccessorName; private IPackageFragment fAccessorPackage; private String fResourceBundleName; @@ -159,7 +166,7 @@ public boolean visit(QualifiedName node) { } SimpleName name= node.getName(); NLSElement element= new NLSElement(node.getName().getIdentifier(), name.getStartPosition(), - name.getLength(), nlsLine.size() - 1, true); + name.getLength(), nlsLine.size() - 1, true); nlsLine.add(element); String bundleName; ICompilationUnit bundleCU= (ICompilationUnit)type.getJavaElement().getAncestor(IJavaElement.COMPILATION_UNIT); @@ -261,10 +268,11 @@ private static AccessorClassReference findFirstAccessorReference(NLSLine[] lines } public static String stripQuotes(String str, IJavaProject project) { - if (JavaModelUtil.is15OrHigher(project)) { - if (str.startsWith("\"\"\"") && str.endsWith("\"\"\"")) { //$NON-NLS-1$ //$NON-NLS-2$ - return getTextBlock(str.substring(3, str.length() - 3)); - } + // Test if the given string is a text block and start with the "cheapest" check + if (str.endsWith("\"\"\"") //$NON-NLS-1$ + && TEXT_BLOCK_START_PATTERN.matcher(str).find() + && JavaModelUtil.is15OrHigher(project)) { + return getTextBlock(str.substring(3, str.length() - 3)); } return str.substring(1, str.length() - 1); } @@ -277,15 +285,10 @@ private static NLSLine[] createRawLines(ICompilationUnit cu) { } } - public String getAccessorClassName() { return fAccessorName; } -// public boolean isEclipseNLS() { -// return fIsEclipseNLS; -// } - public IPackageFragment getAccessorClassPackage() { return fAccessorPackage; } @@ -559,5 +562,4 @@ private static int getHexadecimalValue(char c) { return -1; } } - } diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/nls/NLSHintStripQuotesTest.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/nls/NLSHintStripQuotesTest.java index 254d0c50c62..80daee09ded 100644 --- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/nls/NLSHintStripQuotesTest.java +++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/nls/NLSHintStripQuotesTest.java @@ -109,4 +109,12 @@ public void test08() throws Exception { String expected= " abc\ndef\n"; assertEquals(expected, y); } + + @Test + public void test09() throws Exception { + String x = "\"\"\""; + String y = NLSHint.stripQuotes(x, javaProject15.getJavaProject()); + String expected = "\""; + assertEquals(expected, y); + } }