0

I use bytebuddy-agent to add a dynamic field for Jedis class, and in JedisCluster constructor, there will be three Jedis instance created which will cause JVM loading Jedis class. What make me confused is that when I put code Jedis jedis = new Jedis("localhost", 6379) in front of code JedisCluster cluster = new JedisCluster(nodes), bytebuddy-agent add dynamic field to Jedis class successfully as follow.

@Test
public void testOnConstruct() throws Exception {
   Jedis jedis = new Jedis("localhost", 6379);
   Set<HostAndPort> nodes = new HashSet<>(3);
   nodes.add(new HostAndPort("192.168.146.128", 7001));
   nodes.add(new HostAndPort("192.168.146.128", 7002));
   nodes.add(new HostAndPort("192.168.146.128", 7003));
   JedisCluster cluster = new JedisCluster(nodes);
}

If I put Jedis jedis = new Jedis("localhost", 6379) after JedisCluster cluster = new JedisCluster(nodes), bytebuddy-agent can't add dynamic field to Jedis class successfully.

I need you help, thanks.

Dmitry
  • 6,716
  • 14
  • 37
  • 39
MengZhi
  • 51
  • 5

1 Answers1

0

It is impossible to add a field to a class that is already loaded, the JVM prohibits that. In order to do so, you would need to add the field prior to the first loading.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • It's not the problem of adding a field to a class that is already loaded, It's the order of instantiating Jedis and JedisCluster, if the instantiation of Jedis before the Instantiation of JedisCluster, the test code works well, if the instantiation of Jedis after the intantiation of JedisCluster, the test code faild. – MengZhi Nov 01 '17 at 02:32
  • The instantiation of JedisCluster triger the loading of Jedis. Can bytebuddy-agent add a dynamic field to the class where the another class triger that's loading in instantiation ? – MengZhi Nov 01 '17 at 02:48
  • You have to instrument this class in order to add that field and you need to do so before it is loaded for the first time. – Rafael Winterhalter Nov 01 '17 at 21:06