우리가하는 일은 - Annotation driven Null과 empty values는 Object를 검사합니다.Null 검사에 사용할 디자인 패턴
우리가하는 일 - 하나의 주석을 작성하고 그 주석을 변수 선언에 두는 것.
을 가장 잘 작동하도록 사용하는 데 필요한 디자인 패턴 모르겠습니다. 제발 제안 해주세요.나는 내가 그것을
주석 클래스 - 우리가
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
/**
* Message.
*
* @return the string
*/
public String message();
}
를 선언하는 방법은 변수 -
@Test(message="five message")
private String five;
것은 내가 주석 프로세서를 호출하고 방법 -
Class<?> annotationClass = annotationTestinClass.getClass();
Field[] decalaredFieldsArray = annotationClass.getDeclaredFields();
List<String> lstString = new ArrayList<>();
parseFields(decalaredFieldsArray,annotationTestinClass,lstString);
내가 처리하고 어떻게 객체 -
public static List<String> parseFields(Field[] decalaredFieldsArray,Object obj,List<String> lstString){
Arrays.stream(decalaredFieldsArray).forEach(field ->{
field.setAccessible(true);
Test test = field.getDeclaredAnnotation(Test.class);
if(field.getType().isPrimitive() || field.getType().getName().equals("java.lang.String")){
if(field.isAnnotationPresent(Test.class)){
try {
System.out.println("field value is :"+field.get(obj));
System.out.println("field Name is :"+field.getName());
if(field.get(obj)== null || !StringUtils.isNoneBlank(field.get(obj).toString())){
lstString.add(test.message());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
Field[] objectFields =null;
Object objValue = null;
try {
if(field.isAnnotationPresent(Test.class)){
objValue = field.get(obj);
if(objValue!=null){
objectFields = objValue.getClass().getDeclaredFields();
parseFields(objectFields, objValue, lstString);
}else{
System.out.println("Object value is -"+field.get(obj));
System.out.println("Messsage value is -"+test.message());
lstString.add(test.message());
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
return lstString;
}
여기 Test.Class가 내 Test 주석을 다시 작성합니다.
선언에서 변수에 주석을 추가하는 경우 - 어떤 시점에서 null을 확인 하시겠습니까? 그 선언에서 바로? 매 2 주 일요일? 코드의 어느 부분이 해당 체크를 호출하거나 실행할 것입니까? – f1sh
null 개체 패턴 시도 : http://www.tutorialspoint.com/design_pattern/null_object_pattern.htm –
'선택 사항 '옵션이 있습니까? – ChiefTwoPencils