I'm a Java developer. As a Java developer. There are many annoying things in workspace while I'm working. Such as Using MAP!!!! Let's say. You're going to put Info data into Map
Info info = new Info("name", "password", "alias", "address", "strength", "etc1", "etc2"); Map map = new HashMapI've found tons of lines of codes In my workspace. As a junior Programmer, By the way I've worked as a programmer just about 5 months, I asked my comapany "How can we fix it? This is really annoying?" But they said "I don't know Why you are asking. This is What it is. It is not an error." So I've tried using Java Reflection. It is very Simple that you can easily use it. first. Create Getters(); map.put("name",info.getName()); map.put("password",info.getPassword()); map.put("alias",info.getAlias()); map.put("address",info.getAddress()); map.put("strength",info.getStrength()); map.put("etc1",info.getEtc1()); map.put("etc2",info.getEtc2());
private String craeteGetMethod(String arg) { StringBuffer buffer = new StringBuffer(10); buffer.append("get"); buffer.append(arg.substring(0, 1).toUpperCase()); buffer.append(arg.substring(1)); return buffer.toString(); }Simple so When You put "name" in it. You'll get "getName". Second. Create Invoking Method
private String invokeMethod( String methodName, Object obj) { String res = null; Method me; try { me = obj.getClass().getMethod(methodName); res = (String)me.invoke(obj); } catch (Exception e) { e.printStackTrace(); } return res; }Third. Gather Method and Invoke All
private void ObjectToMap(MapI should have finished refactoring. but this will be enough in this page.map, List methods, Object sms) { for ( String key : methods) { String methodName = craeteGetMethod(key); invokeMethod(methodName, sms); map.put(key, invokeMethod(methodName,sms)); } }
Info info = new Info("name", "password", "alias", "address", "strength", "etc1", "etc2"); Map map = new HashMapBut It's really slow If you are going yo use reflection to put a massive datas into Database. This is not for you. thank you for stopping by. I will be happy with your comments.(); ObjectToMap(map, Arrays.asList("name", "password", "alias", "address", "strength", "etc1", "etc2"), info);
댓글 없음:
댓글 쓰기