There is task A, B and C. And as a matter of fact, perhaps I am not using the correct method to solve problems A and B, but task C is the following. There is an interface containing methods and annotations:
public interface ApiService { @POST("/api/v1/smart/status") Call<SessionStatusResponse> postWorkChangeStatus(@Body WorkChangeStatusRequestData sessionStatusData); @POST("/api/v1/smart/sensor") Call<SensorDataResponse> postWorkSensorData(@Body SensorRequestData sensorData); @GET("/api/v1/smart/sensor") @FormUrlEncoded Call<AuthConfirmPhoneResponse> getWorkSensorData(@Field("number") String id); } I want to get a Map<String, List<String>> in which the path will be the key (the annotation value, for example "/ api / v1 / smart / sensor") and the annotation name value (if you are familiar with okhttp then you they were met). That is, they should receive in this case {"/api/v1/smart/sensor":["POST", "GET"], ...}
I get this data through reflection in the following way:
void parse() { methods = clazz.getMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].isAnnotationPresent(POST.class)) annotationValues.put(methods[i].getAnnotation(POST.class).value(), new ArrayList<String>()); else if (methods[i].isAnnotationPresent(GET.class)) annotationValues.put(methods[i].getAnnotation(GET.class).value(), new ArrayList<String>()); else if (methods[i].isAnnotationPresent(DELETE.class)) annotationValues.put(methods[i].getAnnotation(DELETE.class).value(), new ArrayList<String>()); else if (methods[i].isAnnotationPresent(PUT.class)) annotationValues.put(methods[i].getAnnotation(PUT.class).value(), new ArrayList<String>()); else if (methods[i].isAnnotationPresent(HEAD.class)) annotationValues.put(methods[i].getAnnotation(HEAD.class).value(), new ArrayList<String>()); else if (methods[i].isAnnotationPresent(OPTIONS.class)) annotationValues.put(methods[i].getAnnotation(OPTIONS.class).value(), new ArrayList<String>()); } for (int i = 0; i < methods.length; i++) { if (methods[i].isAnnotationPresent(POST.class)) { // parse(methods[i], POST.class); String path = methods[i].getAnnotation(POST.class).value(); annotationValues.get(path).add(POST.class.getSimpleName()); } else if (methods[i].isAnnotationPresent(GET.class)) { String path = methods[i].getAnnotation(GET.class).value(); annotationValues.get(path).add(GET.class.getSimpleName()); } else if (methods[i].isAnnotationPresent(DELETE.class)) { String path = methods[i].getAnnotation(DELETE.class).value(); annotationValues.get(path).add(DELETE.class.getSimpleName()); } else if (methods[i].isAnnotationPresent(PUT.class)) { String path = methods[i].getAnnotation(PUT.class).value(); annotationValues.get(path).add(PUT.class.getSimpleName()); } else if (methods[i].isAnnotationPresent(HEAD.class)) { String path = methods[i].getAnnotation(HEAD.class).value(); annotationValues.get(path).add(HEAD.class.getSimpleName()); } else if (methods[i].isAnnotationPresent(OPTIONS.class)) { String path = methods[i].getAnnotation(OPTIONS.class).value(); annotationValues.get(path).add(OPTIONS.class.getSimpleName()); } } } Looks not very nice. I decided to write the following method, which accepts the method and the class of the suspected annotation:
private <T extends Annotation> void parse(Method method, Class<T> postClass) { String test = ((T)method.getAnnotation(postClass)).value(); //ошибка! Log.d(logTag, "========TESTVALUE: " + test); } But a compilation error occurs, which is fully justified, since it is not known in advance whether the class T, an Annotation will inherit the value () method. The okhttp annotations did not find a common ancestor
Is it possible in any way to write a similar method or solve this problem more elegantly?
valueby reflexion. - talex