DynamicConstantDesc resolveConstantDesc(Lookup) in Java

resolveConstantDesc(Lookup): This method is available in the java.lang.constant.DynamicConstantDesc class of Java.

Syntax:

Object java.lang.constant.DynamicConstantDesc.resolveConstantDesc(Lookup lookup) throws ReflectiveOperationException

This method takes one argument. This method resolves this descriptor reflectively, emulating the resolution behavior of JVMS 5.4.3 and the access control behavior of JVMS 5.4.4. The resolution and access control context is provided by MethodHandles.Lookupparameter. No caching of the resulting value is performed.

Parameters: One parameter is required for this method.

lookup: The MethodHandles.Lookup to provide name resolution and access control context.

Returns: the resolved constant value.

Throws:

1. ReflectiveOperationException - if a class, method, or field could not be reflectively resolved in the course of resolution.

Approach

Java

package com.DynamicConstantDesc;

import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;
import java.lang.constant.DirectMethodHandleDesc;
import java.lang.constant.DynamicConstantDesc;
import java.lang.constant.MethodTypeDesc;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;

public class DynamicConstantDescresolveConstantDesc {
    public static void main(String[] args) throws ReflectiveOperationException {
        DirectMethodHandleDesc bootstrapMethod = new DirectMethodHandleDesc() {

            @Override
            public Object resolveConstantDesc(Lookup lookup) throws ReflectiveOperationException {
                MethodHandles.Lookup lookup1 = MethodHandles.lookup();
                return lookup1;
            }

            @Override
            public MethodTypeDesc invocationType() {

                return MethodTypeDesc.of(ClassDesc.of("hello"), ClassDesc.of("hello"));
            }

            @Override
            public int refKind() {
                return 5;
            }

            @Override
            public ClassDesc owner() {

                return ClassDesc.of("hello");
            }

            @Override
            public String methodName() {
                return "hello";
            }

            @Override
            public String lookupDescriptor() {
                return "loolup";
            }

            @Override
            public Kind kind() {

                return Kind.CONSTRUCTOR;
            }

            @Override
            public boolean isOwnerInterface() {
                return true;
            }
        };
        DynamicConstantDesc<?> dynamicConstantDesc = DynamicConstantDesc.ofNamed(bootstrapMethod, "name",
                ClassDesc.of("name"), ConstantDescs.BSM_ENUM_CONSTANT);

        MethodHandles.Lookup lookup = MethodHandles.publicLookup();

        System.out.println(dynamicConstantDesc.resolveConstantDesc(lookup));
    }
}

Output:

Exception in thread "main" java.lang.BootstrapMethodError: java.lang.ClassCastException: class java.lang.invoke.MethodHandles$Lookup cannot be cast to class java.lang.invoke.MethodHandle (java.lang.invoke.MethodHandles$Lookup and java.lang.invoke.MethodHandle are in module java.base of loader 'bootstrap') at java.base/java.lang.constant.DynamicConstantDesc.resolveConstantDesc(DynamicConstantDesc.java:279) at com.DynamicConstantDesc.DynamicConstantDescresolveConstantDesc.main(DynamicConstantDescresolveConstantDesc.java:64) Caused by: java.lang.ClassCastException: class java.lang.invoke.MethodHandles$Lookup cannot be cast to class java.lang.invoke.MethodHandle (java.lang.invoke.MethodHandles$Lookup and java.lang.invoke.MethodHandle are in module java.base of loader 'bootstrap') at java.base/java.lang.constant.DynamicConstantDesc.resolveConstantDesc(DynamicConstantDesc.java:262) ... 1 more


Some other methods of DynamicConstantDesc

DynamicConstantDesc.ofNamed(DirectMethodHandleDesc, String, ClassDesc, ConstantDesc...) This method returns a nominal descriptor for a dynamic constant.

bootstrapArgs()This method returns the bootstrap arguments for this constant.

bootstrapArgsList()This method returns the bootstrap arguments for this constant as an immutable List.

bootstrapMethod()This method returns a MethodHandleDesc describing the bootstrap method for this constant.

constantName()This method returns the name that would appear in the NameAndType operand of the LDC for this constant.

constantType()This method returns a ClassDesc describing the type that would appear in the NameAndType operand of the LDC for this constant.

equals(Object)This method compares the specified object with this descriptor for equality. Returns true if and only if the specified object is also a DynamicConstantDesc, and both descriptors have equal bootstrap methods, bootstrap argument lists, constant name, and constant type.

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

DynamicConstantDesc.of(DirectMethodHandleDesc)This method returns a nominal descriptor for a dynamic constant whose bootstrap has no static arguments, and whose name parameter is ConstantDescs.DEFAULT_NAME, and whose type parameter is always the same as the bootstrap method return type.

DynamicConstantDesc.of(DirectMethodHandleDesc, ConstantDesc...)This method returns a nominal descriptor for a dynamic constant whose name parameter is ConstantDescs.DEFAULT_NAME, and whose type parameter is always the same as the bootstrap method return type.

DynamicConstantDesc.ofCanonical(DirectMethodHandleDesc, String, ClassDesc, ConstantDesc[])This method returns a nominal descriptor for a dynamic constant, transforming it into a more specific type if the constant bootstrap is a well-known one and a more specific nominal descriptor type (e.g., ClassDesc) is available.

resolveConstantDesc(Lookup)This method resolves this descriptor reflectively, emulating the resolution behavior of JVMS 5.4.3 and the access control behavior of JVMS 5.4.4. The resolution and access control context is provided by MethodHandles.Lookupparameter. No caching of the resulting value is performed.

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

No comments:

Post a Comment