|
| 1 | +/* |
| 2 | + * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product |
| 5 | + * that is described in this document. In particular, and without limitation, these intellectual property |
| 6 | + * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or |
| 7 | + * more additional patents or pending patent applications in the U.S. and in other countries. |
| 8 | + * |
| 9 | + * U.S. Government Rights - Commercial software. Government users are subject to the Sun |
| 10 | + * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its |
| 11 | + * supplements. |
| 12 | + * |
| 13 | + * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or |
| 14 | + * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks |
| 15 | + * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the |
| 16 | + * U.S. and other countries. |
| 17 | + * |
| 18 | + * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open |
| 19 | + * Company, Ltd. |
| 20 | + */ |
| 21 | +package com.sun.max.program; |
| 22 | + |
| 23 | +import java.io.*; |
| 24 | +import java.util.*; |
| 25 | +import java.util.zip.*; |
| 26 | + |
| 27 | +import com.sun.max.lang.*; |
| 28 | + |
| 29 | +/** |
| 30 | + * Provides a facility for finding classes reachable on a given {@linkplain Classpath classpath}. |
| 31 | + * |
| 32 | + * @author Doug Simon |
| 33 | + */ |
| 34 | +public class ClassSearch extends ClasspathTraversal { |
| 35 | + |
| 36 | + private final HashSet<String> classes; |
| 37 | + |
| 38 | + public ClassSearch() { |
| 39 | + this(false); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a class search object. |
| 44 | + * |
| 45 | + * @param omitDuplicates if true, then each argument passed to {@link #visitClass(String)} is guaranteed to be unique. |
| 46 | + */ |
| 47 | + public ClassSearch(boolean omitDuplicates) { |
| 48 | + if (omitDuplicates) { |
| 49 | + classes = new HashSet<String>(); |
| 50 | + } else { |
| 51 | + classes = null; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Handles a class file encountered during the traversal. |
| 57 | + * |
| 58 | + * @param className |
| 59 | + * the name of the class denoted by the class file |
| 60 | + * @return true if the traversal should continue, false if it should terminate |
| 61 | + */ |
| 62 | + protected boolean visitClass(String className) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Handles a class file encountered during the traversal. Unless this object was initialized to omit duplicates, |
| 68 | + * this method may be called more than once for the same class as class files are not guaranteed to be unique in a |
| 69 | + * classpath. |
| 70 | + * |
| 71 | + * @param isArchiveEntry true if the class is in a .zip or .jar file, false if it is a file in a directory |
| 72 | + * @param className the name of the class denoted by the class file |
| 73 | + * @return true if the traversal should continue, false if it should terminate |
| 74 | + */ |
| 75 | + protected boolean visitClass(boolean isArchiveEntry, String className) { |
| 76 | + if (classes != null) { |
| 77 | + if (classes.contains(className)) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + classes.add(className); |
| 81 | + } |
| 82 | + return visitClass(className); |
| 83 | + } |
| 84 | + |
| 85 | + protected boolean visit(boolean isArchiveEntry, String dottifiedResource) { |
| 86 | + if (dottifiedResource.endsWith(".class")) { |
| 87 | + final String className = Strings.chopSuffix(dottifiedResource, ".class"); |
| 88 | + return visitClass(isArchiveEntry, className); |
| 89 | + } |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected boolean visitArchiveEntry(ZipFile archive, ZipEntry resource) { |
| 95 | + return visit(true, resource.getName().replace('/', '.')); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected boolean visitFile(File parent, String resource) { |
| 100 | + return visit(false, resource.replace(File.separatorChar, '.')); |
| 101 | + } |
| 102 | +} |
0 commit comments