diff options
author | draknyte1 <draknyte1@hotmail.com> | 2016-11-04 15:23:26 +1000 |
---|---|---|
committer | draknyte1 <draknyte1@hotmail.com> | 2016-11-04 15:23:26 +1000 |
commit | 0669f5eb9d5029a8b94ec552171b0837605f7747 (patch) | |
tree | 6b40e64c04d51b7a33cf2f0b35f7232cf37c4247 /src/Java/gtPlusPlus/core/util/ClassUtils.java | |
parent | 3654052fb63a571c5eaca7f20714b87c17f7e966 (diff) | |
download | GT5-Unofficial-0669f5eb9d5029a8b94ec552171b0837605f7747.tar.gz GT5-Unofficial-0669f5eb9d5029a8b94ec552171b0837605f7747.tar.bz2 GT5-Unofficial-0669f5eb9d5029a8b94ec552171b0837605f7747.zip |
$ Cleaned up the entire project.
> Much neat, very nices.
Diffstat (limited to 'src/Java/gtPlusPlus/core/util/ClassUtils.java')
-rw-r--r-- | src/Java/gtPlusPlus/core/util/ClassUtils.java | 79 |
1 files changed, 41 insertions, 38 deletions
diff --git a/src/Java/gtPlusPlus/core/util/ClassUtils.java b/src/Java/gtPlusPlus/core/util/ClassUtils.java index dfd8ec898b..e58c2f2785 100644 --- a/src/Java/gtPlusPlus/core/util/ClassUtils.java +++ b/src/Java/gtPlusPlus/core/util/ClassUtils.java @@ -4,65 +4,53 @@ import java.lang.reflect.*; 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; - } - } - - 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){ + public static Method getMethodViaReflection(final Class<?> lookupClass, final String methodName, + final boolean invoke) throws Exception { + final Class<? extends Class> lookup = lookupClass.getClass(); + final 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){ + public static Class getNonPublicClass(final String className) { Class<?> c = null; try { c = Class.forName(className); - } catch (ClassNotFoundException e) { + } + catch (final ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } - //full package name --------^^^^^^^^^^ - //or simpler without Class.forName: - //Class<package1.A> c = package1.A.class; + // 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 + if (null != c) { + // In our case we need to use Constructor<?> constructor = null; try { constructor = c.getDeclaredConstructor(); - } catch (NoSuchMethodException | SecurityException e) { + } + 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 + // 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! + // now we need to make this constructor accessible + if (null != constructor) { + constructor.setAccessible(true);// ABRACADABRA! try { - Object o = constructor.newInstance(); + final Object o = constructor.newInstance(); return (Class) o; - } catch (InstantiationException | IllegalAccessException - | IllegalArgumentException | InvocationTargetException e) { + } + catch (InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } @@ -71,6 +59,21 @@ public class ClassUtils { return null; } - + /* + * @ 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(final String className) { + try { + Class.forName(className); + return true; + } + catch (final Throwable ex) { + // Class or one of its dependencies is not present... + return false; + } + } } |