[Jarspy-commits] CVS: JarSpy/src/com/ociweb/classinfo AccessFlags.java,NONE,1.1 SignatureDescriptor.
Status: Beta
Brought to you by:
brown_j
|
From: Jeff B. <br...@us...> - 2002-09-29 02:21:28
|
Update of /cvsroot/jarspy/JarSpy/src/com/ociweb/classinfo
In directory usw-pr-cvs1:/tmp/cvs-serv23761
Modified Files:
CRField.java CRMethod.java ClassReader.java
Added Files:
AccessFlags.java SignatureDescriptor.java
Log Message:
refactor some access flag and descriptor code
--- NEW FILE: AccessFlags.java ---
// JarSpy
// Copyright (c) 2001, 2002 Jeff S. Brown
// This file is part of JarSpy.
//
// JarSpy 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 of the License, or
// (at your option) any later version.
//
// JarSpy 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 JarSpy; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.ociweb.classinfo;
import java.util.Map;
import java.util.HashMap;
/**
* @version $Id: AccessFlags.java,v 1.1 2002/09/29 02:21:25 brown_j Exp $
*/
public class AccessFlags {
public static final short ACC_PUBLIC = 0x0001;
public static final short ACC_PRIVATE = 0x0002;
public static final short ACC_PROTECTED = 0x0004;
public static final short ACC_STATIC = 0x0008;
public static final short ACC_FINAL = 0x0010;
public static final short ACC_VOLATILE = 0x0040;
public static final short ACC_TRANSIENT = 0x0080;
private final short accessFlags;
private String accessFlagsString;
private static final Map flagsToObject = new HashMap();
private AccessFlags(short accessFlags) {
this.accessFlags = accessFlags;
StringBuffer buffer = new StringBuffer();
if (0 != (accessFlags & ACC_PUBLIC)) {
buffer.append("public ");
} else if (0 != (accessFlags & ACC_PRIVATE)) {
buffer.append("private ");
} else if (0 != (accessFlags & ACC_PROTECTED)) {
buffer.append("protected ");
}
if (0 != (accessFlags & ACC_STATIC)) {
buffer.append("static ");
}
if (0 != (accessFlags & ACC_FINAL)) {
buffer.append("final ");
} else if (0 != (accessFlags & ACC_VOLATILE)) {
buffer.append("volatile ");
}
if (0 != (accessFlags & ACC_TRANSIENT)) {
buffer.append("transient ");
}
accessFlagsString = buffer.toString();
}
public static final synchronized AccessFlags getAccessFlags(short accessFlags) {
Short key = new Short(accessFlags);
AccessFlags af = (AccessFlags) flagsToObject.get(key);
if(af == null) {
af = new AccessFlags(accessFlags);
flagsToObject.put(key, af);
}
return af;
}
public short getAccessFlags() {
return accessFlags;
}
public String getAccessFlagsString() {
return accessFlagsString;
}
public String toString() {
return getAccessFlagsString();
}
}
--- NEW FILE: SignatureDescriptor.java ---
// Copyright (c) 2001, 2002 Jeff S. Brown
// This file is part of JarSpy.
//
// JarSpy 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 of the License, or
// (at your option) any later version.
//
// JarSpy 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 JarSpy; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.ociweb.classinfo;
/**
* @version $Id: SignatureDescriptor.java,v 1.1 2002/09/29 02:21:25 brown_j Exp $
*/
public class SignatureDescriptor {
private final String descriptor;
public SignatureDescriptor(String descriptor) {
this.descriptor = convertDescriptor(descriptor);
}
public String getDescriptor() {
return descriptor;
}
public String toString() {
return getDescriptor();
}
private String convertDescriptor(String descriptor) {
String typeDescription = "";
while (descriptor.length() > 0) {
if (!"".equals(typeDescription)) {
typeDescription += ", ";
}
if (descriptor.charAt(0) == 'L') {
int endOfType = descriptor.indexOf(';');
if (endOfType < 0) {
endOfType = descriptor.length();
typeDescription +=
descriptor.substring(1, endOfType).replace('/', '.');
descriptor = "";
} else {
typeDescription +=
descriptor.substring(1, endOfType).replace('/', '.');
descriptor = descriptor.substring(endOfType + 1);
}
} else if (descriptor.charAt(0) == '[') {
String brackets = "[]";
int arrayCounter = 1;
while (descriptor.charAt(arrayCounter) == '[') {
brackets += "[]";
arrayCounter++;
}
int endOfType = descriptor.indexOf(';');
if (endOfType < 0) {
typeDescription +=
convertDescriptor(descriptor.substring(1)) + brackets;
descriptor = "";
} else {
typeDescription +=
convertDescriptor(descriptor.substring
(arrayCounter, endOfType)) + brackets;
descriptor = descriptor.substring(endOfType + 1);
}
} else if (descriptor.charAt(0) == 'B') {
typeDescription += "byte";
descriptor = descriptor.substring(1);
} else if (descriptor.charAt(0) == 'C') {
typeDescription += "double";
descriptor = descriptor.substring(1);
} else if (descriptor.charAt(0) == 'D') {
typeDescription += "double";
descriptor = descriptor.substring(1);
} else if (descriptor.charAt(0) == 'F') {
typeDescription += "float";
descriptor = descriptor.substring(1);
} else if (descriptor.charAt(0) == 'I') {
typeDescription += "int";
descriptor = descriptor.substring(1);
} else if (descriptor.charAt(0) == 'J') {
typeDescription += "long";
descriptor = descriptor.substring(1);
} else if (descriptor.charAt(0) == 'S') {
typeDescription += "short";
descriptor = descriptor.substring(1);
} else if (descriptor.charAt(0) == 'V') {
typeDescription += "void";
descriptor = descriptor.substring(1);
} else if (descriptor.charAt(0) == 'Z') {
typeDescription += "boolean";
descriptor = descriptor.substring(1);
} else {
typeDescription += "UNKNOWN TYPE: " + descriptor;
descriptor = "";
}
}
return typeDescription;
}
}
Index: CRField.java
===================================================================
RCS file: /cvsroot/jarspy/JarSpy/src/com/ociweb/classinfo/CRField.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** CRField.java 19 Jun 2002 01:19:10 -0000 1.3
--- CRField.java 29 Sep 2002 02:21:25 -0000 1.4
***************
*** 19,22 ****
--- 19,23 ----
package com.ociweb.classinfo;
+
/**
* @author Jeff Brown
***************
*** 25,31 ****
public class CRField {
! private short accessFlags;
private String methodName;
! private String descriptor;
CRField(String methodName,
--- 26,32 ----
public class CRField {
! private AccessFlags accessFlags;
private String methodName;
! private SignatureDescriptor descriptor;
CRField(String methodName,
***************
*** 33,44 ****
String descriptor) {
this.methodName = methodName;
! this.accessFlags = accessFlags;
! this.descriptor = descriptor;
}
public String toString() {
StringBuffer buffer = new StringBuffer();
! buffer.append(ClassReader.accessFlagsToString(accessFlags));
! buffer.append(ClassReader.convertDescriptor(descriptor));
buffer.append(" ");
buffer.append(methodName);
--- 34,45 ----
String descriptor) {
this.methodName = methodName;
! this.accessFlags = AccessFlags.getAccessFlags(accessFlags);
! this.descriptor = new SignatureDescriptor(descriptor);
}
public String toString() {
StringBuffer buffer = new StringBuffer();
! buffer.append(accessFlags);
! buffer.append(descriptor);
buffer.append(" ");
buffer.append(methodName);
Index: CRMethod.java
===================================================================
RCS file: /cvsroot/jarspy/JarSpy/src/com/ociweb/classinfo/CRMethod.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** CRMethod.java 18 Sep 2002 23:14:34 -0000 1.4
--- CRMethod.java 29 Sep 2002 02:21:25 -0000 1.5
***************
*** 26,31 ****
public class CRMethod {
- private String methodName;
- private short accessFlags;
private String descriptor;
--- 26,29 ----
***************
*** 33,63 ****
short accessFlags,
String descriptor) {
! this.methodName = methodName;
! this.accessFlags = accessFlags;
! this.descriptor = descriptor;
! }
- public String toString() {
- StringBuffer buffer = new StringBuffer();
- buffer.append(ClassReader.accessFlagsToString(accessFlags));
- // if (buffer.length() > 0) buffer.append(" ");
int indexOfLastParen = descriptor.lastIndexOf(')');
String returnType = descriptor.substring(indexOfLastParen + 1);
! buffer.append(ClassReader.convertDescriptor(returnType));
buffer.append(" ");
buffer.append(methodName);
buffer.append("(");
! String arguments = descriptor.substring(1, indexOfLastParen);
! buffer.append(ClassReader.convertDescriptor(arguments));
! // System.out.println("descriptor = " + descriptor);
! // System.out.println("arguments = {" + arguments + "}");
!
! // StringTokenizer tokenizer = new StringTokenizer(arguments, ";");
! // while(tokenizer.hasMoreTokens()) {
! // buffer.append(ClassReader.convertDescriptor(tokenizer.nextToken()));
! // }
buffer.append(")");
! return buffer.toString();
}
}
--- 31,57 ----
short accessFlags,
String descriptor) {
! AccessFlags af = AccessFlags.getAccessFlags(accessFlags);
int indexOfLastParen = descriptor.lastIndexOf(')');
String returnType = descriptor.substring(indexOfLastParen + 1);
! String arguments = descriptor.substring(1, indexOfLastParen);
+ SignatureDescriptor returnTypeDescriptor = new SignatureDescriptor(returnType);
+ SignatureDescriptor argumentsDescriptor = new SignatureDescriptor(arguments);
+
+ StringBuffer buffer = new StringBuffer();
+ buffer.append(af);
+ buffer.append(returnTypeDescriptor);
buffer.append(" ");
buffer.append(methodName);
buffer.append("(");
! buffer.append(argumentsDescriptor);
buffer.append(")");
!
! this.descriptor = buffer.toString();
! }
!
! public String toString() {
! return descriptor;
}
}
Index: ClassReader.java
===================================================================
RCS file: /cvsroot/jarspy/JarSpy/src/com/ociweb/classinfo/ClassReader.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** ClassReader.java 18 Sep 2002 22:58:53 -0000 1.8
--- ClassReader.java 29 Sep 2002 02:21:25 -0000 1.9
***************
*** 28,40 ****
import java.util.Vector;
- // +++++++++++++++++++++++++
- // +++++ WARNING +++++
- // +++++++++++++++++++++++++
- //
- // This file is a bit of a mess right now, and is in the process of being
- // rewritten, so don't get too attached to it! :-)
- //
-
-
/**
* ClassReader is a class that will parse a .class file and provide an API to
--- 28,31 ----
***************
*** 61,64 ****
--- 52,57 ----
private short minorVersion_;
+ private ConstantPool constantPool;
+
private static final int MAGIC_NUMBER = 0xCAFEBABE;
***************
*** 71,89 ****
* value to mask against access_flags to identify signatures
*/
- public static final short ACC_PRIVATE = 0x0002;
-
- /**
- * value to mask against access_flags to identify signatures
- */
- public static final short ACC_PROTECTED = 0x0004;
-
- /**
- * value to mask against access_flags to identify signatures
- */
- public static final short ACC_STATIC = 0x0008;
-
- /**
- * value to mask against access_flags to identify signatures
- */
public static final short ACC_FINAL = 0x0010;
--- 64,67 ----
***************
*** 91,119 ****
* value to mask against access_flags to identify signatures
*/
- // public static final short ACC_SYNCHRONIZED = 0x0020;
-
- /**
- * value to mask against access_flags to identify signatures
- */
- // public static final short ACC_SUPER = 0x0020;
-
- /**
- * value to mask against access_flags to identify signatures
- */
- public static final short ACC_VOLATILE = 0x0040;
-
- /**
- * value to mask against access_flags to identify signatures
- */
- public static final short ACC_TRANSIENT = 0x0080;
-
- /**
- * value to mask against access_flags to identify signatures
- */
- // public static final short ACC_NATIVE = 0x0100;
-
- /**
- * value to mask against access_flags to identify signatures
- */
public static final short ACC_INTERFACE = 0x0200;
--- 69,72 ----
***************
*** 123,147 ****
public static final short ACC_ABSTRACT = 0x0400;
- // --Recycle Bin START (9/13/02 5:57 PM):
- // /**
- // * @param file
- // * <code>ClassReader</code> will attempt to parse. This constructor does
- // * not declare it, but an <code>InvalidClassFileException</code> will be
- // * thrown if the first 4 bytes of the file do not contain 0xCAFEBABE --
- // * the "magic number" of a java class file
- // */
- // public ClassReader(File file) {
- //
- // FileInputStream is = null;
- //
- // try {
- // is = new FileInputStream(file);
- // read(is);//, file.getName());
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // }
- // --Recycle Bin STOP (9/13/02 5:57 PM)
-
public ClassReader(InputStream is) {
read(is);
--- 76,79 ----
***************
*** 313,325 ****
}
- // --Recycle Bin START (9/13/02 6:01 PM):
- // /**
- // * @return the access flags for this class.
- // */
- // public short getAccessFlags() {
- // return accessFlags_;
- // }
- // --Recycle Bin STOP (9/13/02 6:01 PM)
-
/**
* @return true if this class is marked as public, otherwise false
--- 245,248 ----
***************
*** 350,362 ****
}
- // private String[] constantPool;
- ConstantPool constantPool;
-
public Vector getDependencies() {
return classDependencies_.getDependencies();
}
- // --Recycle Bin (9/13/02 5:57 PM): private Vector classRefs_ = new Vector();
-
private CRClassDependencies classDependencies_ =
new CRClassDependencies();
--- 273,280 ----
***************
*** 393,497 ****
}
- public static final String convertDescriptor(String descriptor) {
-
- String typeDescription = "";
- while (descriptor.length() > 0) {
- if (!"".equals(typeDescription)) {
- typeDescription += ", ";
- }
-
- if (descriptor.charAt(0) == 'L') {
- int endOfType = descriptor.indexOf(';');
- if (endOfType < 0) {
- endOfType = descriptor.length();
- typeDescription +=
- descriptor.substring(1, endOfType).replace('/', '.');
- descriptor = "";
- } else {
- typeDescription +=
- descriptor.substring(1, endOfType).replace('/', '.');
- descriptor = descriptor.substring(endOfType + 1);
- }
- } else if (descriptor.charAt(0) == '[') {
- String brackets = "[]";
- int arrayCounter = 1;
- while (descriptor.charAt(arrayCounter) == '[') {
- brackets += "[]";
- arrayCounter++;
- }
- int endOfType = descriptor.indexOf(';');
- if (endOfType < 0) {
- // endOfType = descriptor.length();
- typeDescription +=
- convertDescriptor(descriptor.substring(1)) + brackets;
- descriptor = "";
- } else {
- typeDescription +=
- convertDescriptor(descriptor.substring
- (arrayCounter, endOfType)) + brackets;
- descriptor = descriptor.substring(endOfType + 1);
- }
- } else if (descriptor.charAt(0) == 'B') {
- typeDescription += "byte";
- descriptor = descriptor.substring(1);
- } else if (descriptor.charAt(0) == 'C') {
- typeDescription += "double";
- descriptor = descriptor.substring(1);
- } else if (descriptor.charAt(0) == 'D') {
- typeDescription += "double";
- descriptor = descriptor.substring(1);
- } else if (descriptor.charAt(0) == 'F') {
- typeDescription += "float";
- descriptor = descriptor.substring(1);
- } else if (descriptor.charAt(0) == 'I') {
- typeDescription += "int";
- descriptor = descriptor.substring(1);
- } else if (descriptor.charAt(0) == 'J') {
- typeDescription += "long";
- descriptor = descriptor.substring(1);
- } else if (descriptor.charAt(0) == 'S') {
- typeDescription += "short";
- descriptor = descriptor.substring(1);
- } else if (descriptor.charAt(0) == 'V') {
- typeDescription += "void";
- descriptor = descriptor.substring(1);
- } else if (descriptor.charAt(0) == 'Z') {
- typeDescription += "boolean";
- descriptor = descriptor.substring(1);
- } else {
- typeDescription += "UNKNOWN TYPE: " + descriptor;
- descriptor = "";
- }
- }
- return typeDescription;
- }
-
- public static final String accessFlagsToString(short accessFlags) {
- StringBuffer buffer = new StringBuffer();
-
- if (0 != (accessFlags & ACC_PUBLIC)) {
- buffer.append("public ");
- } else if (0 != (accessFlags & ACC_PRIVATE)) {
- buffer.append("private ");
- } else if (0 != (accessFlags & ACC_PROTECTED)) {
- buffer.append("protected ");
- }
-
- if (0 != (accessFlags & ACC_STATIC)) {
- buffer.append("static ");
- }
-
- if (0 != (accessFlags & ACC_FINAL)) {
- buffer.append("final ");
- } else if (0 != (accessFlags & ACC_VOLATILE)) {
- buffer.append("volatile ");
- }
-
- if (0 != (accessFlags & ACC_TRANSIENT)) {
- buffer.append("transient ");
- }
-
- return buffer.toString();
- }
}
--- 311,314 ----
|