< prev index next >

src/java.base/share/classes/java/lang/invoke/VarHandle.java

Print this page
rev 52981 : 8215300: additional changes to constants API


1847             return vform.memberName_table[ordinal];
1848         }
1849     }
1850 
1851     static final class AccessDescriptor {
1852         final MethodType symbolicMethodTypeErased;
1853         final MethodType symbolicMethodTypeInvoker;
1854         final Class<?> returnType;
1855         final int type;
1856         final int mode;
1857 
1858         public AccessDescriptor(MethodType symbolicMethodType, int type, int mode) {
1859             this.symbolicMethodTypeErased = symbolicMethodType.erase();
1860             this.symbolicMethodTypeInvoker = symbolicMethodType.insertParameterTypes(0, VarHandle.class);
1861             this.returnType = symbolicMethodType.returnType();
1862             this.type = type;
1863             this.mode = mode;
1864         }
1865     }
1866 










1867     @Override
1868     public final boolean equals(Object o) {
1869         if (this == o) return true;
1870         if (o == null || getClass() != o.getClass()) return false;
1871 
1872         VarHandle that = (VarHandle) o;
1873         return accessModeType(AccessMode.GET).equals(that.accessModeType(AccessMode.GET)) &&
1874                internalEquals(that);
1875     }
1876 
1877     abstract boolean internalEquals(VarHandle vh);
1878 
1879     @Override
1880     public final int hashCode() {
1881         return 31 * accessModeType(AccessMode.GET).hashCode() + internalHashCode();
1882     }
1883 
1884     abstract int internalHashCode();
1885 






1886     @Override
1887     public final String toString() {
1888         return String.format("VarHandle[varType=%s, coord=%s]",
1889                              varType().getName(),
1890                              coordinateTypes());
1891     }
1892 
1893     /**
1894      * Returns the variable type of variables referenced by this VarHandle.
1895      *
1896      * @return the variable type of variables referenced by this VarHandle
1897      */
1898     public final Class<?> varType() {
1899         MethodType typeSet = accessModeType(AccessMode.SET);
1900         return typeSet.parameterType(typeSet.parameterCount() - 1);
1901     }
1902 
1903     /**
1904      * Returns the coordinate types for this VarHandle.
1905      *


2255 
2256         @Override
2257         public VarHandle resolveConstantDesc(MethodHandles.Lookup lookup)
2258                 throws ReflectiveOperationException {
2259             switch (kind) {
2260                 case FIELD:
2261                     return lookup.findVarHandle((Class<?>) declaringClass.resolveConstantDesc(lookup),
2262                                                 constantName(),
2263                                                 (Class<?>) varType.resolveConstantDesc(lookup));
2264                 case STATIC_FIELD:
2265                     return lookup.findStaticVarHandle((Class<?>) declaringClass.resolveConstantDesc(lookup),
2266                                                       constantName(),
2267                                                       (Class<?>) varType.resolveConstantDesc(lookup));
2268                 case ARRAY:
2269                     return MethodHandles.arrayElementVarHandle((Class<?>) declaringClass.resolveConstantDesc(lookup));
2270                 default:
2271                     throw new InternalError("Cannot reach here");
2272             }
2273         }
2274 








2275         @Override
2276         public String toString() {
2277             switch (kind) {
2278                 case FIELD:
2279                 case STATIC_FIELD:
2280                     return String.format("VarHandleDesc[%s%s.%s:%s]",
2281                                          (kind == Kind.STATIC_FIELD) ? "static " : "",
2282                                          declaringClass.displayName(), constantName(), varType.displayName());
2283                 case ARRAY:
2284                     return String.format("VarHandleDesc[%s[]]", declaringClass.displayName());
2285                 default:
2286                     throw new InternalError("Cannot reach here");
2287             }
2288         }
2289     }
2290 
2291 }


1847             return vform.memberName_table[ordinal];
1848         }
1849     }
1850 
1851     static final class AccessDescriptor {
1852         final MethodType symbolicMethodTypeErased;
1853         final MethodType symbolicMethodTypeInvoker;
1854         final Class<?> returnType;
1855         final int type;
1856         final int mode;
1857 
1858         public AccessDescriptor(MethodType symbolicMethodType, int type, int mode) {
1859             this.symbolicMethodTypeErased = symbolicMethodType.erase();
1860             this.symbolicMethodTypeInvoker = symbolicMethodType.insertParameterTypes(0, VarHandle.class);
1861             this.returnType = symbolicMethodType.returnType();
1862             this.type = type;
1863             this.mode = mode;
1864         }
1865     }
1866 
1867     /**
1868      * Compare this {@linkplain VarHandle} with another object for equality.
1869      * Two {@linkplain VarHandle}s are considered equal if they both describe the
1870      * same instance field, both describe the same static field, both describe
1871      * array elements for arrays with the same component type, or both describe
1872      * the same component of an off-heap structure.
1873      *
1874      * @param o the other object
1875      * @return Whether this {@linkplain VarHandle} is equal to the other object
1876      */
1877     @Override
1878     public final boolean equals(Object o) {
1879         if (this == o) return true;
1880         if (o == null || getClass() != o.getClass()) return false;
1881 
1882         VarHandle that = (VarHandle) o;
1883         return accessModeType(AccessMode.GET).equals(that.accessModeType(AccessMode.GET)) &&
1884                internalEquals(that);
1885     }
1886 
1887     abstract boolean internalEquals(VarHandle vh);
1888 
1889     @Override
1890     public final int hashCode() {
1891         return 31 * accessModeType(AccessMode.GET).hashCode() + internalHashCode();
1892     }
1893 
1894     abstract int internalHashCode();
1895 
1896     /**
1897      * Returns a compact textual description of this {@linkplain VarHandle},
1898      * including the type of variable described, and a description of its coordinates.
1899      *
1900      * @return A compact textual description of this {@linkplain VarHandle}
1901      */
1902     @Override
1903     public final String toString() {
1904         return String.format("VarHandle[varType=%s, coord=%s]",
1905                              varType().getName(),
1906                              coordinateTypes());
1907     }
1908 
1909     /**
1910      * Returns the variable type of variables referenced by this VarHandle.
1911      *
1912      * @return the variable type of variables referenced by this VarHandle
1913      */
1914     public final Class<?> varType() {
1915         MethodType typeSet = accessModeType(AccessMode.SET);
1916         return typeSet.parameterType(typeSet.parameterCount() - 1);
1917     }
1918 
1919     /**
1920      * Returns the coordinate types for this VarHandle.
1921      *


2271 
2272         @Override
2273         public VarHandle resolveConstantDesc(MethodHandles.Lookup lookup)
2274                 throws ReflectiveOperationException {
2275             switch (kind) {
2276                 case FIELD:
2277                     return lookup.findVarHandle((Class<?>) declaringClass.resolveConstantDesc(lookup),
2278                                                 constantName(),
2279                                                 (Class<?>) varType.resolveConstantDesc(lookup));
2280                 case STATIC_FIELD:
2281                     return lookup.findStaticVarHandle((Class<?>) declaringClass.resolveConstantDesc(lookup),
2282                                                       constantName(),
2283                                                       (Class<?>) varType.resolveConstantDesc(lookup));
2284                 case ARRAY:
2285                     return MethodHandles.arrayElementVarHandle((Class<?>) declaringClass.resolveConstantDesc(lookup));
2286                 default:
2287                     throw new InternalError("Cannot reach here");
2288             }
2289         }
2290 
2291         /**
2292          * Returns a compact textual description of this constant description.
2293          * For a field {@linkplain VarHandle}, includes the owner, name, and type
2294          * of the field, and whether it is static; for an array {@linkplain VarHandle},
2295          * the name of the component type.
2296          *
2297          * @return A compact textual description of this descriptor
2298          */
2299         @Override
2300         public String toString() {
2301             switch (kind) {
2302                 case FIELD:
2303                 case STATIC_FIELD:
2304                     return String.format("VarHandleDesc[%s%s.%s:%s]",
2305                                          (kind == Kind.STATIC_FIELD) ? "static " : "",
2306                                          declaringClass.displayName(), constantName(), varType.displayName());
2307                 case ARRAY:
2308                     return String.format("VarHandleDesc[%s[]]", declaringClass.displayName());
2309                 default:
2310                     throw new InternalError("Cannot reach here");
2311             }
2312         }
2313     }
2314 
2315 }
< prev index next >