Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:26:26+00:00 2026-06-15T03:26:26+00:00

I am using Javassist to extend certain classes at runtime . In a couple

  • 0

I am using Javassist to extend certain classes at runtime .
In a couple of places (in the generation code) I need to create instances of the Javassist ConstPool class.
For example, to mark a generated class as synthetic, I wrote something like this:

CtClass ctClassToExtend = ... //class to extend
CtClass newCtClass = extend(ctClassToExtend, ...); //method to create a new ctClass extending ctClassToExtend
SyntheticAttribute syntheticAttribute = new SyntheticAttribute(ctClassToExtend.getClassFile().getConstPool()); //creating a synthetic attribute using an instance of ConstPool
newCtClass.setAttribute(syntheticAttribute.getName(), syntheticAttribute.get()); //marking the generated class as synthetic

This is working as expected, but I have certain doubts about this being entirely correct. Concretely, my main question is:

Is the call to CtClass.getClassFile().getConstPool() the correct way to get a constant pool in this example?. If not, what is the general proper way to get the right instance of a constant pool when creating a new class at runtime using Javassist?

Also, I am a bit lost regarding what is happening behind the curtains here: Why do we need a constant pool to create a instance of a synthetic attribute ?, or in general, of any other kind of class attributes ?

Thanks for any clarification.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:26:28+00:00Added an answer on June 15, 2026 at 3:26 am

    Don’t know if you’re still interested in the answer, but at least might help others that
    find this question.

    First of all, a small suggestion to everyone that starts creating/modifying bytecode
    and needs more in-depth information on how the JVM internals works, the JVM’s specification documentation might look bulky and scary at first but it’s invaluable help!

    Is the call to CtClass.getClassFile().getConstPool() the correct way to get a constant pool in this example?.

    Yes, it is. Each Java Class has a single constant pool, so basicaly every time you need to access the constant
    pool for a given class you can do ctClass.getClassFile().getConstPool(), although you must keep in mind the
    following:

    1. In javassist the constant pool field from CtClass is an instance field, that means that if you have two CtClass objects
      representing the same Class you’ll have two diferrent instances of constant pool (even though they represent
      the constant pool in the actual class file). When modifying one of the CtClassinstances you must use the
      associated constant pool instance in order to have the expected behaviour.

    2. There are times where you might not have the CtClass but rather a CtMethod or a CtField which don’t let you backtrace to the CtClass instance, in such cases you can use ctMethod.getMethodInfo().getConstPool() and ctField.getFieldInfo().getConstPool() to retrieve the correct constant pool.

      Since I’ve mentioned CtMethod and CtField, keep in mind that if you want to add attributes to any of these, it can’t be through the ClassFile Object, but through MethodInfo and FieldInfo respectively.

    Why do we need a constant pool to create a instance of a synthetic attribute ?, or in general, of any other kind of class attributes ?

    To answer this question, I’ll start quoting section 4.4 regarding JVM 7 specs (like I said, this documentation is quite helpful):

    Java virtual machine instructions do not rely on the runtime layout of classes, interfaces, class instances, or arrays. Instead, instructions refer to symbolic information in the constant_pool table.

    With this in mind, I think the best way to shed some light on this subject is to look at a class file dump. We can achieve this by running the following command:

    javap -s -c -p -v SomeClassFile.class
    

    Javap comes with the java SDK and it’s a good tool to analyse classes at this level, the explanation of each switch

    • -s : Prints internal type signature
    • -c : Prints byte code
    • -p : Prints all class members (methods and fields, including the private ones)
    • -v : Be verbose, will print tack information and class constant pool

    Here’s the output for test.Test1 class that I modified via javassist to have the synthetic attribute both in the class and in the injectedMethod

    Classfile /C:/development/testProject/test/Test1.class
    Last modified 29/Nov/2012; size 612 bytes
    MD5 checksum 858c009090bfb57d704b2eaf91c2cb75
    Compiled from "Test1.java"
    public class test.Test1
    SourceFile: "Test1.java"
    Synthetic: true
    minor version: 0
    major version: 50
    flags: ACC_PUBLIC, ACC_SUPER
    
    Constant pool:
    #1 = Class              #2             //  test/Test1
    #2 = Utf8               test/Test1
    #3 = Class              #4             //  java/lang/Object
    #4 = Utf8               java/lang/Object
    #5 = Utf8               <init>
    #6 = Utf8               ()V
    #7 = Utf8               Code
    #8 = Methodref          #3.#9          //  java/lang/Object."<init>":()V
    #9 = NameAndType        #5:#6          //  "<init>":()V
    #10 = Utf8               LineNumberTable
    #11 = Utf8               LocalVariableTable
    #12 = Utf8               this
    #13 = Utf8               Ltest/Test1;
    #14 = Utf8               SourceFile
    #15 = Utf8               Test1.java
    #16 = Utf8               someInjectedMethod
    #17 = Utf8               java/lang/System
    #18 = Class              #17            //  java/lang/System
    #19 = Utf8               out
    #20 = Utf8               Ljava/io/PrintStream;
    #21 = NameAndType        #19:#20        //  out:Ljava/io/PrintStream;
    #22 = Fieldref           #18.#21        //  java/lang/System.out:Ljava/io/PrintStream;
    #23 = Utf8               injection example
    #24 = String             #23            //  injection example
    #25 = Utf8               java/io/PrintStream
    #26 = Class              #25            //  java/io/PrintStream
    #27 = Utf8               println
    #28 = Utf8               (Ljava/lang/String;)V
    #29 = NameAndType        #27:#28        //  println:(Ljava/lang/String;)V
    #30 = Methodref          #26.#29        //  java/io/PrintStream.println:(Ljava/lang/String;)V
    #31 = Utf8               RuntimeVisibleAnnotations
    #32 = Utf8               Ltest/TestAnnotationToShowItInConstantTable;
    #33 = Utf8               Synthetic
    {
    public com.qubit.augmentation.test.Test1();
    Signature: ()V
    flags: ACC_PUBLIC
    
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0       
         1: invokespecial #8                  // Method java/lang/Object."<init>":()V
         4: return        
      LineNumberTable:
        line 3: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0       5     0  this   Ltest/Test1;
    
    protected void someInjectedMethod();
    Signature: ()V
    flags: ACC_PROTECTED
    
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #24                 // String injection example
         5: invokevirtual #30                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return        
    RuntimeVisibleAnnotations:
      0: #32()
    Synthetic: true
    }
    

    Notice that both the class and the method have the attribute Synthetic: true which mean they are Synthetic but, you the synthetic symbol must also be present in the constant pool (check #33).

    Another example regarding the use of constant pool and class/method attributes is the annotation added to someInjectedMethod with runtime retention policy. The method’s bytecode only has a reference to the constant pool #32 symbol, and only there you learn that
    the annotation is from the type test/TestAnnotationToShowItInConstantTable;

    Hope things got a bit more clear for you now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to analyse byte code with javassist using simple class MyData :
I have been using Javassist to dynamically manipulate classes as they are loaded. While
I'm using javassist.util.proxy.ProxyFactory to create some proxies in my project. And in some other
I'm using Javassist to generate a class foo , with method bar , but
I am dynamically generating new classes (using Javassist) and I would like to be
I need help I'm using EJS javasript template to write a code snippet two
I'm creating a class using javassist and add annotation to it. When I use
I'm using Javassist in a code generator I'm writing. It's pretty nice, but I've
I am considering runtime byte-code generation/modification for a Java project. Two important, and still
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.