Mirror of the JODE repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jode/jode/src/net/sf/jode/bytecode/ConstantPool.java

274 lines
8.1 KiB

/* ConstantPool Copyright (C) 1998-1999 Jochen Hoenicke.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
*/
package net.sf.jode.bytecode;
import java.io.DataInputStream;
import java.io.IOException;
Documentation updates (INSTALL, javadoc). Added JUnit Test cases. * build.xml: Big update. * net/sf/jode/bytecode/BasicBlock.java: (updateMaxStackLocals): new method to calculate maxStack and maxLocals. (setBlocks): fixed calculation of handlers, call updateMaxLocals. * net/sf/jode/bytecode/BasicBlockReader.java: (maxLocals, maxStack): new fields. (readCode): read maxStack/Locals into private fields. (convert): check that maxStack/Locals match what we calculate. * net/sf/jode/bytecode/BinaryInfo.java: (getKnownAttributeCount): renamed to... (getAttributeCount): ... this, and also count internal attributes. Made it protected. (readAttribute): made protected. (drop): made protected. (prepareAttributes): made protected. (writeKnownAttributes): removed. (writeAttributes): made protected, use getAttributeCount. Changed policy: it doesn't call writeKnownAttribute, but instead it expects sub classes to override this method. (getAttributeSize): made protected, subclasses should override it. Changed all subclasses to new policy. * net/sf/jode/bytecode/Block.java: (lineNr): Removed, it wasn't used. (pop,push): Removed, replaced by ... (maxpop,maxpush,delta): ... these, with slightly changed semantics. (stackHeight): New variable. (Block): Default Constructor doesn't initialize fields now. (getCatchers): Renamed to ... (getHandlers): ... this, changed all callers. (initCode): Calculate maxpop, maxpush, delta correctly. (getStackPopPush): Changed accordingly to new fields. (setCode): Removed debugging output for illegal contents. * net/sf/jode/bytecode/Classes.java: Reworked handling of inner classes. (innerClasses): Field mustn't be null anymore when loaded. (setName): Update class in classpath. * net/sf/jode/bytecode/ClassPath.java: (renameClassInfo): new function, should only used by ClassInfo. * net/sf/jode/bytecode/ConstantPool.java: made public. (getUTF8,getRef,getClassType,getClassName): Don't allow the 0 index. (iterateClassNames): New method. * net/sf/jode/decompiler/Main.java: (decompileClass): Catch ClassFormatExceptions and decompile remaining classes. * net/sf/jode/obfuscator/ClassIdentifier.java: Updated handling of inner/extra classes to new ClassInfo behaviour. (initSuperClasses): Load DECLARATION of super classes. * net/sf/jode/obfuscator/PackageIdentifier.java: Replace deprecated methods of ClassInfo with corresponding classpath calls. (loadMatchingClasses): Initialize packages loaded on demand if we are initialize. * net/sf/jode/obfuscator/modules/ConstantAnalyzer.java: Now extends SimpleAnalyzer. (canonizeIfaceRef): Removed; it is now inherited. (canonizeRef): likewise. Big updates to handle jsr correctly. (handleOpcode): Moved method to BlockInfo. * net/sf/jode/obfuscator/modules/SimpleAnalyzer.java: (canonizeIfaceRef): New method, copied from ConstantAnalyzer. (canonizeRef): call canonizeIfaceRef for interfaces. * net/sf/jode/util/UnifyHash.java (iterateHashCode): iterator now supports remove(). (remove): New method. git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@1337 379699f6-c40d-0410-875b-85095c16579e
23 years ago
import java.util.NoSuchElementException;
///#def COLLECTIONS java.util
import java.util.Iterator;
///#enddef
///#def COLLECTIONEXTRA java.lang
import java.lang.UnsupportedOperationException;
///#enddef
/**
Documentation updates (INSTALL, javadoc). Added JUnit Test cases. * build.xml: Big update. * net/sf/jode/bytecode/BasicBlock.java: (updateMaxStackLocals): new method to calculate maxStack and maxLocals. (setBlocks): fixed calculation of handlers, call updateMaxLocals. * net/sf/jode/bytecode/BasicBlockReader.java: (maxLocals, maxStack): new fields. (readCode): read maxStack/Locals into private fields. (convert): check that maxStack/Locals match what we calculate. * net/sf/jode/bytecode/BinaryInfo.java: (getKnownAttributeCount): renamed to... (getAttributeCount): ... this, and also count internal attributes. Made it protected. (readAttribute): made protected. (drop): made protected. (prepareAttributes): made protected. (writeKnownAttributes): removed. (writeAttributes): made protected, use getAttributeCount. Changed policy: it doesn't call writeKnownAttribute, but instead it expects sub classes to override this method. (getAttributeSize): made protected, subclasses should override it. Changed all subclasses to new policy. * net/sf/jode/bytecode/Block.java: (lineNr): Removed, it wasn't used. (pop,push): Removed, replaced by ... (maxpop,maxpush,delta): ... these, with slightly changed semantics. (stackHeight): New variable. (Block): Default Constructor doesn't initialize fields now. (getCatchers): Renamed to ... (getHandlers): ... this, changed all callers. (initCode): Calculate maxpop, maxpush, delta correctly. (getStackPopPush): Changed accordingly to new fields. (setCode): Removed debugging output for illegal contents. * net/sf/jode/bytecode/Classes.java: Reworked handling of inner classes. (innerClasses): Field mustn't be null anymore when loaded. (setName): Update class in classpath. * net/sf/jode/bytecode/ClassPath.java: (renameClassInfo): new function, should only used by ClassInfo. * net/sf/jode/bytecode/ConstantPool.java: made public. (getUTF8,getRef,getClassType,getClassName): Don't allow the 0 index. (iterateClassNames): New method. * net/sf/jode/decompiler/Main.java: (decompileClass): Catch ClassFormatExceptions and decompile remaining classes. * net/sf/jode/obfuscator/ClassIdentifier.java: Updated handling of inner/extra classes to new ClassInfo behaviour. (initSuperClasses): Load DECLARATION of super classes. * net/sf/jode/obfuscator/PackageIdentifier.java: Replace deprecated methods of ClassInfo with corresponding classpath calls. (loadMatchingClasses): Initialize packages loaded on demand if we are initialize. * net/sf/jode/obfuscator/modules/ConstantAnalyzer.java: Now extends SimpleAnalyzer. (canonizeIfaceRef): Removed; it is now inherited. (canonizeRef): likewise. Big updates to handle jsr correctly. (handleOpcode): Moved method to BlockInfo. * net/sf/jode/obfuscator/modules/SimpleAnalyzer.java: (canonizeIfaceRef): New method, copied from ConstantAnalyzer. (canonizeRef): call canonizeIfaceRef for interfaces. * net/sf/jode/util/UnifyHash.java (iterateHashCode): iterator now supports remove(). (remove): New method. git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@1337 379699f6-c40d-0410-875b-85095c16579e
23 years ago
* This class represent the constant pool. You normally don't need to
* access this class, except if you want to add your own custom
* attributes that use the constant pool.
*
* @author Jochen Hoenicke
*/
Documentation updates (INSTALL, javadoc). Added JUnit Test cases. * build.xml: Big update. * net/sf/jode/bytecode/BasicBlock.java: (updateMaxStackLocals): new method to calculate maxStack and maxLocals. (setBlocks): fixed calculation of handlers, call updateMaxLocals. * net/sf/jode/bytecode/BasicBlockReader.java: (maxLocals, maxStack): new fields. (readCode): read maxStack/Locals into private fields. (convert): check that maxStack/Locals match what we calculate. * net/sf/jode/bytecode/BinaryInfo.java: (getKnownAttributeCount): renamed to... (getAttributeCount): ... this, and also count internal attributes. Made it protected. (readAttribute): made protected. (drop): made protected. (prepareAttributes): made protected. (writeKnownAttributes): removed. (writeAttributes): made protected, use getAttributeCount. Changed policy: it doesn't call writeKnownAttribute, but instead it expects sub classes to override this method. (getAttributeSize): made protected, subclasses should override it. Changed all subclasses to new policy. * net/sf/jode/bytecode/Block.java: (lineNr): Removed, it wasn't used. (pop,push): Removed, replaced by ... (maxpop,maxpush,delta): ... these, with slightly changed semantics. (stackHeight): New variable. (Block): Default Constructor doesn't initialize fields now. (getCatchers): Renamed to ... (getHandlers): ... this, changed all callers. (initCode): Calculate maxpop, maxpush, delta correctly. (getStackPopPush): Changed accordingly to new fields. (setCode): Removed debugging output for illegal contents. * net/sf/jode/bytecode/Classes.java: Reworked handling of inner classes. (innerClasses): Field mustn't be null anymore when loaded. (setName): Update class in classpath. * net/sf/jode/bytecode/ClassPath.java: (renameClassInfo): new function, should only used by ClassInfo. * net/sf/jode/bytecode/ConstantPool.java: made public. (getUTF8,getRef,getClassType,getClassName): Don't allow the 0 index. (iterateClassNames): New method. * net/sf/jode/decompiler/Main.java: (decompileClass): Catch ClassFormatExceptions and decompile remaining classes. * net/sf/jode/obfuscator/ClassIdentifier.java: Updated handling of inner/extra classes to new ClassInfo behaviour. (initSuperClasses): Load DECLARATION of super classes. * net/sf/jode/obfuscator/PackageIdentifier.java: Replace deprecated methods of ClassInfo with corresponding classpath calls. (loadMatchingClasses): Initialize packages loaded on demand if we are initialize. * net/sf/jode/obfuscator/modules/ConstantAnalyzer.java: Now extends SimpleAnalyzer. (canonizeIfaceRef): Removed; it is now inherited. (canonizeRef): likewise. Big updates to handle jsr correctly. (handleOpcode): Moved method to BlockInfo. * net/sf/jode/obfuscator/modules/SimpleAnalyzer.java: (canonizeIfaceRef): New method, copied from ConstantAnalyzer. (canonizeRef): call canonizeIfaceRef for interfaces. * net/sf/jode/util/UnifyHash.java (iterateHashCode): iterator now supports remove(). (remove): New method. git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@1337 379699f6-c40d-0410-875b-85095c16579e
23 years ago
public class ConstantPool {
public final static int CLASS = 7;
public final static int FIELDREF = 9;
public final static int METHODREF = 10;
public final static int INTERFACEMETHODREF = 11;
public final static int STRING = 8;
public final static int INTEGER = 3;
public final static int FLOAT = 4;
public final static int LONG = 5;
public final static int DOUBLE = 6;
public final static int NAMEANDTYPE = 12;
public final static int UTF8 = 1;
int count;
int[] tags;
int[] indices1, indices2;
Object[] constants;
public ConstantPool () {
}
public void read(DataInputStream stream)
throws IOException {
count = stream.readUnsignedShort();
tags = new int[count];
indices1 = new int[count];
indices2 = new int[count];
constants = new Object[count];
for (int i=1; i< count; i++) {
int tag = stream.readUnsignedByte();
tags[i] = tag;
switch (tag) {
case CLASS:
indices1[i] = stream.readUnsignedShort();
break;
case FIELDREF:
case METHODREF:
case INTERFACEMETHODREF:
indices1[i] = stream.readUnsignedShort();
indices2[i] = stream.readUnsignedShort();
break;
case STRING:
indices1[i] = stream.readUnsignedShort();
break;
case INTEGER:
constants[i] = new Integer(stream.readInt());
break;
case FLOAT:
constants[i] = new Float(stream.readFloat());
break;
case LONG:
constants[i] = new Long(stream.readLong());
tags[++i] = -LONG;
break;
case DOUBLE:
constants[i] = new Double(stream.readDouble());
tags[++i] = -DOUBLE;
break;
case NAMEANDTYPE:
indices1[i] = stream.readUnsignedShort();
indices2[i] = stream.readUnsignedShort();
break;
case UTF8:
constants[i] = stream.readUTF().intern();
break;
default:
throw new ClassFormatException("unknown constant tag");
}
}
}
public int getTag(int i) throws ClassFormatException {
if (i == 0)
throw new ClassFormatException("null tag");
return tags[i];
}
public String getUTF8(int i) throws ClassFormatException {
if (tags[i] != UTF8)
throw new ClassFormatException("Tag mismatch");
return (String)constants[i];
}
public Reference getRef(int i) throws ClassFormatException {
if (tags[i] != FIELDREF
&& tags[i] != METHODREF && tags[i] != INTERFACEMETHODREF)
throw new ClassFormatException("Tag mismatch");
if (constants[i] == null) {
int classIndex = indices1[i];
int nameTypeIndex = indices2[i];
if (tags[nameTypeIndex] != NAMEANDTYPE)
throw new ClassFormatException("Tag mismatch");
String type = getUTF8(indices2[nameTypeIndex]);
try {
if (tags[i] == FIELDREF)
TypeSignature.checkTypeSig(type);
else
TypeSignature.checkMethodTypeSig(type);
} catch (IllegalArgumentException ex) {
throw new ClassFormatException(ex.getMessage());
}
String clName = getClassType(classIndex);
constants[i] = Reference.getReference
(clName, getUTF8(indices1[nameTypeIndex]), type);
}
return (Reference) constants[i];
}
public Object getConstant(int i) throws ClassFormatException {
if (i == 0)
throw new ClassFormatException("null constant");
switch (tags[i]) {
case ConstantPool.INTEGER:
case ConstantPool.FLOAT:
case ConstantPool.LONG:
case ConstantPool.DOUBLE:
return constants[i];
case ConstantPool.STRING:
return getUTF8(indices1[i]);
}
throw new ClassFormatException("Tag mismatch: "+tags[i]);
}
public String getClassType(int i) throws ClassFormatException {
if (tags[i] != CLASS)
throw new ClassFormatException("Tag mismatch");
String clName = getUTF8(indices1[i]);
if (clName.charAt(0) != '[') {
clName = ("L"+clName+';').intern();
}
try {
TypeSignature.checkTypeSig(clName);
} catch (IllegalArgumentException ex) {
throw new ClassFormatException(ex.getMessage());
}
return clName;
}
public String getClassName(int i) throws ClassFormatException {
if (tags[i] != CLASS)
throw new ClassFormatException("Tag mismatch");
String clName = getUTF8(indices1[i]);
try {
TypeSignature.checkTypeSig("L"+clName+";");
} catch (IllegalArgumentException ex) {
throw new ClassFormatException(ex.getMessage());
}
return clName.replace('/','.').intern();
}
Documentation updates (INSTALL, javadoc). Added JUnit Test cases. * build.xml: Big update. * net/sf/jode/bytecode/BasicBlock.java: (updateMaxStackLocals): new method to calculate maxStack and maxLocals. (setBlocks): fixed calculation of handlers, call updateMaxLocals. * net/sf/jode/bytecode/BasicBlockReader.java: (maxLocals, maxStack): new fields. (readCode): read maxStack/Locals into private fields. (convert): check that maxStack/Locals match what we calculate. * net/sf/jode/bytecode/BinaryInfo.java: (getKnownAttributeCount): renamed to... (getAttributeCount): ... this, and also count internal attributes. Made it protected. (readAttribute): made protected. (drop): made protected. (prepareAttributes): made protected. (writeKnownAttributes): removed. (writeAttributes): made protected, use getAttributeCount. Changed policy: it doesn't call writeKnownAttribute, but instead it expects sub classes to override this method. (getAttributeSize): made protected, subclasses should override it. Changed all subclasses to new policy. * net/sf/jode/bytecode/Block.java: (lineNr): Removed, it wasn't used. (pop,push): Removed, replaced by ... (maxpop,maxpush,delta): ... these, with slightly changed semantics. (stackHeight): New variable. (Block): Default Constructor doesn't initialize fields now. (getCatchers): Renamed to ... (getHandlers): ... this, changed all callers. (initCode): Calculate maxpop, maxpush, delta correctly. (getStackPopPush): Changed accordingly to new fields. (setCode): Removed debugging output for illegal contents. * net/sf/jode/bytecode/Classes.java: Reworked handling of inner classes. (innerClasses): Field mustn't be null anymore when loaded. (setName): Update class in classpath. * net/sf/jode/bytecode/ClassPath.java: (renameClassInfo): new function, should only used by ClassInfo. * net/sf/jode/bytecode/ConstantPool.java: made public. (getUTF8,getRef,getClassType,getClassName): Don't allow the 0 index. (iterateClassNames): New method. * net/sf/jode/decompiler/Main.java: (decompileClass): Catch ClassFormatExceptions and decompile remaining classes. * net/sf/jode/obfuscator/ClassIdentifier.java: Updated handling of inner/extra classes to new ClassInfo behaviour. (initSuperClasses): Load DECLARATION of super classes. * net/sf/jode/obfuscator/PackageIdentifier.java: Replace deprecated methods of ClassInfo with corresponding classpath calls. (loadMatchingClasses): Initialize packages loaded on demand if we are initialize. * net/sf/jode/obfuscator/modules/ConstantAnalyzer.java: Now extends SimpleAnalyzer. (canonizeIfaceRef): Removed; it is now inherited. (canonizeRef): likewise. Big updates to handle jsr correctly. (handleOpcode): Moved method to BlockInfo. * net/sf/jode/obfuscator/modules/SimpleAnalyzer.java: (canonizeIfaceRef): New method, copied from ConstantAnalyzer. (canonizeRef): call canonizeIfaceRef for interfaces. * net/sf/jode/util/UnifyHash.java (iterateHashCode): iterator now supports remove(). (remove): New method. git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@1337 379699f6-c40d-0410-875b-85095c16579e
23 years ago
/**
* Iterates through all class entries in the class pool and returns
* their (dot seperated) class name.
*/
public Iterator iterateClassNames() {
return new Iterator()
{
int entry = 1;
public boolean hasNext() {
try {
while (entry < count
&& (tags[entry] != CLASS
|| getUTF8(indices1[entry])
.charAt(0) == '['))
entry++;
} catch (ClassFormatException ex) {
throw new InternalError(ex.getMessage());
}
return entry < count;
}
public Object next() {
if (!hasNext())
throw new NoSuchElementException();
try {
return getClassName(entry++);
} catch (ClassFormatException ex) {
throw new InternalError(ex.getMessage());
}
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
public String toString(int i) {
switch (tags[i]) {
case CLASS:
return "Class "+toString(indices1[i]);
case STRING:
return "String \""+toString(indices1[i])+"\"";
case INTEGER:
return "Int "+constants[i].toString();
case FLOAT:
return "Float "+constants[i].toString();
case LONG:
return "Long "+constants[i].toString();
case DOUBLE:
return "Double "+constants[i].toString();
case UTF8:
return constants[i].toString();
case FIELDREF:
return "Fieldref: "+toString(indices1[i])+"; "
+ toString(indices2[i]);
case METHODREF:
return "Methodref: "+toString(indices1[i])+"; "
+ toString(indices2[i]);
case INTERFACEMETHODREF:
return "Interfaceref: "+toString(indices1[i])+"; "
+ toString(indices2[i]);
case NAMEANDTYPE:
return "Name "+toString(indices1[i])
+"; Type "+toString(indices2[i]);
default:
return "unknown tag: "+tags[i];
}
}
public int size() {
return count;
}
public String toString() {
Documentation updates (INSTALL, javadoc). Added JUnit Test cases. * build.xml: Big update. * net/sf/jode/bytecode/BasicBlock.java: (updateMaxStackLocals): new method to calculate maxStack and maxLocals. (setBlocks): fixed calculation of handlers, call updateMaxLocals. * net/sf/jode/bytecode/BasicBlockReader.java: (maxLocals, maxStack): new fields. (readCode): read maxStack/Locals into private fields. (convert): check that maxStack/Locals match what we calculate. * net/sf/jode/bytecode/BinaryInfo.java: (getKnownAttributeCount): renamed to... (getAttributeCount): ... this, and also count internal attributes. Made it protected. (readAttribute): made protected. (drop): made protected. (prepareAttributes): made protected. (writeKnownAttributes): removed. (writeAttributes): made protected, use getAttributeCount. Changed policy: it doesn't call writeKnownAttribute, but instead it expects sub classes to override this method. (getAttributeSize): made protected, subclasses should override it. Changed all subclasses to new policy. * net/sf/jode/bytecode/Block.java: (lineNr): Removed, it wasn't used. (pop,push): Removed, replaced by ... (maxpop,maxpush,delta): ... these, with slightly changed semantics. (stackHeight): New variable. (Block): Default Constructor doesn't initialize fields now. (getCatchers): Renamed to ... (getHandlers): ... this, changed all callers. (initCode): Calculate maxpop, maxpush, delta correctly. (getStackPopPush): Changed accordingly to new fields. (setCode): Removed debugging output for illegal contents. * net/sf/jode/bytecode/Classes.java: Reworked handling of inner classes. (innerClasses): Field mustn't be null anymore when loaded. (setName): Update class in classpath. * net/sf/jode/bytecode/ClassPath.java: (renameClassInfo): new function, should only used by ClassInfo. * net/sf/jode/bytecode/ConstantPool.java: made public. (getUTF8,getRef,getClassType,getClassName): Don't allow the 0 index. (iterateClassNames): New method. * net/sf/jode/decompiler/Main.java: (decompileClass): Catch ClassFormatExceptions and decompile remaining classes. * net/sf/jode/obfuscator/ClassIdentifier.java: Updated handling of inner/extra classes to new ClassInfo behaviour. (initSuperClasses): Load DECLARATION of super classes. * net/sf/jode/obfuscator/PackageIdentifier.java: Replace deprecated methods of ClassInfo with corresponding classpath calls. (loadMatchingClasses): Initialize packages loaded on demand if we are initialize. * net/sf/jode/obfuscator/modules/ConstantAnalyzer.java: Now extends SimpleAnalyzer. (canonizeIfaceRef): Removed; it is now inherited. (canonizeRef): likewise. Big updates to handle jsr correctly. (handleOpcode): Moved method to BlockInfo. * net/sf/jode/obfuscator/modules/SimpleAnalyzer.java: (canonizeIfaceRef): New method, copied from ConstantAnalyzer. (canonizeRef): call canonizeIfaceRef for interfaces. * net/sf/jode/util/UnifyHash.java (iterateHashCode): iterator now supports remove(). (remove): New method. git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@1337 379699f6-c40d-0410-875b-85095c16579e
23 years ago
StringBuffer result = new StringBuffer("ConstantPool[ null");
for (int i=1; i< count; i++) {
result.append(", ").append(i).append(" = ").append(toString(i));
}
result.append(" ]");
return result.toString();
}
}