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); List<TypeDeclaration> types = unit.types(); TypeDeclaration r1 = types.get(2); assertEquals("Test_$1", NameTable.getFullName(r1)); boolean found = false; for (FieldDeclaration field : r1.getFields()) { List<VariableDeclarationFragment> vars = field.fragments(); for (VariableDeclaration var : vars) { if (var.getName().getIdentifier().equals("val$i")) { found = true; } } } assertTrue("required field not found", found); // Verify constructor takes both outer field and var. ObjectiveCImplementationGenerator.generate("Test.java", Language.OBJECTIVE_C, unit, source); String translation = getTranslatedFile("Test.m"); assertTranslation( translation, "r = [[[Test_$1 alloc] " + "initWithTest:self withJavaLangInteger:i] autorelease]"); }
/** * Generate an Objective-C implementation file for each type declared in a specified compilation * unit. */ public static void generate(String fileName, CompilationUnit unit, String source) { ObjectiveCImplementationGenerator implementationGenerator = new ObjectiveCImplementationGenerator(fileName, unit, source); implementationGenerator.generate(unit); }