Refactor GeneratedClassLoadingStrategy
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / impl / GeneratedClassLoadingStrategy.java
1 /*
2  * Copyright (c) 2014 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.generator.impl;
9
10 import org.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
12 import org.opendaylight.mdsal.binding.model.api.Type;
13 import org.opendaylight.yangtools.util.ClassLoaderUtils;
14
15 public abstract class GeneratedClassLoadingStrategy implements ClassLoadingStrategy {
16     private static final class AlwaysFailClassLoadingStrategy extends GeneratedClassLoadingStrategy {
17         static final @NonNull AlwaysFailClassLoadingStrategy INSTANCE = new AlwaysFailClassLoadingStrategy();
18
19         @Override
20         public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
21             throw new ClassNotFoundException(fullyQualifiedName);
22         }
23     }
24
25     private static final class TCCLClassLoadingStrategy extends GeneratedClassLoadingStrategy {
26         static final @NonNull TCCLClassLoadingStrategy INSTANCE = new TCCLClassLoadingStrategy();
27
28         @Override
29         public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
30             return ClassLoaderUtils.loadClassWithTCCL(fullyQualifiedName);
31         }
32     }
33
34     protected GeneratedClassLoadingStrategy() {
35
36     }
37
38     public static final @NonNull GeneratedClassLoadingStrategy getTCCLClassLoadingStrategy() {
39         return TCCLClassLoadingStrategy.INSTANCE;
40     }
41
42     public static final @NonNull GeneratedClassLoadingStrategy getAlwaysFailClassLoadingStrategy() {
43         return AlwaysFailClassLoadingStrategy.INSTANCE;
44     }
45
46     @Override
47     public Class<?> loadClass(final Type type) throws ClassNotFoundException {
48         return loadClass(type.getFullyQualifiedName());
49     }
50 }