org.chromium.sdk/src/org/chromium/sdk/internal/protocolparser/dynamicimpl/JsonProtocolParser.java
changeset 355 8726e95bcbba
parent 2 e4420d2515f1
equal deleted inserted replaced
354:0bceeb415e7f 355:8726e95bcbba
   372 
   372 
   373         boolean isOptional = isOptionalField(m);
   373         boolean isOptional = isOptionalField(m);
   374 
   374 
   375         if (fieldTypeParser.asQuickParser() != null) {
   375         if (fieldTypeParser.asQuickParser() != null) {
   376           LazyParseFieldMethodHandler onDemandHandler = new LazyParseFieldMethodHandler(
   376           LazyParseFieldMethodHandler onDemandHandler = new LazyParseFieldMethodHandler(
   377               fieldTypeParser.asQuickParser(), isOptional, fieldName);
   377               fieldTypeParser.asQuickParser(), isOptional, fieldName, typeClass);
   378           onDemandHanlers.add(onDemandHandler);
   378           onDemandHanlers.add(onDemandHandler);
   379           methodHandler = onDemandHandler;
   379           methodHandler = onDemandHandler;
   380         } else {
   380         } else {
   381           int fieldCode = allocateFieldInArray();
   381           int fieldCode = allocateFieldInArray();
   382           FieldLoader fieldLoader = new FieldLoader(fieldCode, fieldName, fieldTypeParser,
   382           FieldLoader fieldLoader = new FieldLoader(fieldCode, fieldName, fieldTypeParser,
   532 
   532 
   533   private static class LazyParseFieldMethodHandler extends MethodHandler {
   533   private static class LazyParseFieldMethodHandler extends MethodHandler {
   534     private final QuickParser<?> quickParser;
   534     private final QuickParser<?> quickParser;
   535     private final boolean isOptional;
   535     private final boolean isOptional;
   536     private final String fieldName;
   536     private final String fieldName;
   537 
   537     private final Class<?> typeClass;
   538     LazyParseFieldMethodHandler(QuickParser<?> quickParser, boolean isOptional, String fieldName) {
   538 
       
   539     LazyParseFieldMethodHandler(QuickParser<?> quickParser, boolean isOptional, String fieldName,
       
   540         Class<?> typeClass) {
   539       this.quickParser = quickParser;
   541       this.quickParser = quickParser;
   540       this.isOptional = isOptional;
   542       this.isOptional = isOptional;
   541       this.fieldName = fieldName;
   543       this.fieldName = fieldName;
       
   544       this.typeClass = typeClass;
   542     }
   545     }
   543 
   546 
   544     @Override
   547     @Override
   545     Object handle(Object myself, ObjectData objectData, Object[] args) {
   548     Object handle(Object myself, ObjectData objectData, Object[] args) {
   546       try {
   549       try {
   547         return parse(objectData);
   550         return parse(objectData);
   548       } catch (JsonProtocolParseException e) {
   551       } catch (JsonProtocolParseException e) {
   549         throw new JsonProtocolParseRuntimeException("On demand parsing failed", e);
   552         throw new JsonProtocolParseRuntimeException(
       
   553             "On demand parsing failed for " + objectData.getUnderlyingObject(), e);
   550       }
   554       }
   551     }
   555     }
   552 
   556 
   553     public Object parse(ObjectData objectData) throws JsonProtocolParseException {
   557     public Object parse(ObjectData objectData) throws JsonProtocolParseException {
   554       Map<?,?> properties = (JSONObject)objectData.getUnderlyingObject();
   558       Map<?,?> properties = (JSONObject)objectData.getUnderlyingObject();
   566         throws JsonProtocolParseException {
   570         throws JsonProtocolParseException {
   567       if (hasValue) {
   571       if (hasValue) {
   568         try {
   572         try {
   569           return quickParser.parseValueQuick(value);
   573           return quickParser.parseValueQuick(value);
   570         } catch (JsonProtocolParseException e) {
   574         } catch (JsonProtocolParseException e) {
   571           throw new JsonProtocolParseException("Failed to parse field " + fieldName, e);
   575           throw new JsonProtocolParseException("Failed to parse field " + fieldName + " in type " +
       
   576               typeClass.getName(), e);
   572         }
   577         }
   573       } else {
   578       } else {
   574         if (!isOptional) {
   579         if (!isOptional) {
   575           throw new JsonProtocolParseException("Field is not optional: " + fieldName);
   580           throw new JsonProtocolParseException("Field is not optional: " + fieldName +
       
   581               " (in type " + typeClass.getName() + ")");
   576         }
   582         }
   577         return null;
   583         return null;
   578       }
   584       }
   579     }
   585     }
   580   }
   586   }