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/04/17 11:37:31 UTC
cvs commit: jakarta-turbine/proposals/jvz/code ImportStatements.java LocalVariables.java
jvanzyl 01/04/17 02:37:31
Added: proposals/jvz/code ImportStatements.java LocalVariables.java
Log:
- little tools for creating fully qualified import statements
from a classfile.
Revision Changes Path
1.1 jakarta-turbine/proposals/jvz/code/ImportStatements.java
Index: ImportStatements.java
===================================================================
package org.apache.turbine.util;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Iterator;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Utility class for creating a list of fully qualified
* import statements from a class file.
*/
public class ImportStatements
{
public ImportStatements(String className)
{
execute(className);
}
public void execute(String className)
{
Hashtable classes = new Hashtable();
try
{
int i;
Iterator iterator;
Class clazz = Class.forName(className);
/*
* We need the base class and any interfaces
* this class might implement.
*/
Class superClass = clazz.getSuperclass();
classes.put(superClass, superClass);
Class[] interfaces = clazz.getInterfaces();
for (i = 0; i < interfaces.length; i++)
{
classes.put(interfaces[i], interfaces[i]);
}
/*
* Process the fields of the class. All
* we need is the Class type for each
* of the fields.
*/
Field[] fields = clazz.getDeclaredFields();
for (i = 0; i < fields.length; i++)
{
Class fieldClass = fields[i].getType();
classes.put(fieldClass, fieldClass);
}
/*
* Process the methods of the class.
* We need the following:
*
* a) Class of return type
* b) Class of parameters
* c) Class of fields in methods
*/
Method[] methods = clazz.getDeclaredMethods();
for (i = 0; i < methods.length; i++)
{
Class returnType = methods[i].getReturnType();
if (returnType != null)
{
classes.put(returnType, returnType);
}
Class[] parameterTypes = methods[i].getParameterTypes();
for (int k = 0; k < parameterTypes.length; k++)
{
classes.put(parameterTypes[k], parameterTypes[k]);
}
}
iterator = classes.values().iterator();
TreeMap classNames = new TreeMap();
classNames.putAll(LocalVariables.localVariables(className));
while (iterator.hasNext())
{
Class c = (Class) iterator.next();
String name = c.getName();
/*
* We don't want primitive types,
* or array types.
*/
if (!c.isPrimitive() && !c.isArray() && !name.startsWith("java.lang"))
{
classNames.put(name, name);
}
else if (c.isArray() && !name.startsWith("java.lang"))
{
/*
* Not sure how to get the fully qualified
* class name any other way then stripping
* the first characters and the last. And
* this might not work in all cases.
*/
name = name.substring(0, name.length() - 1).substring(2);
classNames.put(name, name);
}
}
iterator = classNames.values().iterator();
while (iterator.hasNext())
{
System.out.println(iterator.next());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ImportStatements is = new ImportStatements(args[0]);
}
}
1.1 jakarta-turbine/proposals/jvz/code/LocalVariables.java
Index: LocalVariables.java
===================================================================
package org.apache.turbine.util;
import java.util.Hashtable;
import java.io.*;
import de.fub.bytecode.classfile.*;
import de.fub.bytecode.*;
import de.fub.bytecode.ClassPath;
/**
* Little helper to get at the local variables inside a method,
* as the reflection API doesn't seem to do much for you there.
*/
public class LocalVariables
{
/**
* Dump the disassembled code of all methods in the class.
*/
public static Hashtable localVariables(String name) throws Exception
{
Hashtable vars = new Hashtable();
JavaClass javaClass;
if ((javaClass = Repository.lookupClass(name)) == null)
{
javaClass = new ClassParser(name).parse();
}
Method[] methods = javaClass.getMethods();
for (int i = 0; i < methods.length; i++)
{
Code code = methods[i].getCode();
if (code != null)
{
LocalVariableTable table = code.getLocalVariableTable();
if (table == null)
{
continue;
}
LocalVariable[] lvt = table.getLocalVariableTable();
for (int j = 0; j < lvt.length; j++)
{
String lvar = Utility.signatureToString(lvt[j].getSignature());
/*
* Get rid of anything in java.lang because
* we don't need that in an import statement.
*/
if (lvar.indexOf(".") == -1)
continue;
/*
* Get rid of the array signature
* if present we just need the base type
* signature.
*/
if (lvar.endsWith("[]"))
lvar = lvar.substring(0, lvar.length() - 2);
vars.put(lvar,lvar);
}
}
}
return vars;
}
public static void main(String[] argv) throws Exception
{
localVariables(argv[0]);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org
Re: cvs commit: jakarta-turbine/proposals/jvz/code
ImportStatements.java LocalVariables.java
Posted by Jon Stevens <jo...@latchkey.com>.
on 4/17/01 2:37 AM, "jvanzyl@apache.org" <jv...@apache.org> wrote:
> jvanzyl 01/04/17 02:37:31
>
> Added: proposals/jvz/code ImportStatements.java LocalVariables.java
> Log:
> - little tools for creating fully qualified import statements
> from a classfile.
>
> Revision Changes Path
> 1.1 jakarta-turbine/proposals/jvz/code/ImportStatements.java
>
These would be great to go into the Commons. To bad the commons is so fucked
up that it would probably take you a month of arguing to convince people to
let you check stuff in there.
Thanks for creating these classes Jason.
-jon
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org