0

I have a java class files whose constant pool consist some ConstantUtf8 data as
75. CONSTANT_Utf8 : SampleString
95. CONSTANT_Utf8 : SampleString
means same data on different index, I have written the following code:

ConstantPoolGen cp = classGen.getConstantPool();
    int a = cp.lookupUtf8("SampleString");
    if(a != -1)
    {    
        cp.setConstant(a, new ConstantUtf8("OtherString"));
        System.out.println("Found and Replaced");  
    }
    else
    {
        System.out.println("Not Found!");
    }

The above code replaces "SampleString" with "OtherString" at index 95, but I want to replace all occurrence so I have added a loop like this,

for(int i=0; i<2; i++){ 
int a = cp.lookupUtf8("SampleString");
if(a != -1)
{    
    cp.setConstant(a, new ConstantUtf8("OtherString"));
    System.out.println("Found and Replaced");  
}
else
{
    System.out.println("Not Found!");
}
}

so that it will go through both the index i.e 75 and 95 and replace with new value, but Unfortunately it producing the same result as above means only replacing one occurrence i.e at 75. What can be done to replace all?

ssa
  • 199
  • 5
  • 13
  • 1
    Note that if you used ASM, it would automatically merge identical constant pool entries. – Antimony Dec 19 '13 at 14:59
  • @Antimony ,I have never used ASM and I have only little experience with BCEL and javassist,so can you help me regarding above problem with these libraries OR can you tell me codes to search and replace ConstantUtf8 data from ConstantPool using ASM. – ssa Dec 19 '13 at 19:05

0 Answers0