锁定老帖子 主题:两段java代码的比较
精华帖 (0) :: 良好帖 (16) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-02
Eastsun 写道 Java doesn't support this optimization for a number of reasons, including:
* the security model requires stack frame information * reflection relies on "stack crawling" * stack traces would be incomplete * debugging would be problematic You have the points. BTW: It seems FF3 RC1 is not well supported by JavaEye. |
|
返回顶楼 | |
发表时间:2008-06-02
俺这个递归运行良好,如果从root object开始执行(一个XML的root element),大约100多个对象实例
package byd.biz; import javax.ejb.Stateless; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; /** * * @author ll258583 */ @Stateless(name = "xoServiceBean") public class xoServiceBean implements xoServiceLocal { @PersistenceContext(unitName = "xoServicePU") private EntityManager em; /** * Set JAXB object's value to the EntityBean object * @param jxo * @param eto */ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void x2o(Object jxo, Object eto) { System.out.println("------------------------------------------------------------------"); System.out.println(eto.getClass().getName()); //validate if two papramater are matchable if (eto.getClass().getPackage().getName().equals("byd.entity") && jxo.getClass().getPackage().getName().equals("byd.xoMapping.pip4A3") && eto.getClass().getName().substring(12).equals(jxo.getClass().getName().substring(21))) { Field[] etoDeclaredFields = eto.getClass().getDeclaredFields(); Field[] jxoDeclaredFields = jxo.getClass().getDeclaredFields(); for (int i = 0; i < etoDeclaredFields.length; i++) { for (int j = 0; j < jxoDeclaredFields.length; j++) { Field etoField = etoDeclaredFields[i]; Class etoFieldType = etoField.getType(); String etoFieldName = etoField.getName(); Field jxoField = jxoDeclaredFields[j]; String jxoFieldName = jxoField.getName(); try { if (etoFieldName.toLowerCase().equals(jxoFieldName.toLowerCase())) { if (etoFieldType.getName().equals("java.lang.String")) { //in case of basic type(java.lang.String) filed System.out.println(etoFieldName + " : " + etoFieldType.getName()); Class<?>[] paras = null; Object[] args = null; Method jxoGetMethod = jxo.getClass().getDeclaredMethod("get" + jxoFieldName.substring(0, 1).toUpperCase() + jxoFieldName.substring(1), paras); String value = (String) jxoGetMethod.invoke(jxo, args); if (value != null) { Method etoSetMethod = eto.getClass().getDeclaredMethod("set" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), etoFieldType); etoSetMethod.invoke(eto, value); } else { System.out.println("STRING IS NULL"); } } else if (etoFieldType.getName().equals("java.util.List")) { //in case of java.util.List field System.out.println(etoFieldName + " : " + etoFieldType.getName()); Class<?>[] paras = null; Object[] args = null; Method jxoGetMethod = jxo.getClass().getDeclaredMethod("get" + jxoFieldName.substring(0, 1).toUpperCase() + jxoFieldName.substring(1), paras); Method etoGetMethod = eto.getClass().getDeclaredMethod("get" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), paras); List jxoMemberList = (List) jxoGetMethod.invoke(jxo, args); if (jxoMemberList.size() != 0 && jxoMemberList != null) { List etoMemberList = (List) etoGetMethod.invoke(eto, args); etoMemberList = new ArrayList(); for (Object jxm : jxoMemberList) { Class C = Class.forName("byd.entity._" + jxm.getClass().getName().substring(21)); Object etm = C.newInstance(); this.x2o(jxm, etm); etoMemberList.add(etm); } } else { System.out.println("LIST IS EMPTY OR NULL"); } } else if (etoFieldType.getName().equals("java.util.ArrayList")) { //in case of java.util.ArrayList<String>() field System.out.println(etoFieldName + " : " + etoFieldType.getName()); Class<?>[] paras = null; Object[] args = null; Method jxoGetList = jxo.getClass().getDeclaredMethod("get" + jxoFieldName.substring(0, 1).toUpperCase() + jxoFieldName.substring(1), paras); List<String> stringList_1 = (List<String>) jxoGetList.invoke(jxo, args); if (stringList_1 != null && stringList_1.size() != 0) { Method etoGetList = eto.getClass().getDeclaredMethod("get" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), paras); ArrayList stringList_2 = (ArrayList) etoGetList.invoke(eto, args); stringList_2 = new ArrayList<String>(); for (String str_1 : stringList_1) { String str_2 = new String(str_1); stringList_2.add(str_2); } } else { System.out.println("ArrayList<String> IS NULL OR EMPTY"); } } else { //in case of other class type field System.out.println(etoFieldName + " : " + etoFieldType.getName()); Class<?>[] paras = null; Object[] args = null; Method jxoGetMethod = jxo.getClass().getDeclaredMethod("get" + jxoFieldName.substring(0, 1).toUpperCase() + jxoFieldName.substring(1), paras); Object nextJxo = jxoGetMethod.invoke(jxo, args); if (nextJxo != null) { // Method etoGetMethod = eto.getClass().getDeclaredMethod("get" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), paras); Method etoSetMethod = eto.getClass().getDeclaredMethod("set" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), etoFieldType); Object nextEto = etoFieldType.newInstance(); etoSetMethod.invoke(eto, nextEto); this.x2o(nextJxo, nextEto); } else { System.out.println(jxoFieldName + " IS NULL"); } } } } catch (ClassNotFoundException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } } } } else { // throw new UnmatchableException(); System.out.println("UnmatchableException"); } //persist the entityBean object's value System.out.println("Persist : " + eto.getClass().getName()); em.persist(eto); } /** * Set EntityBean's value to the JAXB object * @param eto * @param jxo */ @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void o2x(Object eto, Object jxo) { System.out.println("------------------------------------------------------------------"); System.out.println(eto.getClass().getName()); //validate if two papramater are matchable if (eto.getClass().getPackage().getName().equals("byd.entity") && jxo.getClass().getPackage().getName().equals("byd.xoMapping.pip4A3") && eto.getClass().getName().substring(12).equals(jxo.getClass().getName().substring(21))) { Field[] etoDeclaredFields = eto.getClass().getDeclaredFields(); Field[] jxoDeclaredFields = jxo.getClass().getDeclaredFields(); for (int i = 0; i < jxoDeclaredFields.length; i++) { for (int j = 0; j < etoDeclaredFields.length; j++) { Field jxoField = jxoDeclaredFields[i]; String jxoFieldName = jxoField.getName(); Field etoField = jxoDeclaredFields[j]; String etoFieldName = etoField.getName(); try { if (etoFieldName.toLowerCase().equals(jxoFieldName.toLowerCase())) { if (jxoField.getType().getName().equals("java.lang.String")) { //in case of basic type(java.lang.String) filed System.out.println(jxoField.getName() + " : " + jxoField.getType().getName()); Class<?>[] paras = null; Object[] args = null; Method etoGet = eto.getClass().getDeclaredMethod("get" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), paras); String value = (String) etoGet.invoke(eto, args); if (value != null) { Method jxoSet = jxo.getClass().getDeclaredMethod("set" + jxoFieldName.substring(0, 1).toUpperCase() + jxoFieldName.substring(1), jxoField.getType()); jxoSet.invoke(jxo, value); } else { System.out.println("STRING IS NULL"); } } else if (jxoField.getType().getName().equals("java.util.List")) { //in case of collection(java.util.List) field System.out.println(jxoField.getName() + " : " + jxoField.getType().getName()); Class<?>[] paras = null; Object[] args = null; Method etoGetList = eto.getClass().getDeclaredMethod("get" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), paras); List etoList = (List) etoGetList.invoke(eto, args); if (etoList.size() != 0 && etoList != null) { Method jxoGetList = jxo.getClass().getDeclaredMethod("get" + jxoFieldName.substring(0, 1).toUpperCase() + jxoFieldName.substring(1), paras); List jxoList = (List) jxoGetList.invoke(jxo, args); jxoList = new ArrayList(); for (Object etm : etoList) { Class C = Class.forName("byd.xoMapping.pip4A3." + etm.getClass().getName().substring(12)); Object jxm = C.newInstance(); this.o2x(etm, jxm); jxoList.add(jxm); } } else { System.out.println("LIST IS NULL OR EMPTY"); } } else if (etoField.getType().getName().equals("java.util.ArrayList")) { //in case of ArrayList<String> field System.out.println(jxoField.getName() + " : " + jxoField.getType().getName()); Class<?>[] paras = null; Object[] args = null; Method etoGetList = eto.getClass().getDeclaredMethod("get" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), paras); ArrayList<String> stringList_1 = (ArrayList<String>) etoGetList.invoke(jxo, args); if (stringList_1 != null && stringList_1.size() != 0) { Method jxoGetList = jxo.getClass().getDeclaredMethod("get" + jxoFieldName.substring(0, 1).toUpperCase() + jxoFieldName.substring(1), paras); List stringList_2 = (List) jxoGetList.invoke(jxo, args); stringList_2 = new ArrayList<String>(); for (String str_1 : stringList_1) { String str_2 = new String(str_1); stringList_2.add(str_2); } } else { System.out.println("ArrayList<String> IS NULL OR EMPTY"); } } else { //in case of other class type field Class<?>[] paras = null; Object[] args = null; Object arg = null; Method etoGet = eto.getClass().getDeclaredMethod("get" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), paras); Object nextEto = etoGet.invoke(jxo, arg); if (nextEto != null) { Method jxoSet = jxo.getClass().getDeclaredMethod("set" + etoFieldName.substring(0, 1).toUpperCase() + etoFieldName.substring(1), jxoField.getType()); Object nextJxo = jxoField.getType().newInstance(); jxoSet.invoke(jxo, nextJxo); this.o2x(nextEto, nextJxo); } else { System.out.println(etoFieldName + " IS NULL"); } } } } catch (InstantiationException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(xoServiceBean.class.getName()).log(Level.SEVERE, null, ex); } } } } else { // throw new UnmatchableException(); System.out.println("UnmatchableException"); } } } |
|
返回顶楼 | |
发表时间:2008-06-02
楼上代码看不懂~!
你方法里的代码是不是太长了啊~! 呵呵 |
|
返回顶楼 | |
发表时间:2008-06-03
楼上的楼上的代码好长
我以前写的递归基本上没有创建大的对象,所以没有碰到此问题,今天楼主提出来了,以后恐怕得多注意了 |
|
返回顶楼 | |
发表时间:2008-06-03
以前遇到过这种问题, 最后还是改成循环调用解决的 ,
|
|
返回顶楼 | |
发表时间:2008-06-05
递归 出栈入栈的事肯定多
|
|
返回顶楼 | |
发表时间:2008-06-05
一般来说没有大问题的。除非很大的递归。这个一般也可以当作性能问题考虑,不用太早考虑,更用不着把一切递归的方法都改成循环。
|
|
返回顶楼 | |
发表时间:2008-06-05
Lucas Lee 写道 一般来说没有大问题的。除非很大的递归。这个一般也可以当作性能问题考虑,不用太早考虑,更用不着把一切递归的方法都改成循环。
嗯,这仅仅是对OOM问题的分析做多一种设想。 |
|
返回顶楼 | |
发表时间:2008-06-05
import java.util.ArrayList; import java.util.List; public class TailRecursionTest { public static void main(String[] args) { TailRecursionTest t = new TailRecursionTest(); t.a(0); } public void a(int j) { System.out.println(j); j++; if (j<10000) //if (j!=10000) 也可以 return; List list = new ArrayList<Integer>(100000); a(j); } } 递归 ,学习,以上我把if条件小改动了下,可以成功,不知道怎么解释:) |
|
返回顶楼 | |
发表时间:2008-06-05
leelj 写道 import java.util.ArrayList; import java.util.List; public class TailRecursionTest { public static void main(String[] args) { TailRecursionTest t = new TailRecursionTest(); t.a(0); } public void a(int j) { System.out.println(j); j++; if (j<10000) //if (j!=10000) 也可以 return; List list = new ArrayList<Integer>(100000); a(j); } } 递归 ,学习,以上我把if条件小改动了下,可以成功,不知道怎么解释:) 无语了,0<10000,一次还没跑完。 |
|
返回顶楼 | |