View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package com.addthis.codec.jackson;
15  
16  import java.util.Collection;
17  
18  import com.fasterxml.jackson.core.JsonLocation;
19  import com.fasterxml.jackson.core.JsonParser;
20  import com.fasterxml.jackson.databind.exc.PropertyBindingException;
21  
22  public class MissingPropertyException extends PropertyBindingException {
23  
24      public MissingPropertyException(String msg,
25                                      JsonLocation loc,
26                                      Class<?> referringClass,
27                                      String propName,
28                                      Collection<Object> propertyIds) {
29          super(msg, loc, referringClass, propName, propertyIds);
30      }
31  
32      public static MissingPropertyException from(JsonParser jp,
33                                                  Object fromObjectOrClass, String propertyName,
34                                                  Collection<Object> propertyIds) {
35          if (fromObjectOrClass == null) {
36              throw new IllegalArgumentException();
37          }
38          Class<?> ref;
39          if (fromObjectOrClass instanceof Class<?>) {
40              ref = (Class<?>) fromObjectOrClass;
41          } else {
42              ref = fromObjectOrClass.getClass();
43          }
44          String msg = "Missing field \""+propertyName+"\" (class "+ref.getName()+"), marked as required";
45          MissingPropertyException e = new MissingPropertyException(
46                  msg, jp.getCurrentLocation(), ref, propertyName, propertyIds);
47          // but let's also ensure path includes this last (missing) segment
48          e.prependPath(fromObjectOrClass, propertyName);
49          return e;
50      }
51  }