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]"); }
public void generate(CompilationUnit unit) { println(J2ObjC.getFileHeader(getSourceFileName())); List<AbstractTypeDeclaration> typesToGenerate = collectTypes(unit); if (!typesToGenerate.isEmpty()) { findBlockComments(unit, typesToGenerate); findInvokedConstructors(unit); printStart(getSourceFileName()); printImports(unit); pushIgnoreDeprecatedDeclarationsPragma(); for (AbstractTypeDeclaration type : typesToGenerate) { generate(type); } popIgnoreDeprecatedDeclarationsPragma(); } else { // Print a dummy C function so compiled object file is valid. List<AbstractTypeDeclaration> types = ASTUtil.getTypes(unit); if (!types.isEmpty()) { printf("void %s_unused() {}\n", NameTable.getFullName(types.get(0))); } } save(unit); }
/** * 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); }