import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import javax.annotation.RegEx;
+import org.checkerframework.checker.regex.qual.Regex;
import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
import org.opendaylight.yangtools.util.ClassLoaderUtils;
import org.opendaylight.yangtools.yang.binding.Action;
import org.opendaylight.yangtools.yang.binding.Augmentable;
import org.opendaylight.yangtools.yang.binding.Augmentation;
+import org.opendaylight.yangtools.yang.binding.AugmentationHolder;
import org.opendaylight.yangtools.yang.binding.BaseIdentity;
import org.opendaylight.yangtools.yang.binding.ChildOf;
import org.opendaylight.yangtools.yang.binding.DataContainer;
private static final long EXPIRATION_TIME = 60;
- @RegEx
+ @Regex
private static final String ROOT_PACKAGE_PATTERN_STRING =
"(org.opendaylight.yang.gen.v1.[a-z0-9_\\.]*\\.(?:rev[0-9][0-9][0-1][0-9][0-3][0-9]|norev))";
private static final Pattern ROOT_PACKAGE_PATTERN = Pattern.compile(ROOT_PACKAGE_PATTERN_STRING);
.expireAfterAccess(EXPIRATION_TIME, TimeUnit.SECONDS)
.build(new ClassToQNameLoader());
+ private static final LoadingCache<ClassLoader, ImmutableSet<YangModuleInfo>> MODULE_INFO_CACHE =
+ CacheBuilder.newBuilder().weakKeys().weakValues().build(
+ new CacheLoader<ClassLoader, ImmutableSet<YangModuleInfo>>() {
+ @Override
+ public ImmutableSet<YangModuleInfo> load(final ClassLoader key) {
+ return loadModuleInfos(key);
+ }
+ });
+
private BindingReflections() {
throw new UnsupportedOperationException("Utility class.");
}
*/
public static Class<? extends Augmentable<?>> findAugmentationTarget(
final Class<? extends Augmentation<?>> augmentation) {
- return ClassLoaderUtils.findFirstGenericArgument(augmentation, Augmentation.class);
+ final Optional<Class<Augmentable<?>>> opt = ClassLoaderUtils.findFirstGenericArgument(augmentation,
+ Augmentation.class);
+ return opt.orElse(null);
}
/**
* @return Parent class, e.g. class of which the childClass is ChildOf.
*/
public static Class<?> findHierarchicalParent(final Class<? extends ChildOf<?>> childClass) {
- return ClassLoaderUtils.findFirstGenericArgument(childClass, ChildOf.class);
+ return ClassLoaderUtils.findFirstGenericArgument(childClass, ChildOf.class).orElse(null);
}
/**
* Find data hierarchy parent from concrete Data class. This method is shorthand which gets DataObject class by
- * invoking {@link DataObject#getImplementedInterface()} and uses {@link #findHierarchicalParent(Class)}.
+ * invoking {@link DataObject#implementedInterface()} and uses {@link #findHierarchicalParent(Class)}.
*
* @param child
* Child object for which the parent needs to be located.
*/
public static Class<?> findHierarchicalParent(final DataObject child) {
if (child instanceof ChildOf) {
- return ClassLoaderUtils.findFirstGenericArgument(child.getImplementedInterface(), ChildOf.class);
+ return ClassLoaderUtils.findFirstGenericArgument(child.implementedInterface(), ChildOf.class).orElse(null);
}
return null;
}
public static Optional<Class<?>> resolveRpcOutputClass(final Method targetMethod) {
checkState(isRpcMethod(targetMethod), "Supplied method is not a RPC invocation method");
Type futureType = targetMethod.getGenericReturnType();
- Type rpcResultType = ClassLoaderUtils.getFirstGenericParameter(futureType);
- Type rpcResultArgument = ClassLoaderUtils.getFirstGenericParameter(rpcResultType);
+ Type rpcResultType = ClassLoaderUtils.getFirstGenericParameter(futureType).orElse(null);
+ Type rpcResultArgument = ClassLoaderUtils.getFirstGenericParameter(rpcResultType).orElse(null);
if (rpcResultArgument instanceof Class && !Void.class.equals(rpcResultArgument)) {
return Optional.of((Class) rpcResultArgument);
}
* When {@link YangModuleInfo} is available, all dependencies are recursively collected into returning set by
* collecting results of {@link YangModuleInfo#getImportedModules()}.
*
- * @param loader
- * Classloader for which {@link YangModuleInfo} should be
- * retrieved.
+ * <p>
+ * Consider using {@link #cacheModuleInfos(ClassLoader)} if the classloader is known to be immutable.
+ *
+ * @param loader Classloader for which {@link YangModuleInfo} should be retrieved.
* @return Set of {@link YangModuleInfo} available for supplied classloader.
*/
public static ImmutableSet<YangModuleInfo> loadModuleInfos(final ClassLoader loader) {
return moduleInfoSet.build();
}
+ /**
+ * Loads {@link YangModuleInfo} instances available on supplied {@link ClassLoader}, assuming the set of available
+ * information does not change. Subsequent accesses may return cached values.
+ *
+ * <p>
+ * {@link YangModuleInfo} are discovered using {@link ServiceLoader} for {@link YangModelBindingProvider}.
+ * {@link YangModelBindingProvider} are simple classes which holds only pointers to actual instance
+ * {@link YangModuleInfo}.
+ *
+ * <p>
+ * When {@link YangModuleInfo} is available, all dependencies are recursively collected into returning set by
+ * collecting results of {@link YangModuleInfo#getImportedModules()}.
+ *
+ * @param loader Class loader for which {@link YangModuleInfo} should be retrieved.
+ * @return Set of {@link YangModuleInfo} available for supplied classloader.
+ */
+ @Beta
+ public static ImmutableSet<YangModuleInfo> cacheModuleInfos(final ClassLoader loader) {
+ return MODULE_INFO_CACHE.getUnchecked(loader);
+ }
+
private static void collectYangModuleInfo(final YangModuleInfo moduleInfo,
final Builder<YangModuleInfo> moduleInfoSet) {
moduleInfoSet.add(moduleInfo);
if (List.class.isAssignableFrom(returnType)) {
try {
return ClassLoaderUtils.callWithClassLoader(method.getDeclaringClass().getClassLoader(), () -> {
- Type listResult = ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType());
- if (listResult instanceof Class && DataContainer.class.isAssignableFrom((Class) listResult)) {
- return Optional.of((Class) listResult);
- }
- return Optional.empty();
+ return ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType()).flatMap(
+ result -> result instanceof Class && DataContainer.class.isAssignableFrom((Class) result)
+ ? Optional.of((Class) result) : Optional.empty());
});
} catch (Exception e) {
/*
* @return Map of augmentations if read was successful, otherwise empty map.
*/
public static Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Augmentable<?> input) {
+ if (input instanceof AugmentationHolder) {
+ return ((AugmentationHolder) input).augmentations();
+ }
return AugmentationFieldGetter.getGetter(input.getClass()).getAugmentations(input);
}