From 3bbedf23419b015bd0c681940ff8074ebe7f03d5 Mon Sep 17 00:00:00 2001 From: Christian Schima Date: Thu, 21 Nov 2024 10:51:29 +0100 Subject: [PATCH] Bugfix for #1803: [Externalize Strings] IndexOutOfBoundsException. --- .../corext/refactoring/nls/NLSHint.java | 28 ++++++++++--------- 1 file changed, 15 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..0ee89f7cd0c 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 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 immediately without a line terminator. + */ + private static final Pattern TEXT_BLOCK_START_PATTERN = Pattern.compile("^\"\"\"\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; } } - }