use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.
the class StatementGenerator method visit.
@Override
public boolean visit(VariableDeclarationExpression node) {
String typeString = nameTable.getObjCType(node.getTypeMirror());
boolean needsAsterisk = typeString.endsWith("*");
buffer.append(typeString);
if (!needsAsterisk) {
buffer.append(' ');
}
for (Iterator<VariableDeclarationFragment> it = node.getFragments().iterator(); it.hasNext(); ) {
VariableDeclarationFragment f = it.next();
f.accept(this);
if (it.hasNext()) {
buffer.append(", ");
if (needsAsterisk) {
buffer.append('*');
}
}
}
return false;
}
use of com.google.devtools.j2objc.ast.VariableDeclarationFragment in project j2objc by google.
the class UnsequencedExpressionRewriter method extractVariableDeclarationFragments.
private void extractVariableDeclarationFragments(List<VariableDeclarationFragment> fragments, List<Statement> stmtList) {
for (int i = 0; i < fragments.size(); i++) {
VariableDeclarationFragment frag = fragments.get(i);
Expression init = frag.getInitializer();
if (init == null) {
continue;
}
newExpression(init);
init.accept(this);
List<VariableAccess> toExtract = getUnsequencedAccesses();
if (!toExtract.isEmpty()) {
if (i > 0) {
// Extract all fragments before the current one to preserve ordering.
VariableDeclarationStatement newDecl = new VariableDeclarationStatement(fragments.get(0).copy());
for (int j = 1; j < i; j++) {
newDecl.addFragment(fragments.get(j).copy());
}
stmtList.add(newDecl);
fragments.subList(0, i).clear();
}
extractOrderedAccesses(stmtList, currentTopNode, toExtract);
i = 0;
}
}
}
use of com.google.devtools.j2objc.ast.VariableDeclarationFragment 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.VariableDeclarationFragment 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