-
심심해서 만들어본 Object in Class, parameter path 접근 FinderIT/java 2021. 2. 4. 15:41
심심해서 하나 만들어봤어요.
class 기능
- parameter path 접근으로 Class Field, Object 접근
예)
class Three{ String o="z"; }
class One{ String o="z"; public One() { } public One(String o) { this.o=o; } public String getO() { return o; } public void setO(String o) { this.o = o; } }
class Tow{ String t="32"; One one = new One(); One[] oneArr = {new One("o1"),new One("o2"),new One("o3")}; public Tow() { } public Tow(String t){ this.t=t; } public String getT() { return t; } public void setT(String t) { this.t = t; } }
public class StringTest { Logger log = LoggerFactory.getLogger(this.getClass()); String a="1"; Tow tow = new Tow(); String[] ar = {"aa","bb","ee"}; Tow[] tt = {new Tow("a"),new Tow("b"),new Tow("c")}; Map map = new HashMap(); Map<String,Tow> mapc = new HashMap<String,Tow>(); List<String> list = new ArrayList<String>(); StringTest ggg = this; public StringTest() { map.put("zzzz", "zval"); map.put("bbb", "bval"); map.put("bsbb", new One()); mapc.put("zzzz", new Tow()); mapc.put("bbb", new Tow()); list.add("zzz"); list.add("c"); list.add("e"); } }
/////////////////////아래 메인/////////// FindItem item = new FindItem(); item.setAuto(new StringTest()); String s = item.find("ar[2]",String.class); System.out.println(s); String ss = item.find("tt[2].oneArr[2].o",String.class); System.out.println(ss);
/* 결과 */ ee o3
다소 지저분합니다 ㅎ Finder
public class FindItem { FindItem parentFindItem = null; // 부모 FindItem Object owner = null; // 자기자신의 소유자 Field field = null; // 자기값의 필드 Map<String,FindItem> childFindItems = null; List<String> ignorePackageRegex = null; LinkedHashMap<String,FindItem> containerItems = null; public FindItem() { init(); } public FindItem(FindItem parentFindItem, Object owner) { setParentFindItem(parentFindItem); setOwner(owner); init(); } public FindItem(Object owner) { setOwner(owner); init(); } private void init() { childFindItems = new LinkedHashMap<String,FindItem>();//자식 containerItems = new LinkedHashMap<String,FindItem>(); ignorePackageRegex = new ArrayList<String>(); ignorePackageRegex.add("^org\\.slf4j\\..*"); ignorePackageRegex.add("^org\\.log4j\\..*"); ignorePackageRegex.add("^org\\.apache\\..*"); ignorePackageRegex.add("^com\\.google\\..*"); ignorePackageRegex.add("^com\\.mybatis\\..*"); ignorePackageRegex.add("^com\\.springframework\\..*"); ignorePackageRegex.add("^oracle\\..*"); } public Object find(String paramPath) throws NoSuchObjectException{ return find(paramPath,Object.class); } public <T> T find(String paramPath, Class<T> klass) throws NoSuchObjectException{ String[] paths = paramPath.split("\\."); FindItem atChild = null; for (int i = 0; i < paths.length; i++) { String atPath = paths[i]; String[] containers = StringUtil.findScope(atPath,"[","]"); if(containers.length>0){//container atPath = atPath.substring(0,atPath.indexOf("[")); } atChild = (null==atChild?this:atChild).getChildFindItem(atPath); if(null!=atChild && containers.length>0){//container atChild = atChild.getContainerItems(containers[0]); } if(null==atChild){ throw new NoSuchObjectException("not found path "+paramPath); } } return atChild.getObject(klass); } public String getFullPath(){ String path = getFullFindItems().stream().filter(at->null!=at.getField()).map(at->at.getField().getName()).collect(Collectors.joining(".")); return path; } public List<FindItem> getFullFindItems(){ List<FindItem> at = getParentFindItems();at.add(this); return at; } public List<FindItem> getParentFindItems(){ List<FindItem> list = new ArrayList<FindItem>(); if(null!=parentFindItem && this!=parentFindItem){ list.addAll(parentFindItem.getParentFindItems());list.add(parentFindItem); } return list; } public boolean isLoop(){ List<FindItem> list = new ArrayList<FindItem>(); list.add(this); return getParentFindItem()==null?false:getParentFindItem().isLoop(list); } private boolean isLoop(List<FindItem> list){ FindItem parent = this; boolean sw = false; for(FindItem at : list){ if( null!=parent && null!=parent.getOwner() && null!=parent.getField() && null != at.getOwner() && (at.getOwner().equals(parent.getOwner()) || at.getOwner() == parent.getOwner()) ){ sw = true; }else{ sw = false; } if(null==parent || sw==false){ break; } parent = parent.getParentFindItem(); } if(sw){ //위파라미터 개수다 비교해서 다 true면 true이다. loop인거다 return sw; } if(null!=getParentFindItem()){ list.add(this); return getParentFindItem().isLoop(list); } return false; } public FindItem start() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ this.setAuto(this.owner); return this; } public void setAuto(Object owner) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ //System.out.println("("+isLoop()+")\tpath:"+getFullPath()); if(isLoop()){ return; } this.owner = owner; childFindItems.clear(); Object atObject = null; if(null==field){ atObject = this.owner; }else{ atObject = get(); } if(null!=atObject&&!TypeUtil.isDataType(atObject) && !TypeUtil.isContainer(atObject)){ Field[] fields = ReflectionUtil.getDeclaredFields(atObject); for(Field atField : fields){ FindItem findItem = new FindItem(); findItem.setParentFindItem(this); findItem.setOwner(atObject); findItem.setField(atField); if(isIgnore(atField)){ continue; }; childFindItems.put(atField.getName(), findItem); findItem.start(); } } //container이면..array,collection, map.. else if(null!=atObject&&TypeUtil.isContainer(atObject)){ if(atObject.getClass().isArray()){ Object[] at = (Object[]) atObject; for (int i = 0; i < at.length; i++) { containerItems.put(String.valueOf(i), new FindItem(this,at[i]).start()); } }else if(Collection.class.isAssignableFrom(atObject.getClass())){ Collection at = (Collection)atObject; int idx=0; for(Object atO : at){ containerItems.put(String.valueOf(idx), new FindItem(this,atO).start()); idx++; } }else if(Map.class.isAssignableFrom(atObject.getClass())){ Map<?,?> at = (Map)atObject; at.entrySet().stream().forEach(sat->{ try { containerItems.put(sat.getKey().toString(), new FindItem(this,sat.getValue()).start()); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException| IllegalAccessException e) { e.printStackTrace(); } }); } } } public FindItem getParentFindItem() { return parentFindItem; } public void setParentFindItem(FindItem parentFindItem) { this.parentFindItem = parentFindItem; } public Object getOwner() { return owner; } public void setOwner(Object owner) { this.owner = owner; } public Field getField() { return field; } public void setField(Field field) { this.field = field; } public boolean isIgnore(Field o){ boolean sw = false; if(null!=o){ for(String regex : ignorePackageRegex){ if(StringUtil.isMatches(o.getType().getName(),regex)){ sw = true; } } } return sw; } public void set(Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ ReflectionUtil.setDeclaredField(this.owner, this.field, value); } public Object get(){ Object val=null; try { val = ReflectionUtil.getDeclaredField(this.owner, this.field); } catch (Exception e) { val=owner; } return val; } public FindItem getContainerItems(String key) { return containerItems.get(key); } public LinkedHashMap<String, FindItem> getContainerItems() { return containerItems; } public void setContainerItems(LinkedHashMap<String, FindItem> containerItems) { this.containerItems = containerItems; } public Object getContainerObject(String key) { return getContainerObject(key,Object.class); } public <T> T getContainerObject(String key, Class<T> klass) { Object atObject = get(); Object rval = null; if(null!=atObject&&TypeUtil.isContainer(atObject)){//container이면.. if(atObject.getClass().isArray()){ Object[] at = (Object[]) atObject; rval = at[Integer.parseInt(key)]; }else if(Collection.class.isAssignableFrom(atObject.getClass())){ Collection at = (Collection)atObject; int wantIdx = Integer.parseInt(key); int idx=0; for(Object atO : at){ if(wantIdx==idx){ rval = atO; break; } idx++; } }else if(Map.class.isAssignableFrom(atObject.getClass())){ Map at = (Map)atObject; rval = at.get(key); } } return (T)rval; } public List<String> getIgnorePackageRegex() { return ignorePackageRegex; } public void setIgnorePackageRegex(List<String> ignorePackageRegex) { this.ignorePackageRegex = ignorePackageRegex; } public FindItem getChildFindItem(String name) { return childFindItems.get(name); } public Map<String, FindItem> getChildFindItems() { return childFindItems; } public void setChildFindItems(Map<String, FindItem> childFindItems) { this.childFindItems = childFindItems; } public <T> T getObject(Class<T> klass) throws ClassCastException { return (T)get(); } }
'IT > java' 카테고리의 다른 글
페이징(Paging) 계산 Class (0) 2021.02.04 String -> 모스(morse)부호 sound출력 -> 녹음(recoding) -> String (0) 2021.02.04