Skip to content

Commit

Permalink
Fix sporadic NullPointerException in VariableUtils
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ptziegler committed Mar 4, 2024
1 parent 138e721 commit a3a4e66
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -15,7 +15,7 @@
import org.eclipse.wb.internal.core.utils.ast.NodeTarget;
import org.eclipse.wb.internal.core.utils.ast.StatementTarget;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.NamingConventions;
import org.eclipse.jdt.core.dom.Statement;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -74,8 +74,7 @@ public String getComponentName() {
name =
new VariableUtils(m_javaInfo).stripPrefixSuffix(
name,
JavaCore.CODEASSIST_FIELD_PREFIXES,
JavaCore.CODEASSIST_FIELD_SUFFIXES);
NamingConventions.VK_INSTANCE_FIELD);
name = StringUtils.capitalize(name);
return m_hostJavaInfo.getVariableSupport().getComponentName() + name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -18,7 +18,7 @@
import org.eclipse.wb.internal.core.utils.ast.StatementTarget;
import org.eclipse.wb.internal.core.utils.check.Assert;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.NamingConventions;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
Expand Down Expand Up @@ -127,10 +127,8 @@ public void convertFieldToLocal() throws Exception {
m_utils.convertName(
assignment.getStartPosition(),
getName(),
JavaCore.CODEASSIST_FIELD_PREFIXES,
JavaCore.CODEASSIST_FIELD_SUFFIXES,
JavaCore.CODEASSIST_LOCAL_PREFIXES,
JavaCore.CODEASSIST_LOCAL_SUFFIXES,
NamingConventions.VK_INSTANCE_FIELD,
NamingConventions.VK_LOCAL,
m_declaration);
setName(localName);
// replace "this.fieldName" with "localName"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -14,7 +14,7 @@
import org.eclipse.wb.internal.core.utils.ast.AstNodeUtils;
import org.eclipse.wb.internal.core.utils.ast.NodeTarget;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.NamingConventions;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldDeclaration;
Expand Down Expand Up @@ -60,8 +60,7 @@ public boolean isValidStatementForChild(Statement statement) {
public String getComponentName() {
return m_utils.stripPrefixSuffix(
getName(),
JavaCore.CODEASSIST_FIELD_PREFIXES,
JavaCore.CODEASSIST_FIELD_SUFFIXES);
NamingConventions.VK_INSTANCE_FIELD);
}

////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -118,8 +117,7 @@ protected final void delete_removeDeclarationField() throws Exception {
String decorateTextName(String newName) {
return m_utils.addPrefixSuffix(
newName,
JavaCore.CODEASSIST_FIELD_PREFIXES,
JavaCore.CODEASSIST_FIELD_SUFFIXES);
NamingConventions.VK_INSTANCE_FIELD);
}

////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2023 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -22,7 +22,7 @@
import org.eclipse.wb.internal.core.utils.check.Assert;
import org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.NamingConventions;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
Expand Down Expand Up @@ -336,8 +336,7 @@ public static String getExpectedMethodName(JavaInfo javaInfo, String fieldName)
String strippedFieldName =
new VariableUtils(javaInfo).stripPrefixSuffix(
fieldName,
JavaCore.CODEASSIST_FIELD_PREFIXES,
JavaCore.CODEASSIST_FIELD_SUFFIXES);
NamingConventions.VK_INSTANCE_FIELD);
return "get" + StringUtils.capitalize(strippedFieldName);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2011, 2024 Google, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -12,16 +12,13 @@

import org.eclipse.wb.core.model.JavaInfo;
import org.eclipse.wb.internal.core.utils.check.Assert;
import org.eclipse.wb.internal.core.utils.jdt.core.ProjectUtils;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.NamingConventions;
import org.eclipse.jdt.core.dom.VariableDeclaration;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
* Utils for using in {@link VariableSupport} implementations.
*
Expand Down Expand Up @@ -52,10 +49,8 @@ public String getUniqueFieldName(String localName, VariableDeclaration excludedV
return convertName(
-1,
localName,
JavaCore.CODEASSIST_LOCAL_PREFIXES,
JavaCore.CODEASSIST_LOCAL_SUFFIXES,
JavaCore.CODEASSIST_FIELD_PREFIXES,
JavaCore.CODEASSIST_FIELD_SUFFIXES,
NamingConventions.VK_LOCAL,
NamingConventions.VK_INSTANCE_FIELD,
excludedVariable);
}

Expand All @@ -64,10 +59,8 @@ public String getUniqueFieldName(String localName, VariableDeclaration excludedV
*/
public String convertName(int position,
String name,
String keyPrefixes_source,
String keySuffixes_source,
String keyPrefixes_target,
String keySuffixes_target,
int variableKind_source,
int variableKind_target,
VariableDeclaration excludedVariable) {
// remove possible _NNN from base name
{
Expand All @@ -80,88 +73,46 @@ public String convertName(int position,
}
}
// remove source prefix/suffix
name = stripPrefixSuffix(name, keyPrefixes_source, keySuffixes_source);
name = stripPrefixSuffix(name, variableKind_source);
// add target prefix/suffix
name = addPrefixSuffix(name, keyPrefixes_target, keySuffixes_target);
name = addPrefixSuffix(name, variableKind_target);
// generate unique name
return m_javaInfo.getEditor().getUniqueVariableName(position, name, excludedVariable);
}

/**
* @return the name with added prefix/suffix.
*
* @param keyPrefixes
* the key of prefixes in {@link IJavaProject} options.
* @param keySuffixes
* the key of suffixes in {@link IJavaProject} options.
* @param variableKind specifies what type the variable is:
* {@link NamingConventions#VK_LOCAL},
* {@link NamingConventions#VK_PARAMETER},
* {@link NamingConventions#VK_STATIC_FIELD},
* {@link NamingConventions#VK_INSTANCE_FIELD} or
* {@link NamingConventions#VK_STATIC_FINAL_FIELD}.
*/
public String addPrefixSuffix(String name, String keyPrefixes, String keySuffixes) {
// add prefix
{
String[] prefixes = getVariablesPrefixSuffixOptions(keyPrefixes);
if (prefixes.length != 0) {
String prefix = prefixes[0];
if (!name.startsWith(prefix)) {
name = prefix + name;
}
}
}
// add suffix
{
String[] suffixes = getVariablesPrefixSuffixOptions(keySuffixes);
if (suffixes.length != 0) {
String suffix = suffixes[0];
if (!name.endsWith(suffix)) {
name = name + suffix;
}
}
}
// return result
return name;
public String addPrefixSuffix(String name, int variableKind) {
Assert.isNotNull(name);
IJavaProject javaProject = m_javaInfo.getEditor().getJavaProject();
String[] variableNames = NamingConventions.suggestVariableNames(variableKind, NamingConventions.BK_NAME, name,
javaProject, 0, null, true);
// The first entry contains the combination prefix + name + suffix
return variableNames[0];
}

/**
* @return the name with removed prefix/suffix.
*
* @param keyPrefixes
* the key of prefixes in {@link IJavaProject} options.
* @param keySuffixes
* the key of suffixes in {@link IJavaProject} options.
* @param variableKind specifies what type the variable is:
* {@link NamingConventions#VK_LOCAL},
* {@link NamingConventions#VK_PARAMETER},
* {@link NamingConventions#VK_STATIC_FIELD},
* {@link NamingConventions#VK_INSTANCE_FIELD} or
* {@link NamingConventions#VK_STATIC_FINAL_FIELD}.
*/
public String stripPrefixSuffix(String name, String keyPrefixes, String keySuffixes) {
public String stripPrefixSuffix(String name, int variableKind) {
Assert.isNotNull(name);
Assert.isNotNull(keyPrefixes);
Assert.isNotNull(keySuffixes);
// remove prefix
{
String[] prefixes = getVariablesPrefixSuffixOptions(keyPrefixes);
for (String prefix : prefixes) {
if (name.startsWith(prefix)) {
name = name.substring(prefix.length());
break;
}
}
}
// remove suffix
{
String[] suffixes = getVariablesPrefixSuffixOptions(keySuffixes);
for (String suffix : suffixes) {
if (name.endsWith(suffix)) {
name = name.substring(0, name.length() - suffix.length());
break;
}
}
}
// return result
return name;
}

/**
* @return the array of prefixes or suffixes for given key.
*/
private String[] getVariablesPrefixSuffixOptions(String key) {
IJavaProject javaProject = m_javaInfo.getEditor().getJavaProject();
Map<String, String> javaOptions = ProjectUtils.getOptions(javaProject);
return StringUtils.split(javaOptions.get(key), ",");
String baseName = NamingConventions.getBaseName(variableKind, name, javaProject);
return baseName;
}
}

0 comments on commit a3a4e66

Please sign in to comment.