This commit is contained in:
Volker Berlin 2020-01-26 12:39:00 +01:00
parent 43b41d4fad
commit f64ed4feee
2 changed files with 46 additions and 58 deletions

View File

@ -226,6 +226,11 @@ public class ClassFile {
return bootstrapMethods[methodIdx]; return bootstrapMethods[methodIdx];
} }
/**
* Get the constant pool of the the current class.
*
* @return the constant pool
*/
public ConstantPool getConstantPool() { public ConstantPool getConstantPool() {
return constantPool; return constantPool;
} }

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2011 - 2019 Volker Berlin (i-net software) Copyright 2011 - 2020 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -46,13 +46,15 @@ public class Code {
private LocalVariableTable localVariableTable; private LocalVariableTable localVariableTable;
/** /**
* The code of a method attribute. * The code of a method attribute. http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.3
* http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.3
* http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1546 * http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#1546
* *
* @param input * @param input
* the stream of the code attribute
* @param constantPool * @param constantPool
* the ConstantPool of the class
* @throws IOException * @throws IOException
* if an I/O error occurs
*/ */
Code( DataInputStream input, @Nonnull ConstantPool constantPool ) throws IOException { Code( DataInputStream input, @Nonnull ConstantPool constantPool ) throws IOException {
this.constantPool = constantPool; this.constantPool = constantPool;
@ -68,19 +70,36 @@ public class Code {
attributes = new Attributes( input, constantPool ); attributes = new Attributes( input, constantPool );
} }
/**
* Get the constant pool of this code.
*
* @return the ConstantPool of the class
*/
@Nonnull @Nonnull
public ConstantPool getConstantPool(){ public ConstantPool getConstantPool() {
return constantPool; return constantPool;
} }
/**
* Get exception table of this code block.
*
* @return the table, can be empty
*/
@Nonnull @Nonnull
public TryCatchFinally[] getExceptionTable() { public TryCatchFinally[] getExceptionTable() {
return exceptionTable; return exceptionTable;
} }
/**
* Get the line number table. is null if the code was optimized.
*
* @return the table or null
* @throws IOException
* if any I/O error occur
*/
@Nullable @Nullable
public LineNumberTable getLineNumberTable() throws IOException { public LineNumberTable getLineNumberTable() throws IOException {
if( lineNumberTable != null ){ if( lineNumberTable != null ) {
return lineNumberTable; return lineNumberTable;
} }
AttributeInfo data = attributes.get( "LineNumberTable" ); AttributeInfo data = attributes.get( "LineNumberTable" );
@ -98,8 +117,8 @@ public class Code {
* if any I/O error occur * if any I/O error occur
*/ */
@Nullable @Nullable
public LocalVariableTable getLocalVariableTable() throws IOException{ public LocalVariableTable getLocalVariableTable() throws IOException {
if( localVariableTable != null ){ if( localVariableTable != null ) {
return localVariableTable; return localVariableTable;
} }
AttributeInfo data = attributes.get( "LocalVariableTable" ); AttributeInfo data = attributes.get( "LocalVariableTable" );
@ -116,22 +135,13 @@ public class Code {
public int getFirstLineNr() throws IOException { public int getFirstLineNr() throws IOException {
LineNumberTable table = getLineNumberTable(); LineNumberTable table = getLineNumberTable();
if( table == null ){ if( table == null ) {
return -1; return -1;
} else { } else {
return table.getMinLineNr(); return table.getMinLineNr();
} }
} }
public int getLastLineNr() throws IOException {
LineNumberTable table = getLineNumberTable();
if( table == null ){
return -1;
} else {
return table.getMaxLineNr();
}
}
/** /**
* Get the stream of Java Byte code instruction of this method. * Get the stream of Java Byte code instruction of this method.
* *
@ -141,39 +151,12 @@ public class Code {
return new CodeInputStream( codeData, 0, codeData.length, this ); return new CodeInputStream( codeData, 0, codeData.length, this );
} }
public int getCodeSize(){ /**
* Get the last position of the code.
*
* @return the size.
*/
public int getCodeSize() {
return codeData.length; return codeData.length;
} }
public boolean startWithSuperInit( ConstantClass superClass ){
if( codeData.length >= 4 && codeData[0] == 0x2a && codeData[1] == (byte)0xb7) {
int idx = ((codeData[2] & 0xFF << 8)) + (codeData[3] & 0xFF);
ConstantMethodRef method = (ConstantMethodRef)constantPool.get( idx );
return method.getConstantClass() == superClass;
}
return false;
}
/**
* Check if the code only contains the default constructor code:
* <pre><code>
* super.&lt;init&gt;;
* return;
* </code></pre>
* In this case the constructor will not be printed
* @param superClass
* @return
*/
public boolean isSuperInitReturn( ConstantClass superClass ) {
if( codeData.length == 5 && codeData[0] == 0x2a && codeData[1] == (byte)0xb7 && codeData[4] == (byte)0xb1 ) {
int idx = ((codeData[2] & 0xFF << 8)) + (codeData[3] & 0xFF);
ConstantMethodRef method = (ConstantMethodRef)constantPool.get( idx );
return method.getConstantClass() == superClass;
}
return false;
}
public byte[] getCodeData() {
return codeData;
}
} }