<div dir="ltr">I think I'm understanding. So <font face="monospace">$</font><font face="arial, sans-serif"> has the same type as the source class, but each field on it is initialized to something with a unique identity, their values aren't important, their identity is.<br><br>The ways out of this are code generation - make your own </font><font face="monospace">$</font><font face="arial, sans-serif"> where everything is a </font><font face="monospace">FieldRef<T></font><font face="arial, sans-serif"> - but that has its own downsides.</font><br><br><font face="monospace"> </font><a class="gmail_plusreply" id="plusReplyChip-0" style="font-family:monospace">@Minimal</a><br><font face="monospace"> public class Person {</font><br><font face="monospace"> public static final Person$ $ = Keys.of(Person$.class);</font><br><br><font face="monospace"> @NotEmpty</font><br><font face="monospace"> public Integer number;</font><br><br><font face="monospace"> @Size(100)</font><br><font face="monospace"> @Searched</font><br><font face="monospace"> @NotEmpty</font><br><font face="monospace"> public String name;</font><br><br><font face="monospace"> public final Address address = new Address();</font><br><font face="monospace"> }</font><br><br><font face="arial, sans-serif">It is very possible to make this generate a matching </font><font face="monospace">Person$</font><font face="arial, sans-serif"> structure, but that comes with obvious downsides.<br><br>Another option is to generically disallow any value types as fields. This means custom wrappers for things like </font><font face="monospace">Integer</font><font face="arial, sans-serif">, </font><font face="monospace">LocalDate</font><font face="arial, sans-serif">, etc. including custom "zero value" logic to get your unique instance.<br><br></font><font face="monospace"> public class Person {<br> public static final Person $ = Keys.of(Person.class);<br><br> @NotEmpty<br> public minimalj.I32 number;<br><br> @Size(100)<br> @Searched<br> @NotEmpty<br> public String name;<br><br> public final Address address = new Address();<br>}</font><font face="arial, sans-serif"><br><br>Another other option is to start to make use of the instance value, not just instance identity. Right now all your Integers are 0. Why not have an AtomicInteger that counts up and use that to give unique Integer instances? <br><br><br> private static final AtomicInteger i = new AtomicInteger();</font><br style="font-family:arial,sans-serif"><span style="font-family:arial,sans-serif"> private static final AtomicLong l = new AtomicLong();<br></span><font face="arial, sans-serif"><br></font> @SuppressWarnings({ "unchecked", "rawtypes" })<br> private static Object createKey(Class<?> clazz, String fieldName, Class<?> declaringClass) {<br> if (clazz == String.class) {<br> return new String(fieldName);<br> } else if (clazz == Integer.class) {<br> return new Integer(i.getAndIncrement());<br> } else if (clazz == Long.class) {<br> return new Long(l.getAndIncrement());<font face="arial, sans-serif"></font></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, Dec 1, 2025 at 2:56 PM Bruno Eberhard <<a href="mailto:bruno.eberhard@pop.ch">bruno.eberhard@pop.ch</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Am 01.12.2025 um 20:37 schrieb Ethan McCue:<br>
> One more question: How does the code today work around things like the <br>
> small integer cache?<br>
It allocates for each field a "new Integer(0)". As long as Integer have <br>
an identity two of these values are distinguishable and can serve as <br>
different keys in a IdentityHashMap .<br>
</blockquote></div>