Skip to content

Commit 65f850f

Browse files
author
tmiddleton
committed
Bug 33168147 - [33158028->21.06.1] PortableType Classes automatically discovered without Id's Do not Serialize Properly
[git-p4: depot-paths = "//dev/coherence-ce/release/coherence-ce-v21.06/": change = 87458]
1 parent c46cbb5 commit 65f850f

File tree

3 files changed

+114
-11
lines changed

3 files changed

+114
-11
lines changed

prj/coherence-core/src/main/java/com/tangosol/io/pof/generator/PortableTypeGenerator.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,16 @@ private void implementReadExternal()
452452

453453
boolean fDelegateToSuper = isPofType(m_classNode.superName);
454454

455-
mn.visitVarInsn(ALOAD, 1);
456-
mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader",
457-
"getUserTypeId", "()I", true);
458-
mn.visitLdcInsn(m_type.getId());
459-
mn.visitJumpInsn(IF_ICMPNE, l0);
455+
// -1 suggests the type-id is generated at runtime and therefore it is unnecessary to verify 'id'
456+
// value in the annotation is used by the PofConfig
457+
if (m_type.getId() != -1)
458+
{
459+
mn.visitVarInsn(ALOAD, 1);
460+
mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader",
461+
"getUserTypeId", "()I", true);
462+
mn.visitLdcInsn(m_type.getId());
463+
mn.visitJumpInsn(IF_ICMPNE, l0);
464+
}
460465

461466
int cPofFields = 0;
462467

@@ -567,11 +572,17 @@ private void implementWriteExternal()
567572

568573
boolean fDelegateToSuper = isPofType(m_classNode.superName);
569574

570-
mn.visitVarInsn(ALOAD, 1);
571-
mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofWriter",
572-
"getUserTypeId", "()I", true);
573-
mn.visitLdcInsn(m_type.getId());
574-
mn.visitJumpInsn(IF_ICMPNE, l0);
575+
// -1 suggests the type-id is generated at runtime and therefore it is unnecessary to verify 'id'
576+
// value in the annotation is used by the PofConfig
577+
if (m_type.getId() != -1)
578+
{
579+
mn.visitVarInsn(ALOAD, 1);
580+
mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofWriter",
581+
"getUserTypeId", "()I", true);
582+
583+
mn.visitLdcInsn(m_type.getId());
584+
mn.visitJumpInsn(IF_ICMPNE, l0);
585+
}
575586

576587
int cPofFields = 0;
577588

prj/coherence-core/src/test/java/com/tangosol/io/pof/generator/PortableTypeGeneratorTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
33
*
44
* Licensed under the Universal Permissive License v 1.0 as shown at
55
* http://oss.oracle.com/licenses/upl.
@@ -12,10 +12,16 @@
1212
import com.tangosol.io.pof.EvolvableObject;
1313
import com.tangosol.io.pof.PortableObject;
1414

15+
import com.tangosol.io.pof.PortableTypeSerializer;
16+
import com.tangosol.io.pof.SimplePofContext;
17+
import com.tangosol.io.pof.generator.data.TestClassWithNoId;
1518
import com.tangosol.io.pof.schema.annotation.internal.Instrumented;
1619

1720
import com.tangosol.util.Base;
1821

22+
import com.tangosol.util.Binary;
23+
import com.tangosol.util.ExternalizableHelper;
24+
1925
import org.junit.Test;
2026

2127
import java.io.File;
@@ -84,6 +90,32 @@ public void shouldCreateSchemaForPackagelessClass() throws Exception
8490
assertThat(schema, is(notNullValue()));
8591
}
8692

93+
@Test
94+
@SuppressWarnings("rawtypes")
95+
public void shouldInstrumentClassWithNoId() throws Exception
96+
{
97+
String sClassName = TestClassWithNoId.class.getName();
98+
URL url = getClass().getResource("/" + sClassName.replaceAll("\\.", "/") + ".class");
99+
File fileClass = new File(url.toURI());
100+
byte[] abBytes = Files.readAllBytes(fileClass.toPath());
101+
Properties properties = new Properties();
102+
Map<String, ?> env = new HashMap<>();
103+
byte[] instrumented = PortableTypeGenerator.instrumentClass(fileClass, abBytes, 0, abBytes.length, properties, env);
104+
105+
SimplePofContext ctx = new SimplePofContext();
106+
107+
ByteArrayClassLoader loader = new ByteArrayClassLoader(Collections.singletonMap(sClassName, instrumented));
108+
Class<?> instrumentedClass = loader.findClass(TestClassWithNoId.class.getName());
109+
110+
ctx.registerUserType(1, instrumentedClass, new PortableTypeSerializer(1, instrumentedClass));
111+
112+
Object testClass = instrumentedClass.getDeclaredConstructor(String.class).newInstance("value");
113+
114+
Binary binTestClass = ExternalizableHelper.toBinary(testClass, ctx);
115+
Object result = ExternalizableHelper.fromBinary(binTestClass, ctx);
116+
assertThat(result.equals(testClass), is(true));
117+
}
118+
87119
@Test
88120
public void shouldInstrumentPackagelessClass() throws Exception
89121
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at
5+
* http://oss.oracle.com/licenses/upl.
6+
*/
7+
8+
package com.tangosol.io.pof.generator.data;
9+
10+
import com.tangosol.io.pof.schema.annotation.PortableType;
11+
12+
/**
13+
* A class to test PortableType generation and serialization with no id.
14+
*
15+
* @author tam 2021.07.28
16+
*/
17+
@PortableType
18+
public class TestClassWithNoId {
19+
20+
public TestClassWithNoId()
21+
{
22+
}
23+
24+
public TestClassWithNoId(String sValue)
25+
{
26+
m_sValue = sValue;
27+
}
28+
29+
public String getValue()
30+
{
31+
return m_sValue;
32+
}
33+
34+
public void setValue(String m_sValue)
35+
{
36+
this.m_sValue = m_sValue;
37+
}
38+
39+
@Override
40+
public boolean equals(Object o)
41+
{
42+
if (this == o) return true;
43+
if (o == null || getClass() != o.getClass())
44+
{
45+
return false;
46+
}
47+
48+
TestClassWithNoId that = (TestClassWithNoId) o;
49+
50+
return m_sValue != null ? m_sValue.equals(that.m_sValue) : that.m_sValue == null;
51+
}
52+
53+
@Override
54+
public int hashCode()
55+
{
56+
return m_sValue != null ? m_sValue.hashCode() : 0;
57+
}
58+
59+
private String m_sValue;
60+
}

0 commit comments

Comments
 (0)