use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class InnerClassExtractorTest method translateClassBody.
protected List<AbstractTypeDeclaration> translateClassBody(String testSource) {
String source = "public class Test { " + testSource + " }";
CompilationUnit unit = translateType("Test", source);
return unit.getTypes();
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class GenerationTest method compileType.
/**
* Compiles Java source, as contained in a source file.
*
* @param name the name of the public type being declared
* @param source the source code
* @return the parsed compilation unit
*/
protected CompilationUnit compileType(String name, String source) {
String mainTypeName = name.substring(name.lastIndexOf('.') + 1);
String path = name.replace('.', '/') + ".java";
int errors = ErrorUtil.errorCount();
parser.setEnableDocComments(options.docCommentsEnabled());
CompilationUnit unit = parser.parse(mainTypeName, path, source);
if (ErrorUtil.errorCount() > errors) {
int newErrorCount = ErrorUtil.errorCount() - errors;
String info = String.format("%d test compilation error%s", newErrorCount, (newErrorCount == 1 ? "" : "s"));
failWithMessages(info, ErrorUtil.getErrorMessages().subList(errors, ErrorUtil.errorCount()));
}
return unit;
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class GenerationTest method translateStatements.
/**
* Translate a string of Java statement(s) into a list of
* JDT DOM Statements. Although JDT has support for statement
* parsing, it doesn't resolve them. The statements are therefore
* wrapped in a type declaration so they having bindings.
*/
protected List<Statement> translateStatements(String stmts) {
// Wrap statements in test class, so type resolution works.
String source = "public class Test { void test() { " + stmts + "}}";
CompilationUnit unit = translateType("Test", source);
final List<Statement> statements = Lists.newArrayList();
unit.accept(new TreeVisitor() {
@Override
public boolean visit(MethodDeclaration node) {
if (ElementUtil.getName(node.getExecutableElement()).equals("test")) {
statements.addAll(node.getBody().getStatements());
}
return false;
}
});
return statements;
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class GenerationTest method translateType.
/**
* Translates Java source, as contained in a source file.
*
* @param typeName the name of the public type being declared
* @param source the source code
* @return the translated compilation unit
*/
protected CompilationUnit translateType(String typeName, String source) {
CompilationUnit newUnit = compileType(typeName, source);
TranslationProcessor.applyMutations(newUnit, deadCodeMap, TimeTracker.noop());
return newUnit;
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class AnonymousClassConverterTest method testMethodVarInAnonymousClass.
public void testMethodVarInAnonymousClass() throws IOException {
String source = "class Test { " + " boolean debug;" + " void foo() { " + " if (true) {" + " if (debug) {" + " final Integer i = 1;" + " Runnable r = new Runnable() { " + " public void run() { int j = i + 1; } }; }}}}";
// Verify method var in r1.run() isn't mistakenly made a field in r1.
CompilationUnit unit = translateType("Test", source);
NameTable nameTable = unit.getEnv().nameTable();
List<AbstractTypeDeclaration> types = unit.getTypes();
AbstractTypeDeclaration r1 = types.get(1);
assertEquals("Test_1", nameTable.getFullName(r1.getTypeElement()));
boolean found = false;
for (VariableDeclarationFragment var : TreeUtil.getAllFields(r1)) {
if (ElementUtil.getName(var.getVariableElement()).equals("val$i")) {
found = true;
}
}
assertTrue("required field not found", found);
// Verify method var is passed to constructor.
String translation = generateFromUnit(unit, "Test.m");
assertTranslation(translation, "r = create_Test_1_initWithJavaLangInteger_(i)");
}
Aggregations