blob: 0a3160c701d7a16cb7eaa1f27be9247f12a24c97 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package org.openslx.taskmanager.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
public class ClassLoaderHack
{
@SuppressWarnings( "rawtypes" )
private static final Class[] parameters = new Class[] { URL.class };
public static void addFile( String s ) throws IOException
{
File f = new File( s );
addFile( f );
}
public static void addFile( File f ) throws IOException
{
addURL( f.toURI().toURL() );
}
public static void addURL( URL u ) throws IOException
{
URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Class<URLClassLoader> sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod( "addURL", parameters );
method.setAccessible( true );
method.invoke( sysloader, new Object[] { u } );
System.out.println( "Loaded " + u.toString() );
} catch ( Throwable t ) {
t.printStackTrace();
throw new IOException( "Error, could not add URL to system classloader" );
}
}
/**
* Get Class meta-object for given class in package. Only return class if it's somehow
* extending from given baseClass.
*
* @param packageName package to search in
* @param className name of class to look for
* @param baseClass class the class in question has to be extended from
* @return class meta object, or null if not found
*/
@SuppressWarnings( "unchecked" )
public static <T> Class<? extends T> getClass( String packageName, String className, Class<T> baseClass )
{
final Class<?> clazz;
try {
clazz = Class.forName( packageName + '.' + className );
} catch ( ClassNotFoundException e ) {
return null;
}
if ( clazz == null || ( baseClass != null && !baseClass.isAssignableFrom( clazz ) ) ) {
return null;
}
return (Class<? extends T>)clazz;
}
}
|