Example usage for javax.lang.model.element VariableElement equals

List of usage examples for javax.lang.model.element VariableElement equals

Introduction

In this page you can find the example usage for javax.lang.model.element VariableElement equals.

Prototype

@Override
boolean equals(Object obj);

Source Link

Document

Returns true if the argument represents the same element as this , or false otherwise.

Usage

From source file:io.realm.processor.RealmProxyClassGenerator.java

private void emitValidateTableMethod(JavaWriter writer) throws IOException {
    writer.beginMethod("void", // Return type
            "validateTable", // Method name
            EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), // Modifiers
            "ImplicitTransaction", "transaction"); // Argument type & argument name

    writer.beginControlFlow("if (transaction.hasTable(\"" + Constants.TABLE_PREFIX + this.className + "\"))");
    writer.emitStatement("Table table = transaction.getTable(\"%s%s\")", Constants.TABLE_PREFIX,
            this.className);

    // verify number of columns
    writer.beginControlFlow("if (table.getColumnCount() != " + metadata.getFields().size() + ")");
    writer.emitStatement(//ww w.jav  a  2  s .  com
            "throw new RealmMigrationNeededException(transaction.getPath(), \"Field count does not match - expected %d but was \" + table.getColumnCount())",
            metadata.getFields().size());
    writer.endControlFlow();

    // create type dictionary for lookup
    writer.emitStatement("Map<String, ColumnType> columnTypes = new HashMap<String, ColumnType>()");
    writer.beginControlFlow("for (long i = 0; i < " + metadata.getFields().size() + "; i++)");
    writer.emitStatement("columnTypes.put(table.getColumnName(i), table.getColumnType(i))");
    writer.endControlFlow();

    // Populate column indices
    writer.emitEmptyLine();
    writer.emitStatement("columnIndices = new HashMap<String, Long>()");
    writer.beginControlFlow("for (String fieldName : getFieldNames())")
            .emitStatement("long index = table.getColumnIndex(fieldName)").beginControlFlow("if (index == -1)")
            .emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Field '\" + fieldName + \"' not found for type %s\")",
                    metadata.getSimpleClassName())
            .endControlFlow().emitStatement("columnIndices.put(fieldName, index)").endControlFlow();
    for (VariableElement field : metadata.getFields()) {
        writer.emitStatement("%s = table.getColumnIndex(\"%s\")", staticFieldIndexVarName(field),
                field.getSimpleName().toString());
    }
    writer.emitEmptyLine();

    // For each field verify there is a corresponding
    long fieldIndex = 0;
    for (VariableElement field : metadata.getFields()) {
        String fieldName = field.getSimpleName().toString();
        String fieldTypeCanonicalName = field.asType().toString();
        String fieldTypeSimpleName = Utils.getFieldTypeSimpleName(field);

        if (JAVA_TO_REALM_TYPES.containsKey(fieldTypeCanonicalName)) {
            // make sure types align
            writer.beginControlFlow("if (!columnTypes.containsKey(\"%s\"))", fieldName);
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Missing field '%s'\")",
                    fieldName);
            writer.endControlFlow();
            writer.beginControlFlow("if (columnTypes.get(\"%s\") != %s)", fieldName,
                    JAVA_TO_COLUMN_TYPES.get(fieldTypeCanonicalName));
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Invalid type '%s' for field '%s'\")",
                    fieldTypeSimpleName, fieldName);
            writer.endControlFlow();

            // Validate @PrimaryKey
            if (field.equals(metadata.getPrimaryKey())) {
                writer.beginControlFlow("if (table.getPrimaryKey() != table.getColumnIndex(\"%s\"))",
                        fieldName);
                writer.emitStatement(
                        "throw new RealmMigrationNeededException(transaction.getPath(), \"Primary key not defined for field '%s'\")",
                        fieldName);
                writer.endControlFlow();
            }

            // Validate @Index
            if (metadata.getIndexedFields().contains(field)) {
                writer.beginControlFlow("if (!table.hasSearchIndex(table.getColumnIndex(\"%s\")))", fieldName);
                writer.emitStatement(
                        "throw new RealmMigrationNeededException(transaction.getPath(), \"Index not defined for field '%s'\")",
                        fieldName);
                writer.endControlFlow();
            }

        } else if (typeUtils.isAssignable(field.asType(), realmObject)) { // Links
            writer.beginControlFlow("if (!columnTypes.containsKey(\"%s\"))", fieldName);
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Missing field '%s'\")",
                    fieldName);
            writer.endControlFlow();
            writer.beginControlFlow("if (columnTypes.get(\"%s\") != ColumnType.LINK)", fieldName);
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Invalid type '%s' for field '%s'\")",
                    fieldTypeSimpleName, fieldName);
            writer.endControlFlow();
            writer.beginControlFlow("if (!transaction.hasTable(\"%s%s\"))", Constants.TABLE_PREFIX,
                    fieldTypeSimpleName);
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Missing class '%s%s' for field '%s'\")",
                    Constants.TABLE_PREFIX, fieldTypeSimpleName, fieldName);
            writer.endControlFlow();

            writer.emitStatement("Table table_%d = transaction.getTable(\"%s%s\")", fieldIndex,
                    Constants.TABLE_PREFIX, fieldTypeSimpleName);
            writer.beginControlFlow("if (!table.getLinkTarget(%s).hasSameSchema(table_%d))",
                    staticFieldIndexVarName(field), fieldIndex);
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Invalid RealmObject for field '%s': '\" + table.getLinkTarget(%s).getName() + \"' expected - was '\" + table_%d.getName() + \"'\")",
                    fieldName, staticFieldIndexVarName(field), fieldIndex);
            writer.endControlFlow();
        } else if (typeUtils.isAssignable(field.asType(), realmList)) { // Link Lists
            String genericType = Utils.getGenericType(field);
            writer.beginControlFlow("if (!columnTypes.containsKey(\"%s\"))", fieldName);
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Missing field '%s'\")",
                    fieldName);
            writer.endControlFlow();
            writer.beginControlFlow("if (columnTypes.get(\"%s\") != ColumnType.LINK_LIST)", fieldName);
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Invalid type '%s' for field '%s'\")",
                    genericType, fieldName);
            writer.endControlFlow();
            writer.beginControlFlow("if (!transaction.hasTable(\"%s%s\"))", Constants.TABLE_PREFIX,
                    genericType);
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Missing class '%s%s' for field '%s'\")",
                    Constants.TABLE_PREFIX, genericType, fieldName);
            writer.endControlFlow();

            writer.emitStatement("Table table_%d = transaction.getTable(\"%s%s\")", fieldIndex,
                    Constants.TABLE_PREFIX, genericType);
            writer.beginControlFlow("if (!table.getLinkTarget(%s).hasSameSchema(table_%d))",
                    staticFieldIndexVarName(field), fieldIndex);
            writer.emitStatement(
                    "throw new RealmMigrationNeededException(transaction.getPath(), \"Invalid RealmList type for field '%s': '\" + table.getLinkTarget(%s).getName() + \"' expected - was '\" + table_%d.getName() + \"'\")",
                    fieldName, staticFieldIndexVarName(field), fieldIndex);
            writer.endControlFlow();
        }
        fieldIndex++;
    }

    writer.nextControlFlow("else");
    writer.emitStatement(
            "throw new RealmMigrationNeededException(transaction.getPath(), \"The %s class is missing from the schema for this Realm.\")",
            metadata.getSimpleClassName());
    writer.endControlFlow();
    writer.endMethod();
    writer.emitEmptyLine();
}