2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.codemanipulation;
7 import net.sourceforge.phpdt.core.ICodeFormatter;
8 import net.sourceforge.phpdt.core.ToolFactory;
10 import org.eclipse.jface.text.BadLocationException;
11 import org.eclipse.jface.text.IDocument;
13 public class StubUtility {
16 // public static class GenStubSettings extends CodeGenerationSettings {
18 // public boolean callSuper;
19 // public boolean methodOverwrites;
20 // public boolean noBody;
22 // public GenStubSettings(CodeGenerationSettings settings) {
23 // settings.setSettings(this);
26 // public GenStubSettings() {
33 // * Generates a stub. Given a template method, a stub with the same signature
34 // * will be constructed so it can be added to a type.
35 // * @param destTypeName The name of the type to which the method will be added to (Used for the constructor)
36 // * @param method A method template (method belongs to different type than the parent)
37 // * @param options Options as defined above (<code>GenStubSettings</code>)
38 // * @param imports Imports required by the stub are added to the imports structure. If imports structure is <code>null</code>
39 // * all type names are qualified.
40 // * @throws JavaModelException
42 // public static String genStub(String destTypeName, IMethod method, GenStubSettings settings, IImportsStructure imports) throws JavaModelException {
43 // IType declaringtype= method.getDeclaringType();
44 // StringBuffer buf= new StringBuffer();
45 // String methodName= method.getElementName();
46 // String[] paramTypes= method.getParameterTypes();
47 // String[] paramNames= method.getParameterNames();
48 // String[] excTypes= method.getExceptionTypes();
49 // String retTypeSig= method.getReturnType();
50 // int flags= method.getFlags();
51 // boolean isConstructor= method.isConstructor();
53 // int lastParam= paramTypes.length -1;
56 // if (settings.createComments) {
57 // if (isConstructor) {
58 // String desc= "Constructor for " + destTypeName; //$NON-NLS-1$
59 // genJavaDocStub(desc, paramNames, Signature.SIG_VOID, excTypes, buf);
62 // if (settings.methodOverwrites) {
63 // boolean isDeprecated= Flags.isDeprecated(flags);
64 // genJavaDocSeeTag(declaringtype, methodName, paramTypes, settings.createNonJavadocComments, isDeprecated, buf);
66 // // generate a default php doc comment
67 // String desc= "Method " + methodName; //$NON-NLS-1$
68 // genJavaDocStub(desc, paramNames, retTypeSig, excTypes, buf);
74 // if (Flags.isPublic(flags) || isConstructor || (declaringtype.isInterface() && !settings.noBody)) {
75 // buf.append("public "); //$NON-NLS-1$
76 // } else if (Flags.isProtected(flags)) {
77 // buf.append("protected "); //$NON-NLS-1$
78 // } else if (Flags.isPrivate(flags)) {
79 // buf.append("private "); //$NON-NLS-1$
81 // if (Flags.isSynchronized(flags)) {
82 // buf.append("synchronized "); //$NON-NLS-1$
84 // if (Flags.isVolatile(flags)) {
85 // buf.append("volatile "); //$NON-NLS-1$
87 // if (Flags.isStrictfp(flags)) {
88 // buf.append("strictfp "); //$NON-NLS-1$
90 // if (Flags.isStatic(flags)) {
91 // buf.append("static "); //$NON-NLS-1$
94 // if (isConstructor) {
95 // buf.append(destTypeName);
98 // if (!isPrimitiveType(retTypeSig)) {
99 // retTypeFrm= resolveAndAdd(retTypeSig, declaringtype, imports);
101 // retTypeFrm= Signature.toString(retTypeSig);
103 // buf.append(retTypeFrm);
105 // buf.append(methodName);
108 // for (int i= 0; i <= lastParam; i++) {
109 // String paramTypeSig= paramTypes[i];
110 // String paramTypeFrm;
112 // if (!isPrimitiveType(paramTypeSig)) {
113 // paramTypeFrm= resolveAndAdd(paramTypeSig, declaringtype, imports);
115 // paramTypeFrm= Signature.toString(paramTypeSig);
117 // buf.append(paramTypeFrm);
119 // buf.append(paramNames[i]);
120 // if (i < lastParam) {
121 // buf.append(", "); //$NON-NLS-1$
126 // int lastExc= excTypes.length - 1;
127 // if (lastExc >= 0) {
128 // buf.append(" throws "); //$NON-NLS-1$
129 // for (int i= 0; i <= lastExc; i++) {
130 // String excTypeSig= excTypes[i];
131 // String excTypeFrm= resolveAndAdd(excTypeSig, declaringtype, imports);
132 // buf.append(excTypeFrm);
133 // if (i < lastExc) {
134 // buf.append(", "); //$NON-NLS-1$
138 // if (settings.noBody) {
139 // buf.append(";\n\n"); //$NON-NLS-1$
141 // buf.append(" {\n\t"); //$NON-NLS-1$
142 // if (!settings.callSuper) {
143 // if (retTypeSig != null && !retTypeSig.equals(Signature.SIG_VOID)) {
145 // if (!isPrimitiveType(retTypeSig) || Signature.getArrayCount(retTypeSig) > 0) {
146 // buf.append("return null;\n\t"); //$NON-NLS-1$
147 // } else if (retTypeSig.equals(Signature.SIG_BOOLEAN)) {
148 // buf.append("return false;\n\t"); //$NON-NLS-1$
150 // buf.append("return 0;\n\t"); //$NON-NLS-1$
155 // if (!isConstructor) {
156 // if (!Signature.SIG_VOID.equals(retTypeSig)) {
157 // buf.append("return "); //$NON-NLS-1$
159 // buf.append("super."); //$NON-NLS-1$
160 // buf.append(methodName);
162 // buf.append("super"); //$NON-NLS-1$
165 // for (int i= 0; i <= lastParam; i++) {
166 // buf.append(paramNames[i]);
167 // if (i < lastParam) {
168 // buf.append(", "); //$NON-NLS-1$
171 // buf.append(");\n\t"); //$NON-NLS-1$
173 // buf.append("}\n"); //$NON-NLS-1$
175 // return buf.toString();
178 // private static boolean isSet(int options, int flag) {
179 // return (options & flag) != 0;
182 // private static boolean isPrimitiveType(String typeName) {
183 // char first= Signature.getElementType(typeName).charAt(0);
184 // return (first != Signature.C_RESOLVED && first != Signature.C_UNRESOLVED);
187 // private static String resolveAndAdd(String refTypeSig, IType declaringType, IImportsStructure imports) throws JavaModelException {
188 // String resolvedTypeName= JavaModelUtil.getResolvedTypeName(refTypeSig, declaringType);
189 // if (resolvedTypeName != null) {
190 // StringBuffer buf= new StringBuffer();
191 // if (imports != null) {
192 // buf.append(imports.addImport(resolvedTypeName));
194 // buf.append(resolvedTypeName);
196 // int arrayCount= Signature.getArrayCount(refTypeSig);
197 // for (int i= 0; i < arrayCount; i++) {
198 // buf.append("[]"); //$NON-NLS-1$
200 // return buf.toString();
202 // return Signature.toString(refTypeSig);
206 // * Generates a default JavaDoc comment stub for a method.
208 // public static void genJavaDocStub(String descr, String[] paramNames, String retTypeSig, String[] excTypeSigs, StringBuffer buf) {
209 // buf.append("/**\n"); //$NON-NLS-1$
210 // buf.append(" * "); buf.append(descr); buf.append(".\n"); //$NON-NLS-2$ //$NON-NLS-1$
211 // for (int i= 0; i < paramNames.length; i++) {
212 // buf.append(" * @param "); buf.append(paramNames[i]); buf.append('\n'); //$NON-NLS-1$
214 // if (retTypeSig != null && !retTypeSig.equals(Signature.SIG_VOID)) {
215 // String simpleName= Signature.getSimpleName(Signature.toString(retTypeSig));
216 // buf.append(" * @return "); buf.append(simpleName); buf.append('\n'); //$NON-NLS-1$
218 // if (excTypeSigs != null) {
219 // for (int i= 0; i < excTypeSigs.length; i++) {
220 // String simpleName= Signature.getSimpleName(Signature.toString(excTypeSigs[i]));
221 // buf.append(" * @throws "); buf.append(simpleName); buf.append('\n'); //$NON-NLS-1$
224 // buf.append(" */"); //$NON-NLS-1$
228 // * Generates a '@see' tag to the defined method.
230 // public static void genJavaDocSeeTag(IType declaringType, String methodName, String[] paramTypes, boolean nonJavaDocComment, boolean isDeprecated, StringBuffer buf) throws JavaModelException {
231 // String[] fullParamNames= new String[paramTypes.length];
232 // for (int i= 0; i < paramTypes.length; i++) {
233 // fullParamNames[i]= JavaModelUtil.getResolvedTypeName(paramTypes[i], declaringType);
235 // String fullTypeName= JavaModelUtil.getFullyQualifiedName(declaringType);
237 // genJavaDocSeeTag(fullTypeName, methodName, fullParamNames, nonJavaDocComment, isDeprecated, buf);
241 // * Generates a '@see' tag to the defined method.
243 // public static void genJavaDocSeeTag(String fullyQualifiedTypeName, String methodName, String[] fullParamTypeNames, boolean nonJavaDocComment, boolean isDeprecated, StringBuffer buf) throws JavaModelException {
244 // // create a @see link
245 // buf.append("/*"); //$NON-NLS-1$
246 // if (!nonJavaDocComment) {
249 // buf.append(" (non-Javadoc)"); //$NON-NLS-1$
251 // buf.append("\n * @see "); //$NON-NLS-1$
252 // buf.append(fullyQualifiedTypeName);
254 // buf.append(methodName);
256 // for (int i= 0; i < fullParamTypeNames.length; i++) {
258 // buf.append(", "); //$NON-NLS-1$
260 // buf.append(fullParamTypeNames[i]);
262 // buf.append(")\n"); //$NON-NLS-1$
263 // if (isDeprecated) {
264 // buf.append(" * @deprecated\n"); //$NON-NLS-1$
266 // buf.append(" */"); //$NON-NLS-1$
272 // * Finds a method in a list of methods.
273 // * @return The found method or null, if nothing found
275 // private static IMethod findMethod(IMethod method, List allMethods) throws JavaModelException {
276 // String name= method.getElementName();
277 // String[] paramTypes= method.getParameterTypes();
278 // boolean isConstructor= method.isConstructor();
280 // for (int i= allMethods.size() - 1; i >= 0; i--) {
281 // IMethod curr= (IMethod) allMethods.get(i);
282 // if (JavaModelUtil.isSameMethodSignature(name, paramTypes, isConstructor, curr)) {
291 * Creates needed constructors for a type.
292 * @param type The type to create constructors for
293 * @param supertype The type's super type
294 * @param settings Options for comment generation
295 * @param imports Required imports are added to the import structure. Structure can be <code>null</code>, types are qualified then.
296 * @return Returns the generated stubs or <code>null</code> if the creation has been canceled
298 // public static String[] evalConstructors(IType type, IType supertype, CodeGenerationSettings settings, IImportsStructure imports) throws JavaModelException {
299 // IMethod[] superMethods= supertype.getMethods();
300 // String typeName= type.getElementName();
301 // IMethod[] methods= type.getMethods();
302 // GenStubSettings genStubSettings= new GenStubSettings(settings);
303 // genStubSettings.callSuper= true;
304 // ArrayList newMethods= new ArrayList(superMethods.length);
305 // for (int i= 0; i < superMethods.length; i++) {
306 // IMethod curr= superMethods[i];
307 // if (curr.isConstructor() && (JavaModelUtil.isVisible(curr, type.getPackageFragment()) || Flags.isProtected(curr.getFlags()))) {
308 // if (JavaModelUtil.findMethod(typeName, curr.getParameterTypes(), true, methods) == null) {
309 // String newStub= genStub(typeName, superMethods[i], genStubSettings, imports);
310 // newMethods.add(newStub);
314 // return (String[]) newMethods.toArray(new String[newMethods.size()]);
318 // * Searches for unimplemented methods of a type.
319 // * @param isSubType If set, the evaluation is for a subtype of the given type. If not set, the
320 // * evaluation is for the type itself.
321 // * @param settings Options for comment generation
322 // * @param selectionQuery If not null will select the methods to implement.
323 // * @param imports Required imports are added to the import structure. Structure can be <code>null</code>, types are qualified then.
324 // * @return Returns the generated stubs or <code>null</code> if the creation has been canceled
326 // public static String[] evalUnimplementedMethods(IType type, ITypeHierarchy hierarchy, boolean isSubType, CodeGenerationSettings settings,
327 // IOverrideMethodQuery selectionQuery, IImportsStructure imports) throws JavaModelException {
328 // List allMethods= new ArrayList();
329 // List toImplement= new ArrayList();
331 // IMethod[] typeMethods= type.getMethods();
332 // for (int i= 0; i < typeMethods.length; i++) {
333 // IMethod curr= typeMethods[i];
334 // if (!curr.isConstructor() && !Flags.isStatic(curr.getFlags()) && !Flags.isPrivate(curr.getFlags())) {
335 // allMethods.add(curr);
339 // IType[] superTypes= hierarchy.getAllSuperclasses(type);
340 // for (int i= 0; i < superTypes.length; i++) {
341 // IMethod[] methods= superTypes[i].getMethods();
342 // for (int k= 0; k < methods.length; k++) {
343 // IMethod curr= methods[k];
344 // if (!curr.isConstructor() && !Flags.isStatic(curr.getFlags()) && !Flags.isPrivate(curr.getFlags())) {
345 // if (findMethod(curr, allMethods) == null) {
346 // allMethods.add(curr);
352 // // do not call super
353 // for (int i= 0; i < allMethods.size(); i++) {
354 // IMethod curr= (IMethod) allMethods.get(i);
355 // if ((Flags.isAbstract(curr.getFlags()) || curr.getDeclaringType().isInterface()) && (isSubType || !type.equals(curr.getDeclaringType()))) {
356 // // implement all abstract methods
357 // toImplement.add(curr);
361 // IType[] superInterfaces= hierarchy.getAllSuperInterfaces(type);
362 // for (int i= 0; i < superInterfaces.length; i++) {
363 // IMethod[] methods= superInterfaces[i].getMethods();
364 // for (int k= 0; k < methods.length; k++) {
365 // IMethod curr= methods[k];
367 // // binary interfaces can contain static initializers (variable intializations)
369 // if (!Flags.isStatic(curr.getFlags())) {
370 // IMethod impl= findMethod(curr, allMethods);
371 // if (impl == null || ((curr.getExceptionTypes().length < impl.getExceptionTypes().length) && !Flags.isFinal(impl.getFlags()))) {
372 // if (impl != null) {
373 // allMethods.remove(impl);
375 // // implement an interface method when it does not exist in the hierarchy
376 // // or when it throws less exceptions that the implemented
377 // toImplement.add(curr);
378 // allMethods.add(curr);
383 // IMethod[] toImplementArray= (IMethod[]) toImplement.toArray(new IMethod[toImplement.size()]);
384 // if (selectionQuery != null) {
386 // allMethods.removeAll(Arrays.asList(typeMethods));
389 // for (int i= allMethods.size() - 1; i >= 0; i--) {
390 // IMethod curr= (IMethod) allMethods.get(i);
391 // if (Flags.isFinal(curr.getFlags())) {
392 // allMethods.remove(i);
395 // IMethod[] choice= (IMethod[]) allMethods.toArray(new IMethod[allMethods.size()]);
396 // toImplementArray= selectionQuery.select(choice, toImplementArray, hierarchy);
397 // if (toImplementArray == null) {
402 // GenStubSettings genStubSettings= new GenStubSettings(settings);
403 // genStubSettings.methodOverwrites= true;
404 // String[] result= new String[toImplementArray.length];
405 // for (int i= 0; i < toImplementArray.length; i++) {
406 // IMethod curr= toImplementArray[i];
407 // IMethod overrides= JavaModelUtil.findMethodImplementationInHierarchy(hierarchy, type, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor());
408 // genStubSettings.callSuper= (overrides != null);
410 // IMethod desc= JavaModelUtil.findMethodDeclarationInHierarchy(hierarchy, type, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor());
411 // if (desc != null) {
414 // result[i]= genStub(type.getElementName(), curr, genStubSettings, imports);
420 * Examines a string and returns the first line delimiter found.
422 // public static String getLineDelimiterUsed(IJavaElement elem) throws JavaModelException {
423 // ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
424 // if (cu != null && cu.exists()) {
425 // IBuffer buf= cu.getBuffer();
426 // int length= buf.getLength();
427 // for (int i= 0; i < length; i++) {
428 // char ch= buf.getChar(i);
429 // if (ch == SWT.CR) {
430 // if (i + 1 < length) {
431 // if (buf.getChar(i + 1) == SWT.LF) {
432 // return "\r\n"; //$NON-NLS-1$
435 // return "\r"; //$NON-NLS-1$
436 // } else if (ch == SWT.LF) {
437 // return "\n"; //$NON-NLS-1$
441 // return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
445 * Embodies the policy which line delimiter to use when inserting into
448 public static String getLineDelimiterFor(IDocument doc) {
449 // new for: 1GF5UU0: ITPJUI:WIN2000 - "Organize Imports" in php editor inserts lines in wrong format
450 String lineDelim= null;
452 lineDelim= doc.getLineDelimiter(0);
453 } catch (BadLocationException e) {
455 if (lineDelim == null) {
456 String systemDelimiter= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
457 String[] lineDelims= doc.getLegalLineDelimiters();
458 for (int i= 0; i < lineDelims.length; i++) {
459 if (lineDelims[i].equals(systemDelimiter)) {
460 lineDelim= systemDelimiter;
464 if (lineDelim == null) {
465 lineDelim= lineDelims.length > 0 ? lineDelims[0] : systemDelimiter;
473 * Evaluates the indention used by a Java element. (in tabulators)
475 // public static int getIndentUsed(IJavaElement elem) throws JavaModelException {
476 // if (elem instanceof ISourceReference) {
477 // ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
479 // IBuffer buf= cu.getBuffer();
480 // int offset= ((ISourceReference)elem).getSourceRange().getOffset();
482 // // find beginning of line
483 // while (i > 0 && !Strings.isLineDelimiterChar(buf.getChar(i - 1)) ){
486 // return Strings.computeIndent(buf.getText(i, offset - i), CodeFormatterUtil.getTabWidth());
492 public static String codeFormat(String sourceString, int initialIndentationLevel, String lineDelim) {
493 ICodeFormatter formatter= ToolFactory.createDefaultCodeFormatter(null);
494 return formatter.format(sourceString, initialIndentationLevel, null, lineDelim);
498 * Returns the element after the give element.
500 // public static IJavaElement findNextSibling(IJavaElement member) throws JavaModelException {
501 // IJavaElement parent= member.getParent();
502 // if (parent instanceof IParent) {
503 // IJavaElement[] elements= ((IParent)parent).getChildren();
504 // for (int i= elements.length - 2; i >= 0 ; i--) {
505 // if (member.equals(elements[i])) {
506 // return elements[i+1];
513 // public static String getTodoTaskTag(IJavaProject project) {
514 // String markers= null;
515 // if (project == null) {
516 // markers= JavaCore.getOption(JavaCore.COMPILER_TASK_TAGS);
518 // markers= project.getOption(JavaCore.COMPILER_TASK_TAGS, true);
521 // if (markers != null && markers.length() > 0) {
522 // int idx= markers.indexOf(',');
526 // return markers.substring(0, idx);