parse InvokeDynamic constants

This commit is contained in:
Volker Berlin 2019-10-26 12:23:36 +02:00
parent 81c865706c
commit 431d5f5a4a
2 changed files with 77 additions and 3 deletions

View File

@ -0,0 +1,63 @@
/*
Copyright 2019 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package de.inetsoftware.classparser;
/**
* @author Volker Berlin
*/
public class ConstantInvokeDynamic implements Member {
private final ConstantNameAndType nameAndType;
/**
* Invoke dynamic info in the constant pool.
* https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.4.10
*
* @param bootstrapMethodAttrIndex
* a valid index into the bootstrap_methods array of the bootstrap method table
* @param nameAndType
* the name and type
*/
ConstantInvokeDynamic( int bootstrapMethodAttrIndex, ConstantNameAndType nameAndType ) {
this.nameAndType = nameAndType;
}
/**
* {@inheritDoc}
*/
@Override
public String getName() {
return nameAndType.getName();
}
/**
* {@inheritDoc}
*/
@Override
public String getClassName() {
return null;
}
/**
* Get the type of the method. For example "(Ljava.lang.String;)I"
*/
@Override
public String getType() {
return nameAndType.getType();
}
}

View File

@ -27,8 +27,7 @@ public class ConstantPool {
private final Object[] constantPool;
/**
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4
* http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#20080
* https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.4
*
* @param input
* the stream of the class
@ -60,6 +59,7 @@ public class ConstantPool {
break;
case 7: //CONSTANT_Class
case 8: //CONSTANT_String
case 16: // CONSTANT_MethodType
case 19: // CONSTANT_Module
case 20: // CONSTANT_Package
pool[i] = new int[] { type, input.readUnsignedShort() };
@ -68,8 +68,12 @@ public class ConstantPool {
case 10: //CONSTANT_Methodref
case 11: //CONSTANT_InterfaceMethodref
case 12: //CONSTANT_NameAndType
case 18: // CONSTANT_InvokeDynamic
pool[i] = new int[] { type, input.readUnsignedShort(), input.readUnsignedShort() };
break;
case 15: // CONSTANT_MethodHandle
pool[i] = new int[] { type, input.readUnsignedByte(), input.readUnsignedShort() };
break;
default:
throw new IOException( "Unknown constant pool type: " + type );
}
@ -85,7 +89,8 @@ public class ConstantPool {
case 7: //CONSTANT_Class
pool[i] = new ConstantClass( (String)pool[data[1]] );
break;
case 8: //CONSTANT_String
case 8: // CONSTANT_String
case 16: // CONSTANT_MethodType
case 19: // CONSTANT_Module
case 20: // CONSTANT_Package
pool[i] = pool[data[1]];
@ -114,6 +119,12 @@ public class ConstantPool {
case 12: //CONSTANT_NameAndType
pool[i] = new ConstantNameAndType( (String)pool[data[1]], (String)pool[data[2]] );
break;
case 15: // CONSTANT_MethodHandle
pool[i] = pool[data[2]];
break;
case 18: // CONSTANT_InvokeDynamic
pool[i] = new ConstantInvokeDynamic( data[1], (ConstantNameAndType)pool[data[2]] );
break;
default:
throw new IOException( "Unknown constant pool type: " + data[0] );
}