Skip to content

Commit

Permalink
Fixed cast exception (issue 27405)
Browse files Browse the repository at this point in the history
R=pquitslund@google.com, scheglov@google.com

Review URL: https://codereview.chromium.org//2355343002 .
  • Loading branch information
bwilkerson committed Sep 21, 2016
1 parent 407d736 commit 6968141
Showing 1 changed file with 49 additions and 42 deletions.
91 changes: 49 additions & 42 deletions lib/src/util/dart_type_utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,49 @@
library linter.src.util.dart_type_utilities;

import 'dart:collection';

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/ast/ast.dart';

typedef bool AstNodePredicate(AstNode node);

class DartTypeUtilities {
static bool unrelatedTypes(DartType leftType, DartType rightType) {
if (leftType == null ||
leftType.isBottom ||
leftType.isDynamic ||
rightType == null ||
rightType.isBottom ||
rightType.isDynamic) {
return false;
}
if (leftType == rightType ||
leftType.isMoreSpecificThan(rightType) ||
rightType.isMoreSpecificThan(leftType)) {
static bool extendsClass(DartType type, String className, String library) =>
type != null &&
type.name == className &&
type.element.library.name == library ||
(type is InterfaceType &&
extendsClass(type.superclass, className, library));

static bool implementsAnyInterface(
DartType type, Iterable<InterfaceTypeDefinition> definitions) {
if (type is! InterfaceType) {
return false;
}
Element leftElement = leftType.element;
Element rightElement = rightType.element;
if (leftElement is ClassElement && rightElement is ClassElement) {
return leftElement.supertype.isObject ||
leftElement.supertype != rightElement.supertype;
}
return false;
}

static bool implementsInterface(
DartType type, String interface, String library) {
bool predicate(InterfaceType i) =>
i.name == interface && i.element.library.name == library;
bool predicate(InterfaceType i) => definitions
.any((d) => i.name == d.name && i.element.library.name == d.library);
ClassElement element = type.element;
return predicate(type) ||
!element.isSynthetic &&
type is InterfaceType &&
element.allSupertypes.any(predicate);
}

static bool implementsAnyInterface(
DartType type, Iterable<InterfaceTypeDefinition> definitions) {
bool predicate(InterfaceType i) => definitions
.any((d) => i.name == d.name && i.element.library.name == d.library);
static bool implementsInterface(
DartType type, String interface, String library) {
if (type is! InterfaceType) {
return false;
}
bool predicate(InterfaceType i) =>
i.name == interface && i.element.library.name == library;
ClassElement element = type.element;
return predicate(type) ||
!element.isSynthetic &&
type is InterfaceType &&
element.allSupertypes.any(predicate);
}

static bool extendsClass(DartType type, String className, String library) =>
type != null &&
type.name == className &&
type.element.library.name == library ||
(type is InterfaceType &&
extendsClass(type.superclass, className, library));

/// Builds the list resulting from traversing the node in DFS and does not
/// include the node itself.
static Iterable<AstNode> traverseNodesInDFS(AstNode node) {
Expand All @@ -74,6 +58,29 @@ class DartTypeUtilities {
});
return nodes;
}

static bool unrelatedTypes(DartType leftType, DartType rightType) {
if (leftType == null ||
leftType.isBottom ||
leftType.isDynamic ||
rightType == null ||
rightType.isBottom ||
rightType.isDynamic) {
return false;
}
if (leftType == rightType ||
leftType.isMoreSpecificThan(rightType) ||
rightType.isMoreSpecificThan(leftType)) {
return false;
}
Element leftElement = leftType.element;
Element rightElement = rightType.element;
if (leftElement is ClassElement && rightElement is ClassElement) {
return leftElement.supertype.isObject ||
leftElement.supertype != rightElement.supertype;
}
return false;
}
}

class InterfaceTypeDefinition {
Expand All @@ -82,6 +89,11 @@ class InterfaceTypeDefinition {

InterfaceTypeDefinition(this.name, this.library);

@override
int get hashCode {
return name.hashCode ^ library.hashCode;
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
Expand All @@ -91,9 +103,4 @@ class InterfaceTypeDefinition {
this.name == other.name &&
this.library == other.library;
}

@override
int get hashCode {
return name.hashCode ^ library.hashCode;
}
}

0 comments on commit 6968141

Please sign in to comment.