8241100: Make Boolean, Character, Byte, and Short implement Constable

Reviewed-by: jrose, briangoetz, psandoz
This commit is contained in:
Jorn Vernee 2020-05-04 09:41:01 -07:00
parent 2d8bea8c1d
commit 31041d406a
8 changed files with 330 additions and 14 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,15 @@ package java.lang;
import jdk.internal.HotSpotIntrinsicCandidate;
import jdk.internal.misc.VM;
import java.lang.constant.Constable;
import java.lang.constant.DynamicConstantDesc;
import java.util.Optional;
import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
import static java.lang.constant.ConstantDescs.CD_byte;
import static java.lang.constant.ConstantDescs.CD_int;
import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
/**
*
* The {@code Byte} class wraps a value of primitive type {@code byte}
@ -44,7 +53,7 @@ import jdk.internal.misc.VM;
* @see java.lang.Number
* @since 1.1
*/
public final class Byte extends Number implements Comparable<Byte> {
public final class Byte extends Number implements Comparable<Byte>, Constable {
/**
* A constant holding the minimum value a {@code byte} can
@ -77,6 +86,18 @@ public final class Byte extends Number implements Comparable<Byte> {
return Integer.toString((int)b, 10);
}
/**
* Returns an {@link Optional} containing the nominal descriptor for this
* instance.
*
* @return an {@link Optional} describing the {@linkplain Byte} instance
* @since 15
*/
@Override
public Optional<DynamicConstantDesc<Byte>> describeConstable() {
return Optional.of(DynamicConstantDesc.ofNamed(BSM_EXPLICIT_CAST, DEFAULT_NAME, CD_byte, intValue()));
}
private static class ByteCache {
private ByteCache() {}