[External] : how does one get value of ConstantValueAttribute?

Mark Roberts markro at cs.washington.edu
Mon Nov 25 23:17:43 UTC 2024


Right! – I’ve used Optional before and knew that.  Sorry for the noise.



*From:* Chen Liang <chen.l.liang at oracle.com>
*Sent:* Monday, November 25, 2024 3:14 PM
*To:* Mark Roberts <markro at cs.washington.edu>; classfile-api-dev at openjdk.org
*Subject:* Re: [External] : how does one get value of
ConstantValueAttribute?



Hi Mark,

You are calling constant() on an Optional. You should call
cva.get().constant().constantValue() instead.



Chen
------------------------------

*From:* Mark Roberts <markro at cs.washington.edu>
*Sent:* Monday, November 25, 2024 5:06 PM
*To:* Chen Liang <chen.l.liang at oracle.com>; classfile-api-dev at openjdk.org <
classfile-api-dev at openjdk.org>
*Subject:* RE: [External] : how does one get value of
ConstantValueAttribute?



Here is snippet from my source:



import java.lang.classfile.*;

import java.lang.classfile.attribute.*;

import java.lang.classfile.attribute.ConstantValueAttribute;

import java.lang.classfile.Attributes;

import java.lang.classfile.components.ClassPrinter;

import java.lang.classfile.constantpool.*;

import java.lang.classfile.instruction.*;

import java.lang.constant.*;

import java.lang.invoke.MethodHandles;

import static java.lang.classfile.constantpool.PoolEntry.*;

import static java.lang.constant.ConstantDescs.*;

.

.

.

      // get constant static fields

      List<FieldModel> fields = classModel.fields();

      for (FieldModel fm : fields) {

        Optional<ConstantValueAttribute> cva =
fm.findAttribute(Attributes.constantValue());

        if (cva.isPresent()) {

          Utf8Entry utf8 = fm.fieldName();

          String value = formatConstantDesc(cva.constant().constantValue());

          System.out.printf("  Constant field: %s, value: %s%n",
utf8.stringValue(), cva);

        }

      }



and here is snippet from compilation:



./daikon/chicory/Instrument.java:316: error: cannot find symbol

          String value = formatConstantDesc(cva.constant().constantValue());


                                                        ^

  symbol:   method constant()

  location: variable cva of type Optional<ConstantValueAttribute>



Let me know if you need more info.  Thanks for the assistance.



Mark





*From:* Chen Liang <chen.l.liang at oracle.com>
*Sent:* Monday, November 25, 2024 2:49 PM
*To:* Mark Roberts <markro at cs.washington.edu>; classfile-api-dev at openjdk.org
*Subject:* Re: [External] : how does one get value of
ConstantValueAttribute?



This is an interface method, which is implicitly public.  You should be
able to call it without any problem. If there is a compile error, can you
share the snippet of code that you managed to reproduce an error?
------------------------------

*From:* Mark Roberts <markro at cs.washington.edu>
*Sent:* Monday, November 25, 2024 4:44 PM
*To:* Chen Liang <chen.l.liang at oracle.com>; classfile-api-dev at openjdk.org <
classfile-api-dev at openjdk.org>
*Subject:* RE: [External] : how does one get value of
ConstantValueAttribute?



Sorry – typo:  ‘constant()’



*From:* Chen Liang <chen.l.liang at oracle.com>
*Sent:* Monday, November 25, 2024 2:35 PM
*To:* Mark Roberts <markro at cs.washington.edu>; classfile-api-dev at openjdk.org
*Subject:* Re: [External] : how does one get value of
ConstantValueAttribute?



Hi Mark,

I don't understand your question.  Nowhere in any document mentions there's
a "const" in ConstantValueAttribute.  Its Javadoc is available here:

https://download.java.net/java/early_access/jdk24/docs/api/java.base/java/lang/classfile/attribute/ConstantValueAttribute.html
<https://urldefense.com/v3/__https:/download.java.net/java/early_access/jdk24/docs/api/java.base/java/lang/classfile/attribute/ConstantValueAttribute.html__;!!ACWV5N9M2RV99hQ!LL2vfYLl-_GR0xLXyaydxqZog5OPnJudBuAZUaFkt9ojJHORh2WZryU1DK0bAH9EPsUz1D3nHmNwq16k4EPL2opBEg$>

So once you get a ConstantValueAttribute from a field, you can check its
constant() accessor, and pattern match to check if it is an
IntegerEntry/StringEntry etc.



Regards,

Chen Liang
------------------------------

*From:* Mark Roberts <markro at cs.washington.edu>
*Sent:* Monday, November 25, 2024 4:17 PM
*To:* Chen Liang <chen.l.liang at oracle.com>; classfile-api-dev at openjdk.org <
classfile-api-dev at openjdk.org>
*Subject:* [External] : how does one get value of ConstantValueAttribute?



ConstantValueAttribute:const() is not public.  The documentation implies it
is.  How do you get the value?



Thanks,

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20241125/60492fee/attachment.htm>


More information about the classfile-api-dev mailing list