/**
* Base Identity
- *
+ *
*/
public abstract class BaseIdentity {
- public final static BaseIdentity INSTANCE = new BaseIdentity() {
+ public static final BaseIdentity INSTANCE = new BaseIdentity() {
};
protected BaseIdentity() {
package org.opendaylight.yangtools.yang.binding;
import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
public static String getGetterSuffix(final QName name) {
checkArgument(name != null, "Name should not be null.");
final String candidate = toFirstUpper(toCamelCase(name.getLocalName()));
- return ("Class".equals(candidate) ? "XmlClass" : candidate);
+ return "Class".equals(candidate) ? "XmlClass" : candidate;
}
public static String getPropertyName(final String yangIdentifier) {
* <code>choice</code> <code>unkeyed list</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void leafNode(String localName, Object value) throws IOException, IllegalArgumentException;
+ void leafNode(String localName, Object value) throws IOException;
/**
*
* <code>choice</code> <code>unkeyed list</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void startLeafSet(String localName, int childSizeHint) throws IOException, IllegalArgumentException;
+ void startLeafSet(String localName, int childSizeHint) throws IOException;
/**
*
* <code>choice</code> <code>unkeyed list</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void startOrderedLeafSet(String localName, int childSizeHint) throws IOException, IllegalArgumentException;
+ void startOrderedLeafSet(String localName, int childSizeHint) throws IOException;
/**
* Emits a leaf set entry node
* If node was emitted outside <code>leaf set</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException;
+ void leafSetEntryNode(Object value) throws IOException;
/**
*
* <code>choice</code> <code>unkeyed list</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void startContainerNode(Class<? extends DataObject> container, int childSizeHint) throws IOException, IllegalArgumentException;
+ void startContainerNode(Class<? extends DataObject> container, int childSizeHint) throws IOException;
/**
*
* <code>choice</code> <code>unkeyed list</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void startUnkeyedList(Class<? extends DataObject> localName, int childSizeHint) throws IOException, IllegalArgumentException;
+ void startUnkeyedList(Class<? extends DataObject> localName, int childSizeHint) throws IOException;
/**
* Emits start of new unkeyed list item.
* If node was emitted outside <code>unkeyed list</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void startUnkeyedListItem(int childSizeHint) throws IOException, IllegalStateException;
+ void startUnkeyedListItem(int childSizeHint) throws IOException;
/**
*
* @throws IOException if an underlying IO error occurs
*/
<T extends DataObject & Identifiable<?>> void startMapNode(Class<T> mapEntryType, int childSizeHint)
- throws IOException, IllegalArgumentException;
+ throws IOException;
/**
*
* @throws IOException if an underlying IO error occurs
*/
<T extends DataObject & Identifiable<?>> void startOrderedMapNode(Class<T> mapEntryType, int childSizeHint)
- throws IOException, IllegalArgumentException;
+ throws IOException;
/**
*
* If node was emitted outside <code>map entry</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void startMapEntryNode(Identifier<?> keyValues, int childSizeHint) throws IOException, IllegalArgumentException;
+ void startMapEntryNode(Identifier<?> keyValues, int childSizeHint) throws IOException;
/**
* Emits start of choice node.
* <code>unkeyed list</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void startChoiceNode(Class<? extends DataContainer> choice, int childSizeHint) throws IOException, IllegalArgumentException;
+ void startChoiceNode(Class<? extends DataContainer> choice, int childSizeHint) throws IOException;
/**
*
* @throws IllegalArgumentException
* @throws IOException if an underlying IO error occurs
*/
- void startCase(Class<? extends DataObject> caze, int childSizeHint) throws IOException, IllegalArgumentException;
+ void startCase(Class<? extends DataObject> caze, int childSizeHint) throws IOException;
/**
* Emits start of augmentation node.
* If augmentation is invalid in current context.
* @throws IOException if an underlying IO error occurs
*/
- void startAugmentationNode(Class<? extends Augmentation<?>> augmentationType) throws IOException, IllegalArgumentException;
+ void startAugmentationNode(Class<? extends Augmentation<?>> augmentationType) throws IOException;
/**
* Emits anyxml node event.
* <code>choice</code> <code>unkeyed list</code> node.
* @throws IOException if an underlying IO error occurs
*/
- void anyxmlNode(String name, Object value) throws IOException, IllegalArgumentException;
+ void anyxmlNode(String name, Object value) throws IOException;
/**
* Emits end event for node.
* @throws IllegalStateException If there is no open node.
* @throws IOException if an underlying IO error occurs
*/
- void endNode() throws IOException, IllegalStateException;
-
+ void endNode() throws IOException;
@Override
void flush() throws IOException;
for (final PathArgument a : pathArguments) {
if (type.equals(a.getType())) {
@SuppressWarnings("unchecked")
- final InstanceIdentifier<I> ret = (InstanceIdentifier<I>) internalCreate(Iterables.limit(pathArguments, i));
+ final InstanceIdentifier<I> ret = (InstanceIdentifier<I>) internalCreate(
+ Iterables.limit(pathArguments, i));
return ret;
}
* @return A builder instance
*/
public InstanceIdentifierBuilder<T> builder() {
- return new InstanceIdentifierBuilderImpl<T>(new Item<T>(targetType), pathArguments, hash, isWildcarded());
+ return new InstanceIdentifierBuilderImpl<>(new Item<T>(targetType), pathArguments, hash, isWildcarded());
}
private InstanceIdentifier<?> childIdentifier(final PathArgument arg) {
Class<? extends DataObject> getType();
}
- private static abstract class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
+ private abstract static class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
private static final long serialVersionUID = 1L;
private final Class<T> type;
@Override
public final InstanceIdentifierBuilder<T> builder() {
- return new InstanceIdentifierBuilderImpl<T>(new InstanceIdentifier.IdentifiableItem<T, K>(getTargetType(), key), pathArguments, hashCode(), isWildcarded());
+ return new InstanceIdentifierBuilderImpl<>(new InstanceIdentifier.IdentifiableItem<T, K>(getTargetType(), key), pathArguments, hashCode(), isWildcarded());
}
@Override
}
};
+ private static final LoadingCache<Class<?>, AugmentationFieldGetter> AUGMENTATION_GETTERS = CacheBuilder.newBuilder().weakKeys().build(
+ new AugmentationGetterLoader());
+
/**
*
* Retrieves augmentations from supplied object
*/
protected abstract Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(final Object input);
- private static final LoadingCache<Class<?>, AugmentationFieldGetter> AUGMENTATION_GETTERS = CacheBuilder
- .newBuilder().weakKeys().build(new AugmentationGetterLoader());
-
public static AugmentationFieldGetter getGetter(final Class<? extends Object> clz) {
if(AugmentationHolder.class.isAssignableFrom(clz)) {
return AUGMENTATION_HOLDER_GETTER;
import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Sets;
import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.URI;
checkArgument(cls != null);
String packageName = getModelRootPackageName(cls.getPackage());
final String potentialClassName = getModuleInfoClassName(packageName);
- return ClassLoaderUtils.withClassLoader(cls.getClassLoader(), new Callable<YangModuleInfo>() {
- @Override
- public YangModuleInfo call() throws ClassNotFoundException, IllegalAccessException,
- IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
- Class<?> moduleInfoClass = Thread.currentThread().getContextClassLoader().loadClass(potentialClassName);
- return (YangModuleInfo) moduleInfoClass.getMethod("getInstance").invoke(null);
- }
- });
+ return ClassLoaderUtils.withClassLoader(cls.getClassLoader(), (Callable<YangModuleInfo>) () -> {
+ Class<?> moduleInfoClass = Thread.currentThread().getContextClassLoader().loadClass(potentialClassName);
+ return (YangModuleInfo) moduleInfoClass.getMethod("getInstance").invoke(null);
+ });
}
public static String getModuleInfoClassName(final String packageName) {
if (DataContainer.class.isAssignableFrom(cls) || Augmentation.class.isAssignableFrom(cls)) {
return true;
}
- return (cls.getName().startsWith(BindingMapping.PACKAGE_PREFIX));
+ return cls.getName().startsWith(BindingMapping.PACKAGE_PREFIX);
}
/**
return ret;
}
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({ "unchecked", "rawtypes" })
private static Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method) {
- if (method.getName().equals("getClass") || !method.getName().startsWith("get")
+ if ("getClass".equals(method.getName()) || !method.getName().startsWith("get")
|| method.getParameterTypes().length > 0) {
return Optional.absent();
}
} else if (List.class.isAssignableFrom(returnType)) {
try {
return ClassLoaderUtils.withClassLoader(method.getDeclaringClass().getClassLoader(),
- new Callable<Optional<Class<? extends DataContainer>>>() {
- @SuppressWarnings("rawtypes")
- @Override
- public Optional<Class<? extends DataContainer>> call() {
- Type listResult = ClassLoaderUtils.getFirstGenericParameter(method
- .getGenericReturnType());
- if (listResult instanceof Class
- && DataContainer.class.isAssignableFrom((Class) listResult)) {
- return Optional.<Class<? extends DataContainer>> of((Class) listResult);
- }
- return Optional.absent();
+ (Callable<Optional<Class<? extends DataContainer>>>) () -> {
+ Type listResult = ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType());
+ if (listResult instanceof Class
+ && DataContainer.class.isAssignableFrom((Class) listResult)) {
+ return Optional.<Class<? extends DataContainer>> of((Class) listResult);
}
-
+ return Optional.absent();
});
} catch (Exception e) {
/*
public Optional<QName> load(final Class<?> key) throws Exception {
return resolveQNameNoCache(key);
}
- }
- /**
- *
- * Tries to resolve QName for supplied class.
- *
- * Looks up for static field with name from constant
- * {@link BindingMapping#QNAME_STATIC_FIELD_NAME} and returns value if
- * present.
- *
- * If field is not present uses {@link #computeQName(Class)} to compute
- * QName for missing types.
- *
- * @param key
- * @return
- */
- private static Optional<QName> resolveQNameNoCache(final Class<?> key) {
- try {
- Field field = key.getField(BindingMapping.QNAME_STATIC_FIELD_NAME);
- Object obj = field.get(null);
- if (obj instanceof QName) {
- return Optional.of((QName) obj);
- }
+ /**
+ *
+ * Tries to resolve QName for supplied class.
+ *
+ * Looks up for static field with name from constant {@link BindingMapping#QNAME_STATIC_FIELD_NAME} and returns
+ * value if present.
+ *
+ * If field is not present uses {@link #computeQName(Class)} to compute QName for missing types.
+ *
+ * @param key
+ * @return
+ */
+ private static Optional<QName> resolveQNameNoCache(final Class<?> key) {
+ try {
+ Field field = key.getField(BindingMapping.QNAME_STATIC_FIELD_NAME);
+ Object obj = field.get(null);
+ if (obj instanceof QName) {
+ return Optional.of((QName) obj);
+ }
- } catch (NoSuchFieldException e) {
- return Optional.of(computeQName(key));
-
- } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
- /*
- *
- * It is safe to log this this exception on debug, since this method
- * should not fail. Only failures are possible if the runtime /
- * backing.
- */
- LOG.debug("Unexpected exception during extracting QName for {}", key, e);
- }
- return Optional.absent();
- }
+ } catch (NoSuchFieldException e) {
+ return Optional.of(computeQName(key));
- /**
- * Computes QName for supplied class
- *
- * Namespace and revision are same as {@link YangModuleInfo} associated with
- * supplied class.
- * <p>
- * If class is
- * <ul>
- * <li>rpc input: local name is "input".
- * <li>rpc output: local name is "output".
- * <li>augmentation: local name is "module name".
- * </ul>
- *
- * There is also fallback, if it is not possible to compute QName using
- * following algorithm returns module QName.
- *
- * FIXME: Extend this algorithm to also provide QName for YANG modeled
- * simple types.
- *
- * @throws IllegalStateException
- * If YangModuleInfo could not be resolved
- * @throws IllegalArgumentException
- * If supplied class was not derived from YANG model.
- *
- */
- @SuppressWarnings({ "rawtypes", "unchecked" })
- private static QName computeQName(final Class key) {
- if (isBindingClass(key)) {
- YangModuleInfo moduleInfo;
- try {
- moduleInfo = getModuleInfo(key);
- } catch (Exception e) {
- throw new IllegalStateException("Unable to get QName for " + key + ". YangModuleInfo was not found.", e);
+ } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
+ /*
+ *
+ * It is safe to log this this exception on debug, since this method
+ * should not fail. Only failures are possible if the runtime /
+ * backing.
+ */
+ LOG.debug("Unexpected exception during extracting QName for {}", key, e);
}
- final QName module = getModuleQName(moduleInfo).intern();
- if (Augmentation.class.isAssignableFrom(key)) {
- return module;
- } else if (isRpcType(key)) {
- final String className = key.getSimpleName();
- if (className.endsWith(BindingMapping.RPC_OUTPUT_SUFFIX)) {
- return QName.create(module, "output").intern();
- } else {
- return QName.create(module, "input").intern();
+ return Optional.absent();
+ }
+
+ /**
+ * Computes QName for supplied class
+ *
+ * Namespace and revision are same as {@link YangModuleInfo} associated with supplied class.
+ * <p>
+ * If class is
+ * <ul>
+ * <li>rpc input: local name is "input".
+ * <li>rpc output: local name is "output".
+ * <li>augmentation: local name is "module name".
+ * </ul>
+ *
+ * There is also fallback, if it is not possible to compute QName using following algorithm returns module
+ * QName.
+ *
+ * FIXME: Extend this algorithm to also provide QName for YANG modeled simple types.
+ *
+ * @throws IllegalStateException If YangModuleInfo could not be resolved
+ * @throws IllegalArgumentException If supplied class was not derived from YANG model.
+ *
+ */
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ private static QName computeQName(final Class key) {
+ if (isBindingClass(key)) {
+ YangModuleInfo moduleInfo;
+ try {
+ moduleInfo = getModuleInfo(key);
+ } catch (Exception e) {
+ throw new IllegalStateException("Unable to get QName for " + key
+ + ". YangModuleInfo was not found.", e);
}
+ final QName module = getModuleQName(moduleInfo).intern();
+ if (Augmentation.class.isAssignableFrom(key)) {
+ return module;
+ } else if (isRpcType(key)) {
+ final String className = key.getSimpleName();
+ if (className.endsWith(BindingMapping.RPC_OUTPUT_SUFFIX)) {
+ return QName.create(module, "output").intern();
+ } else {
+ return QName.create(module, "input").intern();
+ }
+ }
+ /*
+ * Fallback for Binding types which do not have QNAME field
+ */
+ return module;
+ } else {
+ throw new IllegalArgumentException("Supplied class " + key + "is not derived from YANG.");
}
- /*
- * Fallback for Binding types which do not have QNAME field
- */
- return module;
- } else {
- throw new IllegalArgumentException("Supplied class " + key + "is not derived from YANG.");
}
+
}
/**
public class DataObjectReadingUtil {
+ private static final DataObjectReadingStrategy REAUSABLE_AUGMENTATION_READING_STRATEGY = new AugmentationReadingStrategy();
+
private DataObjectReadingUtil() {
throw new UnsupportedOperationException("Utility class. Instantion is not allowed.");
}
private static DataObjectReadingStrategy resolveReadStrategy(final Class<? extends DataContainer> parentClass,
final Class<? extends DataContainer> type) {
- DataObjectReadingStrategy strategy = createReadStrategy(parentClass, type);
// FIXME: Add caching of strategies
- return strategy;
+ return createReadStrategy(parentClass, type);
}
private static DataObjectReadingStrategy createReadStrategy(final Class<? extends DataContainer> parent,
return new ContainerReadingStrategy(parent, child);
}
- private static Method resolveGetterMethod(final Class<? extends DataContainer> parent, final Class<?> child) {
- String methodName = "get" + child.getSimpleName();
- try {
- return parent.getMethod(methodName);
- } catch (NoSuchMethodException e) {
- throw new IllegalArgumentException(e);
- } catch (SecurityException e) {
- throw new IllegalStateException(e);
- }
- }
-
@SuppressWarnings("rawtypes")
- private static abstract class DataObjectReadingStrategy {
+ private abstract static class DataObjectReadingStrategy {
private final Class<? extends DataContainer> parentType;
private final Class<? extends DataContainer> childType;
public abstract DataContainer read(DataContainer parent, Class<?> childType);
+ private static Method resolveGetterMethod(final Class<? extends DataContainer> parent, final Class<?> child) {
+ String methodName = "get" + child.getSimpleName();
+ try {
+ return parent.getMethod(methodName);
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException(e);
+ } catch (SecurityException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
}
@SuppressWarnings("rawtypes")
return readAll(dataList, builder);
}
}
- } catch (InvocationTargetException e) {
- throw new IllegalStateException(e);
- } catch (IllegalAccessException e) {
- throw new IllegalStateException(e);
- } catch (IllegalArgumentException e) {
+ } catch (InvocationTargetException | IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
return Collections.emptyMap();
}
- private static final DataObjectReadingStrategy REAUSABLE_AUGMENTATION_READING_STRATEGY = new AugmentationReadingStrategy();
-
private static final class AugmentationReadingStrategy extends DataObjectReadingStrategy {
public AugmentationReadingStrategy() {
.newBuilder().weakKeys()
.build(new CacheLoader<Class<? extends NotificationListener>, NotificationListenerInvoker>() {
+ private NotificationListenerInvoker createInvoker(
+ final Class<? extends NotificationListener> key) {
+ return new NotificationListenerInvoker(createInvokerMap(key));
+ }
+
+ private Map<QName, MethodHandle> createInvokerMap(final Class<? extends NotificationListener> key) {
+ final Builder<QName, MethodHandle> ret = ImmutableMap.<QName, MethodHandle> builder();
+ for (final Method method : key.getMethods()) {
+ if (BindingReflections.isNotificationCallback(method)) {
+
+ final Class<?> notification = method.getParameterTypes()[0];
+ final QName name = BindingReflections.findQName(notification);
+ MethodHandle handle;
+ try {
+ handle = LOOKUP.unreflect(method).asType(MethodType.methodType(void.class,
+ NotificationListener.class, DataContainer.class));
+ ret.put(name, handle);
+ } catch (final IllegalAccessException e) {
+ throw new IllegalStateException("Can not access public method.", e);
+ }
+ }
+
+ }
+ return ret.build();
+ }
+
@Override
public NotificationListenerInvoker load(final Class<? extends NotificationListener> key) throws Exception {
return createInvoker(key);
}
}
- private static NotificationListenerInvoker createInvoker(final Class<? extends NotificationListener> key) {
- return new NotificationListenerInvoker(createInvokerMap(key));
- }
-
- private static Map<QName, MethodHandle> createInvokerMap(final Class<? extends NotificationListener> key) {
- final Builder<QName, MethodHandle> ret = ImmutableMap.<QName, MethodHandle>builder();
- for (final Method method : key.getMethods()) {
- if (BindingReflections.isNotificationCallback(method)) {
-
- final Class<?> notification = method.getParameterTypes()[0];
- final QName name = BindingReflections.findQName(notification);
- MethodHandle handle;
- try {
- handle = LOOKUP.unreflect(method).asType(
- MethodType.methodType(void.class, NotificationListener.class, DataContainer.class));
- ret.put(name, handle);
- } catch (final IllegalAccessException e) {
- throw new IllegalStateException("Can not access public method.", e);
- }
- }
-
- }
- return ret.build();
- }
-
}