diff options
author | korneel vandamme <krampus.sack.never@gmail.com> | 2020-02-20 17:15:57 +0100 |
---|---|---|
committer | korneel vandamme <krampus.sack.never@gmail.com> | 2020-02-20 17:15:57 +0100 |
commit | 52cec59a102e3b18f8cca75e98c2d797bac4d19f (patch) | |
tree | ef4c05612b350e3aec1632fff29da76a07eb7724 /src/Java/gtPlusPlus/core/util | |
parent | eae5727a0419fec865acddb7374af677f8b83306 (diff) | |
parent | 63d8ed92dc34e62f0b7a5338fd7126e0e6fa01aa (diff) | |
download | GT5-Unofficial-52cec59a102e3b18f8cca75e98c2d797bac4d19f.tar.gz GT5-Unofficial-52cec59a102e3b18f8cca75e98c2d797bac4d19f.tar.bz2 GT5-Unofficial-52cec59a102e3b18f8cca75e98c2d797bac4d19f.zip |
Merge branch 'master' of https://botn365@bitbucket.org/draknyte1/gtpp-development.git
Diffstat (limited to 'src/Java/gtPlusPlus/core/util')
-rw-r--r-- | src/Java/gtPlusPlus/core/util/data/FileUtils.java | 33 | ||||
-rw-r--r-- | src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java | 81 |
2 files changed, 72 insertions, 42 deletions
diff --git a/src/Java/gtPlusPlus/core/util/data/FileUtils.java b/src/Java/gtPlusPlus/core/util/data/FileUtils.java index d7d6b9e36e..bec5e0eeff 100644 --- a/src/Java/gtPlusPlus/core/util/data/FileUtils.java +++ b/src/Java/gtPlusPlus/core/util/data/FileUtils.java @@ -8,6 +8,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.util.ArrayList; import java.util.List; import gtPlusPlus.api.objects.Logger; @@ -26,17 +27,21 @@ public class FileUtils { public static File createFile(String path, String filename, String extension) { File file = new File(Utils.getMcDir(), path + filename + extension); + return createFile(file); + } + + public static File createFile(File aFile) { boolean blnCreated = false; - Logger.INFO("Trying to use path "+file.getPath()); + Logger.INFO("Trying to use path "+aFile.getPath()); try { - Logger.INFO("Trying to use path "+file.getCanonicalPath()); - Logger.INFO("Trying to use absolute path "+file.getAbsolutePath()); - blnCreated = file.createNewFile(); + Logger.INFO("Trying to use path "+aFile.getCanonicalPath()); + Logger.INFO("Trying to use absolute path "+aFile.getAbsolutePath()); + blnCreated = aFile.createNewFile(); } catch (IOException ioe) { Logger.INFO("Error while creating a new empty file :" + ioe); return null; } - return blnCreated ? file : null; + return blnCreated ? aFile : null; } public static File getFile(String filename, String extension) { @@ -92,4 +97,22 @@ public class FileUtils { } return false; } + + /** + * Reads the contents of a file line by line to a List of Strings using the default encoding for the VM. + * The file is always closed. + * + * @param file the file to read, must not be {@code null} + * @return the list of Strings representing each line in the file, never {@code null} + * @throws IOException in case of an I/O error + * @since 1.3 + */ + public static List<String> readLines(File file) { + try { + return org.apache.commons.io.FileUtils.readLines(file, utf8); + } + catch (IOException e) { + return new ArrayList<String>(); + } + } } diff --git a/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java b/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java index 135f98dd17..4d8a02d800 100644 --- a/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java +++ b/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java @@ -43,7 +43,7 @@ public class ReflectionUtils { } } - + private static class CachedMethod { private final boolean STATIC; @@ -134,7 +134,7 @@ public class ReflectionUtils { return false; } - + /** * Returns a cached {@link Constructor} object. * @param aClass - Class containing the Constructor. @@ -145,7 +145,7 @@ public class ReflectionUtils { if (aClass == null || aTypes == null) { return null; } - + String aMethodKey = ArrayUtils.toString(aTypes); //Logger.REFLECTION("Looking up method in cache: "+(aClass.getName()+"."+aMethodName + "." + aMethodKey)); CachedConstructor y = mCachedConstructors.get(aClass.getName() + "." + aMethodKey); @@ -162,9 +162,9 @@ public class ReflectionUtils { return y.get(); } } - - - + + + /** * Returns a cached {@link Class} object. @@ -227,14 +227,14 @@ public class ReflectionUtils { return y.get(); } } - + public static boolean isStaticMethod(Class<?> aClass, String aMethodName, Class<?>... aTypes) { return isStaticMethod(ReflectionUtils.getMethod(aClass, aMethodName, aTypes)); } - + public static boolean isStaticMethod(Method aMethod) { if (aMethod != null && Modifier.isStatic(aMethod.getModifiers())) { - return true; + return true; } return false; } @@ -467,7 +467,7 @@ public class ReflectionUtils { Logger.REFLECTION("Invoke failed or did something wrong."); return false; } - + public static boolean invoke(Object objectInstance, Method method, Object[] values){ if (method == null || values == null || (!ReflectionUtils.isStaticMethod(method) && objectInstance == null)){ //Logger.REFLECTION("Null value when trying to Dynamically invoke "+methodName+" on an object of type: "+objectInstance.getClass().getName()); @@ -774,7 +774,7 @@ public class ReflectionUtils { } return m; } - + private static Constructor<?> getConstructor_Internal(Class<?> aClass, Class<?>... aTypes) { Constructor<?> c = null; try { @@ -801,7 +801,7 @@ public class ReflectionUtils { } return c; } - + private static Constructor<?> getConstructorRecursively(Class<?> aClass, Class<?>... aTypes) throws Exception { try { Logger.REFLECTION("Constructor: Recursion Lookup: "+aClass.getName()); @@ -921,31 +921,40 @@ public class ReflectionUtils { aClassName += (i > 0) ? "."+aData[i] : ""+aData[i]; Logger.REFLECTION("Building: "+aClassName); } - Logger.REFLECTION("Trying to search '"+aClassName+"' for inner classes."); - Class<?> clazz = ReflectionUtils.getClass(aClassName); - - Class[] y = clazz.getDeclaredClasses(); - if (y == null || y.length <= 0) { - Logger.REFLECTION("No hidden inner classes found."); - return null; - } - else { - boolean found = false; - for (Class<?> h : y) { - Logger.REFLECTION("Found hidden inner class: "+h.getCanonicalName()); - if (h.getSimpleName().toLowerCase().equals(aData[aData.length-1].toLowerCase())) { - Logger.REFLECTION("Found correct class. ["+aData[aData.length-1]+"] Caching at correct location: "+string); - Logger.REFLECTION("Found at location: "+h.getCanonicalName()); - ReflectionUtils.mCachedClasses.put(string, h); - aClass = h; - found = true; - break; + if (aClassName != null && aClassName.length() > 0) { + Logger.REFLECTION("Trying to search '"+aClassName+"' for inner classes."); + Class<?> clazz = ReflectionUtils.getClass(aClassName); + if (clazz != null) { + Class[] y = clazz.getDeclaredClasses(); + if (y == null || y.length <= 0) { + Logger.REFLECTION("No hidden inner classes found."); + return null; + } + else { + boolean found = false; + for (Class<?> h : y) { + Logger.REFLECTION("Found hidden inner class: "+h.getCanonicalName()); + if (h.getSimpleName().toLowerCase().equals(aData[aData.length-1].toLowerCase())) { + Logger.REFLECTION("Found correct class. ["+aData[aData.length-1]+"] Caching at correct location: "+string); + Logger.REFLECTION("Found at location: "+h.getCanonicalName()); + ReflectionUtils.mCachedClasses.put(string, h); + aClass = h; + found = true; + break; + } + } + if (!found) { + return null; + } } } - if (!found) { + else { return null; } } + else { + return null; + } } return aClass; } @@ -964,11 +973,9 @@ public class ReflectionUtils { */ private static void makeModifiable(Field nameField) throws Exception { nameField.setAccessible(true); - int modifiers = nameField.getModifiers(); - Field modifierField = nameField.getClass().getDeclaredField("modifiers"); - modifiers = modifiers & ~Modifier.FINAL; - modifierField.setAccessible(true); - modifierField.setInt(nameField, modifiers); + Field modifiers = getField(Field.class, "modifiers"); + modifiers.setAccessible(true); + modifiers.setInt(nameField, nameField.getModifiers() & ~Modifier.FINAL); } |