Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: All user code input should be validated #199 #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ParserException(final String message, final Throwable cause)
*/
public ParserException(String message, List<Problem> problems)
{
super(message);
super(message + "\n" + problems.toString());
this.problems = problems;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,25 @@ public MethodImpl(final O parent, final String method)
init(parent);

String stub = "public class Stub { " + method + " }";
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
MethodSource<JavaClassSource> javaClassSourceMethodSource = methods.get(0);
MethodDeclaration newMethod = (MethodDeclaration) javaClassSourceMethodSource.getInternal();
this.method = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod);
}

private List<MethodSource<JavaClassSource>> getMethodSources(final String stub)
{
List<Problem> problems = Roaster.validateSnippet(stub);
if (!problems.isEmpty())
{
throw new IllegalArgumentException("Invalid method code. " + problems);
}

JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
MethodDeclaration newMethod = (MethodDeclaration) methods.get(0).getInternal();
this.method = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod);
if (methods.isEmpty())
throw new IllegalArgumentException("No methods found - check your method syntax");
return methods;
}

@Override
Expand All @@ -142,7 +157,7 @@ public String toSignature()
StringBuilder signature = new StringBuilder();
signature.append(Visibility.PACKAGE_PRIVATE == this.getVisibility() ? ""
: this.getVisibility()
.scope());
.scope());
signature.append(" ");
signature.append(this.getName()).append("(");
List<ParameterSource<O>> parameters = this.getParameters();
Expand Down Expand Up @@ -266,11 +281,10 @@ public MethodSource<O> setBody(final String body)
List<Problem> problems = Roaster.validateSnippet(body);
if (problems.size() > 0)
{
throw new ParserException(problems);
throw new ParserException("Parse error in:\n" + body, problems);
}
String stub = "public class Stub { public void method() {" + body + "} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody();
block = (Block) ASTNode.copySubtree(method.getAST(), block);
method.setBody(block);
Expand Down Expand Up @@ -336,8 +350,7 @@ public MethodSource<O> setReturnType(final String typeName)
String typeToUse = Types.toResolvedType(typeName, getOrigin());

String stub = "public class Stub { public " + typeToUse + " method() {} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
org.eclipse.jdt.core.dom.Type returnType = ((MethodDeclaration) methods.get(0).getInternal()).getReturnType2();

returnType = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(method.getAST(), returnType);
Expand Down Expand Up @@ -494,8 +507,7 @@ public MethodSource<O> setName(final String name)
public MethodSource<O> setParameters(final String parameters)
{
String stub = "public class Stub { public void method( " + parameters + " ) {} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters();

method.parameters().clear();
Expand Down Expand Up @@ -791,7 +803,7 @@ public MethodSource<O> removeTypeVariable(String name)
{
@SuppressWarnings("unchecked")
List<TypeParameter> typeParameters = method.typeParameters();
for (Iterator<TypeParameter> iter = typeParameters.iterator(); iter.hasNext();)
for (Iterator<TypeParameter> iter = typeParameters.iterator(); iter.hasNext(); )
{
if (Objects.equals(name, iter.next().getName().getIdentifier()))
{
Expand Down Expand Up @@ -834,8 +846,7 @@ public ParameterSource<O> addParameter(String type, String name)
}

String stub = "public class Stub { public void method( " + resolvedType + " " + name + " ) {} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
List<MethodSource<JavaClassSource>> methods = getMethodSources(stub);
List<VariableDeclaration> astParameters = ((MethodDeclaration) methods.get(0).getInternal()).parameters();

ParameterSource<O> param = null;
Expand Down