RFR: 8359707: Add classfile modification code to RedefineClassHelper [v2]

Chen Liang liach at openjdk.org
Tue Jun 24 15:59:32 UTC 2025


On Tue, 24 Jun 2025 12:07:53 GMT, Coleen Phillimore <coleenp at openjdk.org> wrote:

>> test/lib/RedefineClassHelper.java line 87:
>> 
>>> 85:         }
>>> 86:         return 0;
>>> 87:     }
>> 
>> I understand, that this is copy of the old code, but this method does not look good.
>> - it would be better to return -1 if the string not found (0 is valid offset)
>> - calculation of the buf's upper bound is incorrect when offset > 0
>> I'd suggest to update it to something like (not tested, also need to update callers to compare with -1)
>> 
>> 
>> private static int getStringIndex(String needle, byte[] buf, int offset) {
>>     byte[] needleArray = needle.getBytes(StandardCharsets.US_ASCII);
>>     for (int i = offset; i < buf.length - needleArray.length; i++) 
>>         if (Arrays.equals(buf, offset, offset + needleArray.length,
>>                           needleArray, 0, needleArray.length)) {
>>             return offset;
>>         }
>>     }
>>     return -1;
>> }
>
> I did copy this code.  I was hoping instead of class byte modifications that someone would suggest a Classfile API way to modify the class file bytes. @liach ? That way the class names don't have to be the same length.  Above seems better  code but I have to debug it now.

`jdk.internal.classfile.components` has a `ClassRemapper` that may serve this purpose. It updates all internal names and occurrences in descriptors.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/25857#discussion_r2164386948


More information about the serviceability-dev mailing list