use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class TreeConverter method convertCompilationUnit.
public static CompilationUnit convertCompilationUnit(Options options, JavacEnvironment env, JCTree.JCCompilationUnit javacUnit) {
String sourceFilePath = getPath(javacUnit.getSourceFile());
try {
TreeConverter converter = new TreeConverter(javacUnit, env);
JavaFileObject sourceFile = javacUnit.getSourceFile();
String source = sourceFile.getCharContent(false).toString();
String mainTypeName = FileUtil.getMainTypeName(sourceFile);
converter.newUnit = new CompilationUnit(new TranslationEnvironment(options, env), sourceFilePath, mainTypeName, source);
PackageElement pkg = javacUnit.packge != null ? javacUnit.packge : env.defaultPackage();
converter.newUnit.setPackage(converter.convertPackage(pkg, Trees.instance(env.task())));
for (JCTree type : javacUnit.getTypeDecls()) {
TreeNode newNode = converter.convert(type);
if (newNode.getKind() != TreeNode.Kind.EMPTY_STATEMENT) {
converter.newUnit.addType((AbstractTypeDeclaration) newNode);
}
}
addOcniComments(converter.newUnit, options.jsniWarnings());
return converter.newUnit;
} catch (Throwable e) {
ErrorUtil.fatalError(e, sourceFilePath);
return null;
}
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class GenerationTest method translateMethod.
/**
* Translate a Java method into a JDT DOM MethodDeclaration. Although JDT
* has support for parsing methods, it doesn't resolve them. The statements
* are therefore wrapped in a type declaration so they having bindings.
*/
protected MethodDeclaration translateMethod(String method) {
// Wrap statements in test class, so type resolution works.
String source = "public class Test { " + method + " }";
CompilationUnit unit = translateType("Test", source);
final MethodDeclaration[] result = new MethodDeclaration[1];
unit.accept(new TreeVisitor() {
@Override
public boolean visit(MethodDeclaration node) {
String name = ElementUtil.getName(node.getExecutableElement());
if (name.equals(NameTable.INIT_NAME) || name.equals(NameTable.FINALIZE_METHOD) || name.equals(NameTable.DEALLOC_METHOD)) {
return false;
}
assert result[0] == null;
result[0] = node;
return false;
}
});
return result[0];
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class AnonymousClassConverterTest method testMethodVarInSwitch.
public void testMethodVarInSwitch() throws IOException {
String source = "class Test { " + " enum E { ONE, TWO };" + " void foo(E e) { " + " switch (e) {" + " case ONE: {" + " 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(2);
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)");
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class AnonymousClassConverterTest method translateClassBody.
protected List<TypeDeclaration> translateClassBody(String testSource) {
String source = "public class Test { " + testSource + " }";
CompilationUnit unit = translateType("Test", source);
return Lists.newArrayList(Iterables.filter(unit.getTypes(), TypeDeclaration.class));
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class AnonymousClassConverterTest method testMethodVarInNestedAnonymousClass.
public void testMethodVarInNestedAnonymousClass() throws IOException {
String source = "class Test { " + " void bar() { " + " Runnable r1 = new Runnable() { " + " public void run() { " + " final Integer i = 1; " + " Runnable r2 = 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()));
for (VariableDeclarationFragment var : TreeUtil.getAllFields(r1)) {
if (ElementUtil.getName(var.getVariableElement()).equals("val$i")) {
fail("found field that shouldn't be declared");
}
}
// Method var in r1.run() becomes a field in r2.
AbstractTypeDeclaration r2 = types.get(2);
assertEquals("Test_1_1", nameTable.getFullName(r2.getTypeElement()));
boolean found = false;
for (VariableDeclarationFragment var : TreeUtil.getAllFields(r2)) {
if (ElementUtil.getName(var.getVariableElement()).equals("val$i")) {
found = true;
}
}
assertTrue("required field not found", found);
// Verify constructor takes both outer field and var.
String translation = generateFromUnit(unit, "Test.m");
assertTranslation(translation, "r2 = create_Test_1_1_initWithJavaLangInteger_(i)");
}
Aggregations