Introduction
As discussed in the previous post,
Compiling SWT Applications with GCJ, Part 2, we need a way to determine the dependencies of a class file in our topological sort. The tool I mentioned is called
ClassDependsOn, and a simple implementation in
bash is provided below.
Requirements / Assumptions
- The ClassDependsOn script requires a Unix environment.
- The tool jcf-dump from the gcc-java package is required.
- Recommended is to use Cygwin and install the gcc-java package with its installer.
Usage
- Place the ClassDependsOn script in your PATH and then cd into your base package directory. In our example, we are working with org.eclipse.swt, so our base package directory should contain the single folder org.
- Run ClassDependsOn and give as the argument a relative filename to the class without the .class extension.
Example and Expected Output
$ cd /cygdrive/c/MyBasePackageDirectory
$ ClassDependsOn org/eclipse/swt/browser/WebKit
org/eclipse/swt/browser/Browser.class
org/eclipse/swt/browser/BrowserFunction.class
org/eclipse/swt/browser/WebBrowser.class
org/eclipse/swt/browser/WebDownloadDelegate.class
org/eclipse/swt/browser/WebFrameLoadDelegate.class
org/eclipse/swt/browser/WebPolicyDelegate.class
org/eclipse/swt/browser/WebResourceLoadDelegate.class
org/eclipse/swt/browser/WebUIDelegate.class
org/eclipse/swt/graphics/Point.class
org/eclipse/swt/graphics/Rectangle.class
org/eclipse/swt/internal/C.class
org/eclipse/swt/internal/Callback.class
org/eclipse/swt/internal/Library.class
org/eclipse/swt/internal/ole/win32/COM.class
org/eclipse/swt/internal/ole/win32/GUID.class
org/eclipse/swt/internal/webkit/IWebCookieManager.class
org/eclipse/swt/internal/webkit/IWebDataSource.class
org/eclipse/swt/internal/webkit/IWebDocumentRepresentation.class
org/eclipse/swt/internal/webkit/IWebFrame.class
org/eclipse/swt/internal/webkit/IWebIBActions.class
org/eclipse/swt/internal/webkit/IWebMutableURLRequest.class
org/eclipse/swt/internal/webkit/IWebPreferences.class
org/eclipse/swt/internal/webkit/IWebView.class
org/eclipse/swt/internal/webkit/IWebViewPrivate.class
org/eclipse/swt/internal/webkit/JSClassDefinition.class
org/eclipse/swt/internal/webkit/WebKit_win32.class
org/eclipse/swt/internal/win32/OS.class
org/eclipse/swt/internal/win32/RECT.class
org/eclipse/swt/internal/win32/TCHAR.class
org/eclipse/swt/SWT.class
org/eclipse/swt/SWTError.class
org/eclipse/swt/SWTException.class
org/eclipse/swt/widgets/Composite.class
org/eclipse/swt/widgets/Control.class
org/eclipse/swt/widgets/Display.class
org/eclipse/swt/widgets/Event.class
org/eclipse/swt/widgets/Listener.class
org/eclipse/swt/widgets/Widget.class
The Code
#!/bin/bash
# Begin ClassDependsOn
# Syntax: ClassDependsOn CLASSNAME
# Reads a .class file determined by CLASSNAME and prints which packages it depends on
classname="$1"
classfiles="${classname}.class"
# For more convenient operation, change the CLASSPREFIX to an absolute path
# which is your base package directory
CLASSPREFIX="."
# All files that have the class name or that have a $ afterwards are included in processing
for f in "${classname}"\$*
do
if [ -e "$f" ]
then
classfiles="${classfiles} ${f}"
fi
done
# Run 'jcf-dump -c' to get the information. Any strings that look like Java packages are taken.
# Assumes none of the class filenames have blanks in them (this is not allowed in Java classnames)
# Exclude 'This class' line in the output
deps="$(jcf-dump -c $classfiles | grep -Po '(\w+\.)+\w+' | sort | uniq)"
# Check each dependency and see if it is a class file
echo "$deps" | while read
do
package="$REPLY"
package="${package//.//}" # Replace all instances of '.' with '/'
# If package is equivalent to the one we are looking for, exclude it
if [ "$package" == "$classname" ]
then
continue
fi
# If class file exists, output it
if [ -e "${CLASSPREFIX}/${package}.class" ]
then
echo ${package}.class
fi
done
No comments:
Post a Comment