java general? 비슷한 toString 만들기
글쓴이: emptynote / 작성시간: 일, 2011/02/20 - 4:17오전
Out 객체에서 toString 만들기할때 배열의 경우 어떻게 처리를해야 할지 난감했습니다.
늘상 배열값은 메모리 주소? 같은 모양의 문자열을 보여주었는데,
오늘 아래 사이트를 참조해서 대충 만들어 보았는데 일단 만족하네요.
참고 url : http://www.roseindia.net/javatutorials/Generic_toString.shtml
참고 url 사이트 보시면 아시겠지만 general 하게 만든다는것은 어려운듯하네요.
사이트 보다가 필요한 부분만 가져왔습니다.
그런데,
boolean isSynthetic = field.isSynthetic();
if (isSynthetic) continue;
==> 메소드 isSynthetic() API 설명 봐도 참 거시기한합니다.
이 함수에 대한 정확한 번역은 어떤걸까요?
이거 안쓰면 "Class io.AbstractOut can not access a member of class io.out.TestFieldOut$Member$Item with modifiers "final"" 이런 에러가 나오는데요.
Synthetic의 뜻에 대해서 제보 부탁드립니다.
package io; import java.lang.reflect.Array; import java.lang.reflect.Field; public class AbstractOut { private void appendValue(String fieldName, Object fieldValue, StringBuffer strBuff) { String newLine = System.getProperty("line.separator"); strBuff.append(newLine); strBuff.append(fieldName); strBuff.append("=["); strBuff.append(fieldValue); strBuff.append("]"); } private void appendArryValue(String fieldName, int inx, Object fieldValue, StringBuffer strBuff) { String newLine = System.getProperty("line.separator"); strBuff.append(newLine); strBuff.append(fieldName); strBuff.append("["); strBuff.append(inx); strBuff.append("]"); strBuff.append("=["); strBuff.append(fieldValue); strBuff.append("]"); } /** * 1. 참고 url * 1.1 <a href="http://www.javapractices.com/topic/TopicAction.do?Id=55 " rel="nofollow">http://www.javapractices.com/topic/TopicAction.do?Id=55 </a> * 1.2 <a href="http://www.roseindia.net/javatutorials/Generic_toString.shtml " rel="nofollow">http://www.roseindia.net/javatutorials/Generic_toString.shtml </a> * * (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { StringBuffer strBuff = new StringBuffer(); String newLine = System.getProperty("line.separator"); strBuff.append( this.getClass().getName() ); strBuff.append( " Object {" ); Field[] fields = this.getClass().getDeclaredFields(); for ( Field field : fields ) { strBuff.append(" "); try { boolean isSynthetic = field.isSynthetic(); if (isSynthetic) continue; Object fieldValue = field.get(this); String fieldName = field.getName(); if (fieldValue != null && fieldValue.getClass().isArray()) { int length = Array.getLength(fieldValue); for (int i = 0; i < length; i++) { Object arry_value = Array.get(fieldValue, i); appendArryValue(fieldName, i, arry_value, strBuff); } } else { appendValue(fieldName, fieldValue, strBuff); } } catch ( IllegalAccessException ex ) { ex.printStackTrace(); System.exit(1); } } strBuff.append(newLine); strBuff.append("}"); return strBuff.toString(); } }
File attachments:
첨부 | 파일 크기 |
---|---|
![]() | 1.68 KB |
Forums: