Enable spotbugs in mdsal-binding-dom-codec
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / MissingSchemaForClassException.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
10 import com.google.common.base.Preconditions;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
13 import org.opendaylight.yangtools.yang.binding.Augmentation;
14
15 /**
16  * Thrown when Java Binding class was used in data for which codec does not have schema.
17  *
18  * <p>
19  * By serialization /  deserialization of this exception {@link #getBindingClass()} will return null.
20  */
21 public final class MissingSchemaForClassException extends MissingSchemaException {
22
23     private static final long serialVersionUID = 1L;
24
25     @SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
26     private final transient Class<?> bindingClass;
27
28     private MissingSchemaForClassException(final Class<?> clz) {
29         super(String.format("Schema is not available for %s", clz));
30         this.bindingClass = Preconditions.checkNotNull(clz);
31     }
32
33     static MissingSchemaForClassException forClass(final Class<?> clz) {
34         return new MissingSchemaForClassException(clz);
35     }
36
37     public Class<?> getBindingClass() {
38         return bindingClass;
39     }
40
41     public static void check(final BindingRuntimeContext runtimeContext, final Class<?> bindingClass) {
42         final Object schema;
43         if (Augmentation.class.isAssignableFrom(bindingClass)) {
44             schema = runtimeContext.getAugmentationDefinition(bindingClass);
45         } else {
46             schema = runtimeContext.getSchemaDefinition(bindingClass);
47         }
48         if (schema == null) {
49             throw MissingSchemaForClassException.forClass(bindingClass);
50         }
51     }
52 }