javassist icon indicating copy to clipboard operation
javassist copied to clipboard

Why is ConstPool api package-private?

Open Enerccio opened this issue 4 years ago • 0 comments

Honestly, why can't I manipulate constants directly? There is one method to replace class but I can't swap constants at all, without doing this bullshit:

private void renameString(ConstPool cp, String orig, String target) throws Exception {
	int ix = cp.addStringInfo(orig);
	
	Method m;
	Field f;
	
	m = cp.getClass().getDeclaredMethod("getItem", int.class);
	m.setAccessible(true);
	Object info = m.invoke(cp, ix);
	
	f = info.getClass().getDeclaredField("string");
	f.setAccessible(true);
	int stringIx = f.getInt(info);
	
	m = cp.getClass().getDeclaredMethod("getItem", int.class);
	m.setAccessible(true);
	Object utf8Info = m.invoke(cp, stringIx);
	
	f = utf8Info.getClass().getDeclaredField("string");
	f.setAccessible(true);
	f.set(utf8Info, target);
}

Changing a constant should be supported and it would if it was not blocked by design by making it package-private. Wtf...

Enerccio avatar Jul 15 '20 10:07 Enerccio