Search in sources :

Example 1 with Options

use of com.google.devtools.j2objc.Options 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);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) Options(com.google.devtools.j2objc.Options) Import(com.google.devtools.j2objc.types.Import) TypeElement(javax.lang.model.element.TypeElement) ImmutableList(com.google.common.collect.ImmutableList) HeaderImportCollector(com.google.devtools.j2objc.types.HeaderImportCollector) ImplementationImportCollector(com.google.devtools.j2objc.types.ImplementationImportCollector) ImmutableSet(com.google.common.collect.ImmutableSet) NameTable(com.google.devtools.j2objc.util.NameTable)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Options (com.google.devtools.j2objc.Options)1 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)1 HeaderImportCollector (com.google.devtools.j2objc.types.HeaderImportCollector)1 ImplementationImportCollector (com.google.devtools.j2objc.types.ImplementationImportCollector)1 Import (com.google.devtools.j2objc.types.Import)1 NameTable (com.google.devtools.j2objc.util.NameTable)1 TypeElement (javax.lang.model.element.TypeElement)1