Avoid trapping exceptions by using reflection (#59)


diff --git a/lib/mustache_context.dart b/lib/mustache_context.dart
index b84395d..90be84a 100644
--- a/lib/mustache_context.dart
+++ b/lib/mustache_context.dart
@@ -124,12 +124,30 @@
   }
 
   dynamic _getActualValue(String key) {
-    try {
-      return ctx[key];
-    } catch (NoSuchMethodError) {
-      //Try to make dart2js understand that when we define USE_MIRRORS = false
-      //we do not want to use any reflector
-      return (useMirrors && USE_MIRRORS) ? ctxReflector[key] : null;
+    //Try to make dart2js understand that when we define USE_MIRRORS = false
+    //we do not want to use any reflector as that inflates the generated
+    //javascript.
+    if (useMirrors && USE_MIRRORS) {
+      if (reflect(ctx).type.instanceMembers.containsKey(new Symbol("[]"))) {
+        MethodMirror m = reflect(ctx).type.instanceMembers[new Symbol("[]")];
+        TypeMirror reflectedString = reflectType(String);
+        if (reflectedString.isAssignableTo(m.parameters[0].type)) {
+          try {
+            return ctx[key];
+          } catch (NoSuchMethodError) {
+            //This should never happen unless we were trapping a lower level
+            //NoSuchMethodError before.  Continue to do so to be bug-for-bug
+            //compatible.
+          }
+        }
+      }
+      return ctxReflector[key];
+    } else {
+      try {
+        return ctx[key];
+      } catch (NoSuchMethodError) {
+        return null;
+      }
     }
   }
 
diff --git a/lib/src/mustache.dart b/lib/src/mustache.dart
index 67270dd..400c017 100644
--- a/lib/src/mustache.dart
+++ b/lib/src/mustache.dart
@@ -8,10 +8,7 @@
     bool errorOnMissingProperty: false,
     bool assumeNullNonExistingProperty: true}) {
   return compile(template,
-      partial: partial,
-      delimiter: delimiter,
-      ident:
-          ident)(context,
+          partial: partial, delimiter: delimiter, ident: ident)(context,
       out: out,
       errorOnMissingProperty: errorOnMissingProperty,
       assumeNullNonExistingProperty: assumeNullNonExistingProperty);