use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class CopyJUnitTest method testIsWellKnownImmutableInstance.
@Test
public void testIsWellKnownImmutableInstance() {
assertEquals(true, CopyHelper.isWellKnownImmutableInstance("abc"));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Integer.valueOf(0)));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Long.valueOf(0)));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Byte.valueOf((byte) 0)));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Short.valueOf((short) 0)));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Float.valueOf((float) 1.2)));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Double.valueOf(1.2)));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Character.valueOf((char) 0)));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(new BigInteger("1234")));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(new BigDecimal("123.4556")));
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(new UUID(1L, 2L)));
PdxInstance pi = new PdxInstance() {
public Object getObject() {
return null;
}
public Object getObject(Object pdxObject) {
return null;
}
public boolean hasField(String fieldName) {
return false;
}
public List<String> getFieldNames() {
return null;
}
public boolean isIdentityField(String fieldName) {
return false;
}
public Object getField(String fieldName) {
return null;
}
public WritablePdxInstance createWriter() {
return null;
}
public String getClassName() {
return null;
}
public boolean isEnum() {
return false;
}
};
WritablePdxInstance wpi = new WritablePdxInstance() {
public Object getObject() {
return null;
}
public Object getObject(Object pdxObject) {
return null;
}
public boolean hasField(String fieldName) {
return false;
}
public List<String> getFieldNames() {
return null;
}
public boolean isIdentityField(String fieldName) {
return false;
}
public Object getField(String fieldName) {
return null;
}
public WritablePdxInstance createWriter() {
return null;
}
public void setField(String fieldName, Object value) {
}
public String getClassName() {
return null;
}
public boolean isEnum() {
return false;
}
};
assertEquals(true, CopyHelper.isWellKnownImmutableInstance(pi));
assertEquals(false, CopyHelper.isWellKnownImmutableInstance(wpi));
assertEquals(false, CopyHelper.isWellKnownImmutableInstance(new Object()));
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class AbstractRegionEntry method checkOffHeapEquals.
private static boolean checkOffHeapEquals(@Unretained StoredObject ohVal, @Unretained Object obj) {
if (ohVal.isSerializedPdxInstance()) {
PdxInstance pi = InternalDataSerializer.readPdxInstance(ohVal.getSerializedValue(), GemFireCacheImpl.getForPdx("Could not check value equality"));
return checkPdxEquals(pi, obj);
}
if (obj instanceof StoredObject) {
return ohVal.checkDataEquals((StoredObject) obj);
} else {
byte[] serializedObj;
if (obj instanceof CachedDeserializable) {
CachedDeserializable cdObj = (CachedDeserializable) obj;
if (!ohVal.isSerialized()) {
assert cdObj.isSerialized();
return false;
}
serializedObj = cdObj.getSerializedValue();
} else if (obj instanceof byte[]) {
if (ohVal.isSerialized()) {
return false;
}
serializedObj = (byte[]) obj;
} else {
if (!ohVal.isSerialized()) {
return false;
}
if (obj == null || obj == Token.NOT_AVAILABLE || Token.isInvalidOrRemoved(obj)) {
return false;
}
serializedObj = EntryEventImpl.serialize(obj);
}
return ohVal.checkDataEquals(serializedObj);
}
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class AbstractRegionEntry method checkPdxEquals.
/**
* This method fixes bug 43643
*/
private static boolean checkPdxEquals(PdxInstance pdx, Object obj) {
if (!(obj instanceof PdxInstance)) {
// if we are not readSerialized.
if (obj instanceof CachedDeserializable) {
CachedDeserializable cdObj = (CachedDeserializable) obj;
if (!cdObj.isSerialized()) {
// obj is actually a byte[] which will never be equal to a PdxInstance
return false;
}
Object cdVal = cdObj.getValue();
if (cdVal instanceof byte[]) {
byte[] cdValBytes = (byte[]) cdVal;
PdxInstance pi = InternalDataSerializer.readPdxInstance(cdValBytes, GemFireCacheImpl.getForPdx("Could not check value equality"));
if (pi != null) {
return pi.equals(pdx);
} else {
// since obj is serialized as something other than pdx it must not equal our pdx
return false;
}
} else {
// remove the cd wrapper so that obj is the actual value we want to compare.
obj = cdVal;
}
}
if (obj != null && obj.getClass().getName().equals(pdx.getClassName())) {
InternalCache internalCache = GemFireCacheImpl.getForPdx("Could not access Pdx registry");
if (internalCache != null) {
PdxSerializer pdxSerializer;
if (obj instanceof PdxSerializable) {
pdxSerializer = null;
} else {
pdxSerializer = internalCache.getPdxSerializer();
}
if (pdxSerializer != null || obj instanceof PdxSerializable) {
// try to convert obj to a PdxInstance
HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
try {
if (InternalDataSerializer.autoSerialized(obj, hdos) || InternalDataSerializer.writePdx(hdos, internalCache, obj, pdxSerializer)) {
PdxInstance pi = InternalDataSerializer.readPdxInstance(hdos.toByteArray(), internalCache);
if (pi != null) {
obj = pi;
}
}
} catch (IOException | PdxSerializationException ignore) {
// we are not able to convert it so just fall through
}
}
}
}
}
return basicEquals(obj, pdx);
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class TypedJson method visitSpecialObjects.
List<Object> visitSpecialObjects(Writer w, Object object, boolean write) throws IOException {
List<Object> elements = new ArrayList<Object>();
Class clazz = object.getClass();
if (clazz.isArray()) {
if (write) {
writeArray(w, object);
} else {
return getArrayChildren(object);
}
}
if (clazz.isEnum()) {
if (write) {
writeEnum(w, object);
} else {
elements.add(object);
}
return elements;
}
if (object instanceof TypedJson) {
this.writeTypedJson(w, (TypedJson) object);
return elements;
}
if (object instanceof Collection) {
Collection collection = (Collection) object;
Iterator iter = collection.iterator();
int i = 0;
if (write)
w.write('{');
while (iter.hasNext() && i < queryCollectionsDepth) {
Object item = iter.next();
if (write) {
writeKeyValue(w, i, item, item != null ? item.getClass() : null);
} else {
elements.add(item);
}
i++;
}
if (write)
w.write('}');
return elements;
}
if (object instanceof Map) {
Map map = (Map) object;
Iterator it = map.entrySet().iterator();
int i = 0;
if (write)
w.write('{');
while (it.hasNext() && i < queryCollectionsDepth) {
Map.Entry e = (Map.Entry) it.next();
Object value = e.getValue();
if (write) {
writeKeyValue(w, e.getKey(), value, value != null ? value.getClass() : null);
} else {
elements.add(value);
}
i++;
}
if (write)
w.write('}');
return elements;
}
if (object instanceof PdxInstance) {
PdxInstance pdxInstance = (PdxInstance) object;
if (write)
w.write('{');
for (String field : pdxInstance.getFieldNames()) {
Object fieldValue = pdxInstance.getField(field);
if (write) {
writeKeyValue(w, field, fieldValue, fieldValue != null ? fieldValue.getClass() : null);
} else {
elements.add(fieldValue);
}
}
if (write)
w.write('}');
return elements;
}
if (object instanceof Struct) {
StructImpl impl = (StructImpl) object;
String[] fields = impl.getFieldNames();
Object[] values = impl.getFieldValues();
if (write)
w.write('{');
for (int i = 0; i < fields.length; i++) {
Object fieldValue = values[i];
if (write) {
writeKeyValue(w, fields[i], fieldValue, fieldValue != null ? fieldValue.getClass() : null);
} else {
elements.add(fieldValue);
}
}
if (write)
w.write('}');
return elements;
}
if (object instanceof Region.Entry) {
Region.Entry entry = (Region.Entry) object;
Object key = entry.getKey();
Object value = entry.getValue();
if (write) {
w.write('{');
writeKeyValue(w, key, value, value != null ? value.getClass() : null);
w.write('}');
} else {
elements.add(value);
}
return elements;
}
return elements;
}
use of org.apache.geode.pdx.PdxInstance in project geode by apache.
the class DataCommandFunction method getJSONForNonPrimitiveObject.
@SuppressWarnings({ "rawtypes" })
public static Object[] getJSONForNonPrimitiveObject(Object obj) {
Object[] array = new Object[2];
if (obj == null) {
array[0] = null;
array[1] = "<NULL>";
return array;
} else {
array[0] = obj.getClass().getCanonicalName();
Class klass = obj.getClass();
if (JsonUtil.isPrimitiveOrWrapper(klass)) {
array[1] = obj;
} else if (obj instanceof PdxInstance) {
String str = pdxToJson((PdxInstance) obj);
array[1] = str;
} else {
GfJsonObject object = new GfJsonObject(obj, true);
Iterator keysIterator = object.keys();
while (keysIterator.hasNext()) {
String key = (String) keysIterator.next();
Object value = object.get(key);
if (GfJsonObject.isJSONKind(value)) {
GfJsonObject jsonVal = new GfJsonObject(value);
// System.out.println("Re-wrote inner object");
try {
if (jsonVal.has("type-class")) {
object.put(key, jsonVal.get("type-class"));
} else {
// Its Map Value
object.put(key, "a Map");
}
} catch (GfJsonException e) {
throw new RuntimeException(e);
}
} else if (value instanceof JSONArray) {
// Its a collection either a set or list
try {
object.put(key, "a Collection");
} catch (GfJsonException e) {
throw new RuntimeException(e);
}
}
}
String str = object.toString();
array[1] = str;
}
return array;
}
}
Aggregations