Skip to content

Commit a4ef8d9

Browse files
committed
Enh 36466628 - [36404724->24.09] Enh: Return error message if field with POF related annotation declared as final
(auto-submit 108126 after successfully running remote remote.full) Job ID: job.9.20240401210206.27926 [git-p4: depot-paths = "//dev/coherence-ce/main/": change = 108130]
1 parent 8318d03 commit a4ef8d9

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

prj/coherence-core/src/main/java/com/oracle/coherence/common/schema/ClassFileSchemaSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ public static Predicate<ClassNode> hasAnnotation(final Class clzAnnotation)
689689
// ---- Constants -------------------------------------------------------
690690

691691
private static final int EXCLUDED_FIELDS =
692-
Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_TRANSIENT;
692+
Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT;
693693

694694
// ---- Data members ----------------------------------------------------
695695

prj/coherence-core/src/main/java/com/tangosol/io/pof/schema/handler/ClassFileHandler.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Copyright (c) 2013, 2020, Oracle and/or its affiliates.
2+
* Copyright (c) 2013, 2024, Oracle and/or its affiliates.
33
*
44
* Licensed under the Universal Permissive License v 1.0 as shown at
5-
* http://oss.oracle.com/licenses/upl.
5+
* https://oss.oracle.com/licenses/upl.
66
*/
77
package com.tangosol.io.pof.schema.handler;
88

@@ -34,13 +34,15 @@
3434
import com.tangosol.io.pof.schema.annotation.PortableSet;
3535
import com.tangosol.io.pof.schema.annotation.PortableType;
3636

37+
import org.objectweb.asm.Opcodes;
3738
import org.objectweb.asm.Type;
3839
import org.objectweb.asm.tree.AnnotationNode;
3940
import org.objectweb.asm.tree.ClassNode;
4041
import org.objectweb.asm.tree.FieldNode;
4142

4243
import static com.oracle.coherence.common.schema.util.AsmUtils.getAnnotationAttribute;
4344

45+
4446
/**
4547
* Reads {@link PofType} metadata from a class file.
4648
*
@@ -99,6 +101,12 @@ public void importProperty(PofProperty property, FieldNode source, Schema schema
99101
AnnotationNode an = AsmUtils.getAnnotation(source, PORTABLE_ANNOTATIONS);
100102
if (an != null)
101103
{
104+
if ((source.access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL)
105+
{
106+
throw new IllegalStateException(String.format("Field '%s' annotated with '@%s' cannot be final",
107+
source.name, an.desc.substring(1, an.desc.length() - 1).replace('/', '.')));
108+
}
109+
102110
property.setSince((Integer) getAnnotationAttribute(an, "since"));
103111
property.setOrder((Integer) getAnnotationAttribute(an, "order"));
104112

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2023, Oracle and/or its affiliates.
2+
* Copyright (c) 2012, 2024, Oracle and/or its affiliates.
33
*
44
* Licensed under the Universal Permissive License v 1.0 as shown at
55
* https://oss.oracle.com/licenses/upl.
@@ -35,6 +35,7 @@
3535
import java.util.Map;
3636
import java.util.Properties;
3737

38+
import com.tangosol.io.pof.generator.data.FinalFieldValue;
3839
import com.tangosol.io.pof.generator.data.Simple;
3940
import org.hamcrest.MatcherAssert;
4041
import org.junit.Before;
@@ -45,6 +46,8 @@
4546
import static org.hamcrest.CoreMatchers.notNullValue;
4647
import static org.junit.Assert.assertEquals;
4748
import static org.junit.Assert.assertThat;
49+
import static org.junit.Assert.fail;
50+
4851

4952
/**
5053
* @author as 2012.05.27
@@ -298,6 +301,27 @@ public void shouldInstrumentPackagelessClass() throws Exception
298301
MatcherAssert.assertThat(EvolvableObject.class.isAssignableFrom(instrumentedClass), is(true));
299302
}
300303

304+
@Test
305+
public void shouldThrowErrorForFinalField() throws Exception
306+
{
307+
String sClassName = FinalFieldValue.class.getName();
308+
URL url = getClass().getResource("/" + sClassName.replaceAll("\\.", "/") + ".class");
309+
File fileClass = new File(url.toURI());
310+
byte[] abBytes = Files.readAllBytes(fileClass.toPath());
311+
Properties properties = new Properties();
312+
Map<String, ?> env = new HashMap<>();
313+
314+
try
315+
{
316+
PortableTypeGenerator.instrumentClass(fileClass, abBytes, 0, abBytes.length, properties, env);
317+
fail("IllegalStateException should have been thrown");
318+
}
319+
catch (IllegalStateException ise)
320+
{
321+
// Expected
322+
}
323+
}
324+
301325
// ----- inner class: ByteArrayClassLoader ------------------------------
302326

303327
public static class ByteArrayClassLoader
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at
5+
* https://oss.oracle.com/licenses/upl.
6+
*/
7+
8+
package com.tangosol.io.pof.generator.data;
9+
10+
11+
import com.tangosol.io.pof.schema.annotation.Portable;
12+
import com.tangosol.io.pof.schema.annotation.PortableType;
13+
import java.util.Objects;
14+
15+
16+
@PortableType(id = 1001)
17+
public class FinalFieldValue {
18+
19+
// ----- constructors ---------------------------------------------------
20+
21+
public FinalFieldValue(String sFinalField)
22+
{
23+
m_sFinalField = sFinalField;
24+
}
25+
26+
27+
// ----- accessors ------------------------------------------------------
28+
29+
public String getFinalField()
30+
{
31+
return m_sFinalField;
32+
}
33+
34+
35+
// ----- object methods -------------------------------------------------
36+
37+
@Override
38+
public boolean equals(Object o)
39+
{
40+
if (this == o)
41+
{
42+
return true;
43+
}
44+
if (o == null || getClass() != o.getClass())
45+
{
46+
return false;
47+
}
48+
FinalFieldValue value = (FinalFieldValue) o;
49+
return Objects.equals(m_sFinalField, value.m_sFinalField);
50+
}
51+
52+
@Override
53+
public int hashCode()
54+
{
55+
return Objects.hash(m_sFinalField);
56+
}
57+
58+
@Override
59+
public String toString()
60+
{
61+
return "FinalFieldValue{finalField='" + m_sFinalField + "'}";
62+
}
63+
64+
// ----- data members ---------------------------------------------------
65+
66+
@Portable
67+
final String m_sFinalField;
68+
}

0 commit comments

Comments
 (0)