개발중/Java

Field 활용

Binsoo 2021. 4. 19. 13:05
728x90
반응형

java.lang.reflect.Field

 


public List<CodeVo> getParentCodeList( int sysCode, String filedName, String value) {

	List<CodeVo> codeList = getParentCodeList(sysCode);

	try {
		Field field = CodeVo.class.getDeclaredField(filedName);
		field.setAccessible(true);

        Object paramValue;
        if(field.getType() == int.class) {
        	paramValue = Integer.parseInt(value);
        } else if(field.getType() == String.class) {
        	paramValue = value;
        } else {
        	throw new RuntimeException("code value type error");
        }

	return codeList.stream()
    			   .filter(code -> {
						try {
								Object fieldValue = field.get(code);
								if(fieldValue.equals(paramValue)) {
                                return true;
                            }
                            	return false;
                            } catch (IllegalArgumentException|IllegalAccessException e) {
                            	throw new RuntimeException(e);
                            } }).collect(Collectors.toList());

                          } catch (NoSuchFieldException e) {
                          throw new RuntimeException("지원하지 않는 fieldName", e);
                          
                          } catch (SecurityException e) {
                          throw new RuntimeException(e);
                          }
}

 

728x90
반응형