DynamicCallSiteDesc.of(DirectMethodHandleDesc, String, MethodTypeDesc,ConstantDesc...) in Java

DynamicCallSiteDesc.of(DirectMethodHandleDesc, String, MethodTypeDesc, ConstantDesc...): This method is available in the java.lang.constant.DynamicCallSiteDesc class of Java.

Syntax:

DynamicCallSiteDesc java.lang.constant.DynamicCallSiteDesc.of(DirectMethodHandleDesc bootstrapMethod, String invocationName, MethodTypeDesc invocationType, ConstantDesc... bootstrapArgs)

This method takes four arguments. This method creates a nominal descriptor for an invoke dynamic call site.

Parameters: Four parameters are required for this method.

bootstrapMethod: a DirectMethodHandleDesc describing the bootstrap method for the invoke dynamic.

invocationName: The unqualified name that would appear in the NameAndType operand of the invoke dynamic.

invocationType: a MethodTypeDesc describing the invocation type that would appear in the NameAndType operand of the invoke dynamic.

bootstrapArgs: ConstantDescs describing the static arguments to the bootstrap, that would appear in the Bootstrap Methods attribute.

Returns: the nominal descriptor.

Throws:

1. NullPointerException - if any parameter is null.

2. IllegalArgumentException - if the invocation name has the incorrect format.

Approach 1: When no exception

Java

package com.DynamicCallSiteDesc;

import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDesc;
import java.lang.constant.DirectMethodHandleDesc;
import java.lang.constant.DynamicCallSiteDesc;
import java.lang.constant.MethodTypeDesc;
import java.lang.invoke.MethodHandles.Lookup;

public class DynamicCallSiteDescof3 {
    public static void main(String[] args) {

        DirectMethodHandleDesc desc = new DirectMethodHandleDesc() {

            @Override
            public Object resolveConstantDesc(Lookup lookup) throws ReflectiveOperationException {
                return null;
            }

            @Override
            public MethodTypeDesc invocationType() {
                return MethodTypeDesc.of(ClassDesc.of("name"), ClassDesc.of("name"));
            }

            @Override
            public int refKind() {

                return 0;
            }

            @Override
            public ClassDesc owner() {

                return ClassDesc.of("name");
            }

            @Override
            public String methodName() {

                return "name";
            }

            @Override
            public String lookupDescriptor() {

                return "LookupDescriptor";
            }

            @Override
            public Kind kind() {
                return Kind.CONSTRUCTOR;
            }

            @Override
            public boolean isOwnerInterface() {
                return true;
            }
        };
        ConstantDesc constantDesc = new ConstantDesc() {

            @Override
            public Object resolveConstantDesc(Lookup lookup) throws ReflectiveOperationException {
                return null;
            }
        };
        DynamicCallSiteDesc dynamicCallSiteDesc = DynamicCallSiteDesc.of(desc, "Hello",
                MethodTypeDesc.of(ClassDesc.of("Hello"), ClassDesc.of("Hello")), constantDesc);

        System.out.println(dynamicCallSiteDesc);
    }
}

Output:

DynamicCallSiteDesc[name::name(Hello/com.DynamicCallSiteDesc.DynamicCallSiteDescof3$2@7bfcd12c):(Hello)Hello]


Approach 2: NullPointerException

Java

package com.DynamicCallSiteDesc;

import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDesc;
import java.lang.constant.DirectMethodHandleDesc;
import java.lang.constant.DynamicCallSiteDesc;
import java.lang.constant.MethodTypeDesc;
import java.lang.invoke.MethodHandles.Lookup;

public class DynamicCallSiteDescof3 {
    public static void main(String[] args) {

        DirectMethodHandleDesc desc = new DirectMethodHandleDesc() {

            @Override
            public Object resolveConstantDesc(Lookup lookup) throws ReflectiveOperationException {
                return null;
            }

            @Override
            public MethodTypeDesc invocationType() {
                return MethodTypeDesc.of(ClassDesc.of("name"), ClassDesc.of("name"));
            }

            @Override
            public int refKind() {

                return 0;
            }

            @Override
            public ClassDesc owner() {

                return ClassDesc.of("name");
            }

            @Override
            public String methodName() {

                return "name";
            }

            @Override
            public String lookupDescriptor() {

                return "LookupDescriptor";
            }

            @Override
            public Kind kind() {
                return Kind.CONSTRUCTOR;
            }

            @Override
            public boolean isOwnerInterface() {
                return true;
            }
        };
        ConstantDesc constantDesc = new ConstantDesc() {

            @Override
            public Object resolveConstantDesc(Lookup lookup) throws ReflectiveOperationException {
                return null;
            }
        };
        DynamicCallSiteDesc dynamicCallSiteDesc = DynamicCallSiteDesc.of(desc, null,
                MethodTypeDesc.of(ClassDesc.of("Hello"), ClassDesc.of("Hello")), constantDesc);

        System.out.println(dynamicCallSiteDesc);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.lang.constant.DynamicCallSiteDesc.<init>(DynamicCallSiteDesc.java:78) at java.base/java.lang.constant.DynamicCallSiteDesc.of(DynamicCallSiteDesc.java:109) at com.DynamicCallSiteDesc.DynamicCallSiteDescof3.main(DynamicCallSiteDescof3.java:66)


Approach 3: IllegalArgumentException 

Java

package com.DynamicCallSiteDesc;

import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDesc;
import java.lang.constant.DirectMethodHandleDesc;
import java.lang.constant.DynamicCallSiteDesc;
import java.lang.constant.MethodTypeDesc;
import java.lang.invoke.MethodHandles.Lookup;

public class DynamicCallSiteDescof3 {
    public static void main(String[] args) {

        DirectMethodHandleDesc desc = new DirectMethodHandleDesc() {

            @Override
            public Object resolveConstantDesc(Lookup lookup) throws ReflectiveOperationException {
                return null;
            }

            @Override
            public MethodTypeDesc invocationType() {
                return MethodTypeDesc.of(ClassDesc.of("name"), ClassDesc.of("name"));
            }

            @Override
            public int refKind() {

                return 0;
            }

            @Override
            public ClassDesc owner() {

                return ClassDesc.of("name");
            }

            @Override
            public String methodName() {

                return "name";
            }

            @Override
            public String lookupDescriptor() {

                return "LookupDescriptor";
            }

            @Override
            public Kind kind() {
                return Kind.CONSTRUCTOR;
            }

            @Override
            public boolean isOwnerInterface() {
                return true;
            }
        };
        ConstantDesc constantDesc = new ConstantDesc() {

            @Override
            public Object resolveConstantDesc(Lookup lookup) throws ReflectiveOperationException {
                return null;
            }
        };
        DynamicCallSiteDesc dynamicCallSiteDesc = DynamicCallSiteDesc.of(desc, "Hello/",
                MethodTypeDesc.of(ClassDesc.of("Hello"), ClassDesc.of("Hello")), constantDesc);

        System.out.println(dynamicCallSiteDesc);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid member name: Hello/ at java.base/java.lang.constant.ConstantUtils.validateMemberName(ConstantUtils.java:75) at java.base/java.lang.constant.DynamicCallSiteDesc.<init>(DynamicCallSiteDesc.java:78) at java.base/java.lang.constant.DynamicCallSiteDesc.of(DynamicCallSiteDesc.java:109) at com.DynamicCallSiteDesc.DynamicCallSiteDescof3.main(DynamicCallSiteDescof3.java:66)


Some other methods of DynamicCallSiteDesc

bootstrapArgs(): This method returns ConstantDescs describing the bootstrap arguments for the invoke dynamic.

bootstrapMethod(): This method returns a MethodHandleDesc describing the bootstrap method for the invoke dynamic.

equals(Object): This method compares the specified object with this descriptor for equality.

hashCode(): This method returns a hash code value for the object.

invocationName(): This method returns the invocation name that would appear in the NameAndType operand of the invoke dynamic.

invocationType(): This method returns a MethodTypeDesc describing the invocation type that would appear in the NameAndType operand of the invoke dynamic.

DynamicCallSiteDesc.of(DirectMethodHandleDesc, MethodTypeDesc): This method creates a nominal descriptor for an invoke dynamic call site whose bootstrap method has no static arguments and for which the name parameter is ConstantDescs.DEFAULT_NAME.

DynamicCallSiteDesc.of(DirectMethodHandleDesc, String, MethodTypeDesc): This method creates a nominal descriptor for an invokes dynamic call site whose bootstrap method has no static arguments.

DynamicCallSiteDesc.of(DirectMethodHandleDesc, String, MethodTypeDesc, ConstantDesc...): This method creates a nominal descriptor for an invoke dynamic call site.

resolveCallSiteDesc(Lookup): This method reflectively invokes the bootstrap method with the specified arguments and returns the resulting CallSite.

toString(): This method returns a compact textual description of this call site description, including the bootstrap method, the invocation name and type, and the static bootstrap arguments.

withArgs(ConstantDesc...): This method returns a nominal descriptor for an invoke dynamic call site whose bootstrap method, name, and invocation type are the same as this one, but with the specified bootstrap arguments.

withNameAndType(String, MethodTypeDesc): This method returns a nominal descriptor for an invoke dynamic call site whose bootstrap and bootstrap arguments are the same as this one, but with the specified invocationName and invocation invocationType.

No comments:

Post a Comment