2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.codemanipulation;
7 import java.util.ArrayList;
8 import java.util.Arrays;
11 import org.eclipse.swt.SWT;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
16 //import org.phpeclipse.phpdt.core.Flags;
17 //import org.phpeclipse.phpdt.core.IBuffer;
18 import net.sourceforge.phpdt.core.ICodeFormatter;
19 //import org.phpeclipse.phpdt.core.ICompilationUnit;
20 //import org.phpeclipse.phpdt.core.IJavaElement;
21 //import org.phpeclipse.phpdt.core.IJavaProject;
22 //import org.phpeclipse.phpdt.core.IMethod;
23 //import org.phpeclipse.phpdt.core.IParent;
24 //import org.phpeclipse.phpdt.core.ISourceReference;
25 //import org.phpeclipse.phpdt.core.IType;
26 //import org.phpeclipse.phpdt.core.ITypeHierarchy;
27 import net.sourceforge.phpeclipse.PHPCore;
28 //import org.phpeclipse.phpdt.core.JavaModelException;
29 //import org.phpeclipse.phpdt.core.Signature;
30 import net.sourceforge.phpdt.core.ToolFactory;
32 //import net.sourceforge.phpdt.internal.corext.util.CodeFormatterUtil;
33 //import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
34 import net.sourceforge.phpdt.internal.corext.util.Strings;
36 public class StubUtility {
39 // public static class GenStubSettings extends CodeGenerationSettings {
41 // public boolean callSuper;
42 // public boolean methodOverwrites;
43 // public boolean noBody;
45 // public GenStubSettings(CodeGenerationSettings settings) {
46 // settings.setSettings(this);
49 // public GenStubSettings() {
56 // * Generates a stub. Given a template method, a stub with the same signature
57 // * will be constructed so it can be added to a type.
58 // * @param destTypeName The name of the type to which the method will be added to (Used for the constructor)
59 // * @param method A method template (method belongs to different type than the parent)
60 // * @param options Options as defined above (<code>GenStubSettings</code>)
61 // * @param imports Imports required by the stub are added to the imports structure. If imports structure is <code>null</code>
62 // * all type names are qualified.
63 // * @throws JavaModelException
65 // public static String genStub(String destTypeName, IMethod method, GenStubSettings settings, IImportsStructure imports) throws JavaModelException {
66 // IType declaringtype= method.getDeclaringType();
67 // StringBuffer buf= new StringBuffer();
68 // String methodName= method.getElementName();
69 // String[] paramTypes= method.getParameterTypes();
70 // String[] paramNames= method.getParameterNames();
71 // String[] excTypes= method.getExceptionTypes();
72 // String retTypeSig= method.getReturnType();
73 // int flags= method.getFlags();
74 // boolean isConstructor= method.isConstructor();
76 // int lastParam= paramTypes.length -1;
79 // if (settings.createComments) {
80 // if (isConstructor) {
81 // String desc= "Constructor for " + destTypeName; //$NON-NLS-1$
82 // genJavaDocStub(desc, paramNames, Signature.SIG_VOID, excTypes, buf);
85 // if (settings.methodOverwrites) {
86 // boolean isDeprecated= Flags.isDeprecated(flags);
87 // genJavaDocSeeTag(declaringtype, methodName, paramTypes, settings.createNonJavadocComments, isDeprecated, buf);
89 // // generate a default php doc comment
90 // String desc= "Method " + methodName; //$NON-NLS-1$
91 // genJavaDocStub(desc, paramNames, retTypeSig, excTypes, buf);
97 // if (Flags.isPublic(flags) || isConstructor || (declaringtype.isInterface() && !settings.noBody)) {
98 // buf.append("public "); //$NON-NLS-1$
99 // } else if (Flags.isProtected(flags)) {
100 // buf.append("protected "); //$NON-NLS-1$
101 // } else if (Flags.isPrivate(flags)) {
102 // buf.append("private "); //$NON-NLS-1$
104 // if (Flags.isSynchronized(flags)) {
105 // buf.append("synchronized "); //$NON-NLS-1$
107 // if (Flags.isVolatile(flags)) {
108 // buf.append("volatile "); //$NON-NLS-1$
110 // if (Flags.isStrictfp(flags)) {
111 // buf.append("strictfp "); //$NON-NLS-1$
113 // if (Flags.isStatic(flags)) {
114 // buf.append("static "); //$NON-NLS-1$
117 // if (isConstructor) {
118 // buf.append(destTypeName);
120 // String retTypeFrm;
121 // if (!isPrimitiveType(retTypeSig)) {
122 // retTypeFrm= resolveAndAdd(retTypeSig, declaringtype, imports);
124 // retTypeFrm= Signature.toString(retTypeSig);
126 // buf.append(retTypeFrm);
128 // buf.append(methodName);
131 // for (int i= 0; i <= lastParam; i++) {
132 // String paramTypeSig= paramTypes[i];
133 // String paramTypeFrm;
135 // if (!isPrimitiveType(paramTypeSig)) {
136 // paramTypeFrm= resolveAndAdd(paramTypeSig, declaringtype, imports);
138 // paramTypeFrm= Signature.toString(paramTypeSig);
140 // buf.append(paramTypeFrm);
142 // buf.append(paramNames[i]);
143 // if (i < lastParam) {
144 // buf.append(", "); //$NON-NLS-1$
149 // int lastExc= excTypes.length - 1;
150 // if (lastExc >= 0) {
151 // buf.append(" throws "); //$NON-NLS-1$
152 // for (int i= 0; i <= lastExc; i++) {
153 // String excTypeSig= excTypes[i];
154 // String excTypeFrm= resolveAndAdd(excTypeSig, declaringtype, imports);
155 // buf.append(excTypeFrm);
156 // if (i < lastExc) {
157 // buf.append(", "); //$NON-NLS-1$
161 // if (settings.noBody) {
162 // buf.append(";\n\n"); //$NON-NLS-1$
164 // buf.append(" {\n\t"); //$NON-NLS-1$
165 // if (!settings.callSuper) {
166 // if (retTypeSig != null && !retTypeSig.equals(Signature.SIG_VOID)) {
168 // if (!isPrimitiveType(retTypeSig) || Signature.getArrayCount(retTypeSig) > 0) {
169 // buf.append("return null;\n\t"); //$NON-NLS-1$
170 // } else if (retTypeSig.equals(Signature.SIG_BOOLEAN)) {
171 // buf.append("return false;\n\t"); //$NON-NLS-1$
173 // buf.append("return 0;\n\t"); //$NON-NLS-1$
178 // if (!isConstructor) {
179 // if (!Signature.SIG_VOID.equals(retTypeSig)) {
180 // buf.append("return "); //$NON-NLS-1$
182 // buf.append("super."); //$NON-NLS-1$
183 // buf.append(methodName);
185 // buf.append("super"); //$NON-NLS-1$
188 // for (int i= 0; i <= lastParam; i++) {
189 // buf.append(paramNames[i]);
190 // if (i < lastParam) {
191 // buf.append(", "); //$NON-NLS-1$
194 // buf.append(");\n\t"); //$NON-NLS-1$
196 // buf.append("}\n"); //$NON-NLS-1$
198 // return buf.toString();
201 // private static boolean isSet(int options, int flag) {
202 // return (options & flag) != 0;
205 // private static boolean isPrimitiveType(String typeName) {
206 // char first= Signature.getElementType(typeName).charAt(0);
207 // return (first != Signature.C_RESOLVED && first != Signature.C_UNRESOLVED);
210 // private static String resolveAndAdd(String refTypeSig, IType declaringType, IImportsStructure imports) throws JavaModelException {
211 // String resolvedTypeName= JavaModelUtil.getResolvedTypeName(refTypeSig, declaringType);
212 // if (resolvedTypeName != null) {
213 // StringBuffer buf= new StringBuffer();
214 // if (imports != null) {
215 // buf.append(imports.addImport(resolvedTypeName));
217 // buf.append(resolvedTypeName);
219 // int arrayCount= Signature.getArrayCount(refTypeSig);
220 // for (int i= 0; i < arrayCount; i++) {
221 // buf.append("[]"); //$NON-NLS-1$
223 // return buf.toString();
225 // return Signature.toString(refTypeSig);
229 // * Generates a default JavaDoc comment stub for a method.
231 // public static void genJavaDocStub(String descr, String[] paramNames, String retTypeSig, String[] excTypeSigs, StringBuffer buf) {
232 // buf.append("/**\n"); //$NON-NLS-1$
233 // buf.append(" * "); buf.append(descr); buf.append(".\n"); //$NON-NLS-2$ //$NON-NLS-1$
234 // for (int i= 0; i < paramNames.length; i++) {
235 // buf.append(" * @param "); buf.append(paramNames[i]); buf.append('\n'); //$NON-NLS-1$
237 // if (retTypeSig != null && !retTypeSig.equals(Signature.SIG_VOID)) {
238 // String simpleName= Signature.getSimpleName(Signature.toString(retTypeSig));
239 // buf.append(" * @return "); buf.append(simpleName); buf.append('\n'); //$NON-NLS-1$
241 // if (excTypeSigs != null) {
242 // for (int i= 0; i < excTypeSigs.length; i++) {
243 // String simpleName= Signature.getSimpleName(Signature.toString(excTypeSigs[i]));
244 // buf.append(" * @throws "); buf.append(simpleName); buf.append('\n'); //$NON-NLS-1$
247 // buf.append(" */"); //$NON-NLS-1$
251 // * Generates a '@see' tag to the defined method.
253 // public static void genJavaDocSeeTag(IType declaringType, String methodName, String[] paramTypes, boolean nonJavaDocComment, boolean isDeprecated, StringBuffer buf) throws JavaModelException {
254 // String[] fullParamNames= new String[paramTypes.length];
255 // for (int i= 0; i < paramTypes.length; i++) {
256 // fullParamNames[i]= JavaModelUtil.getResolvedTypeName(paramTypes[i], declaringType);
258 // String fullTypeName= JavaModelUtil.getFullyQualifiedName(declaringType);
260 // genJavaDocSeeTag(fullTypeName, methodName, fullParamNames, nonJavaDocComment, isDeprecated, buf);
264 // * Generates a '@see' tag to the defined method.
266 // public static void genJavaDocSeeTag(String fullyQualifiedTypeName, String methodName, String[] fullParamTypeNames, boolean nonJavaDocComment, boolean isDeprecated, StringBuffer buf) throws JavaModelException {
267 // // create a @see link
268 // buf.append("/*"); //$NON-NLS-1$
269 // if (!nonJavaDocComment) {
272 // buf.append(" (non-Javadoc)"); //$NON-NLS-1$
274 // buf.append("\n * @see "); //$NON-NLS-1$
275 // buf.append(fullyQualifiedTypeName);
277 // buf.append(methodName);
279 // for (int i= 0; i < fullParamTypeNames.length; i++) {
281 // buf.append(", "); //$NON-NLS-1$
283 // buf.append(fullParamTypeNames[i]);
285 // buf.append(")\n"); //$NON-NLS-1$
286 // if (isDeprecated) {
287 // buf.append(" * @deprecated\n"); //$NON-NLS-1$
289 // buf.append(" */"); //$NON-NLS-1$
295 // * Finds a method in a list of methods.
296 // * @return The found method or null, if nothing found
298 // private static IMethod findMethod(IMethod method, List allMethods) throws JavaModelException {
299 // String name= method.getElementName();
300 // String[] paramTypes= method.getParameterTypes();
301 // boolean isConstructor= method.isConstructor();
303 // for (int i= allMethods.size() - 1; i >= 0; i--) {
304 // IMethod curr= (IMethod) allMethods.get(i);
305 // if (JavaModelUtil.isSameMethodSignature(name, paramTypes, isConstructor, curr)) {
314 * Creates needed constructors for a type.
315 * @param type The type to create constructors for
316 * @param supertype The type's super type
317 * @param settings Options for comment generation
318 * @param imports Required imports are added to the import structure. Structure can be <code>null</code>, types are qualified then.
319 * @return Returns the generated stubs or <code>null</code> if the creation has been canceled
321 // public static String[] evalConstructors(IType type, IType supertype, CodeGenerationSettings settings, IImportsStructure imports) throws JavaModelException {
322 // IMethod[] superMethods= supertype.getMethods();
323 // String typeName= type.getElementName();
324 // IMethod[] methods= type.getMethods();
325 // GenStubSettings genStubSettings= new GenStubSettings(settings);
326 // genStubSettings.callSuper= true;
327 // ArrayList newMethods= new ArrayList(superMethods.length);
328 // for (int i= 0; i < superMethods.length; i++) {
329 // IMethod curr= superMethods[i];
330 // if (curr.isConstructor() && (JavaModelUtil.isVisible(curr, type.getPackageFragment()) || Flags.isProtected(curr.getFlags()))) {
331 // if (JavaModelUtil.findMethod(typeName, curr.getParameterTypes(), true, methods) == null) {
332 // String newStub= genStub(typeName, superMethods[i], genStubSettings, imports);
333 // newMethods.add(newStub);
337 // return (String[]) newMethods.toArray(new String[newMethods.size()]);
341 // * Searches for unimplemented methods of a type.
342 // * @param isSubType If set, the evaluation is for a subtype of the given type. If not set, the
343 // * evaluation is for the type itself.
344 // * @param settings Options for comment generation
345 // * @param selectionQuery If not null will select the methods to implement.
346 // * @param imports Required imports are added to the import structure. Structure can be <code>null</code>, types are qualified then.
347 // * @return Returns the generated stubs or <code>null</code> if the creation has been canceled
349 // public static String[] evalUnimplementedMethods(IType type, ITypeHierarchy hierarchy, boolean isSubType, CodeGenerationSettings settings,
350 // IOverrideMethodQuery selectionQuery, IImportsStructure imports) throws JavaModelException {
351 // List allMethods= new ArrayList();
352 // List toImplement= new ArrayList();
354 // IMethod[] typeMethods= type.getMethods();
355 // for (int i= 0; i < typeMethods.length; i++) {
356 // IMethod curr= typeMethods[i];
357 // if (!curr.isConstructor() && !Flags.isStatic(curr.getFlags()) && !Flags.isPrivate(curr.getFlags())) {
358 // allMethods.add(curr);
362 // IType[] superTypes= hierarchy.getAllSuperclasses(type);
363 // for (int i= 0; i < superTypes.length; i++) {
364 // IMethod[] methods= superTypes[i].getMethods();
365 // for (int k= 0; k < methods.length; k++) {
366 // IMethod curr= methods[k];
367 // if (!curr.isConstructor() && !Flags.isStatic(curr.getFlags()) && !Flags.isPrivate(curr.getFlags())) {
368 // if (findMethod(curr, allMethods) == null) {
369 // allMethods.add(curr);
375 // // do not call super
376 // for (int i= 0; i < allMethods.size(); i++) {
377 // IMethod curr= (IMethod) allMethods.get(i);
378 // if ((Flags.isAbstract(curr.getFlags()) || curr.getDeclaringType().isInterface()) && (isSubType || !type.equals(curr.getDeclaringType()))) {
379 // // implement all abstract methods
380 // toImplement.add(curr);
384 // IType[] superInterfaces= hierarchy.getAllSuperInterfaces(type);
385 // for (int i= 0; i < superInterfaces.length; i++) {
386 // IMethod[] methods= superInterfaces[i].getMethods();
387 // for (int k= 0; k < methods.length; k++) {
388 // IMethod curr= methods[k];
390 // // binary interfaces can contain static initializers (variable intializations)
392 // if (!Flags.isStatic(curr.getFlags())) {
393 // IMethod impl= findMethod(curr, allMethods);
394 // if (impl == null || ((curr.getExceptionTypes().length < impl.getExceptionTypes().length) && !Flags.isFinal(impl.getFlags()))) {
395 // if (impl != null) {
396 // allMethods.remove(impl);
398 // // implement an interface method when it does not exist in the hierarchy
399 // // or when it throws less exceptions that the implemented
400 // toImplement.add(curr);
401 // allMethods.add(curr);
406 // IMethod[] toImplementArray= (IMethod[]) toImplement.toArray(new IMethod[toImplement.size()]);
407 // if (selectionQuery != null) {
409 // allMethods.removeAll(Arrays.asList(typeMethods));
412 // for (int i= allMethods.size() - 1; i >= 0; i--) {
413 // IMethod curr= (IMethod) allMethods.get(i);
414 // if (Flags.isFinal(curr.getFlags())) {
415 // allMethods.remove(i);
418 // IMethod[] choice= (IMethod[]) allMethods.toArray(new IMethod[allMethods.size()]);
419 // toImplementArray= selectionQuery.select(choice, toImplementArray, hierarchy);
420 // if (toImplementArray == null) {
425 // GenStubSettings genStubSettings= new GenStubSettings(settings);
426 // genStubSettings.methodOverwrites= true;
427 // String[] result= new String[toImplementArray.length];
428 // for (int i= 0; i < toImplementArray.length; i++) {
429 // IMethod curr= toImplementArray[i];
430 // IMethod overrides= JavaModelUtil.findMethodImplementationInHierarchy(hierarchy, type, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor());
431 // genStubSettings.callSuper= (overrides != null);
433 // IMethod desc= JavaModelUtil.findMethodDeclarationInHierarchy(hierarchy, type, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor());
434 // if (desc != null) {
437 // result[i]= genStub(type.getElementName(), curr, genStubSettings, imports);
443 * Examines a string and returns the first line delimiter found.
445 // public static String getLineDelimiterUsed(IJavaElement elem) throws JavaModelException {
446 // ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
447 // if (cu != null && cu.exists()) {
448 // IBuffer buf= cu.getBuffer();
449 // int length= buf.getLength();
450 // for (int i= 0; i < length; i++) {
451 // char ch= buf.getChar(i);
452 // if (ch == SWT.CR) {
453 // if (i + 1 < length) {
454 // if (buf.getChar(i + 1) == SWT.LF) {
455 // return "\r\n"; //$NON-NLS-1$
458 // return "\r"; //$NON-NLS-1$
459 // } else if (ch == SWT.LF) {
460 // return "\n"; //$NON-NLS-1$
464 // return System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
468 * Embodies the policy which line delimiter to use when inserting into
471 public static String getLineDelimiterFor(IDocument doc) {
472 // new for: 1GF5UU0: ITPJUI:WIN2000 - "Organize Imports" in php editor inserts lines in wrong format
473 String lineDelim= null;
475 lineDelim= doc.getLineDelimiter(0);
476 } catch (BadLocationException e) {
478 if (lineDelim == null) {
479 String systemDelimiter= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
480 String[] lineDelims= doc.getLegalLineDelimiters();
481 for (int i= 0; i < lineDelims.length; i++) {
482 if (lineDelims[i].equals(systemDelimiter)) {
483 lineDelim= systemDelimiter;
487 if (lineDelim == null) {
488 lineDelim= lineDelims.length > 0 ? lineDelims[0] : systemDelimiter;
496 * Evaluates the indention used by a Java element. (in tabulators)
498 // public static int getIndentUsed(IJavaElement elem) throws JavaModelException {
499 // if (elem instanceof ISourceReference) {
500 // ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
502 // IBuffer buf= cu.getBuffer();
503 // int offset= ((ISourceReference)elem).getSourceRange().getOffset();
505 // // find beginning of line
506 // while (i > 0 && !Strings.isLineDelimiterChar(buf.getChar(i - 1)) ){
509 // return Strings.computeIndent(buf.getText(i, offset - i), CodeFormatterUtil.getTabWidth());
515 public static String codeFormat(String sourceString, int initialIndentationLevel, String lineDelim) {
516 ICodeFormatter formatter= ToolFactory.createDefaultCodeFormatter(null);
517 return formatter.format(sourceString, initialIndentationLevel, null, lineDelim);
521 * Returns the element after the give element.
523 // public static IJavaElement findNextSibling(IJavaElement member) throws JavaModelException {
524 // IJavaElement parent= member.getParent();
525 // if (parent instanceof IParent) {
526 // IJavaElement[] elements= ((IParent)parent).getChildren();
527 // for (int i= elements.length - 2; i >= 0 ; i--) {
528 // if (member.equals(elements[i])) {
529 // return elements[i+1];
536 // public static String getTodoTaskTag(IJavaProject project) {
537 // String markers= null;
538 // if (project == null) {
539 // markers= JavaCore.getOption(JavaCore.COMPILER_TASK_TAGS);
541 // markers= project.getOption(JavaCore.COMPILER_TASK_TAGS, true);
544 // if (markers != null && markers.length() > 0) {
545 // int idx= markers.indexOf(',');
549 // return markers.substring(0, idx);