diff options
author | Draknyte1 <Draknyte1@hotmail.com> | 2016-09-02 22:40:27 +1000 |
---|---|---|
committer | Draknyte1 <Draknyte1@hotmail.com> | 2016-09-02 22:40:27 +1000 |
commit | f6c3c10323d28c0f34e4e1823e24447f35ae6ffa (patch) | |
tree | 85bac9ed173ec9992acf1169c4f7466d6ec49f0e /src/Java/miscutil/core/util/ClassUtils.java | |
parent | cce709ec7453ea3a6f255e11bd9d61f94dd8b920 (diff) | |
download | GT5-Unofficial-f6c3c10323d28c0f34e4e1823e24447f35ae6ffa.tar.gz GT5-Unofficial-f6c3c10323d28c0f34e4e1823e24447f35ae6ffa.tar.bz2 GT5-Unofficial-f6c3c10323d28c0f34e4e1823e24447f35ae6ffa.zip |
$ Hopefully fixed cell generation.
Diffstat (limited to 'src/Java/miscutil/core/util/ClassUtils.java')
-rw-r--r-- | src/Java/miscutil/core/util/ClassUtils.java | 75 |
1 files changed, 65 insertions, 10 deletions
diff --git a/src/Java/miscutil/core/util/ClassUtils.java b/src/Java/miscutil/core/util/ClassUtils.java index 6ea9336006..ecf83f8b8f 100644 --- a/src/Java/miscutil/core/util/ClassUtils.java +++ b/src/Java/miscutil/core/util/ClassUtils.java @@ -1,23 +1,78 @@ package miscutil.core.util; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + public class ClassUtils { - + /*@ if (isPresent("com.optionaldependency.DependencyClass")) { // This block will never execute when the dependency is not present // There is therefore no more risk of code throwing NoClassDefFoundException. executeCodeLinkingToDependency(); }*/ public static boolean isPresent(String className) { - try { - Class.forName(className); - return true; - } catch (Throwable ex) { - // Class or one of its dependencies is not present... - return false; - } + try { + Class.forName(className); + return true; + } catch (Throwable ex) { + // Class or one of its dependencies is not present... + return false; + } + } + + public static Method getMethodViaReflection(Class<?> lookupClass, String methodName, boolean invoke) throws Exception{ + Class<? extends Class> lookup = lookupClass.getClass(); + Method m = lookup.getDeclaredMethod(methodName); + m.setAccessible(true);// Abracadabra + if (invoke){ + m.invoke(lookup);// now its OK + } + return m; } - - + public static Class getNonPublicClass(String className){ + Class<?> c = null; + try { + c = Class.forName(className); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + //full package name --------^^^^^^^^^^ + //or simpler without Class.forName: + //Class<package1.A> c = package1.A.class; + + if (null != c){ + //In our case we need to use + Constructor<?> constructor = null; + try { + constructor = c.getDeclaredConstructor(); + } catch (NoSuchMethodException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + //note: getConstructor() can return only public constructors + //so we needed to search for any Declared constructor + + //now we need to make this constructor accessible + if (null != constructor){ + constructor.setAccessible(true);//ABRACADABRA! + + try { + Object o = constructor.newInstance(); + return (Class) o; + } catch (InstantiationException | IllegalAccessException + | IllegalArgumentException | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + return null; + } + + + } |