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