KickJava   Java API By Example, From Geeks To Geeks.

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


1 package it.stefanochizzolini.reflex;
2
3 import java.io.File JavaDoc;
4 import java.net.URI JavaDoc;
5 import java.net.URL JavaDoc;
6
7 public class Class
8 {
9   /**
10     Retrieves the primary available location of a class.
11
12     @param className The name of the class whose location has to be retrieved.
13     @return The primary location of the class.
14   */

15   public static String JavaDoc getLocation(
16     String JavaDoc className
17     )
18   {
19     String JavaDoc classResourcePath = className.replace('.', '/') + ".class";
20     // Get class position!
21
URL JavaDoc classUrl = Thread.currentThread().getContextClassLoader().getResource(classResourcePath);
22     if(classUrl == null)
23       return null;
24
25     // Get class location!
26
String JavaDoc location;
27     try
28     {
29       location = new File JavaDoc(classUrl.getFile()).getPath();
30     }
31     catch(Exception JavaDoc e)
32     {throw new RuntimeException JavaDoc(e);}
33     // Is it inside a jar file?
34
int index = location.indexOf("!");
35     if(index >= 0)
36     {
37       // Eliminate the inner path!
38
location = location.substring(0, index);
39     }
40     // Is there a protocol identifier?
41
index = location.indexOf(":");
42     if(index >= 0)
43     {
44       // Eliminate the leading protocol identifier!
45
location = location.substring(++index);
46     }
47
48     return location;
49   }
50 }
Popular Tags