DynamicConstantDesc hashCode() in Java

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

Syntax:

int java.lang.constant.DynamicConstantDesc.hashCode()

This method returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

1. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

2. If two objects are equal according to the equals(Object)method, then calling the hashCode method on each of the two objects must produce the same integer result.

3. It is not required that if two objects are unequal according to the java.lang.Object.equals(java.lang.Object)method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Parameters: NA

Returns: a hash code value for this object.

Exceptions: NA

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.Lookup;

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

            @Override
            public Object resolveConstantDesc(Lookup lookup) throws ReflectiveOperationException {

                return "hello";
            }

            @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);

        System.out.println(dynamicConstantDesc.hashCode());
    }
}

Output:

809689645


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