@@ -74,15 +74,16 @@ public class Reflection {
7474 private static final Map <Class <?>, Map <String , Field >> loadedDeclaredFields = new HashMap <>();
7575 private static final Map <Class <?>, Map <Class <?>, Field >> foundFields = new HashMap <>();
7676
77- private Reflection () {}
77+ private Reflection () {
78+ }
7879
7980 /**
8081 * Gets the version from the version dependent CraftBukkit package. e.g. v1_17_R1
8182 *
8283 * @return The CraftBukkit version; empty when mojang mappings are used
8384 */
8485 public static Optional <String > getVersion () {
85- if (VERSION != null ){
86+ if (VERSION != null ) {
8687 return Optional .of (VERSION );
8788 }
8889 String [] packages = Bukkit .getServer ().getClass ().getPackage ().getName ().split ("\\ ." );
@@ -117,6 +118,27 @@ public static synchronized Class<?> getOBC(String obcClassName) {
117118 return clazz ;
118119 }
119120
121+ public static Class <?> getNMSUnsafe (String nmsClassName ) throws ClassNotFoundException {
122+ if (loadedNMSClasses .containsKey (nmsClassName )) {
123+ return loadedNMSClasses .get (nmsClassName );
124+ }
125+ Class <?> clazz = Class .forName (NMS_PKG + nmsClassName );
126+ loadedNMSClasses .put (nmsClassName , clazz );
127+ return clazz ;
128+ }
129+
130+ public static Class <?> getNMSUnsafe (NMSMapping mapping ) throws ClassNotFoundException {
131+ return getNMSUnsafe (mapping .get ());
132+ }
133+
134+ public static Class <?> getNMSUnsafe (String mojangPkg , String className ) throws ClassNotFoundException {
135+ return getNMSUnsafe (mojangPkg + "." + className );
136+ }
137+
138+ public static Class <?> getNMSUnsafe (String mojangPkg , NMSMapping mapping ) throws ClassNotFoundException {
139+ return getNMSUnsafe (mojangPkg , mapping .get ());
140+ }
141+
120142 /**
121143 * Get an NMS Class
122144 *
@@ -127,16 +149,12 @@ public static Class<?> getNMS(String nmsClassName) {
127149 if (loadedNMSClasses .containsKey (nmsClassName )) {
128150 return loadedNMSClasses .get (nmsClassName );
129151 }
130- String clazzName = NMS_PKG + nmsClassName ;
131- Class <?> clazz ;
132152 try {
133- clazz = Class . forName ( clazzName );
153+ return getNMSUnsafe ( nmsClassName );
134154 } catch (ClassNotFoundException e ) {
135155 WolfyUtilCore .getInstance ().getLogger ().log (Level .SEVERE , "Could not get NMS Class!" , e );
136156 return loadedNMSClasses .put (nmsClassName , null );
137157 }
138- loadedNMSClasses .put (nmsClassName , clazz );
139- return clazz ;
140158 }
141159
142160 public static Class <?> getNMS (NMSMapping mapping ) {
@@ -202,6 +220,25 @@ public static Constructor<?> getConstructor(@NotNull Class<?> clazz, Class<?>...
202220 }
203221 }
204222
223+ /**
224+ * Get a method from a class that has the specific parameters
225+ *
226+ * @param clazz The class we are searching
227+ * @param methodName The name of the method
228+ * @param params Any parameters that the method has
229+ * @return The method with appropriate parameters
230+ */
231+ public static Method getMethodUnsafe (@ NotNull Class <?> clazz , String methodName , Class <?>... params ) throws NoSuchMethodException {
232+ Map <String , Method > methods = loadedMethods .computeIfAbsent (clazz , aClass -> new HashMap <>());
233+ if (methods .containsKey (methodName )) {
234+ return methods .get (methodName );
235+ }
236+ Method method = clazz .getMethod (methodName , params );
237+ methods .put (methodName , method );
238+ loadedMethods .put (clazz , methods );
239+ return method ;
240+ }
241+
205242 /**
206243 * Get a method from a class that has the specific parameters
207244 *
@@ -223,26 +260,41 @@ public static Method getMethod(@NotNull Class<?> clazz, String methodName, Class
223260 * @return The method with appropriate parameters
224261 */
225262 public static Method getMethod (boolean silent , @ NotNull Class <?> clazz , String methodName , Class <?>... params ) {
226- loadedMethods .computeIfAbsent (clazz , aClass -> new HashMap <>());
227- Map <String , Method > methods = loadedMethods .get (clazz );
228- if (methods .containsKey (methodName )) {
229- return methods .get (methodName );
230- }
231263 try {
232- Method method = clazz .getMethod (methodName , params );
233- methods .put (methodName , method );
234- loadedMethods .put (clazz , methods );
235- return method ;
264+ return getMethodUnsafe (clazz , methodName , params );
236265 } catch (Exception e ) {
266+ Map <String , Method > methods = loadedMethods .get (clazz );
237267 if (!silent ) {
238268 WolfyUtilCore .getInstance ().getLogger ().log (Level .SEVERE , "Could not find or invoke Method!" , e );
239269 }
240270 methods .put (methodName , null );
271+ if (methods .containsKey (methodName )) {
272+ return methods .get (methodName );
273+ }
241274 loadedMethods .put (clazz , methods );
242275 return null ;
243276 }
244277 }
245278
279+ /**
280+ * Get a declared method from a class that has the specific parameters
281+ *
282+ * @param clazz The class we are searching
283+ * @param methodName The name of the method
284+ * @param params Any parameters that the method has
285+ * @return The method with appropriate parameters
286+ */
287+ public static Method getDeclaredMethodUnsafe (@ NotNull Class <?> clazz , String methodName , Class <?>... params ) throws NoSuchMethodException {
288+ Map <String , Method > methods = loadedDeclaredMethods .computeIfAbsent (clazz , aClass -> new HashMap <>());
289+ if (methods .containsKey (methodName )) {
290+ return methods .get (methodName );
291+ }
292+ Method method = clazz .getDeclaredMethod (methodName , params );
293+ methods .put (methodName , method );
294+ loadedDeclaredMethods .put (clazz , methods );
295+ return method ;
296+ }
297+
246298 /**
247299 * Get a declared method from a class that has the specific parameters
248300 *
@@ -292,18 +344,50 @@ public static Method getDeclaredMethod(boolean silent, @NotNull Class<?> clazz,
292344 * @param fieldName The name of the field
293345 * @return The field object
294346 */
295- public static Field getField (@ NotNull Class <?> clazz , String fieldName ) {
296- loadedFields .computeIfAbsent (clazz , aClass -> new HashMap <>());
297- Map <String , Field > fields = loadedFields .get (clazz );
347+ public static Field getFieldUnsafe (@ NotNull Class <?> clazz , String fieldName ) throws NoSuchFieldException {
348+ Map <String , Field > fields = loadedFields .computeIfAbsent (clazz , aClass -> new HashMap <>());
349+ if (fields .containsKey (fieldName )) {
350+ return fields .get (fieldName );
351+ }
352+ Field field = clazz .getField (fieldName );
353+ fields .put (fieldName , field );
354+ loadedFields .put (clazz , fields );
355+ return field ;
356+ }
357+
358+ /**
359+ * Get a declared field with a particular name from a class
360+ *
361+ * @param clazz The class
362+ * @param fieldName The name of the field
363+ * @return The field object
364+ */
365+ public static Field getDeclaredFieldUnsafe (@ NotNull Class <?> clazz , String fieldName ) throws NoSuchFieldException {
366+ Map <String , Field > fields = loadedDeclaredFields .computeIfAbsent (clazz , aClass -> new HashMap <>());
298367 if (fields .containsKey (fieldName )) {
299368 return fields .get (fieldName );
300369 }
370+ Field field = clazz .getDeclaredField (fieldName );
371+ fields .put (fieldName , field );
372+ loadedDeclaredFields .put (clazz , fields );
373+ return field ;
374+ }
375+
376+ /**
377+ * Get a field with a particular name from a class
378+ *
379+ * @param clazz The class
380+ * @param fieldName The name of the field
381+ * @return The field object
382+ */
383+ public static Field getField (@ NotNull Class <?> clazz , String fieldName ) {
301384 try {
302- Field field = clazz .getField (fieldName );
303- fields .put (fieldName , field );
304- loadedFields .put (clazz , fields );
305- return field ;
385+ return getFieldUnsafe (clazz , fieldName );
306386 } catch (Exception e ) {
387+ Map <String , Field > fields = loadedFields .get (clazz );
388+ if (fields .containsKey (fieldName )) {
389+ return fields .get (fieldName );
390+ }
307391 WolfyUtilCore .getInstance ().getLogger ().log (Level .SEVERE , "Could not find or access field!" , e );
308392 fields .put (fieldName , null );
309393 loadedFields .put (clazz , fields );
@@ -319,17 +403,13 @@ public static Field getField(@NotNull Class<?> clazz, String fieldName) {
319403 * @return The field object
320404 */
321405 public static Field getDeclaredField (@ NotNull Class <?> clazz , String fieldName ) {
322- loadedDeclaredFields .computeIfAbsent (clazz , aClass -> new HashMap <>());
323- Map <String , Field > fields = loadedDeclaredFields .get (clazz );
324- if (fields .containsKey (fieldName )) {
325- return fields .get (fieldName );
326- }
327406 try {
328- Field field = clazz .getDeclaredField (fieldName );
329- fields .put (fieldName , field );
330- loadedDeclaredFields .put (clazz , fields );
331- return field ;
407+ return getDeclaredFieldUnsafe (clazz , fieldName );
332408 } catch (Exception e ) {
409+ Map <String , Field > fields = loadedDeclaredFields .get (clazz );
410+ if (fields .containsKey (fieldName )) {
411+ return fields .get (fieldName );
412+ }
333413 WolfyUtilCore .getInstance ().getLogger ().log (Level .SEVERE , "Could not find or access field!" , e );
334414 fields .put (fieldName , null );
335415 loadedDeclaredFields .put (clazz , fields );
@@ -354,8 +434,8 @@ public static Field findField(@NotNull Class<?> clazz, Class<?> type) {
354434 List <Field > allFields = new ArrayList <>();
355435 allFields .addAll (Arrays .asList (clazz .getFields ()));
356436 allFields .addAll (Arrays .asList (clazz .getDeclaredFields ()));
357- for (Field f : allFields ){
358- if (type .equals (f .getType ())){
437+ for (Field f : allFields ) {
438+ if (type .equals (f .getType ())) {
359439 fields .put (type , f );
360440 foundFields .put (clazz , fields );
361441 return f ;
0 commit comments