- private
- String
- generateMethodBodyCode
- TypeDescriptor returnTypeDescriptor)
- (MethodInfo methodInfo, List parametersTypeDescriptors,
- throws
- StringBuilder javaCode =
- IOException {
- new
- String currentMethodName = classFile.
- StringBuilder();
- getConstantPool
- ().
- getUtf8Info
- (methodInfo.
- getNameIndex
- ()).
- getValue
- ();
- //寻找CodeAttribute
- CodeAttribute codeAttribute =
- findAttribute
- (methodInfo, Attribute.
- Code
- );
- if
- (codeAttribute ==
- null
- ) {
- throw new
- RuntimeException(
- "无法在Method里找到CodeAttribute"
- Stack opStack =
- }
- );
- new
- Stack<>(
- /*codeAttribute.getMaxStack()*/
- List localVariableNames =
- );
- new
- ArrayList<>(codeAttribute.
- getMaxLocals
- ());
- //初始化本地变量表名,首先如果是实例方法,需要把this放入第一个,然后依次将方法参数名放入
- boolean
- isStaticMethod = methodInfo.
- getAccessFlags
- ().
- hasFlag
- (AccessFlags.
- ACC_STATIC
- );
- if
- localVariableNames.
- (!isStaticMethod) {
- add
- (
- "this"
- }
- );
- for
- (
- int
- x =
- 0
- ; x < parametersTypeDescriptors.
- size
- localVariableNames.
- (); x++) {
- add
- (
- "var"
- + (x +
- 1
- DataInputStream byteCodeInputStream =
- }
- ));
- new
- DataInputStream(
- new
- ByteArrayInputStream(codeAttribute.
- getCode
- ()));
- while
- (byteCodeInputStream.
- available
- () >
- 0
- ) {
- int
- opCode = byteCodeInputStream.
- readByte
- () &
- 0xff
- ;
- switch
- (opCode) {
- case
- System.
- OP_aload_0:
- out
- .
- println
- (
- "aload_0"
- opStack.
- );
- push
- (localVariableNames.
- get
- (
- 0
- ));
- break
- ;
- case
- OP_invokevirtual: {
- int
- methodRefIndex = byteCodeInputStream.
- readUnsignedShort
- System.
- ();
- out
- .
- println
- (
- "invokevirtual #"
- ConstantMethodRefInfo methodRefInfo = classFile.
- + methodRefIndex);
- getConstantPool
- ().
- getCPInfo
- ConstantNameAndTypeInfo nameAndTypeInfo = classFile.
- (methodRefIndex);
- getConstantPool
- ().
- getCPInfo
- (methodRefInfo.
- getNameAndTypeIndex
- String methodName = classFile.
- ());
- getConstantPool
- ().
- getUtf8Info
- (nameAndTypeInfo.
- getNameIndex
- ()).
- getValue
- String typeDescriptor = classFile.
- ();
- getConstantPool
- ().
- getUtf8Info
- (nameAndTypeInfo.
- getDescriptorIndex
- ()).
- getValue
- ();
- int
- methodParameterSize =
- new DescriptorParser
- (typeDescriptor).
- getParameterTypeDescriptors
- ().
- size
- Object parameterNames[] =
- Object targetClassName;
- ();
- new
- Object[methodParameterSize];
- for
- (
- int
- x =
- 0
- parameterNames[methodParameterSize - x -
- ; x < methodParameterSize; x++) {
- 1
- ] = opStack.
- pop
- targetClassName = opStack.
- }
- ();
- pop
- StringBuilder line =
- ();
- new
- line.
- StringBuilder();
- append
- (targetClassName).
- append
- (
- "."
- ).
- append
- (methodName).
- append
- (
- "("
- );
- for
- (
- int
- x =
- 0
- line.
- ; x < methodParameterSize; x++) {
- append
- (parameterNames[x]);
- if
- ((x != methodParameterSize -
- 1
- line.
- )) {
- append
- (
- ","
- line.
- }
- }
- );
- append
- (
- ");"
- opStack.
- );
- push
- (line.
- toString
- ());
- break
- }
- ;
- case
- OP_invokespecial: {
- int
- methodRefIndex = byteCodeInputStream.
- readUnsignedShort
- System.
- ();
- out
- .
- println
- (
- "invokespecial #"
- ConstantMethodRefInfo methodRefInfo = classFile.
- + methodRefIndex);
- getConstantPool
- ().
- getCPInfo
- ConstantNameAndTypeInfo nameAndTypeInfo = classFile.
- (methodRefIndex);
- getConstantPool
- ().
- getCPInfo
- (methodRefInfo.
- getNameAndTypeIndex
- String typeDescriptor = classFile.
- ());
- getConstantPool
- ().
- getUtf8Info
- (nameAndTypeInfo.
- getDescriptorIndex
- ()).
- getValue
- ();
- int
- methodParameterSize =
- new DescriptorParser
- (typeDescriptor).
- getParameterTypeDescriptors
- ().
- size
- Object parameterNames[] =
- Object targetClassName;
- ();
- new
- Object[methodParameterSize];
- if
- (methodParameterSize >
- 0
- ) {
- for
- (
- int
- x =
- 0
- parameterNames[methodParameterSize - x -
- ; x < methodParameterSize; x++) {
- 1
- ] = opStack.
- pop
- targetClassName = opStack.
- }
- }
- ();
- pop
- StringBuilder line =
- ();
- new
- StringBuilder();
- if
- (currentMethodName.
- equals
- (
- "<init>"
- ) && targetClassName.
- equals
- (
- "this"
- line.
- )) {
- append
- (
- "super"
- }
- );
- else
- line.
- {
- append
- (
- "new "
- ).
- append
- line.
- }
- (targetClassName);
- append
- (
- "("
- );
- for
- (
- int
- x =
- 0
- line.
- ; x < methodParameterSize; x++) {
- append
- (parameterNames[x]);
- if
- ((x != methodParameterSize -
- 1
- line.
- )) {
- append
- (
- ","
- line.
- }
- }
- );
- append
- (
- ");"
- opStack.
- );
- push
- (line.
- toString
- ());
- break
- }
- ;
- case
- System.
- OP_getstatic:
- out
- .
- println
- (
- "getstatic"
- );
- break
- ;
- case
- System.
- OP_return:
- out
- .
- println
- (
- "return"
- );
- break
- ;
- case
- OP_new: {
- int
- classIndex = byteCodeInputStream.
- readUnsignedShort
- System.
- ();
- out
- .
- println
- (
- "new #"
- ConstantClassInfo classInfo = classFile.
- + classIndex);
- getConstantPool
- ().
- getCPInfo
- opStack.
- (classIndex);
- push
- (classInfo.
- getName
- ());
- break
- }
- ;
- case
- System.
- OP_dup:
- out
- .
- println
- (
- "dup"
- Object top = opStack.
- );
- pop
- opStack.
- ();
- push
- opStack.
- (top);
- push
- (top);
- break
- ;
- case
- OP_ldc:
- int
- stringIndex = byteCodeInputStream.
- readByte
- () &
- 0xff
- System.
- ;
- out
- .
- println
- (
- "ldc #"
- ConstantStringInfo stringInfo = classFile.
- + stringIndex);
- getConstantPool
- ().
- getCPInfo
- String value = classFile.
- (stringIndex);
- getConstantPool
- ().
- getUtf8Info
- (stringInfo.
- getStringIndex
- ()).
- getValue
- opStack.
- ();
- push
- (value);
- break
- ;
- case
- System.
- OP_iload_1:
- out
- .
- println
- (
- "iload_1"
- opStack.
- );
- push
- (localVariableNames.
- get
- (
- 1
- ));
- break
- ;
- case
- System.
- OP_iload_2:
- out
- .
- println
- (
- "iload_2"
- opStack.
- );
- push
- (localVariableNames.
- get
- (
- 2
- ));
- break
- ;
- case
- System.
- OP_iadd:
- out
- .
- println
- (
- "iadd"
- opStack.
- );
- push
- (opStack.
- pop
- () +
- "+"
- + opStack.
- pop
- ());
- break
- ;
- case
- System.
- OP_ireturn:
- out
- .
- println
- (
- "ireturn"
- opStack.
- );
- push
- (
- "return "
- + opStack.
- pop
- ());
- break
- ;
- case
- System.
- OP_iconst_0:
- out
- .
- println
- (
- "iconst_0"
- opStack.
- );
- push
- (
- "0"
- );
- break
- ;
- case
- System.
- OP_iconst_1:
- out
- .
- println
- (
- "iconst_1"
- opStack.
- );
- push
- (
- "1"
- );
- break
- ;
- case
- System.
- OP_iconst_2:
- out
- .
- println
- (
- "iconst_2"
- opStack.
- );
- push
- (
- "2"
- );
- break
- ;
- case
- System.
- OP_astore_1: {
- out
- .
- println
- (
- "astore_1"
- String obj = opStack.
- );
- pop
- ().
- toString
- String className = opStack.
- ();
- pop
- ().
- toString
- localVariableNames.
- ();
- add
- (
- 1
- ,
- "localVar1"
- opStack.
- );
- push
- (className +
- " localVar1="
- + obj);
- break
- }
- ;
- case
- System.
- OP_astore_2: {
- out
- .
- println
- (
- "astore_2"
- String obj = opStack.
- );
- pop
- ().
- toString
- String className = opStack.
- ();
- pop
- ().
- toString
- localVariableNames.
- ();
- add
- (
- 1
- ,
- "localVar2"
- opStack.
- );
- push
- (className +
- " localVar2="
- + obj);
- break
- }
- ;
- case
- System.
- OP_astore_3: {
- out
- .
- println
- (
- "astore_3"
- String obj = opStack.
- );
- pop
- ().
- toString
- String className = opStack.
- ();
- pop
- ().
- toString
- localVariableNames.
- ();
- add
- (
- 1
- ,
- "localVar3"
- opStack.
- );
- push
- (className +
- " localVar3="
- + obj);
- break
- }
- ;
- case
- System.
- OP_aload_1:
- out
- .
- println
- (
- "aload_1"
- opStack.
- );
- push
- (localVariableNames.
- get
- (
- 1
- ));
- break
- ;
- case
- System.
- OP_pop:
- out
- .
- println
- (
- "pop"
- );
- //opStack.pop();
- break
- ;
- default
- :
- throw new
- RuntimeException(
- "Unknow opCode:0x"
- + opCode +
- " "
- }
- }
- + currentMethodName);
- for
- javaCode.
- (Object s : opStack) {
- append
- (
- " "
- ).
- append
- (s).
- append
- (
- "\r\n"
- }
- );
- return
- javaCode.
- toString
- }
- ();
来源: http://www.cnblogs.com/techspace/p/7163879.html