mdahm 2002/11/10 10:30:05
Modified: examples TransitiveHull.java
src/java/org/apache/bcel/util ClassPath.java
SyntheticRepository.java
Log:
TH works for arrays too, Dont use JDK1.4 regex
Revision Changes Path
1.2 +55 -24 jakarta-bcel/examples/TransitiveHull.java
Index: TransitiveHull.java
===================================================================
RCS file: /home/cvs/jakarta-bcel/examples/TransitiveHull.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TransitiveHull.java 9 Nov 2002 17:09:03 -0000 1.1
+++ TransitiveHull.java 10 Nov 2002 18:30:05 -0000 1.2
@@ -5,16 +5,20 @@
import java.util.*;
import org.apache.bcel.Constants;
import org.apache.bcel.Repository;
+import org.apache.regexp.*;
/**
* Find all classes referenced by given start class and all classes
- * referenced by tjose and so on. In other words: Compute the tranitive
+ * referenced by those and so on. In other words: Compute the transitive
* hull of classes used by a given class. This is done by checking all
* ConstantClass entries and all method and field signatures.<br> This
* may be useful in order to put all class files of an application
- * into a single JAR file.
+ * into a single JAR file, e.g..
* <p>
- * It fails however in the presence of reflection code.
+ * It fails however in the presence of reflexive code aka introspection.
+ * <p>
+ * You'll need Apache's regular expression library supplied together
+ * with BCEL to use this class.
*
* @version $Id$
* @author <A HREF="mailto:[email protected]">M. Dahm</A>
@@ -24,14 +28,20 @@
private ClassQueue _queue;
private ClassSet _set;
private ConstantPool _cp;
+ private String[] _ignored = IGNORED;
- private String[] _ignored = {
+ public static final String[] IGNORED = {
"java[.].*",
"javax[.].*",
- "com[.]sun[.].*"
+ "sun[.].*",
+ "sunw[.].*",
+ "com[.]sun[.].*",
+ "org[.]omg[.].*",
+ "org[.]w3c[.].*",
+ "org[.]xml[.].*",
+ "net[.]jini[.].*"
};
-
public TransitiveHull(JavaClass clazz) {
_queue = new ClassQueue();
_queue.enqueue(clazz);
@@ -63,14 +73,21 @@
private void add(String class_name) {
class_name = class_name.replace('/', '.');
- for(int i = 0; i < _ignored.length; i++) {
- if(class_name.matches(_ignored[i])) {
- return; // Ihh
+ try {
+ for(int i = 0; i < _ignored.length; i++) {
+ RE regex = new RE(_ignored[i]);
+
+ if(regex.match(class_name)) {
+ return; // Ihh
+ }
}
+ } catch(RESyntaxException ex) {
+ System.out.println(ex);
+ return;
}
-
+
JavaClass clazz = Repository.lookupClass(class_name);
-
+
if(clazz != null && _set.add(clazz)) {
_queue.enqueue(clazz);
}
@@ -81,6 +98,16 @@
add(class_name);
}
+ private void checkType(Type type) {
+ if(type instanceof ArrayType) {
+ type = ((ArrayType)type).getBasicType();
+ }
+
+ if(type instanceof ObjectType) {
+ add(((ObjectType)type).getClassName());
+ }
+ }
+
private void visitRef(ConstantCP ccp, boolean method) {
String class_name = ccp.getClass(_cp);
add(class_name);
@@ -92,24 +119,16 @@
if(method) {
Type type = Type.getReturnType(signature);
-
- if(type instanceof ObjectType) {
- add(((ObjectType)type).getClassName());
- }
+
+ checkType(type);
Type[] types = Type.getArgumentTypes(signature);
for(int i = 0; i < types.length; i++) {
- type = types[i];
- if(type instanceof ObjectType) {
- add(((ObjectType)type).getClassName());
- }
+ checkType(types[i]);
}
} else {
- Type type = Type.getType(signature);
- if(type instanceof ObjectType) {
- add(((ObjectType)type).getClassName());
- }
+ checkType(Type.getType(signature));
}
}
@@ -123,6 +142,18 @@
public void visitConstantFieldref(ConstantFieldref cfr) {
visitRef(cfr, false);
+ }
+
+ public String[] getIgnored() {
+ return _ignored;
+ }
+
+ /**
+ * Set the value of _ignored.
+ * @param v Value to assign to _ignored.
+ */
+ public void setIgnored(String[] v) {
+ _ignored = v;
}
public static void main(String[] argv) {
1.7 +2 -2 jakarta-bcel/src/java/org/apache/bcel/util/ClassPath.java
Index: ClassPath.java
===================================================================
RCS file: /home/cvs/jakarta-bcel/src/java/org/apache/bcel/util/ClassPath.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ClassPath.java 29 Oct 2002 18:41:27 -0000 1.6
+++ ClassPath.java 10 Nov 2002 18:30:05 -0000 1.7
@@ -308,7 +308,7 @@
public abstract String getPath();
/** @return base path of found class, i.e. class is contained relative
- * to that path
+ * to that path, which may either denote a directory, or zip file
*/
public abstract String getBase();
1.6 +2 -2 jakarta-bcel/src/java/org/apache/bcel/util/SyntheticRepository.java
Index: SyntheticRepository.java
===================================================================
RCS file: /home/cvs/jakarta-bcel/src/java/org/apache/bcel/util/SyntheticRepository.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- SyntheticRepository.java 11 Oct 2002 20:34:47 -0000 1.5
+++ SyntheticRepository.java 10 Nov 2002 18:30:05 -0000 1.6
@@ -74,7 +74,7 @@
/**
* Abstract definition of a class repository. Instances may be used
* to load classes from different sources and may be used in the
- * Repository.setRpeository method.
+ * Repository.setRepository method.
*
* @see org.apache.bcel.Repository
*
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.