You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jv...@apache.org on 2001/05/03 21:09:24 UTC
cvs commit: jakarta-turbine/proposals/jvz/code ImportStatements.java
jvanzyl 01/05/03 12:09:23
Modified: proposals/jvz/code ImportStatements.java
Log:
- seems to be working for all the turbine files now.
Revision Changes Path
1.8 +137 -9 jakarta-turbine/proposals/jvz/code/ImportStatements.java
Index: ImportStatements.java
===================================================================
RCS file: /home/cvs/jakarta-turbine/proposals/jvz/code/ImportStatements.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ImportStatements.java 2001/04/19 05:55:09 1.7
+++ ImportStatements.java 2001/05/03 19:09:19 1.8
@@ -7,6 +7,10 @@
import de.fub.bytecode.classfile.ConstantClass;
import de.fub.bytecode.classfile.ConstantPool;
import de.fub.bytecode.classfile.ConstantUtf8;
+import de.fub.bytecode.classfile.ConstantCP;
+import de.fub.bytecode.classfile.ConstantMethodref;
+import de.fub.bytecode.classfile.ConstantInterfaceMethodref;
+import de.fub.bytecode.classfile.ConstantNameAndType;
import de.fub.bytecode.classfile.JavaClass;
import de.fub.bytecode.classfile.LocalVariable;
import de.fub.bytecode.classfile.LocalVariableTable;
@@ -24,12 +28,14 @@
* Utility class for creating a list of fully qualified
* import statements.
*
- * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
+ * Class fields
+ * Method return types
+ * Method argument types
+ * Method local variables
+ * Static method references
*
- * Problems:
+ * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
*
- * Whatever[] : class for array declarations are not forming correctly.
- * coming out like Lorg.apache.turbine.util.Whatever;;
*/
public class ImportStatements
{
@@ -67,11 +73,15 @@
if (signature.indexOf("java.lang.") != -1)
{
if (signature.substring("java.lang.".length()).indexOf(".") == -1)
+ {
continue;
+ }
}
if (basePackage(className).equals(basePackage(signature)))
+ {
continue;
+ }
/*
* We also need to get rid of imports for classes
@@ -105,9 +115,9 @@
* @param String className
* @return Hashtable Fully qualified class references.
*/
- public static Hashtable classReferences(String className) throws Exception
+ public static Hashtable classReferences(String className)
+ throws Exception
{
- Hashtable classReferences = new Hashtable();
JavaClass javaClass;
if ((javaClass = Repository.lookupClass(className)) == null)
@@ -116,27 +126,128 @@
}
ConstantPool constantPool = javaClass.getConstantPool();
- Constant[] constantPoolArray = constantPool.getConstantPool();
+ return processConstantPool(constantPool);
+ }
+
+ /**
+ * Determine the class references in a constant pool.
+ *
+ * @param ConstantPool constant pool
+ * @return Hashtable class references
+ */
+ private static Hashtable processConstantPool(ConstantPool constantPool)
+ {
+ Hashtable classReferences = new Hashtable();
+ Constant[] constantPoolArray = constantPool.getConstantPool();
+ String signature;
+ int index;
+
for (int i = 0; i < constantPoolArray.length; i++)
{
if (constantPoolArray[i] instanceof ConstantClass)
{
- int index = ((ConstantClass)
+ index = ((ConstantClass)
constantPoolArray[i]).getNameIndex();
ConstantUtf8 constant = (ConstantUtf8)
constantPool.getConstant(index);
- String signature = new String(constant.getBytes());
+ signature = new String(constant.getBytes());
signature = signature.replace('/', '.');
+
+ if (signature.startsWith("L") && signature.endsWith(";"))
+ {
+ /*
+ * Reference of the form Ljava.util.Hashtable;
+ */
+ signature = signature.substring(1,signature.length()-1);
+ }
+ else if (signature.startsWith("[L") && signature.endsWith(";"))
+ {
+ /*
+ * Reference of the form Ljava.util.Hashtable;
+ */
+ signature = signature.substring(2,signature.length()-1);
+ }
+ else if (signature.startsWith("["))
+ {
+ continue;
+ }
+ else if (signature.indexOf("$") != -1)
+ {
+ continue;
+ }
+
+ if (signature.endsWith("[]"))
+ {
+ signature = signature.substring(0, signature.length()-2);
+ }
+
classReferences.put(signature,signature);
}
+
+ if (constantPoolArray[i] instanceof ConstantCP)
+ {
+ index = ((ConstantCP)
+ constantPoolArray[i]).getNameAndTypeIndex();
+
+ ConstantNameAndType ntConstant = (ConstantNameAndType)
+ constantPool.getConstant(index);
+
+ index = ntConstant.getSignatureIndex();
+
+ ConstantUtf8 constant = (ConstantUtf8)
+ constantPool.getConstant(index);
+
+ signature = new String(constant.getBytes());
+ String returnType = Utility.methodSignatureReturnType(signature);
+ if (returnType.indexOf(".") != -1)
+ {
+ if (returnType.endsWith("[]"))
+ {
+ returnType = returnType.substring(0, returnType.length()-2);
+ }
+
+ if (returnType.indexOf("$") == -1)
+ {
+ classReferences.put(returnType,returnType);
+ }
+ }
+
+ if (constantPoolArray[i] instanceof ConstantMethodref ||
+ constantPoolArray[i] instanceof ConstantInterfaceMethodref)
+ {
+ String[] argumentTypes = Utility.methodSignatureArgumentTypes(signature);
+
+ for (int j = 0; j < argumentTypes.length; j++)
+ {
+ String argType = argumentTypes[j];
+ if (argType.indexOf(".") != -1)
+ {
+ if (argType.endsWith("[]"))
+ {
+ argType = argType.substring(0, argType.length()-2);
+ }
+ if (argType.indexOf("$") == -1)
+ {
+ classReferences.put(argType,argType);
+ }
+ }
+ }
+ }
+ }
}
return classReferences;
}
+ /**
+ * Find all the local variable references within a method.
+ *
+ * @param String name of java class
+ * @return Hashtable
+ */
public static Hashtable localVariables(String name) throws Exception
{
Hashtable vars = new Hashtable();
@@ -169,7 +280,14 @@
String lvar = Utility.signatureToString(lvt[j].getSignature());
if (lvar.indexOf(".") == -1)
+ {
+ continue;
+ }
+
+ if (lvar.indexOf("$") != -1)
+ {
continue;
+ }
/*
* Get rid of the array signature
@@ -177,9 +295,19 @@
* signature.
*/
if (lvar.endsWith("[]"))
+ {
lvar = lvar.substring(0, lvar.length() - 2);
+ }
vars.put(lvar,lvar);
+
+ /*
+ * Process the constant pool of the method.
+ */
+ Hashtable methodClassReferences =
+ processConstantPool(code.getConstantPool());
+
+ vars.putAll(methodClassReferences);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org