Skip to content

Commit

Permalink
ROASTER-122: Validate java code
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Feb 23, 2017
1 parent 8e64b52 commit 8a3551c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
Expand All @@ -25,6 +28,10 @@
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.VariableDeclaration;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
import org.eclipse.jdt.internal.core.util.CodeSnippetParsingUtil;
import org.jboss.forge.roaster.ParserException;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.Annotation;
import org.jboss.forge.roaster.model.JavaType;
Expand Down Expand Up @@ -251,6 +258,7 @@ public String getBody()
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public MethodSource<O> setBody(final String body)
{
Expand All @@ -260,11 +268,27 @@ public MethodSource<O> setBody(final String body)
}
else
{
Hashtable options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
options.put(JavaCore.CORE_ENCODING, "UTF-8");
CodeSnippetParsingUtil codeSnippetParsingUtil = new CodeSnippetParsingUtil(false);
ConstructorDeclaration constructorDeclaration = codeSnippetParsingUtil.parseStatements(body.toCharArray(), 0,
body.length(), options, true, false);
CompilationResult compilationResult = constructorDeclaration.compilationResult();
if (compilationResult.hasErrors())
{
StringBuilder sb = new StringBuilder("PROBLEMS:\n");
for (CategorizedProblem problem : compilationResult.getErrors())
{
sb.append("\t - ").append(problem.getMessage()).append('\n');
}
throw new ParserException(sb.toString());
}
// TODO: Reuse ConstructorDeclaration somehow
String stub = "public class Stub { public void method() {" + body + "} }";
JavaClassSource temp = (JavaClassSource) Roaster.parse(stub);
List<MethodSource<JavaClassSource>> methods = temp.getMethods();
Block block = ((MethodDeclaration) methods.get(0).getInternal()).getBody();

block = (Block) ASTNode.copySubtree(method.getAST(), block);
method.setBody(block);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Enumeration;

import org.jboss.forge.roaster.ParserException;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.Visibility;
import org.jboss.forge.roaster.model.source.AnnotationSource;
Expand Down Expand Up @@ -239,4 +240,29 @@ public void testOmitImportsOfDefaultImplementations()
Assert.assertNull("Import of '" + className + "' should not exist.", implListImport);
}

@Test(expected = ParserException.class)
public void testMethodBodyShouldNotBeEmptyOnInvalidCode()
{
JavaClassSource source = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> method = source.addMethod().setName("foo");
method.setBody("{}{{}{dasfasdfasdfga");
}

@Test
public void testEmptyMethodBodyShouldNotThrowException()
{
JavaClassSource source = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> method = source.addMethod().setName("foo");
method.setBody("");
Assert.assertThat(method.getBody(), equalTo(""));
}

@Test
public void testMethodBodyShouldParseCorrectly()
{
JavaClassSource source = Roaster.create(JavaClassSource.class);
MethodSource<JavaClassSource> method = source.addMethod().setName("foo");
method.setBody("System.out.println(\"Hello World\");");
Assert.assertThat(method.getBody(), equalTo("System.out.println(\"Hello World\");"));
}
}

0 comments on commit 8a3551c

Please sign in to comment.