Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Unified Diff: lib/src/util/dart_type_utilities.dart

Issue 2355343002: Fixed cast exception (issue 27405) (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/util/dart_type_utilities.dart
diff --git a/lib/src/util/dart_type_utilities.dart b/lib/src/util/dart_type_utilities.dart
index 54a1918e9597f2b035600c3809591165d3feb6bc..72691af94dfc60ac0c23608bbdd7a65c47f4fa1a 100644
--- a/lib/src/util/dart_type_utilities.dart
+++ b/lib/src/util/dart_type_utilities.dart
@@ -5,40 +5,28 @@
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 &&
@@ -46,10 +34,13 @@ class DartTypeUtilities {
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 &&
@@ -57,13 +48,6 @@ class DartTypeUtilities {
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) {
@@ -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 {
@@ -83,6 +90,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)) {
return true;
@@ -91,9 +103,4 @@ class InterfaceTypeDefinition {
this.name == other.name &&
this.library == other.library;
}
-
- @override
- int get hashCode {
- return name.hashCode ^ library.hashCode;
- }
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698