summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuchaz/enigma/bytecode/MethodParametersAttribute.java')
-rw-r--r--src/cuchaz/enigma/bytecode/MethodParametersAttribute.java65
1 files changed, 27 insertions, 38 deletions
diff --git a/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java b/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java
index baf1ac1..bf95956 100644
--- a/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java
+++ b/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java
@@ -20,45 +20,38 @@ import javassist.bytecode.AttributeInfo;
20import javassist.bytecode.ConstPool; 20import javassist.bytecode.ConstPool;
21import javassist.bytecode.MethodInfo; 21import javassist.bytecode.MethodInfo;
22 22
23public class MethodParametersAttribute extends AttributeInfo 23public class MethodParametersAttribute extends AttributeInfo {
24{ 24
25 private MethodParametersAttribute( ConstPool pool, List<Integer> parameterNameIndices ) 25 private MethodParametersAttribute(ConstPool pool, List<Integer> parameterNameIndices) {
26 { 26 super(pool, "MethodParameters", writeStruct(parameterNameIndices));
27 super( pool, "MethodParameters", writeStruct( parameterNameIndices ) );
28 } 27 }
29 28
30 public static void updateClass( MethodInfo info, List<String> names ) 29 public static void updateClass(MethodInfo info, List<String> names) {
31 {
32 // add the names to the class const pool 30 // add the names to the class const pool
33 ConstPool constPool = info.getConstPool(); 31 ConstPool constPool = info.getConstPool();
34 List<Integer> parameterNameIndices = new ArrayList<Integer>(); 32 List<Integer> parameterNameIndices = new ArrayList<Integer>();
35 for( String name : names ) 33 for (String name : names) {
36 { 34 if (name != null) {
37 if( name != null ) 35 parameterNameIndices.add(constPool.addUtf8Info(name));
38 { 36 } else {
39 parameterNameIndices.add( constPool.addUtf8Info( name ) ); 37 parameterNameIndices.add(0);
40 }
41 else
42 {
43 parameterNameIndices.add( 0 );
44 } 38 }
45 } 39 }
46 40
47 // add the attribute to the method 41 // add the attribute to the method
48 info.addAttribute( new MethodParametersAttribute( constPool, parameterNameIndices ) ); 42 info.addAttribute(new MethodParametersAttribute(constPool, parameterNameIndices));
49 } 43 }
50 44
51 private static byte[] writeStruct( List<Integer> parameterNameIndices ) 45 private static byte[] writeStruct(List<Integer> parameterNameIndices) {
52 {
53 // JVM 8 Spec says the struct looks like this: 46 // JVM 8 Spec says the struct looks like this:
54 // http://cr.openjdk.java.net/~mr/se/8/java-se-8-fr-spec-01/java-se-8-jvms-fr-diffs.pdf 47 // http://cr.openjdk.java.net/~mr/se/8/java-se-8-fr-spec-01/java-se-8-jvms-fr-diffs.pdf
55 // uint8 num_params 48 // uint8 num_params
56 // for each param: 49 // for each param:
57 // uint16 name_index -> points to UTF8 entry in constant pool, or 0 for no entry 50 // uint16 name_index -> points to UTF8 entry in constant pool, or 0 for no entry
58 // uint16 access_flags -> don't care, just set to 0 51 // uint16 access_flags -> don't care, just set to 0
59 52
60 ByteArrayOutputStream buf = new ByteArrayOutputStream(); 53 ByteArrayOutputStream buf = new ByteArrayOutputStream();
61 DataOutputStream out = new DataOutputStream( buf ); 54 DataOutputStream out = new DataOutputStream(buf);
62 55
63 // NOTE: java hates unsigned integers, so we have to be careful here 56 // NOTE: java hates unsigned integers, so we have to be careful here
64 // the writeShort(), writeByte() methods will read 16,8 low-order bits from the int argument 57 // the writeShort(), writeByte() methods will read 16,8 low-order bits from the int argument
@@ -66,31 +59,27 @@ public class MethodParametersAttribute extends AttributeInfo
66 // if the int is out of range, the byte stream won't look the way we want and weird things will happen 59 // if the int is out of range, the byte stream won't look the way we want and weird things will happen
67 final int SIZEOF_UINT8 = 1; 60 final int SIZEOF_UINT8 = 1;
68 final int SIZEOF_UINT16 = 2; 61 final int SIZEOF_UINT16 = 2;
69 final int MAX_UINT8 = ( 1 << 8 ) - 1; 62 final int MAX_UINT8 = (1 << 8) - 1;
70 final int MAX_UINT16 = ( 1 << 16 ) - 1; 63 final int MAX_UINT16 = (1 << 16) - 1;
71 64
72 try 65 try {
73 { 66 assert (parameterNameIndices.size() >= 0 && parameterNameIndices.size() <= MAX_UINT8);
74 assert( parameterNameIndices.size() >= 0 && parameterNameIndices.size() <= MAX_UINT8 ); 67 out.writeByte(parameterNameIndices.size());
75 out.writeByte( parameterNameIndices.size() );
76 68
77 for( Integer index : parameterNameIndices ) 69 for (Integer index : parameterNameIndices) {
78 { 70 assert (index >= 0 && index <= MAX_UINT16);
79 assert( index >= 0 && index <= MAX_UINT16 ); 71 out.writeShort(index);
80 out.writeShort( index );
81 72
82 // just write 0 for the access flags 73 // just write 0 for the access flags
83 out.writeShort( 0 ); 74 out.writeShort(0);
84 } 75 }
85 76
86 out.close(); 77 out.close();
87 byte[] data = buf.toByteArray(); 78 byte[] data = buf.toByteArray();
88 assert( data.length == SIZEOF_UINT8 + parameterNameIndices.size()*( SIZEOF_UINT16 + SIZEOF_UINT16 ) ); 79 assert (data.length == SIZEOF_UINT8 + parameterNameIndices.size() * (SIZEOF_UINT16 + SIZEOF_UINT16));
89 return data; 80 return data;
90 } 81 } catch (IOException ex) {
91 catch( IOException ex ) 82 throw new Error(ex);
92 {
93 throw new Error( ex );
94 } 83 }
95 } 84 }
96} 85}