1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
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 | |
|
21 | |
|
22 | |
|
23 | |
|
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 = ", "; |
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(), ""); |
61 | |
|
62 | 18 | return result.toString(); |
63 | |
} |
64 | |
|
65 | |
public String methodContentsWithIndex() { |
66 | 5 | String result = " @SuppressWarnings({\"unchecked\", \"rawtypes\"})\n"; |
67 | 5 | result += " public " + ClassUtils.simpleClassName(returnType) + " " + methodName() + methodArgsWithIndex() + " {\n"; |
68 | 5 | result += " Matcher matcher = allOf(" + instanceOf() + (otherMatchers().length() > 0 ? ", " : "") + otherMatchers() + ");\n"; |
69 | 10 | result += " return new " + ClassUtils.simpleClassName(creationType) + "((" + ClassUtils.simpleClassName(widgetType) |
70 | 5 | + ") widget(matcher, index), matcher);\n"; |
71 | 5 | result += " }\n"; |
72 | 5 | return result; |
73 | |
} |
74 | |
|
75 | |
public String methodContents() { |
76 | 2 | String result = ""; |
77 | 2 | result += " public " + ClassUtils.simpleClassName(returnType) + " " + methodName() + methodArgsWithoutIndex() + " {\n"; |
78 | 2 | result += " return " + methodName() + "(" + (methodInvocationArgs().equals(", ") ? "" : methodInvocationArgs()) + "0);\n"; |
79 | 2 | result += " }\n"; |
80 | 2 | return result; |
81 | |
} |
82 | |
|
83 | |
public String commentContents() { |
84 | 0 | String string = ""; |
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 = ""; |
93 | 0 | string += params(); |
94 | 0 | string += "@param index the index of the widget.\n"; |
95 | 0 | string += returnStatement(); |
96 | 0 | string += throwsStatement(); |
97 | 0 | string += "\n"; |
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) + "}"; |
107 | 0 | for (ReferenceBy ref : list) { |
108 | 0 | string += " " + ref.describeJavaDoc(); |
109 | |
} |
110 | 0 | return string + ".\n"; |
111 | |
} |
112 | |
|
113 | |
private String params() { |
114 | 0 | String string = ""; |
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"); |
123 | 0 | StringBuffer buf = new StringBuffer(); |
124 | 0 | buf.append(" /**\n"); |
125 | 0 | for (String line : lines) { |
126 | 0 | buf.append(" * ").append(line).append("\n"); |
127 | |
} |
128 | 0 | buf.append(" */"); |
129 | 0 | return buf.toString(); |
130 | |
} |
131 | |
|
132 | |
private String methodInvocationArgs() { |
133 | 4 | String invocation = ""; |
134 | 4 | String[] methodArgs = methodArguments().split(","); |
135 | 10 | for (String methodArg : methodArgs) { |
136 | 6 | String[] arg = methodArg.split("\\s"); |
137 | 6 | invocation += arg[arg.length - 1] + ", "; |
138 | |
} |
139 | 4 | return invocation; |
140 | |
} |
141 | |
|
142 | |
private String otherMatchers() { |
143 | 10 | StringBuffer result = new StringBuffer(); |
144 | 10 | String delimiter = ", "; |
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); |
154 | |
|
155 | 10 | if (result.lastIndexOf(delimiter) >= 0) |
156 | 10 | result.replace(result.length() - delimiter.length(), result.length(), ""); |
157 | |
|
158 | 10 | return result.toString(); |
159 | |
} |
160 | |
|
161 | |
private boolean hasStyle() { |
162 | 10 | return !"SWT.NONE".equals(style); |
163 | |
} |
164 | |
|
165 | |
private String instanceOf() { |
166 | 5 | return "widgetOfType(" + ClassUtils.simpleClassName(widgetType) + ".class)"; |
167 | |
} |
168 | |
|
169 | |
private String methodArgsWithIndex() { |
170 | 5 | return "(" + methodArguments() + (methodArguments().length() > 0 ? ", " : "") + "int index)"; |
171 | |
} |
172 | |
|
173 | |
private String methodArgsWithoutIndex() { |
174 | 2 | return "(" + methodArguments() + ")"; |
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()); |
188 | 2 | imports.add("import " + widgetType.getName()); |
189 | 2 | return imports; |
190 | |
} |
191 | |
|
192 | |
} |