KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > stefanochizzolini > reflex > Package


1 package it.stefanochizzolini.reflex;
2
3 import java.io.*;
4 import java.util.*;
5 import java.util.jar.*;
6 import java.net.*;
7
8 public class Package
9 {
10   /**
11     Retrieves all the statically-available local classes
12     belonging to the specified package.
13
14     @param packageName The package name to search.
15     @return A list of the available classes within the specified package.
16   */

17   public static List<java.lang.Class JavaDoc> getClasses(
18     String JavaDoc packageName
19     )
20   {
21     return getClasses(
22       packageName,
23       (System.getProperty("sun.boot.class.path") + File.pathSeparator // Bootstrap classes.
24
+ System.getProperty("java.ext.dirs") + File.pathSeparator // Extension classes.
25
+ System.getProperty("java.class.path") // User classes.
26
).split(File.pathSeparator)
27       );
28   }
29
30   /**
31     Retrieves all the statically-available classes
32     belonging to the specified package
33     and located in any specified location.
34
35     @param packageName The package name to search.
36     @param locations The local paths to search.
37     @return A list of the available classes within the specified package.
38   */

39   public static List<java.lang.Class JavaDoc> getClasses(
40     String JavaDoc packageName,
41     String JavaDoc[] locations
42     )
43   {
44     try
45     {
46       String JavaDoc packagePath = packageName.replace('.','/') + '/';
47       String JavaDoc osDependentPackagePath = packagePath.replace('/',File.separatorChar);
48       ArrayList<java.lang.Class JavaDoc> classes = new ArrayList<java.lang.Class JavaDoc>();
49       for(
50         String JavaDoc location : locations
51         )
52       {
53         File locationFile = new File(location);
54         if(!locationFile.exists())
55           continue;
56
57         if(locationFile.isDirectory()) // Directory.
58
{
59           locationFile = new File(location + File.separator + osDependentPackagePath);
60           if(!locationFile.exists())
61             continue;
62
63           // Get the list of the files contained in the package!
64
String JavaDoc[] filePaths = locationFile.list();
65           for(String JavaDoc filePath : filePaths)
66           {
67             // Is it a class?
68
if(filePath.endsWith(".class"))
69             {
70               classes.add(
71                 java.lang.Class.forName(
72                   packageName + '.' + filePath.substring(0, filePath.length() - 6)
73                   )
74                 );
75             }
76           }
77         }
78         else // JAR file.
79
{
80           JarFile jarFile = new JarFile(locationFile);
81           for(
82             Enumeration entries = jarFile.entries();
83             entries.hasMoreElements();
84             )
85           {
86             String JavaDoc jarEntryPath = ((JarEntry)entries.nextElement()).getName();
87             if(
88               jarEntryPath.startsWith(packagePath)
89                 && jarEntryPath.endsWith(".class")
90               )
91             {
92               classes.add(
93                 java.lang.Class.forName(
94                   jarEntryPath.substring(0, jarEntryPath.length() - 6).replaceAll("/",".")
95                   )
96                 );
97             }
98           }
99         }
100       }
101
102       return classes;
103     }
104     catch(Exception JavaDoc e)
105     {throw new RuntimeException JavaDoc(e);}
106   }
107 }
Popular Tags