Coverage Report - org.eclipse.swtbot.generator.MethodGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodGenerator
60%
56/92
67%
19/28
1.684
 
 1  
 /*******************************************************************************
 2  
  * Copyright (c) 2008, 2010 Ketan Padegaonkar 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
 7  
  * 
 8  
  * Contributors:
 9  
  *     Ketan Padegaonkar - initial API and implementation
 10  
  *******************************************************************************/
 11  
 package org.eclipse.swtbot.generator;
 12  
 
 13  
 import java.util.ArrayList;
 14  
 import java.util.List;
 15  
 
 16  
 import org.eclipse.swtbot.swt.finder.ReferenceBy;
 17  
 import org.eclipse.swtbot.swt.finder.utils.ClassUtils;
 18  
 
 19  
 /**
 20  
  * Generate a method given the way a widget can be referenced.
 21  
  *
 22  
  * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 23  
  * @version $Id$
 24  
  */
 25  
 public class MethodGenerator {
 26  
 
 27  
         private final ReferenceBy[]        list;
 28  
         private final Class<?>                returnType;
 29  
         private final String                methodNamePrefix;
 30  
         private final Class<?>                widgetType;
 31  
         private final String                style;
 32  
         private final Class<?>        creationType;
 33  
 
 34  0
         public MethodGenerator(Class<?> returnType, Class<?> creationType, Class<?> widgetType, String methodNamePrefix, String style, List<ReferenceBy> list) {
 35  0
                 this.returnType = returnType;
 36  0
                 this.creationType = creationType;
 37  0
                 this.widgetType = widgetType;
 38  0
                 this.methodNamePrefix = methodNamePrefix;
 39  0
                 this.style = style;
 40  0
                 this.list = list.toArray(new ReferenceBy[] {});
 41  0
         }
 42  
 
 43  50
         public MethodGenerator(Class<?> returnType, Class<?> creationType, Class<?> widgetType, String methodNamePrefix, String style, ReferenceBy... list) {
 44  50
                 this.returnType = returnType;
 45  50
                 this.creationType = creationType;
 46  50
                 this.widgetType = widgetType;
 47  50
                 this.methodNamePrefix = methodNamePrefix;
 48  50
                 this.style = style;
 49  50
                 this.list = list;
 50  50
         }
 51  
 
 52  
         public String methodArguments() {
 53  18
                 StringBuffer result = new StringBuffer();
 54  18
                 String delimiter = ", "; //$NON-NLS-1$
 55  
 
 56  46
                 for (ReferenceBy ref : list) {
 57  28
                         result.append(ref.methodArgument()).append(delimiter);
 58  
                 }
 59  18
                 result.lastIndexOf(delimiter);
 60  18
                 result.replace(result.length() - delimiter.length(), result.length(), ""); //$NON-NLS-1$
 61  
 
 62  18
                 return result.toString();
 63  
         }
 64  
 
 65  
         public String methodContentsWithIndex() {
 66  5
                 String result = "        @SuppressWarnings({\"unchecked\", \"rawtypes\"})\n"; //$NON-NLS-1$
 67  5
                 result += "        public " + ClassUtils.simpleClassName(returnType) + " " + methodName() + methodArgsWithIndex() + " {\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 68  5
                 result += "                Matcher matcher = allOf(" + instanceOf() + (otherMatchers().length() > 0 ? ", " : "") + otherMatchers() + ");\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
 69  10
                 result += "                return new " + ClassUtils.simpleClassName(creationType) + "((" + ClassUtils.simpleClassName(widgetType) //$NON-NLS-1$ //$NON-NLS-2$
 70  5
                                 + ") widget(matcher, index), matcher);\n"; //$NON-NLS-1$
 71  5
                 result += "        }\n"; //$NON-NLS-1$
 72  5
                 return result;
 73  
         }
 74  
 
 75  
         public String methodContents() {
 76  2
                 String result = ""; //$NON-NLS-1$
 77  2
                 result += "        public " + ClassUtils.simpleClassName(returnType) + " " + methodName() + methodArgsWithoutIndex() + " {\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 78  2
                 result += "                return " + methodName() + "(" + (methodInvocationArgs().equals(", ") ? "" : methodInvocationArgs()) + "0);\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
 79  2
                 result += "        }\n"; //$NON-NLS-1$
 80  2
                 return result;
 81  
         }
 82  
 
 83  
         public String commentContents() {
 84  0
                 String string = ""; //$NON-NLS-1$
 85  0
                 string += params();
 86  0
                 string += returnStatement();
 87  0
                 string += throwsStatement();
 88  0
                 return comment(string);
 89  
         }
 90  
 
 91  
         public String commentContentsWithIndex() {
 92  0
                 String string = ""; //$NON-NLS-1$
 93  0
                 string += params();
 94  0
                 string += "@param index the index of the widget.\n"; //$NON-NLS-1$
 95  0
                 string += returnStatement();
 96  0
                 string += throwsStatement();
 97  0
                 string += "\n"; //$NON-NLS-1$
 98  0
                 return comment(string);
 99  
         }
 100  
 
 101  
         private String throwsStatement() {
 102  0
                 return "@throws WidgetNotFoundException if the widget is not found or is disposed.\n";
 103  
         }
 104  
 
 105  
         private String returnStatement() {
 106  0
                 String string = "@return a {@link " + ClassUtils.simpleClassName(returnType) + "}"; //$NON-NLS-1$ //$NON-NLS-2$
 107  0
                 for (ReferenceBy ref : list) {
 108  0
                         string += " " + ref.describeJavaDoc(); //$NON-NLS-1$
 109  
                 }
 110  0
                 return string + ".\n";
 111  
         }
 112  
 
 113  
         private String params() {
 114  0
                 String string = ""; //$NON-NLS-1$
 115  0
                 for (ReferenceBy ref : list) {
 116  0
                         string += ref.paramJavaDoc();
 117  
                 }
 118  0
                 return string;
 119  
         }
 120  
 
 121  
         private String comment(String string) {
 122  0
                 String[] lines = string.split("\n"); //$NON-NLS-1$
 123  0
                 StringBuffer buf = new StringBuffer();
 124  0
                 buf.append("        /**\n"); //$NON-NLS-1$
 125  0
                 for (String line : lines) {
 126  0
                         buf.append("         * ").append(line).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
 127  
                 }
 128  0
                 buf.append("         */"); //$NON-NLS-1$
 129  0
                 return buf.toString();
 130  
         }
 131  
 
 132  
         private String methodInvocationArgs() {
 133  4
                 String invocation = ""; //$NON-NLS-1$
 134  4
                 String[] methodArgs = methodArguments().split(","); //$NON-NLS-1$
 135  10
                 for (String methodArg : methodArgs) {
 136  6
                         String[] arg = methodArg.split("\\s"); //$NON-NLS-1$
 137  6
                         invocation += arg[arg.length - 1] + ", "; //$NON-NLS-1$
 138  
                 }
 139  4
                 return invocation;
 140  
         }
 141  
 
 142  
         private String otherMatchers() {
 143  10
                 StringBuffer result = new StringBuffer();
 144  10
                 String delimiter = ", "; //$NON-NLS-1$
 145  
 
 146  26
                 for (ReferenceBy ref : list) {
 147  16
                         String matcherMethod = ref.matcherMethod();
 148  16
                         if (matcherMethod.trim().length() > 0)
 149  14
                                 result.append(matcherMethod).append(delimiter);
 150  
                 }
 151  
 
 152  10
                 if (hasStyle())
 153  6
                         result.append("withStyle(" + style + ", \"" + style + "\")").append(delimiter); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 154  
 
 155  10
                 if (result.lastIndexOf(delimiter) >= 0)
 156  10
                         result.replace(result.length() - delimiter.length(), result.length(), ""); //$NON-NLS-1$
 157  
 
 158  10
                 return result.toString();
 159  
         }
 160  
 
 161  
         private boolean hasStyle() {
 162  10
                 return !"SWT.NONE".equals(style); //$NON-NLS-1$
 163  
         }
 164  
 
 165  
         private String instanceOf() {
 166  5
                 return "widgetOfType(" + ClassUtils.simpleClassName(widgetType) + ".class)"; //$NON-NLS-1$ //$NON-NLS-2$
 167  
         }
 168  
 
 169  
         private String methodArgsWithIndex() {
 170  5
                 return "(" + methodArguments() + (methodArguments().length() > 0 ? ", " : "") + "int index)"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
 171  
         }
 172  
 
 173  
         private String methodArgsWithoutIndex() {
 174  2
                 return "(" + methodArguments() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
 175  
         }
 176  
 
 177  
         private String methodName() {
 178  9
                 StringBuffer methodName = new StringBuffer(methodNamePrefix);
 179  23
                 for (ReferenceBy ref : list) {
 180  14
                         methodName.append(ref.methodNameSuffix());
 181  
                 }
 182  9
                 return methodName.toString();
 183  
         }
 184  
 
 185  
         public List<String> imports() {
 186  2
                 ArrayList<String> imports = new ArrayList<String>();
 187  2
                 imports.add("import " + returnType.getName()); //$NON-NLS-1$
 188  2
                 imports.add("import " + widgetType.getName()); //$NON-NLS-1$
 189  2
                 return imports;
 190  
         }
 191  
 
 192  
 }