1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 * John Kaplan, johnkaplantech@gmail.com - 108071 [code templates] template for body of newly created class
11 *******************************************************************************/
12 package org.eclipse.jdt.internal.corext.codemanipulation;
14 import java.io.IOException;
15 import java.lang.reflect.Modifier;
16 import java.util.AbstractList;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashSet;
21 import java.util.LinkedHashSet;
22 import java.util.List;
24 import java.util.StringTokenizer;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Platform;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.preferences.IScopeContext;
31 import org.eclipse.core.runtime.preferences.InstanceScope;
33 import org.eclipse.core.resources.IProject;
34 import org.eclipse.core.resources.ProjectScope;
36 import org.eclipse.text.edits.DeleteEdit;
37 import org.eclipse.text.edits.MalformedTreeException;
38 import org.eclipse.text.edits.MultiTextEdit;
40 import org.eclipse.jface.text.BadLocationException;
41 import org.eclipse.jface.text.Document;
42 import org.eclipse.jface.text.IDocument;
43 import org.eclipse.jface.text.IRegion;
44 import org.eclipse.jface.text.templates.Template;
45 import org.eclipse.jface.text.templates.TemplateBuffer;
46 import org.eclipse.jface.text.templates.TemplateException;
47 import org.eclipse.jface.text.templates.TemplateVariable;
48 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
49 import org.eclipse.jface.text.templates.persistence.TemplateStore;
51 import org.eclipse.jdt.core.Flags;
52 import org.eclipse.jdt.core.IBuffer;
53 import org.eclipse.jdt.core.ICompilationUnit;
54 import org.eclipse.jdt.core.IField;
55 import org.eclipse.jdt.core.IJavaElement;
56 import org.eclipse.jdt.core.IJavaProject;
57 import org.eclipse.jdt.core.IMethod;
58 import org.eclipse.jdt.core.IOpenable;
59 import org.eclipse.jdt.core.IPackageFragment;
60 import org.eclipse.jdt.core.IParent;
61 import org.eclipse.jdt.core.ISourceReference;
62 import org.eclipse.jdt.core.IType;
63 import org.eclipse.jdt.core.ITypeParameter;
64 import org.eclipse.jdt.core.ITypeRoot;
65 import org.eclipse.jdt.core.JavaCore;
66 import org.eclipse.jdt.core.JavaModelException;
67 import org.eclipse.jdt.core.NamingConventions;
68 import org.eclipse.jdt.core.Signature;
69 import org.eclipse.jdt.core.dom.AST;
70 import org.eclipse.jdt.core.dom.ASTNode;
71 import org.eclipse.jdt.core.dom.ASTParser;
72 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
73 import org.eclipse.jdt.core.dom.ArrayType;
74 import org.eclipse.jdt.core.dom.CastExpression;
75 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
76 import org.eclipse.jdt.core.dom.CompilationUnit;
77 import org.eclipse.jdt.core.dom.ConstructorInvocation;
78 import org.eclipse.jdt.core.dom.Expression;
79 import org.eclipse.jdt.core.dom.FieldAccess;
80 import org.eclipse.jdt.core.dom.IBinding;
81 import org.eclipse.jdt.core.dom.IMethodBinding;
82 import org.eclipse.jdt.core.dom.ITypeBinding;
83 import org.eclipse.jdt.core.dom.IVariableBinding;
84 import org.eclipse.jdt.core.dom.MethodDeclaration;
85 import org.eclipse.jdt.core.dom.MethodInvocation;
86 import org.eclipse.jdt.core.dom.Name;
87 import org.eclipse.jdt.core.dom.NumberLiteral;
88 import org.eclipse.jdt.core.dom.ParameterizedType;
89 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
90 import org.eclipse.jdt.core.dom.StringLiteral;
91 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
92 import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
93 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
94 import org.eclipse.jdt.core.dom.Type;
95 import org.eclipse.jdt.core.dom.TypeParameter;
96 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
97 import org.eclipse.jdt.core.formatter.IndentManipulation;
99 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
100 import org.eclipse.jdt.internal.corext.dom.Bindings;
101 import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext;
102 import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType;
103 import org.eclipse.jdt.internal.corext.util.Strings;
105 import org.eclipse.jdt.ui.CodeStyleConfiguration;
106 import org.eclipse.jdt.ui.PreferenceConstants;
108 import org.eclipse.jdt.internal.ui.JavaPlugin;
109 import org.eclipse.jdt.internal.ui.JavaUIStatus;
110 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
111 import org.eclipse.jdt.internal.ui.text.correction.ASTResolving;
112 import org.eclipse.jdt.internal.ui.viewsupport.ProjectTemplateStore;
114 public class StubUtility {
116 private static final String[] EMPTY= new String[0];
118 private static final Set<String> VALID_TYPE_BODY_TEMPLATES;
120 VALID_TYPE_BODY_TEMPLATES= new HashSet<String>();
121 VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.CLASSBODY_ID);
122 VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.INTERFACEBODY_ID);
123 VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.ENUMBODY_ID);
124 VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.ANNOTATIONBODY_ID);
128 * Don't use this method directly, use CodeGeneration.
130 public static String getMethodBodyContent(boolean isConstructor, IJavaProject project, String destTypeName, String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
131 String templateName= isConstructor ? CodeTemplateContextType.CONSTRUCTORSTUB_ID : CodeTemplateContextType.METHODSTUB_ID;
132 Template template= getCodeTemplate(templateName, project);
133 if (template == null) {
134 return bodyStatement;
136 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
137 context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
138 context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
139 context.setVariable(CodeTemplateContextType.BODY_STATEMENT, bodyStatement);
140 String str= evaluateTemplate(context, template, new String[] { CodeTemplateContextType.BODY_STATEMENT });
141 if (str == null && !Strings.containsOnlyWhitespaces(bodyStatement)) {
142 return bodyStatement;
148 * Don't use this method directly, use CodeGeneration.
150 public static String getGetterMethodBodyContent(IJavaProject project, String destTypeName, String methodName, String fieldName, String lineDelimiter) throws CoreException {
151 String templateName= CodeTemplateContextType.GETTERSTUB_ID;
152 Template template= getCodeTemplate(templateName, project);
153 if (template == null) {
156 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
157 context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
158 context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
159 context.setVariable(CodeTemplateContextType.FIELD, fieldName);
161 return evaluateTemplate(context, template);
165 * Don't use this method directly, use CodeGeneration.
167 public static String getSetterMethodBodyContent(IJavaProject project, String destTypeName, String methodName, String fieldName, String paramName, String lineDelimiter) throws CoreException {
168 String templateName= CodeTemplateContextType.SETTERSTUB_ID;
169 Template template= getCodeTemplate(templateName, project);
170 if (template == null) {
173 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
174 context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
175 context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
176 context.setVariable(CodeTemplateContextType.FIELD, fieldName);
177 context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldName);
178 context.setVariable(CodeTemplateContextType.PARAM, paramName);
180 return evaluateTemplate(context, template);
183 public static String getCatchBodyContent(ICompilationUnit cu, String exceptionType, String variableName, ASTNode locationInAST, String lineDelimiter) throws CoreException {
184 String enclosingType= ""; //$NON-NLS-1$
185 String enclosingMethod= ""; //$NON-NLS-1$
187 if (locationInAST != null) {
188 MethodDeclaration parentMethod= ASTResolving.findParentMethodDeclaration(locationInAST);
189 if (parentMethod != null) {
190 enclosingMethod= parentMethod.getName().getIdentifier();
191 locationInAST= parentMethod;
193 ASTNode parentType= ASTResolving.findParentType(locationInAST);
194 if (parentType instanceof AbstractTypeDeclaration) {
195 enclosingType= ((AbstractTypeDeclaration)parentType).getName().getIdentifier();
198 return getCatchBodyContent(cu, exceptionType, variableName, enclosingType, enclosingMethod, lineDelimiter);
202 public static String getCatchBodyContent(ICompilationUnit cu, String exceptionType, String variableName, String enclosingType, String enclosingMethod, String lineDelimiter) throws CoreException {
203 Template template= getCodeTemplate(CodeTemplateContextType.CATCHBLOCK_ID, cu.getJavaProject());
204 if (template == null) {
208 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
209 context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, enclosingType);
210 context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, enclosingMethod);
211 context.setVariable(CodeTemplateContextType.EXCEPTION_TYPE, exceptionType);
212 context.setVariable(CodeTemplateContextType.EXCEPTION_VAR, variableName);
213 return evaluateTemplate(context, template);
217 * Don't use this method directly, use CodeGeneration.
218 * @see org.eclipse.jdt.ui.CodeGeneration#getCompilationUnitContent(ICompilationUnit, String, String, String, String)
220 public static String getCompilationUnitContent(ICompilationUnit cu, String fileComment, String typeComment, String typeContent, String lineDelimiter) throws CoreException {
221 IPackageFragment pack= (IPackageFragment)cu.getParent();
222 String packDecl= pack.isDefaultPackage() ? "" : "package " + pack.getElementName() + ';'; //$NON-NLS-1$ //$NON-NLS-2$
223 return getCompilationUnitContent(cu, packDecl, fileComment, typeComment, typeContent, lineDelimiter);
226 public static String getCompilationUnitContent(ICompilationUnit cu, String packDecl, String fileComment, String typeComment, String typeContent, String lineDelimiter) throws CoreException {
227 Template template= getCodeTemplate(CodeTemplateContextType.NEWTYPE_ID, cu.getJavaProject());
228 if (template == null) {
232 IJavaProject project= cu.getJavaProject();
233 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
234 context.setCompilationUnitVariables(cu);
235 context.setVariable(CodeTemplateContextType.PACKAGE_DECLARATION, packDecl);
236 context.setVariable(CodeTemplateContextType.TYPE_COMMENT, typeComment != null ? typeComment : ""); //$NON-NLS-1$
237 context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
238 context.setVariable(CodeTemplateContextType.TYPE_DECLARATION, typeContent);
239 context.setVariable(CodeTemplateContextType.TYPENAME, JavaCore.removeJavaLikeExtension(cu.getElementName()));
241 String[] fullLine= { CodeTemplateContextType.PACKAGE_DECLARATION, CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT };
242 return evaluateTemplate(context, template, fullLine);
247 * Don't use this method directly, use CodeGeneration.
248 * @see org.eclipse.jdt.ui.CodeGeneration#getFileComment(ICompilationUnit, String)
250 public static String getFileComment(ICompilationUnit cu, String lineDelimiter) throws CoreException {
251 Template template= getCodeTemplate(CodeTemplateContextType.FILECOMMENT_ID, cu.getJavaProject());
252 if (template == null) {
256 IJavaProject project= cu.getJavaProject();
257 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
258 context.setCompilationUnitVariables(cu);
259 context.setVariable(CodeTemplateContextType.TYPENAME, JavaCore.removeJavaLikeExtension(cu.getElementName()));
260 return evaluateTemplate(context, template);
264 * Don't use this method directly, use CodeGeneration.
265 * @see org.eclipse.jdt.ui.CodeGeneration#getTypeComment(ICompilationUnit, String, String[], String)
267 public static String getTypeComment(ICompilationUnit cu, String typeQualifiedName, String[] typeParameterNames, String lineDelim) throws CoreException {
268 Template template= getCodeTemplate(CodeTemplateContextType.TYPECOMMENT_ID, cu.getJavaProject());
269 if (template == null) {
272 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
273 context.setCompilationUnitVariables(cu);
274 context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, Signature.getQualifier(typeQualifiedName));
275 context.setVariable(CodeTemplateContextType.TYPENAME, Signature.getSimpleName(typeQualifiedName));
277 TemplateBuffer buffer;
279 buffer= context.evaluate(template);
280 } catch (BadLocationException e) {
281 throw new CoreException(Status.CANCEL_STATUS);
282 } catch (TemplateException e) {
283 throw new CoreException(Status.CANCEL_STATUS);
285 String str= buffer.getString();
286 if (Strings.containsOnlyWhitespaces(str)) {
290 TemplateVariable position= findVariable(buffer, CodeTemplateContextType.TAGS); // look if Javadoc tags have to be added
291 if (position == null) {
295 IDocument document= new Document(str);
296 int[] tagOffsets= position.getOffsets();
297 for (int i= tagOffsets.length - 1; i >= 0; i--) { // from last to first
299 insertTag(document, tagOffsets[i], position.getLength(), EMPTY, EMPTY, null, typeParameterNames, false, lineDelim);
300 } catch (BadLocationException e) {
301 throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
304 return document.get();
308 * Returns the parameters type names used in see tags. Currently, these are always fully qualified.
310 public static String[] getParameterTypeNamesForSeeTag(IMethodBinding binding) {
311 ITypeBinding[] typeBindings= binding.getParameterTypes();
312 String[] result= new String[typeBindings.length];
313 for (int i= 0; i < result.length; i++) {
314 ITypeBinding curr= typeBindings[i];
315 if (curr.isTypeVariable()) {
316 curr= curr.getErasure(); // in Javadoc only use type variable erasure
318 curr= curr.getTypeDeclaration(); // no parameterized types
319 result[i]= curr.getQualifiedName();
325 * Returns the parameters type names used in see tags. Currently, these are always fully qualified.
327 private static String[] getParameterTypeNamesForSeeTag(IMethod overridden) {
329 ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
330 parser.setProject(overridden.getJavaProject());
331 IBinding[] bindings= parser.createBindings(new IJavaElement[] { overridden }, null);
332 if (bindings.length == 1 && bindings[0] instanceof IMethodBinding) {
333 return getParameterTypeNamesForSeeTag((IMethodBinding)bindings[0]);
335 } catch (IllegalStateException e) {
336 // method does not exist
338 // fall back code. Not good for generic methods!
339 String[] paramTypes= overridden.getParameterTypes();
340 String[] paramTypeNames= new String[paramTypes.length];
341 for (int i= 0; i < paramTypes.length; i++) {
342 paramTypeNames[i]= Signature.toString(Signature.getTypeErasure(paramTypes[i]));
344 return paramTypeNames;
347 private static String getSeeTag(String declaringClassQualifiedName, String methodName, String[] parameterTypesQualifiedNames) {
348 StringBuffer buf= new StringBuffer();
349 buf.append("@see "); //$NON-NLS-1$
350 buf.append(declaringClassQualifiedName);
352 buf.append(methodName);
354 for (int i= 0; i < parameterTypesQualifiedNames.length; i++) {
356 buf.append(", "); //$NON-NLS-1$
358 buf.append(parameterTypesQualifiedNames[i]);
361 return buf.toString();
364 public static String[] getTypeParameterNames(ITypeParameter[] typeParameters) {
365 String[] typeParametersNames= new String[typeParameters.length];
366 for (int i= 0; i < typeParameters.length; i++) {
367 typeParametersNames[i]= typeParameters[i].getElementName();
369 return typeParametersNames;
373 * Don't use this method directly, use CodeGeneration.
375 * @param templateID the template id of the type body to get. Valid id's are
376 * {@link CodeTemplateContextType#CLASSBODY_ID},
377 * {@link CodeTemplateContextType#INTERFACEBODY_ID},
378 * {@link CodeTemplateContextType#ENUMBODY_ID},
379 * {@link CodeTemplateContextType#ANNOTATIONBODY_ID},
380 * @param cu the compilation unit to which the template is added
381 * @param typeName the type name
382 * @param lineDelim the line delimiter to use
383 * @return return the type body template or <code>null</code>
384 * @throws CoreException thrown if the template could not be evaluated
385 * @see org.eclipse.jdt.ui.CodeGeneration#getTypeBody(String, ICompilationUnit, String, String)
387 public static String getTypeBody(String templateID, ICompilationUnit cu, String typeName, String lineDelim) throws CoreException {
388 if (!VALID_TYPE_BODY_TEMPLATES.contains(templateID)) {
389 throw new IllegalArgumentException("Invalid code template ID: " + templateID); //$NON-NLS-1$
392 Template template= getCodeTemplate(templateID, cu.getJavaProject());
393 if (template == null) {
396 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
397 context.setCompilationUnitVariables(cu);
398 context.setVariable(CodeTemplateContextType.TYPENAME, typeName);
400 return evaluateTemplate(context, template);
404 * Don't use this method directly, use CodeGeneration.
405 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, String, String[], String[], String, String[], IMethod, String)
407 public static String getMethodComment(ICompilationUnit cu, String typeName, String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig, String[] typeParameterNames,
408 IMethod target, boolean delegate, String lineDelimiter) throws CoreException {
409 String templateName= CodeTemplateContextType.METHODCOMMENT_ID;
410 if (retTypeSig == null) {
411 templateName= CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
412 } else if (target != null) {
414 templateName= CodeTemplateContextType.DELEGATECOMMENT_ID;
416 templateName= CodeTemplateContextType.OVERRIDECOMMENT_ID;
418 Template template= getCodeTemplate(templateName, cu.getJavaProject());
419 if (template == null) {
422 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
423 context.setCompilationUnitVariables(cu);
424 context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
425 context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
427 if (retTypeSig != null) {
428 context.setVariable(CodeTemplateContextType.RETURN_TYPE, Signature.toString(retTypeSig));
430 if (target != null) {
431 String targetTypeName= target.getDeclaringType().getFullyQualifiedName('.');
432 String[] targetParamTypeNames= getParameterTypeNamesForSeeTag(target);
434 context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
436 context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
438 TemplateBuffer buffer;
440 buffer= context.evaluate(template);
441 } catch (BadLocationException e) {
442 throw new CoreException(Status.CANCEL_STATUS);
443 } catch (TemplateException e) {
444 throw new CoreException(Status.CANCEL_STATUS);
446 if (buffer == null) {
450 String str= buffer.getString();
451 if (Strings.containsOnlyWhitespaces(str)) {
454 TemplateVariable position= findVariable(buffer, CodeTemplateContextType.TAGS); // look if Javadoc tags have to be added
455 if (position == null) {
459 IDocument document= new Document(str);
460 String[] exceptionNames= new String[excTypeSig.length];
461 for (int i= 0; i < excTypeSig.length; i++) {
462 exceptionNames[i]= Signature.toString(excTypeSig[i]);
464 String returnType= retTypeSig != null ? Signature.toString(retTypeSig) : null;
465 int[] tagOffsets= position.getOffsets();
466 for (int i= tagOffsets.length - 1; i >= 0; i--) { // from last to first
468 insertTag(document, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParameterNames, false, lineDelimiter);
469 } catch (BadLocationException e) {
470 throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
473 return document.get();
476 // remove lines for empty variables
477 private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException {
478 IDocument doc= new Document(buffer.getString());
479 int nLines= doc.getNumberOfLines();
480 MultiTextEdit edit= new MultiTextEdit();
481 HashSet<Integer> removedLines= new HashSet<Integer>();
482 for (int i= 0; i < variables.length; i++) {
483 TemplateVariable position= findVariable(buffer, variables[i]); // look if Javadoc tags have to be added
484 if (position == null || position.getLength() > 0) {
487 int[] offsets= position.getOffsets();
488 for (int k= 0; k < offsets.length; k++) {
489 int line= doc.getLineOfOffset(offsets[k]);
490 IRegion lineInfo= doc.getLineInformation(line);
491 int offset= lineInfo.getOffset();
492 String str= doc.get(offset, lineInfo.getLength());
493 if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) {
494 int nextStart= doc.getLineOffset(line + 1);
495 edit.addChild(new DeleteEdit(offset, nextStart - offset));
504 * Don't use this method directly, use CodeGeneration.
506 public static String getFieldComment(ICompilationUnit cu, String typeName, String fieldName, String lineDelimiter) throws CoreException {
507 Template template= getCodeTemplate(CodeTemplateContextType.FIELDCOMMENT_ID, cu.getJavaProject());
508 if (template == null) {
511 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
512 context.setCompilationUnitVariables(cu);
513 context.setVariable(CodeTemplateContextType.FIELD_TYPE, typeName);
514 context.setVariable(CodeTemplateContextType.FIELD, fieldName);
516 return evaluateTemplate(context, template);
521 * Don't use this method directly, use CodeGeneration.
522 * @see org.eclipse.jdt.ui.CodeGeneration#getSetterComment(ICompilationUnit, String, String, String, String, String, String, String)
524 public static String getSetterComment(ICompilationUnit cu, String typeName, String methodName, String fieldName, String fieldType, String paramName, String bareFieldName, String lineDelimiter)
525 throws CoreException {
526 String templateName= CodeTemplateContextType.SETTERCOMMENT_ID;
527 Template template= getCodeTemplate(templateName, cu.getJavaProject());
528 if (template == null) {
532 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
533 context.setCompilationUnitVariables(cu);
534 context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
535 context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
536 context.setVariable(CodeTemplateContextType.FIELD, fieldName);
537 context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldType);
538 context.setVariable(CodeTemplateContextType.BARE_FIELD_NAME, bareFieldName);
539 context.setVariable(CodeTemplateContextType.PARAM, paramName);
541 return evaluateTemplate(context, template);
545 * Don't use this method directly, use CodeGeneration.
546 * @see org.eclipse.jdt.ui.CodeGeneration#getGetterComment(ICompilationUnit, String, String, String, String, String, String)
548 public static String getGetterComment(ICompilationUnit cu, String typeName, String methodName, String fieldName, String fieldType, String bareFieldName, String lineDelimiter) throws CoreException {
549 String templateName= CodeTemplateContextType.GETTERCOMMENT_ID;
550 Template template= getCodeTemplate(templateName, cu.getJavaProject());
551 if (template == null) {
554 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
555 context.setCompilationUnitVariables(cu);
556 context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
557 context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
558 context.setVariable(CodeTemplateContextType.FIELD, fieldName);
559 context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldType);
560 context.setVariable(CodeTemplateContextType.BARE_FIELD_NAME, bareFieldName);
562 return evaluateTemplate(context, template);
565 private static String evaluateTemplate(CodeTemplateContext context, Template template) throws CoreException {
566 TemplateBuffer buffer;
568 buffer= context.evaluate(template);
569 } catch (BadLocationException e) {
570 throw new CoreException(Status.CANCEL_STATUS);
571 } catch (TemplateException e) {
572 throw new CoreException(Status.CANCEL_STATUS);
576 String str= buffer.getString();
577 if (Strings.containsOnlyWhitespaces(str)) {
583 private static String evaluateTemplate(CodeTemplateContext context, Template template, String[] fullLineVariables) throws CoreException {
584 TemplateBuffer buffer;
586 buffer= context.evaluate(template);
589 String str= fixEmptyVariables(buffer, fullLineVariables);
590 if (Strings.containsOnlyWhitespaces(str)) {
594 } catch (BadLocationException e) {
595 throw new CoreException(Status.CANCEL_STATUS);
596 } catch (TemplateException e) {
597 throw new CoreException(Status.CANCEL_STATUS);
603 * Don't use this method directly, use CodeGeneration.
604 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, boolean, String, String[], String)
606 public static String getMethodComment(ICompilationUnit cu, String typeName, MethodDeclaration decl, boolean isDeprecated, String targetName, String targetMethodDeclaringTypeName,
607 String[] targetMethodParameterTypeNames, boolean delegate, String lineDelimiter) throws CoreException {
608 boolean needsTarget= targetMethodDeclaringTypeName != null && targetMethodParameterTypeNames != null;
609 String templateName= CodeTemplateContextType.METHODCOMMENT_ID;
610 if (decl.isConstructor()) {
611 templateName= CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
612 } else if (needsTarget) {
614 templateName= CodeTemplateContextType.DELEGATECOMMENT_ID;
616 templateName= CodeTemplateContextType.OVERRIDECOMMENT_ID;
618 Template template= getCodeTemplate(templateName, cu.getJavaProject());
619 if (template == null) {
622 CodeTemplateContext context= new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
623 context.setCompilationUnitVariables(cu);
624 context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
625 context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, decl.getName().getIdentifier());
626 if (!decl.isConstructor()) {
627 context.setVariable(CodeTemplateContextType.RETURN_TYPE, ASTNodes.asString(getReturnType(decl)));
631 context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
633 context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
636 TemplateBuffer buffer;
638 buffer= context.evaluate(template);
639 } catch (BadLocationException e) {
640 throw new CoreException(Status.CANCEL_STATUS);
641 } catch (TemplateException e) {
642 throw new CoreException(Status.CANCEL_STATUS);
646 String str= buffer.getString();
647 if (Strings.containsOnlyWhitespaces(str)) {
650 TemplateVariable position= findVariable(buffer, CodeTemplateContextType.TAGS); // look if Javadoc tags have to be added
651 if (position == null) {
655 IDocument textBuffer= new Document(str);
656 List<TypeParameter> typeParams= shouldGenerateMethodTypeParameterTags(cu.getJavaProject()) ? decl.typeParameters() : Collections.emptyList();
657 String[] typeParamNames= new String[typeParams.size()];
658 for (int i= 0; i < typeParamNames.length; i++) {
659 TypeParameter elem= typeParams.get(i);
660 typeParamNames[i]= elem.getName().getIdentifier();
662 List<SingleVariableDeclaration> params= decl.parameters();
663 String[] paramNames= new String[params.size()];
664 for (int i= 0; i < paramNames.length; i++) {
665 SingleVariableDeclaration elem= params.get(i);
666 paramNames[i]= elem.getName().getIdentifier();
668 List<Name> exceptions= decl.thrownExceptions();
669 String[] exceptionNames= new String[exceptions.size()];
670 for (int i= 0; i < exceptionNames.length; i++) {
671 exceptionNames[i]= ASTNodes.getSimpleNameIdentifier(exceptions.get(i));
674 String returnType= null;
675 if (!decl.isConstructor()) {
676 returnType= ASTNodes.asString(getReturnType(decl));
678 int[] tagOffsets= position.getOffsets();
679 for (int i= tagOffsets.length - 1; i >= 0; i--) { // from last to first
681 insertTag(textBuffer, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParamNames, isDeprecated, lineDelimiter);
682 } catch (BadLocationException e) {
683 throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
686 return textBuffer.get();
689 public static boolean shouldGenerateMethodTypeParameterTags(IJavaProject project) {
690 return JavaCore.ENABLED.equals(project.getOption(JavaCore.COMPILER_PB_MISSING_JAVADOC_TAGS_METHOD_TYPE_PARAMETERS, true));
694 * @param decl the method declaration
695 * @return the return type
696 * @deprecated Deprecated to avoid deprecated warnings
698 private static ASTNode getReturnType(MethodDeclaration decl) {
699 // used from API, can't eliminate
700 return decl.getAST().apiLevel() == AST.JLS2 ? decl.getReturnType() : decl.getReturnType2();
704 private static TemplateVariable findVariable(TemplateBuffer buffer, String variable) {
705 TemplateVariable[] positions= buffer.getVariables();
706 for (int i= 0; i < positions.length; i++) {
707 TemplateVariable curr= positions[i];
708 if (variable.equals(curr.getType())) {
715 private static void insertTag(IDocument textBuffer, int offset, int length, String[] paramNames, String[] exceptionNames, String returnType, String[] typeParameterNames, boolean isDeprecated,
716 String lineDelimiter) throws BadLocationException {
717 IRegion region= textBuffer.getLineInformationOfOffset(offset);
718 if (region == null) {
721 String lineStart= textBuffer.get(region.getOffset(), offset - region.getOffset());
723 StringBuffer buf= new StringBuffer();
724 for (int i= 0; i < typeParameterNames.length; i++) {
725 if (buf.length() > 0) {
726 buf.append(lineDelimiter).append(lineStart);
728 buf.append("@param <").append(typeParameterNames[i]).append('>'); //$NON-NLS-1$
730 for (int i= 0; i < paramNames.length; i++) {
731 if (buf.length() > 0) {
732 buf.append(lineDelimiter).append(lineStart);
734 buf.append("@param ").append(paramNames[i]); //$NON-NLS-1$
736 if (returnType != null && !returnType.equals("void")) { //$NON-NLS-1$
737 if (buf.length() > 0) {
738 buf.append(lineDelimiter).append(lineStart);
740 buf.append("@return"); //$NON-NLS-1$
742 if (exceptionNames != null) {
743 for (int i= 0; i < exceptionNames.length; i++) {
744 if (buf.length() > 0) {
745 buf.append(lineDelimiter).append(lineStart);
747 buf.append("@throws ").append(exceptionNames[i]); //$NON-NLS-1$
751 if (buf.length() > 0) {
752 buf.append(lineDelimiter).append(lineStart);
754 buf.append("@deprecated"); //$NON-NLS-1$
756 if (buf.length() == 0 && isAllCommentWhitespace(lineStart)) {
757 int prevLine= textBuffer.getLineOfOffset(offset) - 1;
759 IRegion prevRegion= textBuffer.getLineInformation(prevLine);
760 int prevLineEnd= prevRegion.getOffset() + prevRegion.getLength();
762 textBuffer.replace(prevLineEnd, offset + length - prevLineEnd, ""); //$NON-NLS-1$
766 textBuffer.replace(offset, length, buf.toString());
769 private static boolean isAllCommentWhitespace(String lineStart) {
770 for (int i= 0; i < lineStart.length(); i++) {
771 char ch= lineStart.charAt(i);
772 if (!Character.isWhitespace(ch) && ch != '*') {
780 * Returns the line delimiter which is used in the specified project.
782 * @param project the java project, or <code>null</code>
783 * @return the used line delimiter
785 public static String getLineDelimiterUsed(IJavaProject project) {
786 return getProjectLineDelimiter(project);
789 private static String getProjectLineDelimiter(IJavaProject javaProject) {
790 IProject project= null;
791 if (javaProject != null)
792 project= javaProject.getProject();
794 String lineDelimiter= getLineDelimiterPreference(project);
795 if (lineDelimiter != null)
796 return lineDelimiter;
798 return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
801 public static String getLineDelimiterPreference(IProject project) {
802 IScopeContext[] scopeContext;
803 if (project != null) {
804 // project preference
805 scopeContext= new IScopeContext[] { new ProjectScope(project) };
806 String lineDelimiter= Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext);
807 if (lineDelimiter != null)
808 return lineDelimiter;
810 // workspace preference
811 scopeContext= new IScopeContext[] { InstanceScope.INSTANCE };
812 String platformDefault= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
813 return Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, platformDefault, scopeContext);
817 * Examines a string and returns the first line delimiter found.
819 * @param elem the element
820 * @return the line delimiter used for the element
822 public static String getLineDelimiterUsed(IJavaElement elem) {
823 IOpenable openable= elem.getOpenable();
824 if (openable instanceof ITypeRoot) {
826 return openable.findRecommendedLineSeparator();
827 } catch (JavaModelException exception) {
828 // Use project setting
831 IJavaProject project= elem.getJavaProject();
832 return getProjectLineDelimiter(project.exists() ? project : null);
836 * Evaluates the indentation used by a Java element. (in tabulators)
838 * @param elem the element to get the indent of
839 * @return return the indent unit
840 * @throws JavaModelException thrown if the element could not be accessed
842 public static int getIndentUsed(IJavaElement elem) throws JavaModelException {
843 IOpenable openable= elem.getOpenable();
844 if (openable instanceof ITypeRoot) {
845 IBuffer buf= openable.getBuffer();
847 int offset= ((ISourceReference)elem).getSourceRange().getOffset();
848 return getIndentUsed(buf, offset, elem.getJavaProject());
854 public static int getIndentUsed(IBuffer buffer, int offset, IJavaProject project) {
856 // find beginning of line
857 while (i > 0 && !IndentManipulation.isLineDelimiterChar(buffer.getChar(i - 1))) {
860 return Strings.computeIndentUnits(buffer.getText(i, offset - i), project);
866 * Returns the element after the give element.
868 * @param member a Java element
869 * @return the next sibling of the given element or <code>null</code>
870 * @throws JavaModelException thrown if the element could not be accessed
872 public static IJavaElement findNextSibling(IJavaElement member) throws JavaModelException {
873 IJavaElement parent= member.getParent();
874 if (parent instanceof IParent) {
875 IJavaElement[] elements= ((IParent)parent).getChildren();
876 for (int i= elements.length - 2; i >= 0; i--) {
877 if (member.equals(elements[i])) {
878 return elements[i + 1];
885 public static String getTodoTaskTag(IJavaProject project) {
886 String markers= null;
887 if (project == null) {
888 markers= JavaCore.getOption(JavaCore.COMPILER_TASK_TAGS);
890 markers= project.getOption(JavaCore.COMPILER_TASK_TAGS, true);
893 if (markers != null && markers.length() > 0) {
894 int idx= markers.indexOf(',');
898 return markers.substring(0, idx);
904 private static String removeTypeArguments(String baseName) {
905 int idx= baseName.indexOf('<');
907 return baseName.substring(0, idx);
913 // --------------------------- name suggestions --------------------------
915 public static String[] getVariableNameSuggestions(int variableKind, IJavaProject project, ITypeBinding expectedType, Expression assignedExpression, Collection<String> excluded) {
916 LinkedHashSet<String> res= new LinkedHashSet<String>(); // avoid duplicates but keep order
918 if (assignedExpression != null) {
919 String nameFromExpression= getBaseNameFromExpression(project, assignedExpression, variableKind);
920 if (nameFromExpression != null) {
921 add(getVariableNameSuggestions(variableKind, project, nameFromExpression, 0, excluded, false), res); // pass 0 as dimension, base name already contains plural.
924 String nameFromParent= getBaseNameFromLocationInParent(assignedExpression);
925 if (nameFromParent != null) {
926 add(getVariableNameSuggestions(variableKind, project, nameFromParent, 0, excluded, false), res); // pass 0 as dimension, base name already contains plural.
929 if (expectedType != null) {
930 expectedType= Bindings.normalizeTypeBinding(expectedType);
931 if (expectedType != null) {
933 if (expectedType.isArray()) {
934 dim= expectedType.getDimensions();
935 expectedType= expectedType.getElementType();
937 if (expectedType.isParameterizedType()) {
938 expectedType= expectedType.getTypeDeclaration();
940 String typeName= expectedType.getName();
941 if (typeName.length() > 0) {
942 add(getVariableNameSuggestions(variableKind, project, typeName, dim, excluded, false), res);
947 return getDefaultVariableNameSuggestions(variableKind, excluded);
949 return res.toArray(new String[res.size()]);
952 public static String[] getVariableNameSuggestions(int variableKind, IJavaProject project, Type expectedType, Expression assignedExpression, Collection<String> excluded) {
953 LinkedHashSet<String> res= new LinkedHashSet<String>(); // avoid duplicates but keep order
955 if (assignedExpression != null) {
956 String nameFromExpression= getBaseNameFromExpression(project, assignedExpression, variableKind);
957 if (nameFromExpression != null) {
958 add(getVariableNameSuggestions(variableKind, project, nameFromExpression, 0, excluded, false), res); // pass 0 as dimension, base name already contains plural.
961 String nameFromParent= getBaseNameFromLocationInParent(assignedExpression);
962 if (nameFromParent != null) {
963 add(getVariableNameSuggestions(variableKind, project, nameFromParent, 0, excluded, false), res); // pass 0 as dimension, base name already contains plural.
966 if (expectedType != null) {
967 String[] names= getVariableNameSuggestions(variableKind, project, expectedType, excluded, false);
968 for (int i= 0; i < names.length; i++) {
973 return getDefaultVariableNameSuggestions(variableKind, excluded);
975 return res.toArray(new String[res.size()]);
978 private static String[] getVariableNameSuggestions(int variableKind, IJavaProject project, Type expectedType, Collection<String> excluded, boolean evaluateDefault) {
980 if (expectedType.isArrayType()) {
981 ArrayType arrayType= (ArrayType)expectedType;
982 dim= arrayType.getDimensions();
983 expectedType= arrayType.getElementType();
985 if (expectedType.isParameterizedType()) {
986 expectedType= ((ParameterizedType)expectedType).getType();
988 String typeName= ASTNodes.getTypeName(expectedType);
990 if (typeName.length() > 0) {
991 return getVariableNameSuggestions(variableKind, project, typeName, dim, excluded, evaluateDefault);
996 private static String[] getDefaultVariableNameSuggestions(int variableKind, Collection<String> excluded) {
997 String prop= variableKind == NamingConventions.VK_STATIC_FINAL_FIELD ? "X" : "x"; //$NON-NLS-1$//$NON-NLS-2$
1000 while (excluded.contains(name)) {
1003 return new String[] { name };
1007 * Returns variable name suggestions for the given base name. This is a layer over the JDT.Core
1008 * NamingConventions API to fix its shortcomings. JDT UI code should only use this API.
1010 * @param variableKind specifies what type the variable is: {@link NamingConventions#VK_LOCAL},
1011 * {@link NamingConventions#VK_PARAMETER}, {@link NamingConventions#VK_STATIC_FIELD},
1012 * {@link NamingConventions#VK_INSTANCE_FIELD}, or
1013 * {@link NamingConventions#VK_STATIC_FINAL_FIELD}.
1014 * @param project the current project
1015 * @param baseName the base name to make a suggestion on. The base name is expected to be a name
1016 * without any pre- or suffixes in singular form. Type name are accepted as well.
1017 * @param dimensions if greater than 0, the resulting name will be in plural form
1018 * @param excluded a collection containing all excluded names or <code>null</code> if no names
1020 * @param evaluateDefault if set, the result is guaranteed to contain at least one result. If
1021 * not, the result can be an empty array.
1023 * @return the name suggestions sorted by relevance (best proposal first). If
1024 * <code>evaluateDefault</code> is set to true, the returned array is never empty. If
1025 * <code>evaluateDefault</code> is set to false, an empty array is returned if there is
1026 * no good suggestion for the given base name.
1028 public static String[] getVariableNameSuggestions(int variableKind, IJavaProject project, String baseName, int dimensions, Collection<String> excluded, boolean evaluateDefault) {
1029 return NamingConventions.suggestVariableNames(variableKind, NamingConventions.BK_TYPE_NAME, removeTypeArguments(baseName), project, dimensions, getExcludedArray(excluded), evaluateDefault);
1032 private static String[] getExcludedArray(Collection<String> excluded) {
1033 if (excluded == null) {
1035 } else if (excluded instanceof ExcludedCollection) {
1036 return ((ExcludedCollection)excluded).getExcludedArray();
1038 return excluded.toArray(new String[excluded.size()]);
1042 private static final String[] KNOWN_METHOD_NAME_PREFIXES= { "get", "is", "to" }; //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-1$
1045 private static void add(String[] names, Set<String> result) {
1046 for (int i= 0; i < names.length; i++) {
1047 result.add(names[i]);
1051 private static String getBaseNameFromExpression(IJavaProject project, Expression assignedExpression, int variableKind) {
1053 if (assignedExpression instanceof CastExpression) {
1054 assignedExpression= ((CastExpression)assignedExpression).getExpression();
1056 if (assignedExpression instanceof Name) {
1057 Name simpleNode= (Name)assignedExpression;
1058 IBinding binding= simpleNode.resolveBinding();
1059 if (binding instanceof IVariableBinding)
1060 return getBaseName((IVariableBinding)binding, project);
1062 return ASTNodes.getSimpleNameIdentifier(simpleNode);
1063 } else if (assignedExpression instanceof MethodInvocation) {
1064 name= ((MethodInvocation)assignedExpression).getName().getIdentifier();
1065 } else if (assignedExpression instanceof SuperMethodInvocation) {
1066 name= ((SuperMethodInvocation)assignedExpression).getName().getIdentifier();
1067 } else if (assignedExpression instanceof FieldAccess) {
1068 return ((FieldAccess)assignedExpression).getName().getIdentifier();
1069 } else if (variableKind == NamingConventions.VK_STATIC_FINAL_FIELD && (assignedExpression instanceof StringLiteral || assignedExpression instanceof NumberLiteral)) {
1070 String string= assignedExpression instanceof StringLiteral ? ((StringLiteral)assignedExpression).getLiteralValue() : ((NumberLiteral)assignedExpression).getToken();
1071 StringBuffer res= new StringBuffer();
1072 boolean needsUnderscore= false;
1073 for (int i= 0; i < string.length(); i++) {
1074 char ch= string.charAt(i);
1075 if (Character.isJavaIdentifierPart(ch)) {
1076 if (res.length() == 0 && !Character.isJavaIdentifierStart(ch) || needsUnderscore) {
1080 needsUnderscore= false;
1082 needsUnderscore= res.length() > 0;
1085 if (res.length() > 0) {
1086 return res.toString();
1090 for (int i= 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) {
1091 String curr= KNOWN_METHOD_NAME_PREFIXES[i];
1092 if (name.startsWith(curr)) {
1093 if (name.equals(curr)) {
1094 return null; // don't suggest 'get' as variable name
1095 } else if (Character.isUpperCase(name.charAt(curr.length()))) {
1096 return name.substring(curr.length());
1104 private static String getBaseNameFromLocationInParent(Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) {
1105 if (binding == null)
1108 ITypeBinding[] parameterTypes= binding.getParameterTypes();
1109 if (parameterTypes.length != arguments.size()) // beware of guessed method bindings
1112 int index= arguments.indexOf(assignedExpression);
1116 ITypeBinding expressionBinding= assignedExpression.resolveTypeBinding();
1117 if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index]))
1121 IJavaElement javaElement= binding.getJavaElement();
1122 if (javaElement instanceof IMethod) {
1123 IMethod method= (IMethod)javaElement;
1124 if (method.getOpenable().getBuffer() != null) { // avoid dummy names and lookup from Javadoc
1125 String[] parameterNames= method.getParameterNames();
1126 if (index < parameterNames.length) {
1127 return NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject());
1131 } catch (JavaModelException e) {
1138 private static String getBaseNameFromLocationInParent(Expression assignedExpression) {
1139 StructuralPropertyDescriptor location= assignedExpression.getLocationInParent();
1140 if (location == MethodInvocation.ARGUMENTS_PROPERTY) {
1141 MethodInvocation parent= (MethodInvocation)assignedExpression.getParent();
1142 return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveMethodBinding());
1143 } else if (location == ClassInstanceCreation.ARGUMENTS_PROPERTY) {
1144 ClassInstanceCreation parent= (ClassInstanceCreation)assignedExpression.getParent();
1145 return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveConstructorBinding());
1146 } else if (location == SuperMethodInvocation.ARGUMENTS_PROPERTY) {
1147 SuperMethodInvocation parent= (SuperMethodInvocation)assignedExpression.getParent();
1148 return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveMethodBinding());
1149 } else if (location == ConstructorInvocation.ARGUMENTS_PROPERTY) {
1150 ConstructorInvocation parent= (ConstructorInvocation)assignedExpression.getParent();
1151 return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveConstructorBinding());
1152 } else if (location == SuperConstructorInvocation.ARGUMENTS_PROPERTY) {
1153 SuperConstructorInvocation parent= (SuperConstructorInvocation)assignedExpression.getParent();
1154 return getBaseNameFromLocationInParent(assignedExpression, parent.arguments(), parent.resolveConstructorBinding());
1159 public static String[] getArgumentNameSuggestions(IType type, String[] excluded) {
1160 return getVariableNameSuggestions(NamingConventions.VK_PARAMETER, type.getJavaProject(), type.getElementName(), 0, new ExcludedCollection(excluded), true);
1163 public static String[] getArgumentNameSuggestions(IJavaProject project, Type type, String[] excluded) {
1164 return getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, type, new ExcludedCollection(excluded), true);
1167 public static String[] getArgumentNameSuggestions(IJavaProject project, ITypeBinding binding, String[] excluded) {
1168 return getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, binding, null, new ExcludedCollection(excluded));
1171 public static String[] getArgumentNameSuggestions(IJavaProject project, String baseName, int dimensions, String[] excluded) {
1172 return getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, baseName, dimensions, new ExcludedCollection(excluded), true);
1175 public static String[] getFieldNameSuggestions(IType type, int fieldModifiers, String[] excluded) {
1176 return getFieldNameSuggestions(type.getJavaProject(), type.getElementName(), 0, fieldModifiers, excluded);
1179 public static String[] getFieldNameSuggestions(IJavaProject project, String baseName, int dimensions, int modifiers, String[] excluded) {
1180 if (Flags.isFinal(modifiers) && Flags.isStatic(modifiers)) {
1181 return getVariableNameSuggestions(NamingConventions.VK_STATIC_FINAL_FIELD, project, baseName, dimensions, new ExcludedCollection(excluded), true);
1182 } else if (Flags.isStatic(modifiers)) {
1183 return getVariableNameSuggestions(NamingConventions.VK_STATIC_FIELD, project, baseName, dimensions, new ExcludedCollection(excluded), true);
1185 return getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, baseName, dimensions, new ExcludedCollection(excluded), true);
1188 public static String[] getLocalNameSuggestions(IJavaProject project, String baseName, int dimensions, String[] excluded) {
1189 return getVariableNameSuggestions(NamingConventions.VK_LOCAL, project, baseName, dimensions, new ExcludedCollection(excluded), true);
1192 public static String suggestArgumentName(IJavaProject project, String baseName, String[] excluded) {
1193 return suggestVariableName(NamingConventions.VK_PARAMETER, project, baseName, 0, excluded);
1196 private static String suggestVariableName(int varKind, IJavaProject project, String baseName, int dimension, String[] excluded) {
1197 return getVariableNameSuggestions(varKind, project, baseName, dimension, new ExcludedCollection(excluded), true)[0];
1201 public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, String[] paramNames) {
1202 String[][] newNames= new String[paramNames.length][];
1203 ArrayList<String> takenNames= new ArrayList<String>();
1205 // Ensure that the code generation preferences are respected
1206 for (int i= 0; i < paramNames.length; i++) {
1207 String curr= paramNames[i];
1208 String baseName= NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, curr, project);
1210 String[] proposedNames= getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, curr, 0, takenNames, true);
1211 if (!curr.equals(baseName)) {
1212 // make the existing name to favorite
1213 LinkedHashSet<String> updatedNames= new LinkedHashSet<String>();
1214 updatedNames.add(curr);
1215 for (int k= 0; k < proposedNames.length; k++) {
1216 updatedNames.add(proposedNames[k]);
1218 proposedNames= updatedNames.toArray(new String[updatedNames.size()]);
1220 newNames[i]= proposedNames;
1221 takenNames.add(proposedNames[0]);
1226 public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, IMethodBinding binding) {
1227 int nParams= binding.getParameterTypes().length;
1230 IMethod method= (IMethod)binding.getMethodDeclaration().getJavaElement();
1231 if (method != null) {
1232 String[] parameterNames= method.getParameterNames();
1233 if (parameterNames.length == nParams) {
1234 return suggestArgumentNamesWithProposals(project, parameterNames);
1237 } catch (JavaModelException e) {
1241 String[][] names= new String[nParams][];
1242 for (int i= 0; i < names.length; i++) {
1243 names[i]= new String[] { "arg" + i }; //$NON-NLS-1$
1249 public static String[] suggestArgumentNames(IJavaProject project, IMethodBinding binding) {
1250 int nParams= binding.getParameterTypes().length;
1254 IMethod method= (IMethod)binding.getMethodDeclaration().getJavaElement();
1255 if (method != null) {
1256 String[] paramNames= method.getParameterNames();
1257 if (paramNames.length == nParams) {
1258 String[] namesArray= EMPTY;
1259 ArrayList<String> newNames= new ArrayList<String>(paramNames.length);
1260 // Ensure that the code generation preferences are respected
1261 for (int i= 0; i < paramNames.length; i++) {
1262 String curr= paramNames[i];
1263 String baseName= NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, curr, method.getJavaProject());
1264 if (!curr.equals(baseName)) {
1265 // make the existing name the favorite
1268 newNames.add(suggestArgumentName(project, curr, namesArray));
1270 namesArray= newNames.toArray(new String[newNames.size()]);
1275 } catch (JavaModelException e) {
1279 String[] names= new String[nParams];
1280 for (int i= 0; i < names.length; i++) {
1281 names[i]= "arg" + i; //$NON-NLS-1$
1286 public static String getBaseName(IField field) throws JavaModelException {
1287 return NamingConventions.getBaseName(getFieldKind(field.getFlags()), field.getElementName(), field.getJavaProject());
1290 public static String getBaseName(IVariableBinding binding, IJavaProject project) {
1291 return NamingConventions.getBaseName(getKind(binding), binding.getName(), project);
1295 * Returns the kind of the given binding.
1297 * @param binding variable binding
1298 * @return one of the <code>NamingConventions.VK_*</code> constants
1301 private static int getKind(IVariableBinding binding) {
1302 if (binding.isField())
1303 return getFieldKind(binding.getModifiers());
1305 if (binding.isParameter())
1306 return NamingConventions.VK_PARAMETER;
1308 return NamingConventions.VK_LOCAL;
1311 private static int getFieldKind(int modifiers) {
1312 if (!Modifier.isStatic(modifiers))
1313 return NamingConventions.VK_INSTANCE_FIELD;
1315 if (!Modifier.isFinal(modifiers))
1316 return NamingConventions.VK_STATIC_FIELD;
1318 return NamingConventions.VK_STATIC_FINAL_FIELD;
1321 private static class ExcludedCollection extends AbstractList<String> {
1322 private String[] fExcluded;
1324 public ExcludedCollection(String[] excluded) {
1325 fExcluded= excluded;
1328 public String[] getExcludedArray() {
1334 return fExcluded.length;
1338 public String get(int index) {
1339 return fExcluded[index];
1343 public int indexOf(Object o) {
1344 if (o instanceof String) {
1345 for (int i= 0; i < fExcluded.length; i++) {
1346 if (o.equals(fExcluded[i]))
1354 public boolean contains(Object o) {
1355 return indexOf(o) != -1;
1360 public static boolean hasFieldName(IJavaProject project, String name) {
1361 String prefixes= project.getOption(JavaCore.CODEASSIST_FIELD_PREFIXES, true);
1362 String suffixes= project.getOption(JavaCore.CODEASSIST_FIELD_SUFFIXES, true);
1363 String staticPrefixes= project.getOption(JavaCore.CODEASSIST_STATIC_FIELD_PREFIXES, true);
1364 String staticSuffixes= project.getOption(JavaCore.CODEASSIST_STATIC_FIELD_SUFFIXES, true);
1367 return hasPrefixOrSuffix(prefixes, suffixes, name)
1368 || hasPrefixOrSuffix(staticPrefixes, staticSuffixes, name);
1371 public static boolean hasParameterName(IJavaProject project, String name) {
1372 String prefixes= project.getOption(JavaCore.CODEASSIST_ARGUMENT_PREFIXES, true);
1373 String suffixes= project.getOption(JavaCore.CODEASSIST_ARGUMENT_SUFFIXES, true);
1374 return hasPrefixOrSuffix(prefixes, suffixes, name);
1377 public static boolean hasLocalVariableName(IJavaProject project, String name) {
1378 String prefixes= project.getOption(JavaCore.CODEASSIST_LOCAL_PREFIXES, true);
1379 String suffixes= project.getOption(JavaCore.CODEASSIST_LOCAL_SUFFIXES, true);
1380 return hasPrefixOrSuffix(prefixes, suffixes, name);
1383 public static boolean hasConstantName(IJavaProject project, String name) {
1384 if (Character.isUpperCase(name.charAt(0)))
1386 String prefixes= project.getOption(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_PREFIXES, true);
1387 String suffixes= project.getOption(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_SUFFIXES, true);
1388 return hasPrefixOrSuffix(prefixes, suffixes, name);
1392 private static boolean hasPrefixOrSuffix(String prefixes, String suffixes, String name) {
1393 final String listSeparartor= ","; //$NON-NLS-1$
1395 StringTokenizer tok= new StringTokenizer(prefixes, listSeparartor);
1396 while (tok.hasMoreTokens()) {
1397 String curr= tok.nextToken();
1398 if (name.startsWith(curr)) {
1403 tok= new StringTokenizer(suffixes, listSeparartor);
1404 while (tok.hasMoreTokens()) {
1405 String curr= tok.nextToken();
1406 if (name.endsWith(curr)) {
1413 // -------------------- preference access -----------------------
1415 public static boolean useThisForFieldAccess(IJavaProject project) {
1416 return Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_KEYWORD_THIS, project)).booleanValue();
1419 public static boolean useIsForBooleanGetters(IJavaProject project) {
1420 return Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_IS_FOR_GETTERS, project)).booleanValue();
1423 public static String getExceptionVariableName(IJavaProject project) {
1424 return PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_EXCEPTION_VAR_NAME, project);
1427 public static boolean doAddComments(IJavaProject project) {
1428 return Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_ADD_COMMENTS, project)).booleanValue();
1432 * Only to be used by tests
1434 * @param templateId the template id
1435 * @param pattern the new pattern
1436 * @param project not used
1438 public static void setCodeTemplate(String templateId, String pattern, IJavaProject project) {
1439 TemplateStore codeTemplateStore= JavaPlugin.getDefault().getCodeTemplateStore();
1440 TemplatePersistenceData data= codeTemplateStore.getTemplateData(templateId);
1441 Template orig= data.getTemplate();
1442 Template copy= new Template(orig.getName(), orig.getDescription(), orig.getContextTypeId(), pattern, true);
1443 data.setTemplate(copy);
1446 public static Template getCodeTemplate(String id, IJavaProject project) {
1447 if (project == null)
1448 return JavaPlugin.getDefault().getCodeTemplateStore().findTemplateById(id);
1449 ProjectTemplateStore projectStore= new ProjectTemplateStore(project.getProject());
1451 projectStore.load();
1452 } catch (IOException e) {
1455 return projectStore.findTemplateById(id);
1459 public static ImportRewrite createImportRewrite(ICompilationUnit cu, boolean restoreExistingImports) throws JavaModelException {
1460 return CodeStyleConfiguration.createImportRewrite(cu, restoreExistingImports);
1464 * Returns a {@link ImportRewrite} using {@link ImportRewrite#create(CompilationUnit, boolean)} and
1465 * configures the rewriter with the settings as specified in the JDT UI preferences.
1467 * This method sets {@link ImportRewrite#setUseContextToFilterImplicitImports(boolean)} to <code>true</code>
1468 * iff the given AST has been resolved with bindings. Clients should always supply a context
1469 * when they call one of the <code>addImport(...)</code> methods.
1472 * @param astRoot the AST root to create the rewriter on
1473 * @param restoreExistingImports specifies if the existing imports should be kept or removed.
1474 * @return the new rewriter configured with the settings as specified in the JDT UI preferences.
1476 * @see ImportRewrite#create(CompilationUnit, boolean)
1478 public static ImportRewrite createImportRewrite(CompilationUnit astRoot, boolean restoreExistingImports) {
1479 ImportRewrite rewrite= CodeStyleConfiguration.createImportRewrite(astRoot, restoreExistingImports);
1480 if (astRoot.getAST().hasResolvedBindings()) {
1481 rewrite.setUseContextToFilterImplicitImports(true);