diff options
Diffstat (limited to 'src/cuchaz/enigma/bytecode/MethodParametersAttribute.java')
| -rw-r--r-- | src/cuchaz/enigma/bytecode/MethodParametersAttribute.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java b/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java new file mode 100644 index 0000000..512e65a --- /dev/null +++ b/src/cuchaz/enigma/bytecode/MethodParametersAttribute.java | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.bytecode; | ||
| 12 | |||
| 13 | import java.io.ByteArrayOutputStream; | ||
| 14 | import java.io.DataOutputStream; | ||
| 15 | import java.io.IOException; | ||
| 16 | import java.util.ArrayList; | ||
| 17 | import java.util.List; | ||
| 18 | |||
| 19 | import javassist.bytecode.AttributeInfo; | ||
| 20 | import javassist.bytecode.ConstPool; | ||
| 21 | import javassist.bytecode.MethodInfo; | ||
| 22 | |||
| 23 | public class MethodParametersAttribute extends AttributeInfo { | ||
| 24 | |||
| 25 | private MethodParametersAttribute(ConstPool pool, List<Integer> parameterNameIndices) { | ||
| 26 | super(pool, "MethodParameters", writeStruct(parameterNameIndices)); | ||
| 27 | } | ||
| 28 | |||
| 29 | public static void updateClass(MethodInfo info, List<String> names) { | ||
| 30 | |||
| 31 | // add the names to the class const pool | ||
| 32 | ConstPool constPool = info.getConstPool(); | ||
| 33 | List<Integer> parameterNameIndices = new ArrayList<Integer>(); | ||
| 34 | for (String name : names) { | ||
| 35 | if (name != null) { | ||
| 36 | parameterNameIndices.add(constPool.addUtf8Info(name)); | ||
| 37 | } else { | ||
| 38 | parameterNameIndices.add(0); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | // add the attribute to the method | ||
| 43 | info.addAttribute(new MethodParametersAttribute(constPool, parameterNameIndices)); | ||
| 44 | } | ||
| 45 | |||
| 46 | private static byte[] writeStruct(List<Integer> parameterNameIndices) { | ||
| 47 | // JVM 8 Spec says the struct looks like this: | ||
| 48 | // http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.24 | ||
| 49 | // uint8 num_params | ||
| 50 | // for each param: | ||
| 51 | // uint16 name_index -> points to UTF8 entry in constant pool, or 0 for no entry | ||
| 52 | // uint16 access_flags -> don't care, just set to 0 | ||
| 53 | |||
| 54 | ByteArrayOutputStream buf = new ByteArrayOutputStream(); | ||
| 55 | DataOutputStream out = new DataOutputStream(buf); | ||
| 56 | |||
| 57 | // NOTE: java hates unsigned integers, so we have to be careful here | ||
| 58 | // the writeShort(), writeByte() methods will read 16,8 low-order bits from the int argument | ||
| 59 | // as long as the int argument is in range of the unsigned short/byte type, it will be written as an unsigned short/byte | ||
| 60 | // if the int is out of range, the byte stream won't look the way we want and weird things will happen | ||
| 61 | final int SIZEOF_UINT8 = 1; | ||
| 62 | final int SIZEOF_UINT16 = 2; | ||
| 63 | final int MAX_UINT8 = (1 << 8) - 1; | ||
| 64 | final int MAX_UINT16 = (1 << 16) - 1; | ||
| 65 | |||
| 66 | try { | ||
| 67 | assert (parameterNameIndices.size() >= 0 && parameterNameIndices.size() <= MAX_UINT8); | ||
| 68 | out.writeByte(parameterNameIndices.size()); | ||
| 69 | |||
| 70 | for (Integer index : parameterNameIndices) { | ||
| 71 | assert (index >= 0 && index <= MAX_UINT16); | ||
| 72 | out.writeShort(index); | ||
| 73 | |||
| 74 | // just write 0 for the access flags | ||
| 75 | out.writeShort(0); | ||
| 76 | } | ||
| 77 | |||
| 78 | out.close(); | ||
| 79 | byte[] data = buf.toByteArray(); | ||
| 80 | assert (data.length == SIZEOF_UINT8 + parameterNameIndices.size() * (SIZEOF_UINT16 + SIZEOF_UINT16)); | ||
| 81 | return data; | ||
| 82 | } catch (IOException ex) { | ||
| 83 | throw new Error(ex); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||