Skip to content

Commit cf8b291

Browse files
committed
squash: replace all
1 parent a08a8ba commit cf8b291

1 file changed

Lines changed: 29 additions & 53 deletions

File tree

impl/src/main/java/org/jboss/forge/roaster/model/impl/MethodImpl.java

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,24 @@
66
*/
77
package org.jboss.forge.roaster.model.impl;
88

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.*;
2310
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;
2911
import org.jboss.forge.roaster.ParserException;
3012
import org.jboss.forge.roaster.Problem;
3113
import org.jboss.forge.roaster.Roaster;
3214
import org.jboss.forge.roaster.model.Annotation;
33-
import org.jboss.forge.roaster.model.JavaType;
3415
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.*;
3717
import org.jboss.forge.roaster.model.ast.AnnotationAccessor;
3818
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.*;
4720
import org.jboss.forge.roaster.model.util.Methods;
4821
import org.jboss.forge.roaster.model.util.Types;
4922

23+
import java.lang.reflect.Method;
24+
import java.lang.reflect.Modifier;
25+
import java.util.*;
26+
5027
/**
5128
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
5229
*/
@@ -125,31 +102,34 @@ else if (Modifier.isPrivate(mod))
125102
}
126103
}
127104

128-
public MethodImpl(final O parent, final String method)
129-
{
105+
public MethodImpl(final O parent, final String method) {
130106
init(parent);
131107

132108
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);
142111
MethodDeclaration newMethod = (MethodDeclaration) javaClassSourceMethodSource.getInternal();
143112
this.method = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod);
144113
}
145114

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+
146127
@Override
147-
public String toSignature()
148-
{
128+
public String toSignature() {
149129
StringBuilder signature = new StringBuilder();
150130
signature.append(Visibility.PACKAGE_PRIVATE == this.getVisibility() ? ""
151-
: this.getVisibility()
152-
.scope());
131+
: this.getVisibility()
132+
.scope());
153133
signature.append(" ");
154134
signature.append(this.getName()).append("(");
155135
List<ParameterSource<O>> parameters = this.getParameters();
@@ -276,8 +256,7 @@ public MethodSource<O> setBody(final String body)
276256
throw new ParserException(problems);
277257
}
278258
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);
281260
Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody();
282261
block = (Block) ASTNode.copySubtree(method.getAST(), block);
283262
method.setBody(block);
@@ -343,8 +322,7 @@ public MethodSource<O> setReturnType(final String typeName)
343322
String typeToUse = Types.toResolvedType(typeName, getOrigin());
344323

345324
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);
348326
org.eclipse.jdt.core.dom.Type returnType = ((MethodDeclaration) methods.get(0).getInternal()).getReturnType2();
349327

350328
returnType = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(method.getAST(), returnType);
@@ -501,8 +479,7 @@ public MethodSource<O> setName(final String name)
501479
public MethodSource<O> setParameters(final String parameters)
502480
{
503481
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);
506483
List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters();
507484

508485
method.parameters().clear();
@@ -841,8 +818,7 @@ public ParameterSource<O> addParameter(String type, String name)
841818
}
842819

843820
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);
846822
List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters();
847823

848824
ParameterSource<O> param = null;

0 commit comments

Comments
 (0)