import com.google.common.collect.ImmutableSortedMap;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
abstract class DataObjectCodecContext<D extends DataObject,T extends DataNodeContainer> extends DataContainerCodecContext<D,T> {
private static final Logger LOG = LoggerFactory.getLogger(DataObjectCodecContext.class);
- private static final Lookup LOOKUP = MethodHandles.publicLookup();
private static final MethodType CONSTRUCTOR_TYPE = MethodType.methodType(void.class, InvocationHandler.class);
private static final MethodType DATAOBJECT_TYPE = MethodType.methodType(DataObject.class, InvocationHandler.class);
private static final Comparator<Method> METHOD_BY_ALPHABET = new Comparator<Method>() {
final Class<?> proxyClass = Proxy.getProxyClass(getBindingClass().getClassLoader(), new Class[] { getBindingClass(), AugmentationHolder.class });
try {
- proxyConstructor = LOOKUP.findConstructor(proxyClass, CONSTRUCTOR_TYPE).asType(DATAOBJECT_TYPE);
+ proxyConstructor = MethodHandles.publicLookup().findConstructor(proxyClass, CONSTRUCTOR_TYPE)
+ .asType(DATAOBJECT_TYPE);
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new IllegalStateException("Failed to find contructor for class " + proxyClass);
}
import com.google.common.collect.ImmutableMap;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodHandles.Lookup;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collections;
return q1.getLocalName().compareToIgnoreCase(q2.getLocalName());
}
};
- private static final Lookup LOOKUP = MethodHandles.publicLookup();
private final Map<QName, ValueContext> keyValueContexts;
private final List<QName> keysInBindingOrder;
private final ListSchemaNode schema;
this.identifiable = identifiable;
try {
- ctor = LOOKUP.unreflectConstructor(getConstructor(keyClass));
+ ctor = MethodHandles.publicLookup().unreflectConstructor(getConstructor(keyClass));
} catch (IllegalAccessException e) {
throw new IllegalArgumentException("Missing construct in class " + keyClass);
}
return new NodeIdentifierWithPredicates(schema.getQName(), values);
}
-
@SuppressWarnings("unchecked")
private static Constructor<? extends Identifier<?>> getConstructor(final Class<? extends Identifier<?>> clazz) {
for (@SuppressWarnings("rawtypes") final Constructor constr : clazz.getConstructors()) {
}
throw new IllegalArgumentException("Supplied class " + clazz + "does not have required constructor.");
}
-
}
\ No newline at end of file
import com.google.common.io.BaseEncoding;
import com.google.common.util.concurrent.ExecutionError;
import com.google.common.util.concurrent.UncheckedExecutionException;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import org.opendaylight.yangtools.yang.binding.BindingMapping;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
final class UnionTypeCodec extends ReflectionBasedCodec {
+ private static final MethodType CHARARRAY_LOOKUP_TYPE = MethodType.methodType(void.class, char[].class);
+ private static final MethodType CHARARRAY_INVOKE_TYPE = MethodType.methodType(Object.class, char[].class);
+ private static final MethodType CLASS_LOOKUP_TYPE = MethodType.methodType(void.class, Class.class);
+ private static final MethodType CLASS_INVOKE_TYPE = MethodType.methodType(Object.class, Object.class);
+ private static final Logger LOG = LoggerFactory.getLogger(UnionTypeCodec.class);
+
+ private final Codec<Object, Object> idRefCodec;
+ private final MethodHandle idrefConstructor;
- private final Codec<Object, Object> identityrefCodec;
private final ImmutableSet<UnionValueOptionContext> typeCodecs;
- private final Constructor<?> charConstructor;
+ private final MethodHandle charConstructor;
private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs,
@Nullable final Codec<Object, Object> identityrefCodec) {
super(unionCls);
- this.identityrefCodec = identityrefCodec;
+ this.idRefCodec = identityrefCodec;
+ if (idRefCodec != null) {
+ try {
+ idrefConstructor = MethodHandles.publicLookup().findConstructor(unionCls, CLASS_LOOKUP_TYPE)
+ .asType(CLASS_INVOKE_TYPE);
+ } catch (IllegalAccessException | NoSuchMethodException e) {
+ throw new IllegalStateException("Failed to get identityref constructor", e);
+ }
+ } else {
+ idrefConstructor = null;
+ }
+
try {
- charConstructor = unionCls.getConstructor(char[].class);
- typeCodecs = ImmutableSet.copyOf(codecs);
- } catch (NoSuchMethodException | SecurityException e) {
- throw new IllegalStateException("Required constructor is not available.",e);
+ charConstructor = MethodHandles.publicLookup().findConstructor(unionCls, CHARARRAY_LOOKUP_TYPE)
+ .asType(CHARARRAY_INVOKE_TYPE);
+ } catch (IllegalAccessException | NoSuchMethodException e) {
+ throw new IllegalStateException("Failed to instantiate handle for constructor", e);
}
+
+ typeCodecs = ImmutableSet.copyOf(codecs);
}
static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
};
}
+ private Object deserializeString(final Object input) {
+ final String str = input instanceof byte[] ? BaseEncoding.base64().encode((byte[]) input) : input.toString();
+ try {
+ return charConstructor.invokeExact(str.toCharArray());
+ } catch (Throwable e) {
+ throw new IllegalStateException("Could not construct instance", e);
+ }
+ }
+
@Override
public Object deserialize(final Object input) {
- if (identityrefCodec != null) {
+ if (idRefCodec != null) {
+ final Object identityref;
try {
- Object identityref = identityrefCodec.deserialize(input);
- return getTypeClass().getConstructor(Class.class).newInstance(identityref);
+ identityref = idRefCodec.deserialize(input);
} catch (UncheckedExecutionException | ExecutionError e) {
- // ignore this exception caused by deserialize()
- } catch (NoSuchMethodException e) {
- // caused by getContructor(). this case shouldn't happen.
- throw new IllegalStateException("Could not construct instance", e);
- } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
- // ignore this exception caused by newInstance()
+ LOG.debug("Deserialization of {} as identityref failed", e);
+ return deserializeString(input);
}
- }
- try {
- if (input instanceof byte[]) {
- return charConstructor.newInstance(BaseEncoding.base64().encode((byte[]) input).toCharArray());
- } else {
- return charConstructor.newInstance((input.toString().toCharArray()));
+
+ try {
+ return idrefConstructor.invokeExact(identityref);
+ } catch (Throwable e) {
+ LOG.debug("Failed to instantiate based on identityref {}", identityref, e);
}
- } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
- throw new IllegalStateException("Could not construct instance",e);
}
+
+ return deserializeString(input);
}
@Override
}
return null;
}
-
}
import com.google.common.base.Throwables;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import org.opendaylight.yangtools.concepts.Codec;
final class UnionValueOptionContext {
- private static final Lookup LOOKUP = MethodHandles.publicLookup();
private static final MethodType OBJECT_TYPE = MethodType.methodType(Object.class, Object.class);
private final Class<?> bindingType;
- // FIXME: migrate to invocation
private final MethodHandle getter;
private final Codec<Object,Object> codec;
this.codec = Preconditions.checkNotNull(codec);
try {
- this.getter = LOOKUP.unreflect(getter).asType(OBJECT_TYPE);
+ this.getter = MethodHandles.publicLookup().unreflect(getter).asType(OBJECT_TYPE);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Failed to access method " + getter, e);
}
import com.google.common.base.Throwables;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import org.opendaylight.yangtools.concepts.Codec;
final class ValueContext {
- private static final Lookup LOOKUP = MethodHandles.publicLookup();
private static final MethodType OBJECT_METHOD = MethodType.methodType(Object.class, Object.class);
private final Codec<Object, Object> codec;
private final MethodHandle getter;
ValueContext(final Class<?> identifier, final LeafNodeCodecContext <?>leaf) {
getterName = leaf.getGetter().getName();
try {
- getter = LOOKUP.unreflect(identifier.getMethod(getterName)).asType(OBJECT_METHOD);
+ getter = MethodHandles.publicLookup().unreflect(identifier.getMethod(getterName)).asType(OBJECT_METHOD);
} catch (IllegalAccessException | NoSuchMethodException | SecurityException e) {
throw new IllegalStateException(String.format("Cannot find method %s in class %s", getterName, identifier), e);
}