diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Agile Software Development/TestApp/src/testapp/Locate.java | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Master/Agile Software Development/TestApp/src/testapp/Locate.java')
| -rw-r--r-- | Master/Agile Software Development/TestApp/src/testapp/Locate.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Master/Agile Software Development/TestApp/src/testapp/Locate.java b/Master/Agile Software Development/TestApp/src/testapp/Locate.java new file mode 100644 index 0000000..49f96a9 --- /dev/null +++ b/Master/Agile Software Development/TestApp/src/testapp/Locate.java @@ -0,0 +1,90 @@ +/* + * + */ + +package testapp; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.logging.*; + +/**@author McDowell*/ +public class Locate { + + /** + * Returns the URL of a given class. + * @param c a non-null class + * @return the URL for that class + */ + public static URL getUrlOfClass(Class c) { + if(c==null) { + throw new NullPointerException(); + } + String className = c.getName(); + String resourceName = className.replace('.', '/') + ".class"; + ClassLoader classLoader = c.getClassLoader(); + if(classLoader==null) { + classLoader = ClassLoader.getSystemClassLoader(); + } + URL url = classLoader.getResource(resourceName); + return url; + } + + /** + * Finds the location of a given class file on the file system. + * Throws an IOException if the class cannot be found. + * <br> + * If the class is in an archive (JAR, ZIP), then the returned object + * will point to the archive file. + * <br> + * If the class is in a directory, the base directory will be returned + * with the package directory removed. + * <br> + * The <code>File.isDirectory()</code> method can be used to + * determine which is the case. + * <br> + * @param c a given class + * @return a File object + * @throws IOException + */ + public static File getClassLocation(Class c) throws IOException, FileNotFoundException { + if(c==null) { + throw new NullPointerException(); + } + + String className = c.getName(); + String resourceName = className.replace('.', '/') + ".class"; + ClassLoader classLoader = c.getClassLoader(); + if(classLoader==null) { + classLoader = ClassLoader.getSystemClassLoader(); + } + URL url = classLoader.getResource(resourceName); + + String szUrl = url.toString(); + if(szUrl.startsWith("jar:file:")) { + try { + szUrl = szUrl.substring("jar:".length(), szUrl.lastIndexOf("!")); + URI uri = new URI(szUrl); + return new File(uri); + } catch(URISyntaxException e) { + throw new IOException(e.toString()); + } + } else if(szUrl.startsWith("file:")) { + try { + szUrl = szUrl.substring(0, szUrl.length() - resourceName.length()); + URI uri = new URI(szUrl); + return new File(uri); + } catch(URISyntaxException e) { + throw new IOException(e.toString()); + } + } + + throw new FileNotFoundException(szUrl); + } + +} + |
