I am trying to learn about how classes work. I am trying to add a final field to my class using this code
dout.writeShort(1);//field count
dout.writeShort(Modifier.PUBLIC|Modifier.STATIC|Modifier.FINAL);//modifiers
dout.writeShort(utfConstant("jjj"));//name
dout.writeShort(utfConstant("I"));//signature
dout.writeShort(1);//attribute count
dout.writeShort(utfConstant("ConstantValue"));//constant value attribute
dout.writeShort(2);//size of attribute
dout.writeShort(intConstant(8));//value
But i am getting this error when i try to compile it
Exception in thread "main" java.lang.ClassFormatError: Invalid ConstantValue field attribute length 131082 in class file Test
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at Bytecode.BytecodeTest$BytecodeClassLoader.buildClass(BytecodeTest.java:161)
at Bytecode.BytecodeTest.makeClass(BytecodeTest.java:39)
at Bytecode.BytecodeTest.buildClass(BytecodeTest.java:24)
at Bytecode.BytecodeTest.main(BytecodeTest.java:17)
Here is my other code
private void writeConstantPool(DataOutputStream dout) throws IOException
{
dout.writeShort(poolIndex);
for (Data data : poolMap)
{
int tag = (byte) data.getData()[0];
dout.writeByte(tag);
switch (tag)
{
case CONSTANT_Utf8:
dout.writeUTF((String) data.getData()[1]);
break;
case CONSTANT_Class:
dout.writeShort((Integer) data.getData()[1]);
break;
case CONSTANT_Integer:
dout.writeInt((Integer) data.getData()[1]);
break;
default:
throw new RuntimeException();
}
}
}
private int utfConstant(String s)
{
return constant(CONSTANT_Utf8, s);
}
private int intConstant(int i)
{
return constant(CONSTANT_Integer, i);
}
private int classConstant(String s)
{
int classNameIndex = utfConstant(s.replace('.', '/'));
return constant(CONSTANT_Class, classNameIndex);
}
private int constant(Object... data)
{
Data constantData = new Data(data);
if (poolMap.contains(constantData))
return poolMap.indexOf(constantData)+1;
poolMap.add(poolIndex-1, constantData);
return poolIndex++;
}
private final Stack<Data> poolMap = new Stack<Data>();
private int poolIndex = 1;
private static class Data
{
private Object[] data;
public Data(Object... data)
{
this.data = data;
}
public Object[] getData()
{
return data;
}
public boolean equals(Object o)
{
if(o instanceof Data)
{
Data other = (Data) o;
return Arrays.equals(data, other.getData());
}
return false;
}
}
The way im doing this is by generating all the methods and fields and then generating the constant pool, and then i am putting the constant pool in the byte array before the methods and fields.
Can anyone tell me what i am doing wrong i could show more code if necessary, additionally i think the problem might be with intConstant or the constant value attribute.
After looking at this with a bytecode editor it looks like the intConstant method works fine so i think it is the field code that isnt working. Also i looked at a constant in a bytecode editor and it looked the same i am confused.
I believe the length
of an attribute is supposed to be a 32-bit integer, not a 16-bit short.