Skip to content

Commit a3a4e66

Browse files
committed
Fix sporadic NullPointerException in VariableUtils
Use the JDT NamingConventions class for adding or removing the prefix and/or suffix of a given variable name. This avoids having to explicitly load the project-specific prefix/suffix properties. Resolves #717
1 parent 138e721 commit a3a4e66

File tree

5 files changed

+44
-99
lines changed

5 files changed

+44
-99
lines changed

org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/variable/ExposedFieldVariableSupport.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -15,7 +15,7 @@
1515
import org.eclipse.wb.internal.core.utils.ast.NodeTarget;
1616
import org.eclipse.wb.internal.core.utils.ast.StatementTarget;
1717

18-
import org.eclipse.jdt.core.JavaCore;
18+
import org.eclipse.jdt.core.NamingConventions;
1919
import org.eclipse.jdt.core.dom.Statement;
2020

2121
import org.apache.commons.lang3.StringUtils;
@@ -74,8 +74,7 @@ public String getComponentName() {
7474
name =
7575
new VariableUtils(m_javaInfo).stripPrefixSuffix(
7676
name,
77-
JavaCore.CODEASSIST_FIELD_PREFIXES,
78-
JavaCore.CODEASSIST_FIELD_SUFFIXES);
77+
NamingConventions.VK_INSTANCE_FIELD);
7978
name = StringUtils.capitalize(name);
8079
return m_hostJavaInfo.getVariableSupport().getComponentName() + name;
8180
}

org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/variable/FieldUniqueVariableSupport.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -18,7 +18,7 @@
1818
import org.eclipse.wb.internal.core.utils.ast.StatementTarget;
1919
import org.eclipse.wb.internal.core.utils.check.Assert;
2020

21-
import org.eclipse.jdt.core.JavaCore;
21+
import org.eclipse.jdt.core.NamingConventions;
2222
import org.eclipse.jdt.core.dom.AST;
2323
import org.eclipse.jdt.core.dom.Assignment;
2424
import org.eclipse.jdt.core.dom.Block;
@@ -127,10 +127,8 @@ public void convertFieldToLocal() throws Exception {
127127
m_utils.convertName(
128128
assignment.getStartPosition(),
129129
getName(),
130-
JavaCore.CODEASSIST_FIELD_PREFIXES,
131-
JavaCore.CODEASSIST_FIELD_SUFFIXES,
132-
JavaCore.CODEASSIST_LOCAL_PREFIXES,
133-
JavaCore.CODEASSIST_LOCAL_SUFFIXES,
130+
NamingConventions.VK_INSTANCE_FIELD,
131+
NamingConventions.VK_LOCAL,
134132
m_declaration);
135133
setName(localName);
136134
// replace "this.fieldName" with "localName"

org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/variable/FieldVariableSupport.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -14,7 +14,7 @@
1414
import org.eclipse.wb.internal.core.utils.ast.AstNodeUtils;
1515
import org.eclipse.wb.internal.core.utils.ast.NodeTarget;
1616

17-
import org.eclipse.jdt.core.JavaCore;
17+
import org.eclipse.jdt.core.NamingConventions;
1818
import org.eclipse.jdt.core.dom.Block;
1919
import org.eclipse.jdt.core.dom.Expression;
2020
import org.eclipse.jdt.core.dom.FieldDeclaration;
@@ -60,8 +60,7 @@ public boolean isValidStatementForChild(Statement statement) {
6060
public String getComponentName() {
6161
return m_utils.stripPrefixSuffix(
6262
getName(),
63-
JavaCore.CODEASSIST_FIELD_PREFIXES,
64-
JavaCore.CODEASSIST_FIELD_SUFFIXES);
63+
NamingConventions.VK_INSTANCE_FIELD);
6564
}
6665

6766
////////////////////////////////////////////////////////////////////////////
@@ -118,8 +117,7 @@ protected final void delete_removeDeclarationField() throws Exception {
118117
String decorateTextName(String newName) {
119118
return m_utils.addPrefixSuffix(
120119
newName,
121-
JavaCore.CODEASSIST_FIELD_PREFIXES,
122-
JavaCore.CODEASSIST_FIELD_SUFFIXES);
120+
NamingConventions.VK_INSTANCE_FIELD);
123121
}
124122

125123
////////////////////////////////////////////////////////////////////////////

org.eclipse.wb.core.java/src/org/eclipse/wb/internal/core/model/variable/LazyVariableSupportUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2023 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -22,7 +22,7 @@
2222
import org.eclipse.wb.internal.core.utils.check.Assert;
2323
import org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils;
2424

25-
import org.eclipse.jdt.core.JavaCore;
25+
import org.eclipse.jdt.core.NamingConventions;
2626
import org.eclipse.jdt.core.dom.ASTNode;
2727
import org.eclipse.jdt.core.dom.Assignment;
2828
import org.eclipse.jdt.core.dom.Block;
@@ -336,8 +336,7 @@ public static String getExpectedMethodName(JavaInfo javaInfo, String fieldName)
336336
String strippedFieldName =
337337
new VariableUtils(javaInfo).stripPrefixSuffix(
338338
fieldName,
339-
JavaCore.CODEASSIST_FIELD_PREFIXES,
340-
JavaCore.CODEASSIST_FIELD_SUFFIXES);
339+
NamingConventions.VK_INSTANCE_FIELD);
341340
return "get" + StringUtils.capitalize(strippedFieldName);
342341
}
343342

Lines changed: 30 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011 Google, Inc.
2+
* Copyright (c) 2011, 2024 Google, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -12,16 +12,13 @@
1212

1313
import org.eclipse.wb.core.model.JavaInfo;
1414
import org.eclipse.wb.internal.core.utils.check.Assert;
15-
import org.eclipse.wb.internal.core.utils.jdt.core.ProjectUtils;
1615

1716
import org.eclipse.jdt.core.IJavaProject;
18-
import org.eclipse.jdt.core.JavaCore;
17+
import org.eclipse.jdt.core.NamingConventions;
1918
import org.eclipse.jdt.core.dom.VariableDeclaration;
2019

2120
import org.apache.commons.lang3.StringUtils;
2221

23-
import java.util.Map;
24-
2522
/**
2623
* Utils for using in {@link VariableSupport} implementations.
2724
*
@@ -52,10 +49,8 @@ public String getUniqueFieldName(String localName, VariableDeclaration excludedV
5249
return convertName(
5350
-1,
5451
localName,
55-
JavaCore.CODEASSIST_LOCAL_PREFIXES,
56-
JavaCore.CODEASSIST_LOCAL_SUFFIXES,
57-
JavaCore.CODEASSIST_FIELD_PREFIXES,
58-
JavaCore.CODEASSIST_FIELD_SUFFIXES,
52+
NamingConventions.VK_LOCAL,
53+
NamingConventions.VK_INSTANCE_FIELD,
5954
excludedVariable);
6055
}
6156

@@ -64,10 +59,8 @@ public String getUniqueFieldName(String localName, VariableDeclaration excludedV
6459
*/
6560
public String convertName(int position,
6661
String name,
67-
String keyPrefixes_source,
68-
String keySuffixes_source,
69-
String keyPrefixes_target,
70-
String keySuffixes_target,
62+
int variableKind_source,
63+
int variableKind_target,
7164
VariableDeclaration excludedVariable) {
7265
// remove possible _NNN from base name
7366
{
@@ -80,88 +73,46 @@ public String convertName(int position,
8073
}
8174
}
8275
// remove source prefix/suffix
83-
name = stripPrefixSuffix(name, keyPrefixes_source, keySuffixes_source);
76+
name = stripPrefixSuffix(name, variableKind_source);
8477
// add target prefix/suffix
85-
name = addPrefixSuffix(name, keyPrefixes_target, keySuffixes_target);
78+
name = addPrefixSuffix(name, variableKind_target);
8679
// generate unique name
8780
return m_javaInfo.getEditor().getUniqueVariableName(position, name, excludedVariable);
8881
}
8982

9083
/**
9184
* @return the name with added prefix/suffix.
9285
*
93-
* @param keyPrefixes
94-
* the key of prefixes in {@link IJavaProject} options.
95-
* @param keySuffixes
96-
* the key of suffixes in {@link IJavaProject} options.
86+
* @param variableKind specifies what type the variable is:
87+
* {@link NamingConventions#VK_LOCAL},
88+
* {@link NamingConventions#VK_PARAMETER},
89+
* {@link NamingConventions#VK_STATIC_FIELD},
90+
* {@link NamingConventions#VK_INSTANCE_FIELD} or
91+
* {@link NamingConventions#VK_STATIC_FINAL_FIELD}.
9792
*/
98-
public String addPrefixSuffix(String name, String keyPrefixes, String keySuffixes) {
99-
// add prefix
100-
{
101-
String[] prefixes = getVariablesPrefixSuffixOptions(keyPrefixes);
102-
if (prefixes.length != 0) {
103-
String prefix = prefixes[0];
104-
if (!name.startsWith(prefix)) {
105-
name = prefix + name;
106-
}
107-
}
108-
}
109-
// add suffix
110-
{
111-
String[] suffixes = getVariablesPrefixSuffixOptions(keySuffixes);
112-
if (suffixes.length != 0) {
113-
String suffix = suffixes[0];
114-
if (!name.endsWith(suffix)) {
115-
name = name + suffix;
116-
}
117-
}
118-
}
119-
// return result
120-
return name;
93+
public String addPrefixSuffix(String name, int variableKind) {
94+
Assert.isNotNull(name);
95+
IJavaProject javaProject = m_javaInfo.getEditor().getJavaProject();
96+
String[] variableNames = NamingConventions.suggestVariableNames(variableKind, NamingConventions.BK_NAME, name,
97+
javaProject, 0, null, true);
98+
// The first entry contains the combination prefix + name + suffix
99+
return variableNames[0];
121100
}
122101

123102
/**
124103
* @return the name with removed prefix/suffix.
125104
*
126-
* @param keyPrefixes
127-
* the key of prefixes in {@link IJavaProject} options.
128-
* @param keySuffixes
129-
* the key of suffixes in {@link IJavaProject} options.
105+
* @param variableKind specifies what type the variable is:
106+
* {@link NamingConventions#VK_LOCAL},
107+
* {@link NamingConventions#VK_PARAMETER},
108+
* {@link NamingConventions#VK_STATIC_FIELD},
109+
* {@link NamingConventions#VK_INSTANCE_FIELD} or
110+
* {@link NamingConventions#VK_STATIC_FINAL_FIELD}.
130111
*/
131-
public String stripPrefixSuffix(String name, String keyPrefixes, String keySuffixes) {
112+
public String stripPrefixSuffix(String name, int variableKind) {
132113
Assert.isNotNull(name);
133-
Assert.isNotNull(keyPrefixes);
134-
Assert.isNotNull(keySuffixes);
135-
// remove prefix
136-
{
137-
String[] prefixes = getVariablesPrefixSuffixOptions(keyPrefixes);
138-
for (String prefix : prefixes) {
139-
if (name.startsWith(prefix)) {
140-
name = name.substring(prefix.length());
141-
break;
142-
}
143-
}
144-
}
145-
// remove suffix
146-
{
147-
String[] suffixes = getVariablesPrefixSuffixOptions(keySuffixes);
148-
for (String suffix : suffixes) {
149-
if (name.endsWith(suffix)) {
150-
name = name.substring(0, name.length() - suffix.length());
151-
break;
152-
}
153-
}
154-
}
155-
// return result
156-
return name;
157-
}
158-
159-
/**
160-
* @return the array of prefixes or suffixes for given key.
161-
*/
162-
private String[] getVariablesPrefixSuffixOptions(String key) {
163114
IJavaProject javaProject = m_javaInfo.getEditor().getJavaProject();
164-
Map<String, String> javaOptions = ProjectUtils.getOptions(javaProject);
165-
return StringUtils.split(javaOptions.get(key), ",");
115+
String baseName = NamingConventions.getBaseName(variableKind, name, javaProject);
116+
return baseName;
166117
}
167118
}

0 commit comments

Comments
 (0)