use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class TypeDeclarationGeneratorTest method testEmptyStatementsIgnored.
// Verify that an empty statement following a type declaration is ignored.
// The JDT parser discards them, while javac includes them in the compilation unit.
public void testEmptyStatementsIgnored() throws IOException {
String source = "public interface A { void bar(); };";
CompilationUnit unit = translateType("A", source);
assertEquals(1, unit.getTypes().size());
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class InnerClassExtractorTest method testMultipleThisReferencesWithPreviousReference.
/**
* This test differs from the last one only in the addition of another
* 'this' reference before the anonymous class creation.
*/
public void testMultipleThisReferencesWithPreviousReference() throws IOException {
String source = "class A { private int x = 0; " + " interface Foo { void doSomething(); } " + " class Inner { private int x = 1; " + " public void blah() { " + " A.this.x = 2; " + " new Foo() { public void doSomething() { " + " Inner.this.x = 3; A.this.x = 4; }}; }}}";
CompilationUnit unit = translateType("A", source);
List<AbstractTypeDeclaration> types = unit.getTypes();
assertEquals(4, types.size());
String translation = translateSourceFile(source, "A", "A.m");
// Anonymous class constructor in Inner.blah()
assertTranslation(translation, "create_A_Inner_1_initWithA_Inner_(self)");
// A.x referred to in A.Inner.
assertTranslation(translation, "this$0_->x_ = 2");
// A.Inner.x referred to in anonymous Foo.
assertTranslation(translation, "this$0_->x_ = 3");
// A.x referred to in anonymous Foo
assertTranslation(translation, "this$0_->this$0_->x_ = 4");
// A.Inner init in anonymous Foo's constructor
assertTranslation(translation, "JreStrongAssign(&self->this$0_, outer$)");
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class InnerClassExtractorTest method testMultipleThisReferences.
public void testMultipleThisReferences() throws IOException {
String source = "class A { private int x = 0; " + " interface Foo { void doSomething(); } " + " class Inner { private int x = 1; " + " public void blah() { " + " new Foo() { public void doSomething() { " + " Inner.this.x = 2; A.this.x = 3; }}; }}}";
CompilationUnit unit = translateType("A", source);
List<AbstractTypeDeclaration> types = unit.getTypes();
assertEquals(4, types.size());
String translation = translateSourceFile(source, "A", "A.m");
// Anonymous class constructor in Inner.blah()
assertTranslation(translation, "create_A_Inner_1_initWithA_Inner_(self)");
// A.Inner.x referred to in anonymous Foo
assertTranslation(translation, "this$0_->x_ = 2");
// A.x referred to in anonymous Foo
assertTranslation(translation, "this$0_->this$0_->x_ = 3");
// A.Inner init in anonymous Foo's constructor
assertTranslation(translation, "JreStrongAssign(&self->this$0_, outer$)");
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class InnerClassExtractor method endHandleType.
private void endHandleType(AbstractTypeDeclaration node) {
int insertIdx = typeOrderStack.remove(typeOrderStack.size() - 1);
TreeNode parentNode = node.getParent();
if (!(parentNode instanceof CompilationUnit)) {
// Remove this type declaration from its current location.
node.remove();
if (parentNode instanceof TypeDeclarationStatement) {
parentNode.remove();
}
addCaptureFields(node);
// Make this node non-private, if necessary, and add it to the unit's type
// list.
node.removeModifiers(Modifier.PRIVATE);
unitTypes.add(insertIdx, node);
// Check for erroneous WeakOuter annotation on static inner class.
TypeElement type = node.getTypeElement();
if (ElementUtil.isStatic(type) && ElementUtil.hasAnnotation(type, WeakOuter.class)) {
ErrorUtil.warning("static class " + type.getQualifiedName() + " has WeakOuter annotation");
}
}
}
use of com.google.devtools.j2objc.ast.CompilationUnit in project j2objc by google.
the class GeneratedType method fromTypeDeclaration.
public static GeneratedType fromTypeDeclaration(AbstractTypeDeclaration typeNode) {
TypeElement typeElement = typeNode.getTypeElement();
CompilationUnit unit = TreeUtil.getCompilationUnit(typeNode);
NameTable nameTable = unit.getEnv().nameTable();
boolean emitLineDirectives = unit.getEnv().options().emitLineDirectives();
ImmutableList.Builder<String> superTypes = ImmutableList.builder();
TypeElement superclass = ElementUtil.getSuperclass(typeElement);
if (superclass != null) {
superTypes.add(nameTable.getFullName(superclass));
}
for (TypeElement superInterface : ElementUtil.getInterfaces(typeElement)) {
superTypes.add(nameTable.getFullName(superInterface));
}
HeaderImportCollector headerCollector = new HeaderImportCollector(unit, HeaderImportCollector.Filter.PUBLIC_ONLY);
typeNode.accept(headerCollector);
HeaderImportCollector privateDeclarationCollector = new HeaderImportCollector(unit, HeaderImportCollector.Filter.PRIVATE_ONLY);
typeNode.accept(privateDeclarationCollector);
ImplementationImportCollector importCollector = new ImplementationImportCollector(unit);
typeNode.accept(importCollector);
SourceBuilder builder = new SourceBuilder(emitLineDirectives);
TypeDeclarationGenerator.generate(builder, typeNode);
String publicDeclarationCode = builder.toString();
builder = new SourceBuilder(emitLineDirectives);
TypePrivateDeclarationGenerator.generate(builder, typeNode);
String privateDeclarationCode;
String implementationCode;
Options options = unit.getEnv().options();
if (unit.getEnv().translationUtil().generateImplementation(typeElement)) {
builder = new SourceBuilder(options.emitLineDirectives());
TypePrivateDeclarationGenerator.generate(builder, typeNode);
privateDeclarationCode = builder.toString();
builder = new SourceBuilder(options.emitLineDirectives());
TypeImplementationGenerator.generate(builder, typeNode);
implementationCode = builder.toString();
} else {
privateDeclarationCode = "";
implementationCode = String.format("// Implementation not generated because %s is on the bootclasspath.\n", ElementUtil.getQualifiedName(typeElement));
}
ImmutableSet.Builder<Import> implementationIncludes = ImmutableSet.builder();
implementationIncludes.addAll(privateDeclarationCollector.getSuperTypes());
implementationIncludes.addAll(importCollector.getImports());
return new GeneratedType(nameTable.getFullName(typeElement), typeNode.hasPrivateDeclaration(), superTypes.build(), ImmutableSet.copyOf(headerCollector.getForwardDeclarations()), ImmutableSet.copyOf(headerCollector.getSuperTypes()), ImmutableSet.copyOf(privateDeclarationCollector.getForwardDeclarations()), implementationIncludes.build(), publicDeclarationCode, privateDeclarationCode, implementationCode);
}
Aggregations