On Sat, 26 Jun 2021 23:55:46 GMT, Weijun Wang <wei...@openjdk.org> wrote:

>> src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java 
>> line 53:
>> 
>>> 51:     private static final long CURRENT_PID = 
>>> AccessController.doPrivileged(
>>> 52:             (PrivilegedAction<ProcessHandle>) 
>>> ProcessHandle::current).pid();
>>> 53: 
>> 
>> The original code separated out the declaration of the PrivilegedAction to 
>> avoid this cast. If you move the code from the original static initializer 
>> into a static method that it called from initializer then it might provide 
>> you with a cleaner way to refactor this. There are several other places in 
>> this patch that could do with similar cleanup.
>
> This cast is only to tell the compiler which overloaded method to call, and I 
> don't think there will be a real cast at runtime. It might look a little ugly 
> but extracting it into a variable declaration/definition plus a new 
> `initStatic` method seems not worth doing, IMHO.

Why not simply declare a local variable in the static initializer below?


    private static final long CURRENT_PID;
    private static final boolean ALLOW_ATTACH_SELF;
    static {
        PrivilegedAction<ProcessHandle> pa = ProcessHandle::current;
        @SuppressWarnings("removal")
        long pid = AccessController.doPrivileged(pa).pid();
        CURRENT_PID = pid;
        String s = VM.getSavedProperty("jdk.attach.allowAttachSelf");
        ALLOW_ATTACH_SELF = "".equals(s) || Boolean.parseBoolean(s);
    }

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

PR: https://git.openjdk.java.net/jdk17/pull/152

Reply via email to