From e2cff453f5705cb825b3278915a22c35c72c5d65 Mon Sep 17 00:00:00 2001 From: gotty Date: Thu, 14 Nov 2019 22:00:43 +0000 Subject: [PATCH] [LIB-8] Modify URL entity annotation functionality git-svn-id: https://svn.hedgecode.org/lib/snooker-score-api/trunk@177 fb0bcced-7025-49ed-a12f-f98bce993226 --- .../org/hedgecode/snooker/json/JsonURLEntity.java | 37 ++++++++++------------ 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/hedgecode/snooker/json/JsonURLEntity.java b/src/main/java/org/hedgecode/snooker/json/JsonURLEntity.java index 9798671..f49f399 100644 --- a/src/main/java/org/hedgecode/snooker/json/JsonURLEntity.java +++ b/src/main/java/org/hedgecode/snooker/json/JsonURLEntity.java @@ -94,7 +94,7 @@ public abstract class JsonURLEntity implements URLEntity { field.getName(), (String) field.get(this) ); - } catch (Exception ignored) { + } catch (ReflectiveOperationException ignored) { } } } @@ -106,18 +106,16 @@ public abstract class JsonURLEntity implements URLEntity { String fieldName ) { String result = null; - Class clazz = getClass(); - for (Field field : clazz.getDeclaredFields()) { + try { + Class clazz = getClass(); + Field field = clazz.getDeclaredField(fieldName); if (field.isAnnotationPresent(annotationClass) - && field.getName().equals(fieldName) && field.getType().equals(String.class)) { field.setAccessible(true); - try { - result = (String) field.get(this); - } catch (Exception ignored) { - } + result = (String) field.get(this); } + } catch (ReflectiveOperationException ignored) { } return result; } @@ -127,17 +125,17 @@ public abstract class JsonURLEntity implements URLEntity { ) { Map result = new HashMap<>(); Class clazz = getClass(); - for (Method method : clazz.getMethods()) { + for (Method method : clazz.getDeclaredMethods()) { if (method.isAnnotationPresent(annotationClass) - && method.getReturnType().equals(String.class) - && method.getParameterCount() == 0) + && method.getParameterCount() == 0 + && method.getReturnType().equals(String.class)) { try { result.put( method.getName(), (String) method.invoke(this) ); - } catch (Exception ignored) { + } catch (ReflectiveOperationException ignored) { } } } @@ -149,18 +147,15 @@ public abstract class JsonURLEntity implements URLEntity { String methodName ) { String result = null; - Class clazz = getClass(); - for (Method method : clazz.getMethods()) { + try { + Class clazz = getClass(); + Method method = clazz.getDeclaredMethod(methodName); if (method.isAnnotationPresent(annotationClass) - && method.getName().equals(methodName) - && method.getReturnType().equals(String.class) - && method.getParameterCount() == 0) + && method.getReturnType().equals(String.class)) { - try { - result = (String) method.invoke(this); - } catch (Exception ignored) { - } + result = (String) method.invoke(this); } + } catch (ReflectiveOperationException ignored) { } return result; } -- 2.10.0