English 中文(简体)
如何改变“这种无障碍环境”
原标题:How to re move "This accessibility bypass should be removed." by using Reflect

我做了以下守则:

public static <T> T mergeObjects(T draft, T existing)
      throws IllegalAccessException, InstantiationException, NoSuchMethodException,
          InvocationTargetException {
    Class<?> clazz = draft.getClass();
    Field[] fields = clazz.getDeclaredFields();
    Object returnValue = clazz.getDeclaredConstructor().newInstance();
    for (Field field : fields) {
      field.setAccessible(true);
      Object draftValue = field.get(draft);
      Object existingValue = field.get(existing);
      if (Objects.equals(existingValue, draftValue)) field.set(returnValue, existingValue);
      else field.set(returnValue, draftValue);
    }
    return (T) returnValue;
  }

但是,它给定点的幼 the问题。

  1. field.setAccessible(true);
  2. if (Objects.equals(existingValue, draftValue)) field.set(returnValue, existingValue);
  3. else field.set(returnValue, draftValue);

I have tried many things but not working. Can anyone know hoe to resolve it, please tell me.

问题回答
  • If you just want to remove the lint issue, you can use org.springframework.util.ReflectionUtils.makeAccessible(field); and ReflectionUtils.setField(field,target,value)
  • example code:
ReflectionUtils.makeAccessible(field);//no sonar lint issue
  • Warning:this solution didn t solve any potential issue, because the code in makeAccessible also use the field.setAccessible(true); with some check if statement, just use a static util method to get around the sonar check. If you care about the potential issue, pls check the "useful link" in below.
  • ReflectionUtils.makeAccessible code:
    public static void makeAccessible(Field field) {
        if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
            field.setAccessible(true);
        }
    }
  • useful link:
  1. is-field-setaccessibletrue-bad-practice
  2. java-reflection-impact-of-setaccessibletrue
  3. SEC05 wiki




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签