PlatformMethods.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * This file is part of architectury.
  3. * Copyright (C) 2020, 2021 architectury
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.architectury;
  20. import dev.architectury.injectables.targets.ArchitecturyTarget;
  21. import me.shedaniel.architectury.utils.PlatformExpectedError;
  22. import org.jetbrains.annotations.ApiStatus;
  23. import java.lang.invoke.*;
  24. @Deprecated
  25. @ApiStatus.ScheduledForRemoval(inVersion = "2.0")
  26. @ApiStatus.Internal
  27. public class PlatformMethods {
  28. public static CallSite platform(MethodHandles.Lookup lookup, String name, MethodType type) {
  29. Class<?> lookupClass = lookup.lookupClass();
  30. String lookupType = lookupClass.getName().replace("$", "") + "Impl";
  31. String platformExpectedClass = lookupType.substring(0, lookupType.lastIndexOf('.')) + "." + ArchitecturyTarget.getCurrentTarget() + "." +
  32. lookupType.substring(lookupType.lastIndexOf('.') + 1);
  33. Class<?> newClass;
  34. try {
  35. newClass = Class.forName(platformExpectedClass, false, lookupClass.getClassLoader());
  36. } catch (ClassNotFoundException exception) {
  37. throw new PlatformExpectedError(lookupClass.getName() + "#" + name + " expected platform implementation in " + platformExpectedClass +
  38. "#" + name + ", but the class doesn't exist!", exception);
  39. }
  40. MethodHandle platformMethod;
  41. try {
  42. platformMethod = lookup.findStatic(newClass, name, type);
  43. } catch (NoSuchMethodException exception) {
  44. throw new PlatformExpectedError(lookupClass.getName() + "#" + name + " expected platform implementation in " + platformExpectedClass +
  45. "#" + name + ", but the method doesn't exist!", exception);
  46. } catch (IllegalAccessException exception) {
  47. throw new PlatformExpectedError(lookupClass.getName() + "#" + name + " expected platform implementation in " + platformExpectedClass +
  48. "#" + name + ", but the method's modifier doesn't match the access requirements!", exception);
  49. }
  50. return new ConstantCallSite(platformMethod);
  51. }
  52. }