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
1212
1313import org .eclipse .wb .core .model .JavaInfo ;
1414import org .eclipse .wb .internal .core .utils .check .Assert ;
15- import org .eclipse .wb .internal .core .utils .jdt .core .ProjectUtils ;
1615
1716import org .eclipse .jdt .core .IJavaProject ;
18- import org .eclipse .jdt .core .JavaCore ;
17+ import org .eclipse .jdt .core .NamingConventions ;
1918import org .eclipse .jdt .core .dom .VariableDeclaration ;
2019
2120import 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