import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.text.Annotation;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
class ClassReader {
public ClassReader(String crpath, String string) {
fileName = crpath; // stores info of file name
clsname=string;
}
public ClassReader(String string) {
clsname=string;
fileName="empty";
}
/**
* This method identifies version and other info like methods and feilds of class
* @return Map
*/
public Map versionInfo() {
boolean flag=false;
String urlString=null;
System.out.println("Ur Using JAVA "
+ System.getProperty("java.version"));
try {
if(fileName.equals("empty")){
flag = true;
}
if(!flag ){
cs = new DataInputStream(new FileInputStream(fileName));
}
}catch(IOException e){
flag=true;
}
try{
if(flag){
final String classLocation = clsname.replace('.', '/') + ".class";
cs = new DataInputStream(ClassLoader.getSystemResource(classLocation).openStream());
}
cs.readInt(); // holds info about magic number of class file format
cs.readUnsignedShort();// holds info about minor version of class file format
cs.readUnsignedShort();// holds info about major version of class file format
// Read constant pool
int constantCount = cs.readUnsignedShort();
UTStrings = new String[constantCount];
for (int i = 1; i < constantCount; i++) {
if(i==305){
System.out.println("sddfs");
}
if(readsConstantPool(i)){
++i;
}
}
// Read basic class information
cs.readShort(); // Reading access flags
cs.readShort(); // Reading this class
cs.readShort(); // Reading superclass
//This for reading interfaces
int interfaceCount = cs.readShort();
System.out.println("interfaceCount" + interfaceCount);
readInterfaceInfo(interfaceCount);
// Read fields
int fieldCount = cs.readUnsignedShort();
for (int i = 0; i < fieldCount; i++)
readMethodsOrFieldsInfo("Field");
// Read methods
int methodCount = cs.readUnsignedShort();
for (int i = 0; i < methodCount; i++)
readMethodsOrFieldsInfo("Method");
// Read top-level attributes (of entire class)
int attributeCount = cs.readUnsignedShort();
}catch(ClassCastException ce){
System.out.println (ce.getMessage());
}
catch (Exception e) {
System.out.println (e.getMessage());
}
/*for (int m = 1; m < UTStrings.length; m++) {
if (UTStrings[m] == null)
continue;
if ((UTStrings[m] == thisClass) ||
(UTStrings[m] == superClass))
continue;
if (UTStrings[m].type == UTStrings.CLASS) {
String s = UTStrings[i].arg1.strValue;
if (s.charAt(0) == '[')
continue;
s = printClassName(UTStrings[i].arg1.strValue);
if ((packageName != null) && (s.startsWith(packageName)))
continue;
System.out.println(printClassName(s)+";");
}
}*/
return map;
}
/**
* read constant number 'k' in the constant pool
* @param k
* @throws IOException
*/
private boolean readsConstantPool(int k) throws IOException {
byte tag = cs.readByte();
boolean ldflag=false;
if (tag == 1) { // UTF text
String stg = cs.readUTF();
System.out.println(k + ". UTF: " + stg);
UTStrings[k] = stg;
//map.put(stg,"CP");
cpcnt++;
} else if (tag == 2) {// unused tag
} else if (tag == 3) {
int intValue = cs.readInt();
System.out.println(k + ". Integer: " + intValue);
} else if (tag == 4) {// float
cs.readFloat();
} else if (tag == 5) {// long
cs.readLong();
ldflag=true;
} else if (tag == 6) {// double
cs.readDouble();
ldflag=true;
} else if (tag == 7) {// class
int UTFindex1 = cs.readUnsignedShort();
System.out.println(k + ". Class: " + UTFindex1);
//map.put(UTStrings[UTFindex1],"Pack");
} else if (tag == 8) {// String
int UTFindex2 = cs.readUnsignedShort();
System.out.println(k + ". String: " + UTFindex2);
} else if (tag == 9) {// Fieldref
int class1 = cs.readUnsignedShort();
int nameAndType1 = cs.readUnsignedShort();
System.out
.println(k + ". Fieldref: " + class1 + "/" + nameAndType1);
} else if (tag == 10) {// Methodref
int class2 = cs.readUnsignedShort();
int nameAndType2 = cs.readUnsignedShort();
System.out
.println(k + ". Fieldref: " + class2 + "/" + nameAndType2);
} else if (tag == 11) {// Interface Methodref
cs.readUnsignedShort();
cs.readUnsignedShort();
} else if (tag == 12) {// NameAndType
int name = cs.readUnsignedShort();
int type = cs.readUnsignedShort();
System.out.println(k + ". NameAndType: " + name + " " + type);
}
return ldflag;
}
private static void readInterfaceInfo(int count) throws IOException {
System.out.print("count:" + count);
for (int i = 0; i < count; ++i) {
short tag = cs.readShort();
if (tag > 1 && tag < cpcnt-1){
System.out.print(UTStrings[tag]);
}
}
}
/**
* read a field or method
* @param label
* @throws IOException
*/
private void readMethodsOrFieldsInfo(String label) throws IOException {
cs.readUnsignedShort();
int name = cs.readUnsignedShort();
int type =cs.readUnsignedShort();
System.out.println ();
System.out.println (label + " name: " + name +
" [" + UTStrings[name] + "]");
System.out.println ("Type: " + type +
" [" + UTStrings[type] + "]");
int attributeCount = cs.readUnsignedShort();
//if (attributeCount > 1 && attributeCount < cpcnt - 1){
for (int i = 0; i < attributeCount; i++) {
int attributeName = cs.readUnsignedShort();
//if (attributeName > 1 && attributeName < cpcnt - 1){
try{
if (UTStrings[attributeName].equals("Deprecated")) {
map.put(UTStrings[name], "Deprecated");
System.out.println(name + "depreacted");
}
int length = cs.readInt();
readAttributeValue(length);
}catch(Exception e){}
//}
}
//}
}
/**
* read the value of an attribute: a sequence of 'length' bytes
* @param length
* @throws IOException
*/
public void readAttributeValue(int length) throws IOException {
for (int i = 0; i < length; i++) {
int val = cs.readUnsignedByte() + (0x100);
}
}
public void readClassName(String nam) {
fileName = nam;
}
/* private String printClassName(String s) {
StringBuffer x;
if (s.charAt(0) == '[') {
return(typeString(s, ""));
}
x = new StringBuffer();
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == '/')
x.append('.');
else
x.append(s.charAt(j));
}
return (x.toString());
}*/
static DataInputStream cs;
static String[] UTStrings = null;
static String fileName = null;
static int cpcnt=0;
private String clsname=null;
Map map = new HashMap();
}