|
6 | 6 | */ |
7 | 7 | package org.jboss.forge.roaster.model.impl; |
8 | 8 |
|
9 | | -import java.lang.reflect.Method; |
10 | | -import java.lang.reflect.Modifier; |
11 | | -import java.util.ArrayList; |
12 | | -import java.util.Collections; |
13 | | -import java.util.Iterator; |
14 | | -import java.util.List; |
15 | | -import java.util.Objects; |
16 | | - |
17 | | -import org.eclipse.jdt.core.dom.AST; |
18 | | -import org.eclipse.jdt.core.dom.ASTNode; |
19 | | -import org.eclipse.jdt.core.dom.Block; |
20 | | -import org.eclipse.jdt.core.dom.CompilationUnit; |
21 | | -import org.eclipse.jdt.core.dom.Javadoc; |
22 | | -import org.eclipse.jdt.core.dom.MethodDeclaration; |
| 9 | +import org.eclipse.jdt.core.dom.*; |
23 | 10 | import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword; |
24 | | -import org.eclipse.jdt.core.dom.Name; |
25 | | -import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
26 | | -import org.eclipse.jdt.core.dom.Statement; |
27 | | -import org.eclipse.jdt.core.dom.TypeParameter; |
28 | | -import org.eclipse.jdt.core.dom.VariableDeclaration; |
29 | 11 | import org.jboss.forge.roaster.ParserException; |
30 | 12 | import org.jboss.forge.roaster.Problem; |
31 | 13 | import org.jboss.forge.roaster.Roaster; |
32 | 14 | import org.jboss.forge.roaster.model.Annotation; |
33 | | -import org.jboss.forge.roaster.model.JavaType; |
34 | 15 | import org.jboss.forge.roaster.model.Type; |
35 | | -import org.jboss.forge.roaster.model.TypeVariable; |
36 | | -import org.jboss.forge.roaster.model.Visibility; |
| 16 | +import org.jboss.forge.roaster.model.*; |
37 | 17 | import org.jboss.forge.roaster.model.ast.AnnotationAccessor; |
38 | 18 | import org.jboss.forge.roaster.model.ast.ModifierAccessor; |
39 | | -import org.jboss.forge.roaster.model.source.AnnotationSource; |
40 | | -import org.jboss.forge.roaster.model.source.Import; |
41 | | -import org.jboss.forge.roaster.model.source.JavaClassSource; |
42 | | -import org.jboss.forge.roaster.model.source.JavaDocSource; |
43 | | -import org.jboss.forge.roaster.model.source.JavaSource; |
44 | | -import org.jboss.forge.roaster.model.source.MethodSource; |
45 | | -import org.jboss.forge.roaster.model.source.ParameterSource; |
46 | | -import org.jboss.forge.roaster.model.source.TypeVariableSource; |
| 19 | +import org.jboss.forge.roaster.model.source.*; |
47 | 20 | import org.jboss.forge.roaster.model.util.Methods; |
48 | 21 | import org.jboss.forge.roaster.model.util.Types; |
49 | 22 |
|
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.lang.reflect.Modifier; |
| 25 | +import java.util.*; |
| 26 | + |
50 | 27 | /** |
51 | 28 | * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
52 | 29 | */ |
@@ -125,31 +102,34 @@ else if (Modifier.isPrivate(mod)) |
125 | 102 | } |
126 | 103 | } |
127 | 104 |
|
128 | | - public MethodImpl(final O parent, final String method) |
129 | | - { |
| 105 | + public MethodImpl(final O parent, final String method) { |
130 | 106 | init(parent); |
131 | 107 |
|
132 | 108 | String stub = "public class Stub { " + method + " }"; |
133 | | - JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); |
134 | | - List<Problem> problems = Roaster.validateSnippet(stub); |
135 | | - if (!problems.isEmpty()) { |
136 | | - throw new IllegalArgumentException("Invalid method code. " + problems.toString()); |
137 | | - } |
138 | | - List<MethodSource<JavaClassSource>> methods = temp.getMethods(); |
139 | | - if (methods.isEmpty()) |
140 | | - throw new IllegalArgumentException("No methods found - check your method syntax"); |
141 | | - MethodSource<JavaClassSource> javaClassSourceMethodSource = methods.get(0); // don't lookup indexes without validating them |
| 109 | + List<MethodSource<JavaClassSource>> methods = getMethodSources(stub); |
| 110 | + MethodSource<JavaClassSource> javaClassSourceMethodSource = methods.get(0); |
142 | 111 | MethodDeclaration newMethod = (MethodDeclaration) javaClassSourceMethodSource.getInternal(); |
143 | 112 | this.method = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod); |
144 | 113 | } |
145 | 114 |
|
| 115 | + private List<MethodSource<JavaClassSource>> getMethodSources(final String stub) { |
| 116 | + JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); |
| 117 | + List<Problem> problems = Roaster.validateSnippet(stub); |
| 118 | + if (!problems.isEmpty()) { |
| 119 | + throw new IllegalArgumentException("Invalid method code. " + problems.toString()); |
| 120 | + } |
| 121 | + List<MethodSource<JavaClassSource>> methods = temp.getMethods(); |
| 122 | + if (methods.isEmpty()) |
| 123 | + throw new IllegalArgumentException("No methods found - check your method syntax"); |
| 124 | + return methods; |
| 125 | + } |
| 126 | + |
146 | 127 | @Override |
147 | | - public String toSignature() |
148 | | - { |
| 128 | + public String toSignature() { |
149 | 129 | StringBuilder signature = new StringBuilder(); |
150 | 130 | signature.append(Visibility.PACKAGE_PRIVATE == this.getVisibility() ? "" |
151 | | - : this.getVisibility() |
152 | | - .scope()); |
| 131 | + : this.getVisibility() |
| 132 | + .scope()); |
153 | 133 | signature.append(" "); |
154 | 134 | signature.append(this.getName()).append("("); |
155 | 135 | List<ParameterSource<O>> parameters = this.getParameters(); |
@@ -276,8 +256,7 @@ public MethodSource<O> setBody(final String body) |
276 | 256 | throw new ParserException(problems); |
277 | 257 | } |
278 | 258 | String stub = "public class Stub { public void method() {" + body + "} }"; |
279 | | - JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); |
280 | | - List<MethodSource<JavaClassSource>> methods = temp.getMethods(); |
| 259 | + List<MethodSource<JavaClassSource>> methods = getMethodSources(stub); |
281 | 260 | Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody(); |
282 | 261 | block = (Block) ASTNode.copySubtree(method.getAST(), block); |
283 | 262 | method.setBody(block); |
@@ -343,8 +322,7 @@ public MethodSource<O> setReturnType(final String typeName) |
343 | 322 | String typeToUse = Types.toResolvedType(typeName, getOrigin()); |
344 | 323 |
|
345 | 324 | String stub = "public class Stub { public " + typeToUse + " method() {} }"; |
346 | | - JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); |
347 | | - List<MethodSource<JavaClassSource>> methods = temp.getMethods(); |
| 325 | + List<MethodSource<JavaClassSource>> methods = getMethodSources(stub); |
348 | 326 | org.eclipse.jdt.core.dom.Type returnType = ((MethodDeclaration) methods.get(0).getInternal()).getReturnType2(); |
349 | 327 |
|
350 | 328 | returnType = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(method.getAST(), returnType); |
@@ -501,8 +479,7 @@ public MethodSource<O> setName(final String name) |
501 | 479 | public MethodSource<O> setParameters(final String parameters) |
502 | 480 | { |
503 | 481 | String stub = "public class Stub { public void method( " + parameters + " ) {} }"; |
504 | | - JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); |
505 | | - List<MethodSource<JavaClassSource>> methods = temp.getMethods(); |
| 482 | + List<MethodSource<JavaClassSource>> methods = getMethodSources(stub); |
506 | 483 | List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters(); |
507 | 484 |
|
508 | 485 | method.parameters().clear(); |
@@ -841,8 +818,7 @@ public ParameterSource<O> addParameter(String type, String name) |
841 | 818 | } |
842 | 819 |
|
843 | 820 | String stub = "public class Stub { public void method( " + resolvedType + " " + name + " ) {} }"; |
844 | | - JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); |
845 | | - List<MethodSource<JavaClassSource>> methods = temp.getMethods(); |
| 821 | + List<MethodSource<JavaClassSource>> methods = getMethodSources(stub); |
846 | 822 | List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters(); |
847 | 823 |
|
848 | 824 | ParameterSource<O> param = null; |
|
0 commit comments