diff options
author | huajijam <strhuaji@gmail.com> | 2019-03-18 20:48:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-18 20:48:01 +0800 |
commit | 847934e3f0ba597f6d43d5fafdd0e6192d007585 (patch) | |
tree | c82b2784655f51b48c430d0cdd22e70b0523aa11 /src/Java/gtPlusPlus/core/util | |
parent | 40d7e5da9f5b84213e2c3e4596fdc69b94bd523e (diff) | |
parent | f93d9fb323a5aee2ed5c30320998f26bc177d707 (diff) | |
download | GT5-Unofficial-847934e3f0ba597f6d43d5fafdd0e6192d007585.tar.gz GT5-Unofficial-847934e3f0ba597f6d43d5fafdd0e6192d007585.tar.bz2 GT5-Unofficial-847934e3f0ba597f6d43d5fafdd0e6192d007585.zip |
just fix more bugs
just fix more bugs
Diffstat (limited to 'src/Java/gtPlusPlus/core/util')
13 files changed, 666 insertions, 303 deletions
diff --git a/src/Java/gtPlusPlus/core/util/Utils.java b/src/Java/gtPlusPlus/core/util/Utils.java index eef768e799..d032384638 100644 --- a/src/Java/gtPlusPlus/core/util/Utils.java +++ b/src/Java/gtPlusPlus/core/util/Utils.java @@ -46,6 +46,7 @@ import gtPlusPlus.api.objects.data.AutoMap; import gtPlusPlus.api.objects.data.Pair; import gtPlusPlus.core.item.ModItems; import gtPlusPlus.core.lib.CORE; +import gtPlusPlus.core.lib.LoadedMods; import gtPlusPlus.core.material.Material; import gtPlusPlus.core.util.math.MathUtils; import gtPlusPlus.core.util.minecraft.FluidUtils; @@ -791,7 +792,13 @@ public class Utils { if (GT_Mod.VERSION == 509){ Class<GT_Mod> clazz; try { - clazz = (Class<GT_Mod>) Class.forName("gregtech.GT_Mod"); + + if (LoadedMods.BeyondRealityCore) { + //Safely assume it's Beyond Reality running .28-pre (If it's not, tough shit really?) + return new Pair<Integer, Integer>(9, 28); + } + + clazz = (Class<GT_Mod>) ReflectionUtils.getClass("gregtech.GT_Mod"); Field mSubversion = ReflectionUtils.getField(clazz, "SUBVERSION"); if (mSubversion != null){ int mSub = 0; @@ -802,7 +809,9 @@ public class Utils { } } } - catch (Throwable t){} + catch (Throwable t){ + + } } //5.08.33 else if (GT_Mod.VERSION == 508){ @@ -939,4 +948,16 @@ public class Utils { return aOther; } + public static long getMillisSince(long aStartTime, long aCurrentTime) { + return (aCurrentTime - aStartTime); + } + + public static long getSecondsFromMillis(long aMillis) { + return (aMillis/1000); + } + + public static long getTicksFromSeconds(long aSeconds) { + return (aSeconds*20); + } + } diff --git a/src/Java/gtPlusPlus/core/util/data/StringUtils.java b/src/Java/gtPlusPlus/core/util/data/StringUtils.java index e58b68665a..2372d04c15 100644 --- a/src/Java/gtPlusPlus/core/util/data/StringUtils.java +++ b/src/Java/gtPlusPlus/core/util/data/StringUtils.java @@ -1,5 +1,9 @@ package gtPlusPlus.core.util.data; +import gtPlusPlus.api.objects.Logger; +import gtPlusPlus.api.objects.data.AutoMap; +import gtPlusPlus.core.util.Utils; + public class StringUtils { public static String superscript(String str) { @@ -116,4 +120,100 @@ public class StringUtils { String restLetters = data.substring(1).toLowerCase(); return firstLetter + restLetters; } + + public static <V> String getDataStringFromArray(V[] parameterTypes) { + if (parameterTypes == null || parameterTypes.length == 0) { + return "empty/null"; + } + else { + String aData = ""; + for (V y : parameterTypes) { + if (y != null) { + aData += ", "+y.toString(); + } + } + return aData; + } + } + + + + /** + * Is this a special regex character for delimination? (.$|()[]{}^?*+\\) + * @param aChar - The char to test + * @return - Is this a special character? + */ + public static boolean isSpecialCharacter(char aChar) { + if (aChar == '"' || aChar == '.' || aChar == '$' || aChar == '|' || aChar == '(' || aChar == ')' || aChar == '[' + || aChar == ']' || aChar == '{' || aChar == '}' || aChar == '^' || aChar == '?' || aChar == '*' + || aChar == '+' || aChar == '\\') { + return true; + } + return false; + } + + public static boolean isEscaped(String aString) { + return aString.substring(0, 1).equals("\\"); + } + + public static String splitAndUppercase(String aInput, String aDelim) { + + if (!isEscaped(aDelim)) { + boolean isSpecial = false; + for (int o=0;o<aInput.length();o++) { + if (isSpecialCharacter(aInput.charAt(o))) { + isSpecial = true; + } + } + if (isSpecial) { + aDelim = "\\"+aDelim; + } + } + + + Logger.INFO("Splitting "+aInput); + String[] aSplit = aInput.split(aDelim); + Logger.INFO("Split into "+aSplit == null ? ""+0 : aSplit.length+" parts."); + if (aSplit == null || aSplit.length == 0) { + return aInput; + } + else { + AutoMap<String> aTemp = new AutoMap<String>(); + for (String s : aSplit) { + Logger.INFO("Found: "+s); + s = s.replace(".", ""); + s = Utils.sanitizeString(s); + s = firstLetterCaps(s); + Logger.INFO("Formatted & Captilized: "+s); + aTemp.put(s); + } + Logger.INFO("Rebuilding"); + String aReturn = ""; + for (String s : aTemp) { + aReturn += s; + Logger.INFO("Step: "+aReturn); + } + return aReturn; + } + } + + public static int characterCount(String aString, char aChar) { + return characterCount(aString, ""+aChar); + } + + public static int characterCount(String aString, String aChar) { + int aLength = aString.length(); + int aFound = 0; + if (aLength == 0 || !aString.contains(aChar)) { + return 0; + } + else { + for (int index = 0; index < aLength; index++) { + if (aString.substring(index, index+1).equals(aChar)) { + aFound++; + } + } + return aFound; + } + } } diff --git a/src/Java/gtPlusPlus/core/util/math/MathUtils.java b/src/Java/gtPlusPlus/core/util/math/MathUtils.java index 40ac23a1b8..f9a4d8f548 100644 --- a/src/Java/gtPlusPlus/core/util/math/MathUtils.java +++ b/src/Java/gtPlusPlus/core/util/math/MathUtils.java @@ -463,14 +463,14 @@ public class MathUtils { return 0; } int divisor = aDataSet.length; - Logger.INFO("Calculating Average Short. Divisor: "+divisor); + Logger.WARNING("Calculating Average Short. Divisor: "+divisor); short total = 0; for (short i : aDataSet) { - Logger.INFO("Adding "+i); + Logger.WARNING("Adding "+i); total += i; } short result = safeShort((total/divisor)); - Logger.INFO("Average: "+result); + Logger.WARNING("Average: "+result); return result; } public static int getIntAverage(int[] aDataSet) { @@ -681,4 +681,15 @@ public class MathUtils { return i; } + /** + * Balances a number within a range. + * @param aInput - The number to balance + * @param aMin - The minimum bounds + * @param aMax - The maximum bounds + * @return - An Integer which will be between the bounds, or a boundary value. + */ + public static int balance(int aInput, int aMin, int aMax) { + return Math.max(Math.min(aInput, aMax), aMin); + } + } diff --git a/src/Java/gtPlusPlus/core/util/minecraft/FluidUtils.java b/src/Java/gtPlusPlus/core/util/minecraft/FluidUtils.java index ed1cbe0c64..0ae751a20b 100644 --- a/src/Java/gtPlusPlus/core/util/minecraft/FluidUtils.java +++ b/src/Java/gtPlusPlus/core/util/minecraft/FluidUtils.java @@ -10,9 +10,7 @@ import gregtech.api.util.GT_LanguageManager; import gtPlusPlus.api.objects.Logger; import gtPlusPlus.api.objects.minecraft.FluidGT6; -import gtPlusPlus.core.fluids.GenericFluid; import gtPlusPlus.core.item.base.BaseItemComponent; -import gtPlusPlus.core.item.base.cell.BaseItemCell; import gtPlusPlus.core.item.base.cell.BaseItemPlasmaCell; import gtPlusPlus.core.material.Material; import gtPlusPlus.core.material.MaterialGenerator; @@ -93,82 +91,6 @@ public class FluidUtils { } - - /** - * @param String displayName - * @param String fluidName - * @param int meltingPointC Temp - * @param short[] rgba - * @param byte state - * States: 0 (Solid), 1 (Fluid), 2(Gas), 3(Plasma) 4(Fuel I think? Don't use.) - * - * @return short[] - */ - public static Fluid generateFluid(final String displayName, final String fluidName, final int tempK, final short[] rgba ,final int aState){ - Fluid generatedFluid = null; - switch (aState) { - case 0: { - generatedFluid = new GenericFluid(displayName, fluidName, 0, 100, tempK, 10000, false, rgba); - break; - } - default: - case 1: - case 4: { - generatedFluid = new GenericFluid(displayName, fluidName, 0, 100, tempK, 1000, false, rgba); - break; - } - case 2: { - generatedFluid = new GenericFluid(displayName, fluidName, 0, -100, tempK, 200, true, rgba); - break; - } - case 3: { - generatedFluid = new GenericFluid(displayName, fluidName, 15, -10000, tempK, 10, true, rgba); - break; - } - } - return generatedFluid; - } - /** - * - * @param String fluidName - * @param int meltingPointC Temp - * @param short[] rgba - * @param byte state - * States: 0 (Solid), 1 (Fluid), 2(Gas), 3(Plasma) 4(Fuel I think? Don't use.) - * - * @return short[] - */ - public static Fluid generateFluid(final Material material ,final int aState){ - final int tempK = material.getMeltingPointC(); - Fluid generatedFluid = null; - switch (aState) { - case 0: { - generatedFluid = new GenericFluid(material, 0, 100, tempK, 10000, false); - break; - } - default: - case 1: - case 4: { - generatedFluid = new GenericFluid(material, 0, 100, tempK, 1000, false); - break; - } - case 2: { - generatedFluid = new GenericFluid(material, 0, -100, tempK, 200, true); - break; - } - case 3: { - generatedFluid = new GenericFluid(material, 15, -10000, tempK, 10, true); - break; - } - } - return generatedFluid; - } - - - - - - public static Fluid addGtFluid(final String aName, final String aLocalized, final GT_Materials aMaterial, final int aState, final long aTemperatureK, final ItemStack aFullContainer, final ItemStack aEmptyContainer, final int aFluidAmount) { return addGtFluid(aName, aLocalized, aMaterial, aState, aTemperatureK, aFullContainer, aEmptyContainer, aFluidAmount, true); } diff --git a/src/Java/gtPlusPlus/core/util/minecraft/FoodUtils.java b/src/Java/gtPlusPlus/core/util/minecraft/FoodUtils.java index a1c2bf6104..9f5d4f36ca 100644 --- a/src/Java/gtPlusPlus/core/util/minecraft/FoodUtils.java +++ b/src/Java/gtPlusPlus/core/util/minecraft/FoodUtils.java @@ -13,7 +13,7 @@ public class FoodUtils { public static final Class IEdibleClass; static { - IEdibleClass = ReflectionUtils.getClassByName("squeek.applecore.api.food.IEdible"); + IEdibleClass = ReflectionUtils.getClass("squeek.applecore.api.food.IEdible"); } public static boolean isFood(ItemStack food) { diff --git a/src/Java/gtPlusPlus/core/util/minecraft/ItemUtils.java b/src/Java/gtPlusPlus/core/util/minecraft/ItemUtils.java index a5cf9527a9..d32ff4e160 100644 --- a/src/Java/gtPlusPlus/core/util/minecraft/ItemUtils.java +++ b/src/Java/gtPlusPlus/core/util/minecraft/ItemUtils.java @@ -35,6 +35,7 @@ import net.minecraft.init.Items; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.Item.ToolMaterial; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.StatCollector; @@ -123,7 +124,7 @@ public class ItemUtils { final int meta) { try { Item em = null; - final Item em1 = getItem(FQRN); + final Item em1 = getItemFromFQRN(FQRN); // Utils.LOG_WARNING("Found: "+em1.getUnlocalizedName()+":"+meta); if (em1 != null) { em = em1; @@ -157,7 +158,7 @@ public class ItemUtils { if (MOD) { try { Item em = null; - final Item em1 = getItem(FQRN); + final Item em1 = getItemFromFQRN(FQRN); // Utils.LOG_WARNING("Found: "+em1.getUnlocalizedName()+":"+meta); if (em1 != null) { if (null == em) { @@ -180,7 +181,7 @@ public class ItemUtils { public static ItemStack simpleMetaStack(final String FQRN, final int meta, final int itemstackSize) { try { Item em = null; - final Item em1 = getItem(FQRN); + final Item em1 = getItemFromFQRN(FQRN); // Utils.LOG_WARNING("Found: "+em1.getUnlocalizedName()+":"+meta); if (em1 != null) { if (null == em) { @@ -232,7 +233,7 @@ public class ItemUtils { ItemStack temp; if (fqrn.toLowerCase().contains(oreDict.toLowerCase())) { final String sanitizedName = fqrn.replace(oreDict, ""); - temp = ItemUtils.getItemStack(sanitizedName, stackSize); + temp = ItemUtils.getItemStackFromFQRN(sanitizedName, stackSize); return temp; } final String[] fqrnSplit = fqrn.split(":"); @@ -257,13 +258,13 @@ public class ItemUtils { return null; } - public static Item getItem(final String fqrn) // fqrn = fully qualified resource name + public static Item getItemFromFQRN(final String fqrn) // fqrn = fully qualified resource name { final String[] fqrnSplit = fqrn.split(":"); return GameRegistry.findItem(fqrnSplit[0], fqrnSplit[1]); } - public static ItemStack getItemStack(final String fqrn, final int Size) // fqrn = fully qualified resource name + public static ItemStack getItemStackFromFQRN(final String fqrn, final int Size) // fqrn = fully qualified resource name { final String[] fqrnSplit = fqrn.split(":"); return GameRegistry.findItemStack(fqrnSplit[0], fqrnSplit[1], Size); @@ -395,6 +396,61 @@ public class ItemUtils { new BaseItemDustUnique("itemDust" + unlocalizedName, materialName, mChemForm, Colour, "Dust"), new BaseItemDustUnique("itemDustSmall" + unlocalizedName, materialName, mChemForm, Colour, "Small"), new BaseItemDustUnique("itemDustTiny" + unlocalizedName, materialName, mChemForm, Colour, "Tiny") }; + + //Generate Shaped/Shapeless Recipes + + final ItemStack normalDust = ItemUtils.getSimpleStack(output[0]); + final ItemStack smallDust = ItemUtils.getSimpleStack(output[1]); + final ItemStack tinyDust = ItemUtils.getSimpleStack(output[2]); + + + if (ItemUtils.checkForInvalidItems(tinyDust) && ItemUtils.checkForInvalidItems(normalDust)) { + if (RecipeUtils.recipeBuilder( + tinyDust, tinyDust, tinyDust, + tinyDust, tinyDust, tinyDust, + tinyDust, tinyDust, tinyDust, + normalDust)){ + Logger.WARNING("9 Tiny dust to 1 Dust Recipe: "+materialName+" - Success"); + } + else { + Logger.WARNING("9 Tiny dust to 1 Dust Recipe: "+materialName+" - Failed"); + } + + if (RecipeUtils.recipeBuilder( + normalDust, null, null, + null, null, null, + null, null, null, + ItemUtils.getSimpleStack(tinyDust, 9))){ + Logger.WARNING("9 Tiny dust from 1 Recipe: "+materialName+" - Success"); + } + else { + Logger.WARNING("9 Tiny dust from 1 Recipe: "+materialName+" - Failed"); + } + } + + if (ItemUtils.checkForInvalidItems(smallDust) && ItemUtils.checkForInvalidItems(normalDust)) { + if (RecipeUtils.recipeBuilder( + smallDust, smallDust, null, + smallDust, smallDust, null, + null, null, null, + normalDust)){ + Logger.WARNING("4 Small dust to 1 Dust Recipe: "+materialName+" - Success"); + } + else { + Logger.WARNING("4 Small dust to 1 Dust Recipe: "+materialName+" - Failed"); + } + if (RecipeUtils.recipeBuilder( + null, normalDust, null, + null, null, null, + null, null, null, + ItemUtils.getSimpleStack(smallDust, 4))){ + Logger.WARNING("4 Small dust from 1 Dust Recipe: "+materialName+" - Success"); + } + else { + Logger.WARNING("4 Small dust from 1 Dust Recipe: "+materialName+" - Failed"); + } + } + return output; } @@ -607,7 +663,7 @@ public class ItemUtils { } public static String getArrayStackNames(final ItemStack[] aStack) { - String itemNames = "Item Array: "; + String itemNames = ""; int aPos = 0; for (final ItemStack alph : aStack) { if (alph == null) { @@ -1012,4 +1068,50 @@ public class ItemUtils { } + public static String getItemName(ItemStack aStack) { + if (aStack == null) { + return "ERROR - Empty Stack"; + } + String aDisplay = null; + try { + aDisplay = ("" + StatCollector + .translateToLocal(aStack.getItem().getUnlocalizedNameInefficiently(aStack) + ".name")) + .trim(); + if (aStack.hasTagCompound()) { + if (aStack.stackTagCompound != null && aStack.stackTagCompound.hasKey("display", 10)) { + NBTTagCompound nbttagcompound = aStack.stackTagCompound.getCompoundTag("display"); + + if (nbttagcompound.hasKey("Name", 8)) { + aDisplay = nbttagcompound.getString("Name"); + } + } + } + } catch (Throwable t) { + + } + if (aDisplay == null || aDisplay.length() <= 0) { + aDisplay = aStack.getUnlocalizedName() + ":" + aStack.getItemDamage(); + } else { + aDisplay += " | Meta: " + aStack.getItemDamage(); + } + return aDisplay; + } + + public static String getUnlocalizedItemName(ItemStack aStack) { + if (aStack == null) { + return "ERROR.Empty.Stack"; + } + String aDisplay = null; + try { + aDisplay = (aStack.getUnlocalizedName()).trim(); + + } catch (Throwable t) { + aDisplay = aStack.getItem().getUnlocalizedName(); + } + if (aDisplay == null || aDisplay.length() <= 0) { + aDisplay = aStack.getItem().getUnlocalizedNameInefficiently(aStack); + } + return aDisplay; + } + } diff --git a/src/Java/gtPlusPlus/core/util/minecraft/MaterialUtils.java b/src/Java/gtPlusPlus/core/util/minecraft/MaterialUtils.java index 45d21a3016..a7fc4507e8 100644 --- a/src/Java/gtPlusPlus/core/util/minecraft/MaterialUtils.java +++ b/src/Java/gtPlusPlus/core/util/minecraft/MaterialUtils.java @@ -322,7 +322,7 @@ public class MaterialUtils { mName = (String) ReflectionUtils.getField(Materials.class, "mName").get(mat); } } - catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) { + catch (IllegalArgumentException | IllegalAccessException e) { } @@ -348,12 +348,30 @@ public class MaterialUtils { TextureSet o = (r != null && r.isPresent() && r.get() != null) ? r.get() : null; return o;*/ } + + + + public static Materials getMaterial(String aMaterialName, String aFallbackMaterialName) { + Materials g = getMaterial(aMaterialName); + if (g == null) { + g = getMaterial(aFallbackMaterialName); + } + if (g == null) { + Logger.INFO("Failed finding material '"+aMaterialName+"' & fallback '"+aFallbackMaterialName+"', returning _NULL."); + g = Materials._NULL; + } + return g; + } public static Materials getMaterial(String aMaterialName) { Materials m = gtPlusPlus.xmod.gregtech.common.StaticFields59.getMaterial(aMaterialName); if (m == null) { m = getMaterialByName(aMaterialName); } + if (m == null) { + Logger.INFO("Failed finding material '"+aMaterialName+"', returning _NULL."); + m = Materials._NULL; + } return m; } diff --git a/src/Java/gtPlusPlus/core/util/minecraft/MiningUtils.java b/src/Java/gtPlusPlus/core/util/minecraft/MiningUtils.java index e7ced98f5a..080b5665a3 100644 --- a/src/Java/gtPlusPlus/core/util/minecraft/MiningUtils.java +++ b/src/Java/gtPlusPlus/core/util/minecraft/MiningUtils.java @@ -152,7 +152,7 @@ public class MiningUtils { try { aTextWorldGen = (String) ReflectionUtils.getField(GT_Worldgen_GT_Ore_Layer.class, "aTextWorldgen").get(h); - } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) { + } catch (IllegalArgumentException | IllegalAccessException e) { aTextWorldGen = h.mWorldGenName; } @@ -180,27 +180,27 @@ public class MiningUtils { boolean aEndAsteroids; try { - if (Class.forName("micdoodle8.mods.galacticraft.core.util.ConfigManagerCore") != null && mMoonID == -99) { - mMoonID = ReflectionUtils.getField(Class.forName("micdoodle8.mods.galacticraft.core.util.ConfigManagerCore"), "idDimensionMoon").getInt(null); + if (ReflectionUtils.getClass("micdoodle8.mods.galacticraft.core.util.ConfigManagerCore") != null && mMoonID == -99) { + mMoonID = ReflectionUtils.getField(ReflectionUtils.getClass("micdoodle8.mods.galacticraft.core.util.ConfigManagerCore"), "idDimensionMoon").getInt(null); } } - catch (ClassNotFoundException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {} + catch (IllegalArgumentException | IllegalAccessException e) {} //Gets Mars ID try { - if (Class.forName("micdoodle8.mods.galacticraft.planets.mars.ConfigManagerMars") != null && mMarsID == -99) { - mMarsID = ReflectionUtils.getField(Class.forName("micdoodle8.mods.galacticraft.planets.mars.ConfigManagerMars"), "dimensionIDMars").getInt(null); + if (ReflectionUtils.getClass("micdoodle8.mods.galacticraft.planets.mars.ConfigManagerMars") != null && mMarsID == -99) { + mMarsID = ReflectionUtils.getField(ReflectionUtils.getClass("micdoodle8.mods.galacticraft.planets.mars.ConfigManagerMars"), "dimensionIDMars").getInt(null); } } - catch (ClassNotFoundException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {} + catch (IllegalArgumentException | IllegalAccessException e) {} //Get Comets ID try { - if (Class.forName("micdoodle8.mods.galacticraft.planets.asteroids.ConfigManagerAsteroids") != null && mCometsID == -99) { - mCometsID = ReflectionUtils.getField(Class.forName("micdoodle8.mods.galacticraft.planets.asteroids.ConfigManagerAsteroids"), "dimensionIDAsteroids").getInt(null); + if (ReflectionUtils.getClass("micdoodle8.mods.galacticraft.planets.asteroids.ConfigManagerAsteroids") != null && mCometsID == -99) { + mCometsID = ReflectionUtils.getField(ReflectionUtils.getClass("micdoodle8.mods.galacticraft.planets.asteroids.ConfigManagerAsteroids"), "dimensionIDAsteroids").getInt(null); } } - catch (ClassNotFoundException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {} + catch (IllegalArgumentException | IllegalAccessException e) {} //Clear Cache Ores_Overworld.clear(); @@ -215,7 +215,7 @@ public class MiningUtils { try { aEndAsteroids = ReflectionUtils.getField(GT_Worldgen_GT_Ore_Layer.class, "mEndAsteroid").getBoolean(x); } - catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) { + catch (IllegalArgumentException | IllegalAccessException e) { aEndAsteroids = false; } diff --git a/src/Java/gtPlusPlus/core/util/minecraft/RecipeUtils.java b/src/Java/gtPlusPlus/core/util/minecraft/RecipeUtils.java index a5f5c778bf..774c8b6f13 100644 --- a/src/Java/gtPlusPlus/core/util/minecraft/RecipeUtils.java +++ b/src/Java/gtPlusPlus/core/util/minecraft/RecipeUtils.java @@ -5,15 +5,10 @@ import java.util.Iterator; import java.util.List; import cpw.mods.fml.common.registry.GameRegistry; - -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.item.crafting.CraftingManager; -import net.minecraft.item.crafting.IRecipe; - import gregtech.api.enums.Materials; import gregtech.api.util.GT_ModHandler; import gregtech.api.util.GT_Recipe; +import gregtech.api.util.GT_Recipe.GT_Recipe_Map; import gtPlusPlus.GTplusplus; import gtPlusPlus.api.interfaces.RunnableWithInfo; import gtPlusPlus.api.objects.Logger; @@ -22,9 +17,12 @@ import gtPlusPlus.api.objects.minecraft.ShapedRecipe; import gtPlusPlus.core.handler.COMPAT_HANDLER; import gtPlusPlus.core.handler.Recipes.LateRegistrationHandler; import gtPlusPlus.core.handler.Recipes.RegistrationHandler; -import gtPlusPlus.core.lib.CORE; import gtPlusPlus.core.recipe.common.CI; import gtPlusPlus.core.util.reflect.ReflectionUtils; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.CraftingManager; +import net.minecraft.item.crafting.IRecipe; import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.ShapedOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe; @@ -33,7 +31,8 @@ public class RecipeUtils { public static int mInvalidID = 1; public static boolean recipeBuilder(final Object slot_1, final Object slot_2, final Object slot_3, final Object slot_4, final Object slot_5, final Object slot_6, final Object slot_7, final Object slot_8, final Object slot_9, ItemStack resultItem){ - if (gtPlusPlus.GTplusplus.CURRENT_LOAD_PHASE != GTplusplus.INIT_PHASE.POST_INIT) { + //Old Debug Code, useful for finding recipes loading too early. + /*if (gtPlusPlus.GTplusplus.CURRENT_LOAD_PHASE != GTplusplus.INIT_PHASE.POST_INIT) { Logger.INFO(ReflectionUtils.getMethodName(1)); Logger.INFO(ReflectionUtils.getMethodName(2)); Logger.INFO(ReflectionUtils.getMethodName(3)); @@ -44,7 +43,7 @@ public static int mInvalidID = 1; Logger.INFO(ReflectionUtils.getMethodName(8)); Logger.INFO(ReflectionUtils.getMethodName(9)); System.exit(1); - } + }*/ if (resultItem == null){ Logger.RECIPE("[Fix] Found a recipe with an invalid output, yet had a valid inputs. Using Dummy output so recipe can be found.."); @@ -212,7 +211,7 @@ public static int mInvalidID = 1; public static boolean removeCraftingRecipe(Object x){ if (null == x){return false;} if (x instanceof String){ - final Item R = ItemUtils.getItem((String) x); + final Item R = ItemUtils.getItemFromFQRN((String) x); if (R != null){ x = R; } @@ -395,6 +394,7 @@ public static int mInvalidID = 1; else { Logger.RECIPE("[Fix] Output is Null for a recipe. Report to Alkalus."); output = ItemUtils.getItemStackOfAmountFromOreDict("sadibasdkjnad", 1); + RegistrationHandler.recipesFailed++; } } } @@ -574,5 +574,13 @@ public static int mInvalidID = 1; } + public static boolean removeGtRecipe(GT_Recipe aRecipeToRemove, GT_Recipe_Map aRecipeMap) { + if (aRecipeMap.mRecipeList.contains(aRecipeToRemove)) { + return aRecipeMap.mRecipeList.remove(aRecipeToRemove); + } + return false; + } + + } diff --git a/src/Java/gtPlusPlus/core/util/minecraft/gregtech/PollutionUtils.java b/src/Java/gtPlusPlus/core/util/minecraft/gregtech/PollutionUtils.java index dde785cee6..aeeb4ae5be 100644 --- a/src/Java/gtPlusPlus/core/util/minecraft/gregtech/PollutionUtils.java +++ b/src/Java/gtPlusPlus/core/util/minecraft/gregtech/PollutionUtils.java @@ -39,7 +39,7 @@ public class PollutionUtils { return mPollution.getBoolean(GT_Pollution); } } - } catch (SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) { + } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) { } return false; } @@ -53,16 +53,16 @@ public class PollutionUtils { if (mAddPollution != null) { mAddPollution.invoke(null, te, pollutionValue); } - Class<?> GT_Pollution = Class.forName("gregtech.common.GT_Pollution"); + Class<?> GT_Pollution = ReflectionUtils.getClass("gregtech.common.GT_Pollution"); if (GT_Pollution != null) { - Method addPollution = GT_Pollution.getMethod("addPollution", IGregTechTileEntity.class, int.class); + Method addPollution = ReflectionUtils.getMethod(GT_Pollution, "addPollution", IGregTechTileEntity.class, int.class); if (addPollution != null) { mAddPollution = addPollution; addPollution.invoke(null, te, pollutionValue); return true; } } - } catch (ClassNotFoundException | SecurityException | NoSuchMethodException | IllegalAccessException + } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } return false; @@ -92,16 +92,16 @@ public class PollutionUtils { mAddPollution2.invoke(null, aChunk, pollutionValue); return true; } - Class<?> GT_Pollution = Class.forName("gregtech.common.GT_Pollution"); + Class<?> GT_Pollution = ReflectionUtils.getClass("gregtech.common.GT_Pollution"); if (GT_Pollution != null) { - Method addPollution = GT_Pollution.getMethod("addPollution", Chunk.class, int.class); + Method addPollution = ReflectionUtils.getMethod(GT_Pollution, "addPollution", Chunk.class, int.class); if (addPollution != null) { mAddPollution2 = addPollution; mAddPollution2.invoke(null, aChunk, pollutionValue); return true; } } - } catch (ClassNotFoundException | SecurityException | NoSuchMethodException | IllegalAccessException + } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } return false; @@ -128,15 +128,15 @@ public class PollutionUtils { if (mGetPollution != null) { mGetPollution.invoke(null, te); } - Class<?> GT_Pollution = Class.forName("gregtech.common.GT_Pollution"); + Class<?> GT_Pollution = ReflectionUtils.getClass("gregtech.common.GT_Pollution"); if (GT_Pollution != null) { - Method addPollution = GT_Pollution.getMethod("getPollution", IGregTechTileEntity.class); + Method addPollution = ReflectionUtils.getMethod(GT_Pollution, "getPollution", IGregTechTileEntity.class); if (addPollution != null) { mGetPollution = addPollution; return (int) addPollution.invoke(null, te); } } - } catch (ClassNotFoundException | SecurityException | NoSuchMethodException | IllegalAccessException + } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } return 0; @@ -151,15 +151,15 @@ public class PollutionUtils { if (mGetPollution2 != null) { mGetPollution2.invoke(null, te); } - Class<?> GT_Pollution = Class.forName("gregtech.common.GT_Pollution"); + Class<?> GT_Pollution = ReflectionUtils.getClass("gregtech.common.GT_Pollution"); if (GT_Pollution != null) { - Method addPollution = GT_Pollution.getMethod("getPollution", Chunk.class); + Method addPollution = ReflectionUtils.getMethod(GT_Pollution, "getPollution", Chunk.class); if (addPollution != null) { mGetPollution2 = addPollution; return (int) addPollution.invoke(null, te); } } - } catch (ClassNotFoundException | SecurityException | NoSuchMethodException | IllegalAccessException + } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } return 0; diff --git a/src/Java/gtPlusPlus/core/util/reflect/ClientProxyFinder.java b/src/Java/gtPlusPlus/core/util/reflect/ClientProxyFinder.java deleted file mode 100644 index 99a9bf2fa9..0000000000 --- a/src/Java/gtPlusPlus/core/util/reflect/ClientProxyFinder.java +++ /dev/null @@ -1,32 +0,0 @@ -package gtPlusPlus.core.util.reflect; - -import java.lang.reflect.Field; - -import cpw.mods.fml.common.SidedProxy; - -public class ClientProxyFinder { - - public static Object getInstance(final Object modInstance) throws ReflectiveOperationException { - for(final Field field : modInstance.getClass().getDeclaredFields()) { - if(field.isAnnotationPresent(SidedProxy.class)) { - final SidedProxy sidedProxy = field.getAnnotation(SidedProxy.class); - final Object fieldValue = field.get(modInstance); - try { - final Class<?> clientSideClass = Class.forName(sidedProxy.clientSide()); - if(clientSideClass.isAssignableFrom(fieldValue.getClass())) { - final Object clientProxy = clientSideClass.cast(fieldValue); - //do what you want with client proxy instance - return clientProxy; - } - - } catch (final NoClassDefFoundError err) { - //its server side - return null; - } - break; - } - } - return null; - } - -}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/reflect/ProxyFinder.java b/src/Java/gtPlusPlus/core/util/reflect/ProxyFinder.java index 85599e4695..d22fafb37b 100644 --- a/src/Java/gtPlusPlus/core/util/reflect/ProxyFinder.java +++ b/src/Java/gtPlusPlus/core/util/reflect/ProxyFinder.java @@ -12,7 +12,7 @@ public class ProxyFinder { final SidedProxy sidedProxy = field.getAnnotation(SidedProxy.class); final Object fieldValue = field.get(modInstance); try { - final Class<?> serverSideClass = Class.forName(sidedProxy.serverSide()); + final Class<?> serverSideClass = ReflectionUtils.getClass(sidedProxy.serverSide()); if(serverSideClass.isAssignableFrom(fieldValue.getClass())) { final Object serverProxy = serverSideClass.cast(fieldValue); //do what you want with server proxy instance @@ -35,7 +35,7 @@ public class ProxyFinder { final SidedProxy sidedProxy = field.getAnnotation(SidedProxy.class); final Object fieldValue = field.get(modInstance); try { - final Class<?> clientSideClass = Class.forName(sidedProxy.clientSide()); + final Class<?> clientSideClass = ReflectionUtils.getClass(sidedProxy.clientSide()); if(clientSideClass.isAssignableFrom(fieldValue.getClass())) { final Object clientProxy = clientSideClass.cast(fieldValue); //do what you want with client proxy instance diff --git a/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java b/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java index dee9d76a4c..722a4f3ff7 100644 --- a/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java +++ b/src/Java/gtPlusPlus/core/util/reflect/ReflectionUtils.java @@ -1,110 +1,245 @@ package gtPlusPlus.core.util.reflect; import java.io.IOException; -import java.lang.reflect.*; -import java.net.URL; -import java.util.HashSet; -import java.util.Scanner; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.LinkedHashMap; +import java.util.Map; -import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.commons.lang3.ArrayUtils; import com.google.common.reflect.ClassPath; -import net.minecraft.client.Minecraft; - -import gregtech.GT_Mod; -import gtPlusPlus.GTplusplus; import gtPlusPlus.api.objects.Logger; -import gtPlusPlus.core.lib.CORE; +import gtPlusPlus.core.util.data.StringUtils; public class ReflectionUtils { - public static Field getField(final Class<?> clazz, final String fieldName) throws NoSuchFieldException { - try { - Field k = clazz.getDeclaredField(fieldName); - makeAccessible(k); - //Logger.REFLECTION("Got Field from Class. "+fieldName+" did exist within "+clazz.getCanonicalName()+"."); - return k; - } catch (final NoSuchFieldException e) { - final Class<?> superClass = clazz.getSuperclass(); - if (superClass == null) { - //Logger.REFLECTION("Failed to get Field from Class. "+fieldName+" does not existing within "+clazz.getCanonicalName()+"."); - throw e; - } - //Logger.REFLECTION("Failed to get Field from Class. "+fieldName+" does not existing within "+clazz.getCanonicalName()+". Trying super class."); - return getField(superClass, fieldName); + public static Map<String, Class> mCachedClasses = new LinkedHashMap<String, Class>(); + public static Map<String, CachedMethod> mCachedMethods = new LinkedHashMap<String, CachedMethod>(); + public static Map<String, CachedField> mCachedFields = new LinkedHashMap<String, CachedField>(); + + private static class CachedMethod { + + private final boolean STATIC; + private final Method METHOD; + + public CachedMethod(Method aMethod, boolean isStatic) { + METHOD = aMethod; + STATIC = isStatic; + } + + public Method get() { + return METHOD; } + + public boolean type() { + return STATIC; + } + } - public static void makeAccessible(final Field field) { - if (!Modifier.isPublic(field.getModifiers()) || - !Modifier.isPublic(field.getDeclaringClass().getModifiers())) - { - field.setAccessible(true); + private static class CachedField { + + private final boolean STATIC; + private final Field FIELD; + + public CachedField(Field aField, boolean isStatic) { + FIELD = aField; + STATIC = isStatic; } + + public Field get() { + return FIELD; + } + + public boolean type() { + return STATIC; + } + } - //Some Reflection utils - http://stackoverflow.com/questions/14374878/using-reflection-to-set-an-object-property - @SuppressWarnings("unchecked") - public static <V> V getField(final Object object, final String fieldName) { - Class<?> clazz = object.getClass(); - while (clazz != null) { - try { - final Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - return (V) field.get(object); - } catch (final NoSuchFieldException e) { - Logger.REFLECTION("getField("+object.toString()+", "+fieldName+") failed."); - clazz = clazz.getSuperclass(); - } catch (final Exception e) { - Logger.REFLECTION("getField("+object.toString()+", "+fieldName+") failed."); - throw new IllegalStateException(e); + private static boolean cacheClass(Class aClass) { + if (aClass == null) { + return false; + } + Class y = mCachedClasses.get(aClass.getCanonicalName()); + if (y == null) { + mCachedClasses.put(aClass.getCanonicalName(), aClass); + return true; + } + return false; + } + + private static boolean cacheMethod(Class aClass, Method aMethod) { + if (aMethod == null) { + return false; + } + boolean isStatic = Modifier.isStatic(aMethod.getModifiers()); + CachedMethod y = mCachedMethods.get(aClass.getName()+"."+aMethod.getName()+"."+ArrayUtils.toString(aMethod.getParameterTypes())); + if (y == null) { + mCachedMethods.put(aClass.getName()+"."+aMethod.getName()+"."+ArrayUtils.toString(aMethod.getParameterTypes()), new CachedMethod(aMethod, isStatic)); + return true; + } + return false; + } + + private static boolean cacheField(Class aClass, Field aField) { + if (aField == null) { + return false; + } + boolean isStatic = Modifier.isStatic(aField.getModifiers()); + CachedField y = mCachedFields.get(aClass.getName()+"."+aField.getName()); + if (y == null) { + mCachedFields.put(aClass.getName()+"."+aField.getName(), new CachedField(aField, isStatic)); + return true; + } + return false; + } + + + /** + * Returns a cached {@link Class} object. + * @param aClassCanonicalName - The canonical name of the underlying class. + * @return - Valid, {@link Class} object, or {@link null}. + */ + public static Class getClass(String aClassCanonicalName) { + if (aClassCanonicalName == null || aClassCanonicalName.length() <= 0) { + return null; + } + Class y = mCachedClasses.get(aClassCanonicalName); + if (y == null) { + y = getClass_Internal(aClassCanonicalName); + if (y != null) { + Logger.REFLECTION("Caching Class: "+aClassCanonicalName); + cacheClass(y); } } - return null; + return y; } - public static boolean setField(final Object object, final String fieldName, final Object fieldValue) { - Class<?> clazz = object.getClass(); - while (clazz != null) { + + + /** + * Returns a cached {@link Method} object. Wraps {@link #getMethod(Class, String, Class...)}. + * @param aObject - Object containing the Method. + * @param aMethodName - Method's name in {@link String} form. + * @param aTypes - Class Array of Types for {@link Method}'s constructor. + * @return - Valid, non-final, {@link Method} object, or {@link null}. + */ + public static Method getMethod(Object aObject, String aMethodName, Class[] aTypes) { + return getMethod(aObject.getClass(), aMethodName, aTypes); + } + + + /** + * Returns a cached {@link Method} object. + * @param aClass - Class containing the Method. + * @param aMethodName - Method's name in {@link String} form. + * @param aTypes - Varags Class Types for {@link Method}'s constructor. + * @return - Valid, non-final, {@link Method} object, or {@link null}. + */ + public static Method getMethod(Class aClass, String aMethodName, Class... aTypes) { + if (aClass == null || aMethodName == null || aMethodName.length() <= 0) { + return null; + } + String aMethodKey = ArrayUtils.toString(aTypes); + //Logger.REFLECTION("Looking up method in cache: "+(aClass.getName()+"."+aMethodName + "." + aMethodKey)); + CachedMethod y = mCachedMethods.get(aClass.getName()+"."+aMethodName + "." + aMethodKey); + if (y == null) { + Method u = getMethod_Internal(aClass, aMethodName, aTypes); + if (u != null) { + Logger.REFLECTION("Caching Method: "+aMethodName + "." + aMethodKey); + cacheMethod(aClass, u); + return u; + } else { + return null; + } + } else { + return y.get(); + } + } + + + + /** + * Returns a cached {@link Field} object. + * @param aClass - Class containing the Method. + * @param aFieldName - Field name in {@link String} form. + * @return - Valid, non-final, {@link Field} object, or {@link null}. + */ + public static Field getField(final Class aClass, final String aFieldName) { + if (aClass == null || aFieldName == null || aFieldName.length() <= 0) { + return null; + } + CachedField y = mCachedFields.get(aClass.getName()+"."+aFieldName); + if (y == null) { + Field u; try { - final Field field = getField(clazz, fieldName); - if (field != null) { - setValue(object, field, fieldValue); - return true; + u = getField_Internal(aClass, aFieldName); + if (u != null) { + Logger.REFLECTION("Caching Field '"+aFieldName+"' from "+aClass.getName()); + cacheField(aClass, u); + return u; } - } catch (final NoSuchFieldException e) { - Logger.REFLECTION("setField("+object.toString()+", "+fieldName+") failed."); - clazz = clazz.getSuperclass(); - } catch (final Exception e) { - Logger.REFLECTION("setField("+object.toString()+", "+fieldName+") failed."); - throw new IllegalStateException(e); + } catch (NoSuchFieldException e) { } + return null; + + } else { + return y.get(); } - return false; } - - public static boolean becauseIWorkHard(){ - /* TODO: fix this stuff \u002a\u002f\u0066\u0069\u006e\u0061\u006c\u0020\u0048\u0061\u0073\u0068\u0053\u0065\u0074\u003c\u0053\u0074\u0072\u0069\u006e\u0067\u003e\u0020\u0078\u0043\u0061\u0070\u0065\u004c\u0069\u0073\u0074\u0020\u003d\u0020\u006e\u0065\u0077\u0020\u0048\u0061\u0073\u0068\u0053\u0065\u0074\u003c\u003e\u0028\u0029\u003b\u000a\u0009\u0009\u004f\u0062\u006a\u0065\u0063\u0074\u0020\u0070\u0072\u006f\u0078\u0079\u0043\u006c\u0069\u0065\u006e\u0074\u0047\u0054\u003b\u0009\u000a\u0009\u0009\u0074\u0072\u0079\u0020\u007b\u000a\u0009\u0009\u0009\u0070\u0072\u006f\u0078\u0079\u0043\u006c\u0069\u0065\u006e\u0074\u0047\u0054\u0020\u003d\u0020\u0043\u006c\u0069\u0065\u006e\u0074\u0050\u0072\u006f\u0078\u0079\u0046\u0069\u006e\u0064\u0065\u0072\u002e\u0067\u0065\u0074\u0049\u006e\u0073\u0074\u0061\u006e\u0063\u0065\u0028\u0047\u0054\u005f\u004d\u006f\u0064\u002e\u0069\u006e\u0073\u0074\u0061\u006e\u0063\u0065\u0029\u003b\u0009\u0009\u0009\u0009\u000a\u0009\u0009\u007d\u0020\u0063\u0061\u0074\u0063\u0068\u0020\u0028\u0066\u0069\u006e\u0061\u006c\u0020\u0052\u0065\u0066\u006c\u0065\u0063\u0074\u0069\u0076\u0065\u004f\u0070\u0065\u0072\u0061\u0074\u0069\u006f\u006e\u0045\u0078\u0063\u0065\u0070\u0074\u0069\u006f\u006e\u0020\u0065\u0031\u0029\u0020\u007b\u000a\u0009\u0009\u0009\u0070\u0072\u006f\u0078\u0079\u0043\u006c\u0069\u0065\u006e\u0074\u0047\u0054\u0020\u003d\u0020\u006e\u0075\u006c\u006c\u003b\u000a\u0009\u0009\u0009Logger\u002eINFO\u0028\u0022\u0046\u0061\u0069\u006c\u0065\u0064\u0020\u006f\u0062\u0074\u0061\u0069\u006e\u0065\u0064\u0020\u0069\u006e\u0073\u0074\u0061\u006e\u0063\u0065\u0020\u006f\u0066\u0020\u0061\u0020\u0063\u006c\u0069\u0065\u006e\u0074\u0020\u0070\u0072\u006f\u0078\u0079\u002e\u0022\u0029\u003b\u000a\u0009\u0009\u0009\u0072\u0065\u0074\u0075\u0072\u006e\u0020\u0066\u0061\u006c\u0073\u0065\u003b\u000a\u0009\u0009\u007d\u000a\u0009\u0009\u0074\u0072\u0079\u0020\u007b\u000a\u0009\u0009\u0009\u0066\u0069\u006e\u0061\u006c\u0020\u0053\u0063\u0061\u006e\u006e\u0065\u0072\u0020\u0074\u0053\u0063\u0061\u006e\u006e\u0065\u0072\u0020\u003d\u0020\u006e\u0065\u0077\u0020\u0053\u0063\u0061\u006e\u006e\u0065\u0072\u0028\u006e\u0065\u0077\u0020\u0055\u0052\u004c\u0028\u0022\u0068\u0074\u0074\u0070\u003a\u002f\u002f\u0067\u0072\u0065\u0067\u0074\u0065\u0063\u0068\u002e\u006f\u0076\u0065\u0072\u006d\u0069\u006e\u0064\u0064\u006c\u0031\u002e\u0063\u006f\u006d\u002f\u0063\u006f\u006d\u002f\u0067\u0072\u0065\u0067\u006f\u0072\u0069\u0075\u0073\u0074\u002f\u0067\u0072\u0065\u0067\u0074\u0065\u0063\u0068\u002f\u0073\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0072\u006c\u0069\u0073\u0074\u002e\u0074\u0078\u0074\u0022\u0029\u002e\u006f\u0070\u0065\u006e\u0053\u0074\u0072\u0065\u0061\u006d\u0028\u0029\u0029\u003b\u000a\u0009\u0009\u0009Logger\u002eWARNING\u0028\u0022\u0054\u0072\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0062\u0075\u0069\u006c\u0064\u0020\u0061\u0020\u0048\u0061\u0073\u0068\u0053\u0065\u0074\u002e\u0022\u0029\u003b\u000a\u0009\u0009\u0009\u0077\u0068\u0069\u006c\u0065\u0020\u0028\u0074\u0053\u0063\u0061\u006e\u006e\u0065\u0072\u002e\u0068\u0061\u0073\u004e\u0065\u0078\u0074\u004c\u0069\u006e\u0065\u0028\u0029\u0029\u0020\u007b\u000a\u0009\u0009\u0009\u0009\u0066\u0069\u006e\u0061\u006c\u0020\u0053\u0074\u0072\u0069\u006e\u0067\u0020\u0074\u004e\u0061\u006d\u0065\u0020\u003d\u0020\u0074\u0053\u0063\u0061\u006e\u006e\u0065\u0072\u002e\u006e\u0065\u0078\u0074\u004c\u0069\u006e\u0065\u0028\u0029\u003b\u000a\u000a\u0009\u0009\u0009\u0009\u0069\u0066\u0020\u0028\u0021\u0078\u0043\u0061\u0070\u0065\u004c\u0069\u0073\u0074\u002e\u0063\u006f\u006e\u0074\u0061\u0069\u006e\u0073\u0028\u0074\u004e\u0061\u006d\u0065\u002e\u0074\u006f\u004c\u006f\u0077\u0065\u0072\u0043\u0061\u0073\u0065\u0028\u0029\u0029\u0029\u0020\u007b\u000a\u0009\u0009\u0009\u0009\u0009\u0078\u0043\u0061\u0070\u0065\u004c\u0069\u0073\u0074\u002e\u0061\u0064\u0064\u0028\u0074\u004e\u0061\u006d\u0065\u002e\u0074\u006f\u004c\u006f\u0077\u0065\u0072\u0043\u0061\u0073\u0065\u0028\u0029\u0029\u003b\u000a\u0009\u0009\u0009\u0009\u007d\u000a\u0009\u0009\u0009\u0009\u000a\u0009\u0009\u0009\u0009\u002f\u002f\u0041\u0064\u0064\u0020\u004d\u0079\u0073\u0065\u006c\u0066\u000a\u0009\u0009\u0009\u0009\u0069\u0066\u0020\u0028\u0021\u0078\u0043\u0061\u0070\u0065\u004c\u0069\u0073\u0074\u002e\u0063\u006f\u006e\u0074\u0061\u0069\u006e\u0073\u0028\u0022\u0064\u0072\u0061\u006b\u006e\u0079\u0074\u0065\u0031\u0022\u0029\u0029\u0020\u007b\u000a\u0009\u0009\u0009\u0009\u0009Logger\u002eWARNING\u0028\u0022\u0041\u0064\u0064\u0065\u0064\u0020\u006d\u0069\u0073\u0073\u0069\u006e\u0067\u0020\u0076\u0061\u006c\u0075\u0065\u002e\u0022\u0029\u003b\u000a\u0009\u0009\u0009\u0009\u0009\u0078\u0043\u0061\u0070\u0065\u004c\u0069\u0073\u0074\u002e\u0061\u0064\u0064\u0028\u0022\u0064\u0072\u0061\u006b\u006e\u0079\u0074\u0065\u0031\u0022\u0029\u003b\u000a\u0009\u0009\u0009\u0009\u007d\u000a\u0009\u0009\u0009\u0009\u000a\u0009\u0009\u0009\u0009\u002f\u002f\u0041\u0064\u0064\u0020\u0074\u0068\u0065\u0020\u0063\u0061\u0070\u0065\u0064\u0020\u0074\u0065\u0073\u0074\u0020\u0068\u0065\u0072\u006f\u000a\u0009\u0009\u0009\u0009\u0069\u0066\u0020\u0028\u0043\u004f\u0052\u0045\u002e\u0044\u0045\u0056\u0045\u004e\u0056\u0029\u007b\u000a\u0009\u0009\u0009\u0009\u0009\u0074\u0072\u0079\u0020\u007b\u000a\u0009\u0009\u0009\u0009\u0009\u0009\u0053\u0074\u0072\u0069\u006e\u0067\u0020\u0064\u0065\u0076\u0050\u006c\u0061\u0079\u0065\u0072\u0020\u003d\u0020\u004d\u0069\u006e\u0065\u0063\u0072\u0061\u0066\u0074\u002e\u0067\u0065\u0074\u004d\u0069\u006e\u0065\u0063\u0072\u0061\u0066\u0074\u0028\u0029\u002e\u0067\u0065\u0074\u0053\u0065\u0073\u0073\u0069\u006f\u006e\u0028\u0029\u002e\u0067\u0065\u0074\u0055\u0073\u0065\u0072\u006e\u0061\u006d\u0065\u0028\u0029\u002e\u0074\u006f\u004c\u006f\u0077\u0065\u0072\u0043\u0061\u0073\u0065\u0028\u0029\u003b\u000a\u0009\u0009\u0009\u0009\u0009\u0009Logger\u002eINFO\u0028\u0022\u0046\u006f\u0075\u006e\u0064\u0020\u0022\u002b\u0064\u0065\u0076\u0050\u006c\u0061\u0079\u0065\u0072\u0029\u003b\u000a\u0009\u0009\u0009\u0009\u0009\u0009\u0069\u0066\u0020\u0028\u0021\u0078\u0043\u0061\u0070\u0065\u004c\u0069\u0073\u0074\u002e\u0063\u006f\u006e\u0074\u0061\u0069\u006e\u0073\u0028\u0064\u0065\u0076\u0050\u006c\u0061\u0079\u0065\u0072\u0029\u0029\u0020\u007b\u000a\u0009\u0009\u0009\u0009\u0009\u0009\u0009Logger\u002eWARNING\u0028\u0022\u0041\u0064\u0064\u0065\u0064\u0020\u006d\u0069\u0073\u0073\u0069\u006e\u0067\u0020\u0076\u0061\u006c\u0075\u0065\u002e\u0022\u0029\u003b\u000a\u0009\u0009\u0009\u0009\u0009\u0009\u0009\u0078\u0043\u0061\u0070\u0065\u004c\u0069\u0073\u0074\u002e\u0061\u0064\u0064\u0028\u0064\u0065\u0076\u0050\u006c\u0061\u0079\u0065\u0072\u0029\u003b\u000a\u0009\u0009\u0009\u0009\u0009\u0009\u007d\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0009\u000a\u0009\u0009\u0009\u0009\u0009\u007d\u0020\u0063\u0061\u0074\u0063\u0068\u0020\u0028\u0054\u0068\u0072\u006f\u0077\u0061\u0062\u006c\u0065\u0020\u0074\u0029\u007b\u000a\u0009\u0009\u0009\u0009\u0009\u0009Logger\u002eINFO\u0028\u0022\u0046\u0061\u0069\u006c\u0065\u0064\u0020\u0061\u0064\u0064\u0069\u006e\u0067\u0020\u006d\u0069\u0073\u0073\u0069\u006e\u0067\u0020\u0076\u0061\u006c\u0075\u0065\u0020\u0069\u006e\u0020\u0063\u0075\u0072\u0072\u0065\u006e\u0074\u0020\u0065\u006e\u0076\u0069\u0072\u006f\u006e\u006d\u0065\u006e\u0074\u002e\u0022\u0029\u003b\u000a\u0009\u0009\u0009\u0009\u0009\u007d\u000a\u0009\u0009\u0009\u0009\u007d\u000a\u000a\u0009\u0009\u0009\u007d\u000a\u0009\u0009\u0009\u0074\u0053\u0063\u0061\u006e\u006e\u0065\u0072\u002e\u0063\u006c\u006f\u0073\u0065\u0028\u0029\u003b\u000a\u0009\u0009\u007d\u0020\u0063\u0061\u0074\u0063\u0068\u0020\u0028\u0066\u0069\u006e\u0061\u006c\u0020\u0054\u0068\u0072\u006f\u0077\u0061\u0062\u006c\u0065\u0020\u0065\u0029\u0020\u007b\u000a\u0009\u0009\u0009Logger\u002eWARNING\u0028\u0022\u0046\u0061\u0069\u006c\u0065\u0064\u0020\u0067\u0065\u0074\u0074\u0069\u006e\u0067\u0020\u0074\u0068\u0065\u0020\u0077\u0065\u0062\u0020\u006c\u0069\u0073\u0074\u002e\u0022\u0029\u003b\u000a\u0009\u0009\u0009\u0072\u0065\u0074\u0075\u0072\u006e\u0020\u0066\u0061\u006c\u0073\u0065\u003b\u0020\u0020\u0020\u0020\u0020\u0009\u000a\u0009\u0009\u007d\u0009\u0009\u000a\u000a\u0009\u0009\u0074\u0072\u0079\u0020\u007b\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0009\u000a\u0009\u0009\u0009\u0046\u0069\u0065\u006c\u0064\u0055\u0074\u0069\u006c\u0073\u002e\u0077\u0072\u0069\u0074\u0065\u0046\u0069\u0065\u006c\u0064\u0028\u0070\u0072\u006f\u0078\u0079\u0043\u006c\u0069\u0065\u006e\u0074\u0047\u0054\u002c\u0020\u0022\u006d\u0043\u0061\u0070\u0065\u004c\u0069\u0073\u0074\u0022\u002c\u0020\u0078\u0043\u0061\u0070\u0065\u004c\u0069\u0073\u0074\u002c\u0020\u0074\u0072\u0075\u0065\u0029\u003b\u000a\u0009\u0009\u0009Logger\u002eWARNING\u0028\u0022\u0041\u0064\u0064\u0065\u0064\u0020\u006d\u006f\u0064\u0069\u0066\u0069\u0065\u0064\u0020\u0068\u0061\u0073\u0068\u0073\u0065\u0074\u0020\u0062\u0061\u0063\u006b\u0020\u0069\u006e\u0074\u006f\u0020\u0074\u0068\u0065\u0020\u0069\u006e\u0073\u0074\u0061\u006e\u0063\u0065\u002e\u0022\u0029\u003b\u0020\u0020\u0009\u0009\u0009\u000a\u0009\u0009\u0009\u0072\u0065\u0074\u0075\u0072\u006e\u0020\u0074\u0072\u0075\u0065\u003b\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u0020\u000a\u0009\u0009\u007d\u0020\u0063\u0061\u0074\u0063\u0068\u0020\u0028\u0066\u0069\u006e\u0061\u006c\u0020\u0054\u0068\u0072\u006f\u0077\u0061\u0062\u006c\u0065\u0020\u0065\u0029\u0020\u007b\u000a\u0009\u0009\u0009Logger\u002eINFO\u0028\u0022\u0052\u0065\u0066\u006c\u0065\u0063\u0074\u0069\u006f\u006e\u0020\u0069\u006e\u0074\u006f\u0020\u0061\u0063\u0074\u0069\u0076\u0065\u0020\u0063\u006c\u0069\u0065\u006e\u0074\u0020\u0070\u0072\u006f\u0078\u0079\u0020\u0069\u006e\u0073\u0074\u0061\u006e\u0063\u0065\u0020\u0066\u0061\u0069\u006c\u0065\u0064\u002e\u0022\u0029\u003b\u000a\u0009\u0009\u0009\u0065\u002e\u0070\u0072\u0069\u006e\u0074\u0053\u0074\u0061\u0063\u006b\u0054\u0072\u0061\u0063\u0065\u0028\u0029\u003b\u0020\u0020\u0020\u0020\u0020\u0020\u000a\u0009\u0009\u0009\u0072\u0065\u0074\u0075\u0072\u006e\u0020\u0066\u0061\u006c\u0073\u0065\u003b\u0020\u0020\u0009\u000a\u0009\u0009\u007d\u002f\u002a */ + + /** + * Returns a cached {@link Field} object. + * @param aInstance - {@link Object} to get the field instance from. + * @param aFieldName - Field name in {@link String} form. + * @return - Valid, non-final, {@link Field} object, or {@link null}. + */ + public static <T> T getField(final Object aInstance, final String aFieldName) { + try { + return (T) getField(aInstance.getClass(), aFieldName).get(aInstance); + } catch (IllegalArgumentException | IllegalAccessException e) { + return null; + } } + + + + /* + * Utility Functions + */ + public static boolean doesClassExist(final String classname) { - boolean exists = true; - try { - // Load any class that should be present if driver's available - Class.forName(classname); - } catch (final ClassNotFoundException e) { - // Driver is not available - exists = false; + return isClassPresent(classname); + } + + public static void makeFieldAccessible(final Field field) { + if (!Modifier.isPublic(field.getModifiers()) || + !Modifier.isPublic(field.getDeclaringClass().getModifiers())) + { + field.setAccessible(true); + } + } + + public static void makeMethodAccessible(final Method field) { + if (!Modifier.isPublic(field.getModifiers()) || + !Modifier.isPublic(field.getDeclaringClass().getModifiers())) + { + field.setAccessible(true); } - return exists; } /** * Get the method name for a depth in call stack. <br /> * Utility function * @param depth depth in the call stack (0 means current method, 1 means call method, ...) - * @return method name + * @return Method name */ public static String getMethodName(final int depth) { final StackTraceElement[] ste = new Throwable().getStackTrace(); @@ -114,42 +249,60 @@ public class ReflectionUtils { /** - * Allows to change the state of an immutable instance. Huh?!? + * + * @param aPackageName - The full {@link Package} name in {@link String} form. + * @return - {@link Boolean} object. True if loaded > 0 classes. */ - public static void setFieldValue(Class<?> clazz, String fieldName, Object newValue) throws Exception { - Field nameField = getField(clazz, fieldName); - setValue(clazz, nameField, newValue); - } + public static boolean dynamicallyLoadClassesInPackage(String aPackageName) { + ClassLoader classLoader = ReflectionUtils.class.getClassLoader(); + int loaded = 0; + try { + ClassPath path = ClassPath.from(classLoader); + for (ClassPath.ClassInfo info : path.getTopLevelClassesRecursive(aPackageName)) { + Class<?> clazz = Class.forName(info.getName(), true, classLoader); + if (clazz != null) { + loaded++; + Logger.INFO("Found "+clazz.getCanonicalName()+". ["+loaded+"]"); + } + } + } catch (ClassNotFoundException | IOException e) { - /** - * Allows to change the state of final statics. Huh?!? - */ - public static void setDefault(Class<?> clazz, String fieldName, Object newValue) throws Exception { - Field staticField = clazz.getDeclaredField(fieldName); - setValue(null, staticField, newValue); + } + + return loaded > 0; } + - /** - * - * Set the value of a field reflectively. - */ - protected static void setValue(Object owner, Field field, Object value) throws Exception { - makeModifiable(field); - field.set(owner, value); + public static boolean setField(final Object object, final String fieldName, final Object fieldValue) { + Class<?> clazz = object.getClass(); + while (clazz != null) { + try { + final Field field = getField(clazz, fieldName); + if (field != null) { + setFieldValue_Internal(object, field, fieldValue); + return true; + } + } catch (final NoSuchFieldException e) { + Logger.REFLECTION("setField("+object.toString()+", "+fieldName+") failed."); + clazz = clazz.getSuperclass(); + } catch (final Exception e) { + Logger.REFLECTION("setField("+object.toString()+", "+fieldName+") failed."); + throw new IllegalStateException(e); + } + } + return false; } + /** - * Force the field to be modifiable and accessible. + * Allows to change the state of an immutable instance. Huh?!? */ - protected 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); + public static void setFinalFieldValue(Class<?> clazz, String fieldName, Object newValue) throws Exception { + Field nameField = getField(clazz, fieldName); + setFieldValue_Internal(clazz, nameField, newValue); } + @Deprecated public static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); @@ -258,13 +411,58 @@ public class ReflectionUtils { return null; } + + + + + + + + + + + + + + + + + + + + /* - * @ if (isPresent("com.optionaldependency.DependencyClass")) { // This - * block will never execute when the dependency is not present // There is + * Internal Magic that probably should not get exposed. + */ + + + private static Field getField_Internal(final Class<?> clazz, final String fieldName) throws NoSuchFieldException { + try { + Logger.REFLECTION("Field: Internal Lookup: "+fieldName); + Field k = clazz.getDeclaredField(fieldName); + makeFieldAccessible(k); + //Logger.REFLECTION("Got Field from Class. "+fieldName+" did exist within "+clazz.getCanonicalName()+"."); + return k; + } catch (final NoSuchFieldException e) { + Logger.REFLECTION("Field: Internal Lookup Failed: "+fieldName); + final Class<?> superClass = clazz.getSuperclass(); + if (superClass == null) { + Logger.REFLECTION("Unable to find field '"+fieldName+"'"); + //Logger.REFLECTION("Failed to get Field from Class. "+fieldName+" does not existing within "+clazz.getCanonicalName()+"."); + throw e; + } + Logger.REFLECTION("Method: Recursion Lookup: "+fieldName+" - Checking in "+superClass.getName()); + //Logger.REFLECTION("Failed to get Field from Class. "+fieldName+" does not existing within "+clazz.getCanonicalName()+". Trying super class."); + return getField_Internal(superClass, fieldName); + } + } + + /** + * 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) { + private static boolean isClassPresent(final String className) { try { Class.forName(className); return true; @@ -274,7 +472,6 @@ public class ReflectionUtils { } } - @SuppressWarnings("rawtypes") @Deprecated public static Method getMethodViaReflection(final Class<?> lookupClass, final String methodName, final boolean invoke) throws Exception { @@ -287,16 +484,10 @@ public class ReflectionUtils { return m; } - /** - * Removes final modifier & returns a {@link Method} object. - * @param aClass - Class containing the Method. - * @param aMethodName - Method's name in {@link String} form. - * @param aTypes - Varags Class Types for {@link Method}'s constructor. - * @return - Valid, non-final, {@link Method} object. - */ - public static Method getMethod(Class aClass, String aMethodName, Class... aTypes) { + private static Method getMethod_Internal(Class aClass, String aMethodName, Class... aTypes) { Method m = null; try { + Logger.REFLECTION("Method: Internal Lookup: "+aMethodName); m = aClass.getDeclaredMethod(aMethodName, aTypes); if (m != null) { m.setAccessible(true); @@ -308,35 +499,55 @@ public class ReflectionUtils { } } catch (Throwable t) { + Logger.REFLECTION("Method: Internal Lookup Failed: "+aMethodName); + try { + m = getMethodRecursively(aClass, aMethodName); + } catch (NoSuchMethodException e) { + Logger.REFLECTION("Unable to find method '"+aMethodName+"'"); + e.printStackTrace(); + dumpClassInfo(aClass); + } } return m; } - - public static Method getMethodRecursively(final Class<?> clazz, final String fieldName) throws NoSuchMethodException { + + private static Method getMethodRecursively(final Class<?> clazz, final String aMethodName) throws NoSuchMethodException { try { - Method k = clazz.getDeclaredMethod(fieldName); + Logger.REFLECTION("Method: Recursion Lookup: "+aMethodName); + Method k = clazz.getDeclaredMethod(aMethodName); makeMethodAccessible(k); return k; } catch (final NoSuchMethodException e) { final Class<?> superClass = clazz.getSuperclass(); - if (superClass == null) { + if (superClass == null || superClass == Object.class) { throw e; } - return getMethod(superClass, fieldName); - } - } - - public static void makeMethodAccessible(final Method field) { - if (!Modifier.isPublic(field.getModifiers()) || - !Modifier.isPublic(field.getDeclaringClass().getModifiers())) - { - field.setAccessible(true); + return getMethod_Internal(superClass, aMethodName); } - } - + } + private static void dumpClassInfo(Class aClass) { + Logger.INFO("We ran into an error processing reflection in "+aClass.getName()+", dumping all data for debugging."); + // Get the methods + Method[] methods = aClass.getDeclaredMethods(); + Field[] fields = aClass.getDeclaredFields(); + Constructor[] consts = aClass.getDeclaredConstructors(); + + Logger.INFO("Dumping all Methods."); + for (Method method : methods) { + System.out.println(method.getName()+" | "+StringUtils.getDataStringFromArray(method.getParameterTypes())); + } + Logger.INFO("Dumping all Fields."); + for (Field f : fields) { + System.out.println(f.getName()); + } + Logger.INFO("Dumping all Constructors."); + for (Constructor c : consts) { + System.out.println(c.getName()+" | "+c.getParameterCount()+" | "+StringUtils.getDataStringFromArray(c.getParameterTypes())); + } + } - public static Class<?> getNonPublicClass(final String className) { + private static Class<?> getNonPublicClass(final String className) { Class<?> c = null; try { c = Class.forName(className); @@ -377,7 +588,7 @@ public class ReflectionUtils { return null; } - public static Class<?> getClassByName(String string) { + private static Class<?> getClass_Internal(String string) { if (ReflectionUtils.doesClassExist(string)) { try { return Class.forName(string); @@ -389,23 +600,25 @@ public class ReflectionUtils { return null; } - public static boolean dynamicallyLoadClassesInPackage(String aPackageName) { - ClassLoader classLoader = GTplusplus.class.getClassLoader(); - int loaded = 0; - try { - ClassPath path = ClassPath.from(classLoader); - for (ClassPath.ClassInfo info : path.getTopLevelClassesRecursive(aPackageName)) { - Class<?> clazz = Class.forName(info.getName(), true, classLoader); - if (clazz != null) { - loaded++; - Logger.INFO("Found "+clazz.getCanonicalName()+". ["+loaded+"]"); - } - } - } catch (ClassNotFoundException | IOException e) { - - } + /** + * + * Set the value of a field reflectively. + */ + private static void setFieldValue_Internal(Object owner, Field field, Object value) throws Exception { + makeModifiable(field); + field.set(owner, value); + } - return loaded > 0; + /** + * Force the field to be modifiable and accessible. + */ + 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); } |