PDA

View Full Version : HttpRemote exception:I can not get the data from server!



Jesse_Dht
Mar 24th, 2005, 12:12 AM
when i run client application via webstart,i found the data can not got from server.The configuration files and some codes as follows:

clientContext.xml:

<bean id="depotSubFrameManager"
class="org.springframework.remoting.httpinvoker.HttpInvok erProxyFactoryBean"><property name="serviceUrl"><value>http&#58;//$&#123;serverName&#125;&#58;$&#123;httpPort$&#123;contextPath&#125;/remoting/DepotSubFrameManager</value></property>
<property name="serviceInterface">
<value>com.jctx.trms.hibernate.bussiness.DepotSubFrameMan ager</value></property>
</bean>


remoting-servlet.xml:

<bean name="/DepotSubFrameManager"
class="org.springframework.remoting.httpinvoker.HttpInvok erServiceExporter">
<property name="service">
<ref bean="depotSubFrameManager"/>
</property>
<property name="serviceInterface">
<value>com.jctx.trms.hibernate.bussiness.DepotSubFrameMan ager</value></property>
</bean>


applicationContext.xml:

<bean id="depotSubFrameDAO" class="com.jctx.trms.hibernate.DAO.DepotSubFrameDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="depotSubFrameManager" parent="txProxyTemplate">
<property name="target">
<bean class="com.jctx.trms.hibernate.bussiness.DepotSubFrameMan agerImpl">
<property name="depotSubFrameDAO"><ref bean="depotSubFrameDAO"/></property>
</bean>
</property>
</bean>


get the bean:

public static DepotSubFrameManager getDepotSubFrameManager&#40;&#41; &#123;
return &#40;DepotSubFrameManager&#41; &#40;getContext&#40;&#41;.getBean&#40;"depotSubFrameManager"&#41;&#41;;
&#125;


call the service:

DepotSubFrameManager depotsubframem = &#40;DepotSubFrameManager&#41; GlobalApplicationContext.getDepotSubFrameManager&#40;&#41; ;
System.out.println&#40;"the value of depotsubframem is " + depotsubframem&#41;;
List lts = depotsubframem.findAll&#40;&#41;; &#91;color=red&#93;//&#40;1&#41;&#91;/color&#93;
Iterator its = lts.iterator&#40;&#41;;

my dao is:

public List findAll&#40;&#41;&#123;
List list = getHibernateTemplate&#40;&#41;.find&#40;"from com.jctx.trms.hibernate.persistence.DepotSubFrame"&#41;;
System.out.println&#40;"the length of size is " + list.size&#40;&#41;&#41;; &#91;color=red&#93; //&#40;2&#41;&#91;/color&#93; return list;
&#125;

in server,the console print the size of list is 6,but in client,the program run to the row which marked (1) throw the java.lang.NullPointerException,and in the console of client ,the value of depotsubframem is


HTTP invoker proxy for service URL &#91;http&#58;//localhost&#58;8080/trms/remoting/DepotSubFrameManager&#93;

the exception of client console is:


java.lang.NullPointerException
at com.jctx.trms.hibernate.persistence.BaseObject.has hCodeReflectionAppend&#40;BaseObject.java&#58;115&#41;
at com.jctx.trms.hibernate.persistence.BaseObject.has hCode&#40;BaseObject.java&#58;98&#41;
at org.apache.commons.lang.builder.HashCodeBuilder.ap pend&#40;HashCodeBuilder.java&#58;392&#41;
at com.jctx.trms.hibernate.persistence.BaseObject.has hCodeReflectionAppend&#40;BaseObject.java&#58;118&#41;
at com.jctx.trms.hibernate.persistence.BaseObject.has hCode&#40;BaseObject.java&#58;98&#41;
at java.util.HashMap.hash&#40;HashMap.java&#58;261&#41;
at java.util.HashMap.putForCreate&#40;HashMap.java&#58;404&#41;
at java.util.HashMap.readObject&#40;HashMap.java&#58;1007&#41;
at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Nativ e Method&#41;
at sun.reflect.NativeMethodAccessorImpl.invoke&#40;Native MethodAccessorImpl.java&#58;39&#41;
at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;De legatingMethodAccessorImpl.java&#58;25&#41;
at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
at java.io.ObjectStreamClass.invokeReadObject&#40;ObjectS treamClass.java&#58;838&#41;
at java.io.ObjectInputStream.readSerialData&#40;ObjectInp utStream.java&#58;1746&#41;
at java.io.ObjectInputStream.readOrdinaryObject&#40;Objec tInputStream.java&#58;1646&#41;
at java.io.ObjectInputStream.readObject0&#40;ObjectInputS tream.java&#58;1274&#41;
at java.io.ObjectInputStream.defaultReadFields&#40;Object InputStream.java&#58;1845&#41;
at java.io.ObjectInputStream.readSerialData&#40;ObjectInp utStream.java&#58;1769&#41;
at java.io.ObjectInputStream.readOrdinaryObject&#40;Objec tInputStream.java&#58;1646&#41;
at java.io.ObjectInputStream.readObject0&#40;ObjectInputS tream.java&#58;1274&#41;
at java.io.ObjectInputStream.defaultReadFields&#40;Object InputStream.java&#58;1845&#41;
at java.io.ObjectInputStream.readSerialData&#40;ObjectInp utStream.java&#58;1769&#41;
at java.io.ObjectInputStream.readOrdinaryObject&#40;Objec tInputStream.java&#58;1646&#41;
at java.io.ObjectInputStream.readObject0&#40;ObjectInputS tream.java&#58;1274&#41;
at java.io.ObjectInputStream.defaultReadFields&#40;Object InputStream.java&#58;1845&#41;
at java.io.ObjectInputStream.readSerialData&#40;ObjectInp utStream.java&#58;1769&#41;
at java.io.ObjectInputStream.readOrdinaryObject&#40;Objec tInputStream.java&#58;1646&#41;
at java.io.ObjectInputStream.readObject0&#40;ObjectInputS tream.java&#58;1274&#41;
at java.io.ObjectInputStream.defaultReadFields&#40;Object InputStream.java&#58;1845&#41;
at java.io.ObjectInputStream.readSerialData&#40;ObjectInp utStream.java&#58;1769&#41;
at java.io.ObjectInputStream.readOrdinaryObject&#40;Objec tInputStream.java&#58;1646&#41;
at java.io.ObjectInputStream.readObject0&#40;ObjectInputS tream.java&#58;1274&#41;
at java.io.ObjectInputStream.readObject&#40;ObjectInputSt ream.java&#58;324&#41;
at java.util.ArrayList.readObject&#40;ArrayList.java&#58;547&#41;
at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Nativ e Method&#41;
at sun.reflect.NativeMethodAccessorImpl.invoke&#40;Native MethodAccessorImpl.java&#58;39&#41;
at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;De legatingMethodAccessorImpl.java&#58;25&#41;
at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
at java.io.ObjectStreamClass.invokeReadObject&#40;ObjectS treamClass.java&#58;838&#41;
at java.io.ObjectInputStream.readSerialData&#40;ObjectInp utStream.java&#58;1746&#41;
at java.io.ObjectInputStream.readOrdinaryObject&#40;Objec tInputStream.java&#58;1646&#41;
at java.io.ObjectInputStream.readObject0&#40;ObjectInputS tream.java&#58;1274&#41;
at java.io.ObjectInputStream.defaultReadFields&#40;Object InputStream.java&#58;1845&#41;
at java.io.ObjectInputStream.readSerialData&#40;ObjectInp utStream.java&#58;1769&#41;
at java.io.ObjectInputStream.readOrdinaryObject&#40;Objec tInputStream.java&#58;1646&#41;
at java.io.ObjectInputStream.readObject0&#40;ObjectInputS tream.java&#58;1274&#41;
at java.io.ObjectInputStream.readObject&#40;ObjectInputSt ream.java&#58;324&#41;
at org.springframework.remoting.httpinvoker.AbstractH ttpInvokerRequestExecutor.doReadRemoteInvocationRe sult&#40;AbstractHttpInvokerRequestExecutor.java&#58;233&#41;
at org.springframework.remoting.httpinvoker.AbstractH ttpInvokerRequestExecutor.readRemoteInvocationResu lt&#40;AbstractHttpInvokerRequestExecutor.java&#58;181&#41;
at org.springframework.remoting.httpinvoker.SimpleHtt pInvokerRequestExecutor.doExecuteRequest&#40;SimpleHtt pInvokerRequestExecutor.java&#58;55&#41;
at org.springframework.remoting.httpinvoker.AbstractH ttpInvokerRequestExecutor.executeRequest&#40;AbstractH ttpInvokerRequestExecutor.java&#58;68&#41;
at org.springframework.remoting.httpinvoker.HttpInvok erClientInterceptor.executeRequest&#40;HttpInvokerClie ntInterceptor.java&#58;138&#41;
at org.springframework.remoting.httpinvoker.HttpInvok erClientInterceptor.invoke&#40;HttpInvokerClientInterc eptor.java&#58;112&#41;
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed&#40;ReflectiveMethodInvocation.java &#58;144&#41;
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke&#40;JdkDynamicAopProxy.java&#58;174&#41;
at $Proxy39.findAll&#40;Unknown Source&#41;
at com.jctx.trms.hibernate.bussiness.IniTree.getTreeL ist&#40;IniTree.java&#58;70&#41;
at com.jctx.trms.tree.TreeBean.<init>&#40;TreeBean.java&#58;19&#41;
at com.jctx.trms.tree.TreeBean.getInstance&#40;TreeBean.j ava&#58;29&#41;
at com.jctx.trms.tree.MainTree$JCTreeContentProvider. getElements&#40;MainTree.java&#58;241&#41;
at org.eclipse.jface.viewers.StructuredViewer.getRawC hildren&#40;StructuredViewer.java&#58;532&#41;
at org.eclipse.jface.viewers.AbstractTreeViewer.getRa wChildren&#40;AbstractTreeViewer.java&#58;704&#41;
at org.eclipse.jface.viewers.StructuredViewer.getFilt eredChildren&#40;StructuredViewer.java&#58;477&#41;
at org.eclipse.jface.viewers.StructuredViewer.getSort edChildren&#40;StructuredViewer.java&#58;585&#41;
at org.eclipse.jface.viewers.AbstractTreeViewer$1.run &#40;AbstractTreeViewer.java&#58;359&#41;
at org.eclipse.swt.custom.BusyIndicator.showWhile&#40;Bus yIndicator.java&#58;69&#41;
at org.eclipse.jface.viewers.AbstractTreeViewer.creat eChildren&#40;AbstractTreeViewer.java&#58;342&#41;
at org.eclipse.jface.viewers.AbstractTreeViewer$5.run &#40;AbstractTreeViewer.java&#58;787&#41;
at org.eclipse.jface.viewers.StructuredViewer.preserv ingSelection&#40;StructuredViewer.java&#58;862&#41;
at org.eclipse.jface.viewers.AbstractTreeViewer.input Changed&#40;AbstractTreeViewer.java&#58;777&#41;
at org.eclipse.jface.viewers.ContentViewer.setInput&#40;C ontentViewer.java&#58;238&#41;
at org.eclipse.jface.viewers.StructuredViewer.setInpu t&#40;StructuredViewer.java&#58;1059&#41;
at com.jctx.trms.tree.MainTree.createTreeViewer&#40;MainT ree.java&#58;100&#41;
at com.jctx.trms.views.SearchView.createPartControl&#40;S earchView.java&#58;15&#41;
at org.eclipse.ui.internal.PartPane$2.run&#40;PartPane.ja va&#58;137&#41;
at org.eclipse.core.internal.runtime.InternalPlatform .run&#40;InternalPlatform.java&#58;616&#41;
at org.eclipse.core.runtime.Platform.run&#40;Platform.jav a&#58;747&#41;
at org.eclipse.ui.internal.PartPane.createChildContro l&#40;PartPane.java&#58;133&#41;
at org.eclipse.ui.internal.ViewPane.createChildContro l&#40;ViewPane.java&#58;135&#41;
at org.eclipse.ui.internal.ViewFactory$1.run&#40;ViewFact ory.java&#58;351&#41;
at org.eclipse.core.internal.runtime.InternalPlatform .run&#40;InternalPlatform.java&#58;616&#41;
at org.eclipse.core.runtime.Platform.run&#40;Platform.jav a&#58;747&#41;
at org.eclipse.ui.internal.ViewFactory.busyRestoreVie w&#40;ViewFactory.java&#58;273&#41;
at org.eclipse.ui.internal.ViewFactory$2.run&#40;ViewFact ory.java&#58;530&#41;
at org.eclipse.swt.custom.BusyIndicator.showWhile&#40;Bus yIndicator.java&#58;69&#41;
at org.eclipse.ui.internal.ViewFactory.restoreView&#40;Vi ewFactory.java&#58;528&#41;
at org.eclipse.ui.internal.ViewFactory$ViewReference. getPart&#40;ViewFactory.java&#58;102&#41;
at org.eclipse.ui.internal.WorkbenchPage$1.propertyCh ange&#40;WorkbenchPage.java&#58;132&#41;
at org.eclipse.ui.internal.LayoutPart.setVisible&#40;Layo utPart.java&#58;268&#41;
at org.eclipse.ui.internal.PartPane.setVisible&#40;PartPa ne.java&#58;317&#41;
at org.eclipse.ui.internal.ViewPane.setVisible&#40;ViewPa ne.java&#58;568&#41;
at org.eclipse.ui.internal.presentations.PresentableP art.setVisible&#40;PresentablePart.java&#58;122&#41;
at org.eclipse.ui.internal.presentations.DefaultPartP resentation.selectPart&#40;DefaultPartPresentation.jav a&#58;1116&#41;
at org.eclipse.ui.internal.PartStack.refreshPresentat ionSelection&#40;PartStack.java&#58;946&#41;
at org.eclipse.ui.internal.PartStack.setSelection&#40;Par tStack.java&#58;921&#41;
at org.eclipse.ui.internal.PartStack.showPart&#40;PartSta ck.java&#58;1020&#41;
at org.eclipse.ui.internal.PartStack.createControl&#40;Pa rtStack.java&#58;383&#41;
at org.eclipse.ui.internal.PartStack.createControl&#40;Pa rtStack.java&#58;367&#41;
at org.eclipse.ui.internal.PartSashContainer.createCo ntrol&#40;PartSashContainer.java&#58;432&#41;
at org.eclipse.ui.internal.PerspectiveHelper.activate &#40;PerspectiveHelper.java&#58;155&#41;
at org.eclipse.ui.internal.Perspective.onActivate&#40;Per spective.java&#58;732&#41;
at org.eclipse.ui.internal.WorkbenchPage.onActivate&#40;W orkbenchPage.java&#58;2063&#41;
at org.eclipse.ui.internal.WorkbenchWindow$4.run&#40;Work benchWindow.java&#58;1858&#41;
at org.eclipse.swt.custom.BusyIndicator.showWhile&#40;Bus yIndicator.java&#58;69&#41;
at org.eclipse.ui.internal.WorkbenchWindow.setActiveP age&#40;WorkbenchWindow.java&#58;1845&#41;
at org.eclipse.ui.internal.WorkbenchWindow.busyOpenPa ge&#40;WorkbenchWindow.java&#58;498&#41;
at org.eclipse.ui.internal.WorkbenchWindow$3.run&#40;Work benchWindow.java&#58;1076&#41;
at org.eclipse.swt.custom.BusyIndicator.showWhile&#40;Bus yIndicator.java&#58;69&#41;
at org.eclipse.ui.internal.WorkbenchWindow.openPage&#40;W orkbenchWindow.java&#58;1073&#41;
at org.eclipse.ui.internal.Workbench.openFirstTimeWin dow&#40;Workbench.java&#58;992&#41;
at org.eclipse.ui.internal.WorkbenchConfigurer.openFi rstTimeWindow&#40;WorkbenchConfigurer.java&#58;174&#41;
at org.eclipse.ui.application.WorkbenchAdvisor.openWi ndows&#40;WorkbenchAdvisor.java&#58;658&#41;
at org.eclipse.ui.internal.Workbench.init&#40;Workbench.j ava&#58;819&#41;
at org.eclipse.ui.internal.Workbench.runUI&#40;Workbench. java&#58;1325&#41;
at org.eclipse.ui.internal.Workbench.createAndRunWork bench&#40;Workbench.java&#58;254&#41;
at org.eclipse.ui.PlatformUI.createAndRunWorkbench&#40;Pl atformUI.java&#58;141&#41;
at com.jctx.trms.TrmsApplication.run&#40;TrmsApplication. java&#58;14&#41;
at org.eclipse.core.internal.runtime.PlatformActivato r$1.run&#40;PlatformActivator.java&#58;335&#41;
at org.eclipse.core.runtime.adaptor.EclipseStarter.ru n&#40;EclipseStarter.java&#58;273&#41;
at org.eclipse.core.runtime.adaptor.EclipseStarter.ru n&#40;EclipseStarter.java&#58;129&#41;
at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Nativ e Method&#41;
at sun.reflect.NativeMethodAccessorImpl.invoke&#40;Native MethodAccessorImpl.java&#58;39&#41;
at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;De legatingMethodAccessorImpl.java&#58;25&#41;
at java.lang.reflect.Method.invoke&#40;Method.java&#58;324&#41;
at org.eclipse.core.launcher.Main.basicRun&#40;Main.java&#58; 185&#41;
at org.eclipse.core.launcher.Main.run&#40;Main.java&#58;704&#41;
at org.eclipse.core.launcher.Main.main&#40;Main.java&#58;688&#41;

Can anyone please spot what I have done wrong, thanks very much.

Jesse

Alef Arendsen
Mar 26th, 2005, 03:27 AM
Could you show me the hashCode implementation for BaseObject, as well as the hashCodeReflectionAppend implementation?

thanx,
Alef Arendsen

Jesse_Dht
Apr 1st, 2005, 01:22 AM
The class is :


package com.jctx.trms.hibernate.persistence;

import java.io.Serializable;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class BaseObject implements Serializable &#123;
public String toString&#40;&#41; &#123;
return ToStringBuilder.reflectionToString&#40;this, ToStringStyle.MULTI_LINE_STYLE&#41;;
&#125;

public boolean equals&#40;Object o&#41; &#123;
if &#40;this == o&#41; &#123;
return true;
&#125;
if &#40;o == null&#41; &#123;
return false;
&#125;
// Find the leaf class since there may be transients in the leaf
// class or in classes between the leaf and root.
// If we are not testing transients or a subclass has no ivars,
// then a subclass can test equals to a superclass.
Class lhsClass = getClass&#40;&#41;;
Class rhsClass = o.getClass&#40;&#41;;
Class testClass;
if &#40;lhsClass.isInstance&#40;o&#41;&#41; &#123;
testClass = lhsClass;
if &#40;!rhsClass.isInstance&#40;this&#41;&#41; &#123;
// rhsClass is a subclass of lhsClass
testClass = rhsClass;
&#125;
&#125;
else if &#40;rhsClass.isInstance&#40;this&#41;&#41; &#123;
testClass = rhsClass;
if &#40;!lhsClass.isInstance&#40;o&#41;&#41; &#123;
// lhsClass is a subclass of rhsClass
testClass = lhsClass;
&#125;
&#125;
else &#123;
// The two classes are not related.
return false;
&#125;
EqualsBuilder equalsBuilder = new EqualsBuilder&#40;&#41;;
try &#123;
equalsReflectionAppend&#40;o, testClass, equalsBuilder&#41;;
while &#40;testClass.getSuperclass&#40;&#41; != null
&& !testClass.getSuperclass&#40;&#41;.equals&#40;Object.class&#41;&#41; &#123;
testClass = testClass.getSuperclass&#40;&#41;;
equalsReflectionAppend&#40;o, testClass, equalsBuilder&#41;;
&#125;
&#125;
catch &#40;IllegalArgumentException e&#41; &#123;
// In this case, we tried to test a subclass vs. a superclass and
// the subclass has ivars or the ivars are transient and
// we are testing transients.
// If a subclass has ivars that we are trying to test them, we get an
// exception and we know that the objects are not equal.
return false;
&#125;
return equalsBuilder.isEquals&#40;&#41;;
&#125;

private void equalsReflectionAppend&#40;Object rhs, Class clazz, EqualsBuilder builder&#41; &#123;
Field&#91;&#93; fields = clazz.getDeclaredFields&#40;&#41;;
AccessibleObject.setAccessible&#40;fields, true&#41;;
for &#40;int i = 0; i < fields.length && builder.isEquals&#40;&#41;; i++&#41; &#123;
Field f = fields&#91;i&#93;;
if &#40;&#40;f.getName&#40;&#41;.indexOf&#40;'$'&#41; == -1&#41; && &#40;!Modifier.isTransient&#40;f.getModifiers&#40;&#41;&#41;&#41;
&& &#40;!Modifier.isStatic&#40;f.getModifiers&#40;&#41;&#41;&#41;&#41; &#123;
try &#123;
if &#40;Collection.class.isAssignableFrom&#40;f.getType&#40;&#41;&#41; &#41; &#123;
builder.append&#40;f.get&#40;this&#41;.getClass&#40;&#41;, f.get&#40;rhs&#41;.getClass&#40;&#41;&#41;;
&#125;
else &#123;
builder.append&#40;f.get&#40;this&#41;, f.get&#40;rhs&#41;&#41;;
&#125;
&#125;
catch &#40;IllegalAccessException e&#41; &#123;
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError&#40;"Unexpected IllegalAccessException"&#41;;
&#125;
&#125;
&#125;
&#125;

public int hashCode&#40;&#41; &#123;
HashCodeBuilder builder = new HashCodeBuilder&#40;17, 37&#41;;
Class clazz = getClass&#40;&#41;;
hashCodeReflectionAppend&#40;clazz, builder&#41;;
while &#40;clazz.getSuperclass&#40;&#41; != null && clazz.getSuperclass&#40;&#41;.equals&#40;Object.class&#41;&#41; &#123;
clazz = clazz.getSuperclass&#40;&#41;;
hashCodeReflectionAppend&#40;clazz, builder&#41;;
&#125;
return builder.toHashCode&#40;&#41;;
&#125;

private void hashCodeReflectionAppend&#40;Class clazz, HashCodeBuilder builder&#41; &#123;
Field&#91;&#93; fields = clazz.getDeclaredFields&#40;&#41;;
AccessibleObject.setAccessible&#40;fields, true&#41;;
for &#40;int i = 0; i < fields.length; i++&#41; &#123;
Field f = fields&#91;i&#93;;
if &#40;&#40;f.getName&#40;&#41;.indexOf&#40;'$'&#41; == -1&#41; && &#40;!Modifier.isTransient&#40;f.getModifiers&#40;&#41;&#41;&#41;
&& &#40;!Modifier.isStatic&#40;f.getModifiers&#40;&#41;&#41;&#41;&#41; &#123;
try &#123;
if &#40;Collection.class.isAssignableFrom&#40;f.getType&#40;&#41;&#41; &#41; &#123;
builder.append&#40;f.get&#40;this&#41;.getClass&#40;&#41;&#41;;
&#125;
else &#123;
builder.append&#40;f.get&#40;this&#41;&#41;;
&#125;
&#125;
catch &#40;IllegalAccessException e&#41; &#123;
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError&#40;"Unexpected IllegalAccessException"&#41;;
&#125;
&#125;
&#125;
&#125;
&#125;


Is it the class you want? And it was copied from the internet!

Jesse_Dht
Apr 4th, 2005, 01:15 AM
I have solve the problem.the problem is that the f.get(this) is null,the code as follows:


private void hashCodeReflectionAppend&#40;Class clazz, HashCodeBuilder builder&#41; &#123;
Field&#91;&#93; fields = clazz.getDeclaredFields&#40;&#41;;
AccessibleObject.setAccessible&#40;fields, true&#41;;
for &#40;int i = 0; i < fields.length; i++&#41; &#123;
Field f = fields&#91;i&#93;;
if &#40;&#40;f.getName&#40;&#41;.indexOf&#40;'$'&#41; == -1&#41; && &#40;!Modifier.isTransient&#40;f.getModifiers&#40;&#41;&#41;&#41;
&& &#40;!Modifier.isStatic&#40;f.getModifiers&#40;&#41;&#41;&#41;&#41; &#123;
try &#123;
&#91;b&#93;if &#40;f.get&#40;this&#41; != null&#41; &#91;/b&#93;&#123;
if &#40;Collection.class.isAssignableFrom&#40;f.getType&#40;&#41;&#41; &#41; &#123;
// System.out.println&#40;"BaseObject.java " +
// f.get&#40;this&#41;.getClass&#40;&#41;.toString&#40;&#41;&#41;;
builder.append&#40;f.get&#40;this&#41;.getClass&#40;&#41;&#41;;
&#125;
else &#123;
// System.out.println&#40;"BaseObject.java " + f.get&#40;this&#41;.toString&#40;&#41;&#41;;
builder.append&#40;f.get&#40;this&#41;&#41;;
&#125;
&#125;
&#125;
catch &#40;IllegalAccessException e&#41; &#123;
//this can't happen. Would get a Security exception instead
//throw a runtime exception in case the impossible happens.
throw new InternalError&#40;"Unexpected IllegalAccessException"&#41;;
&#125;
catch &#40;Exception e&#41; &#123;
System.out.println&#40;"BaseObject.java Error &#58;" + e.getMessage&#40;&#41;&#41;;
&#125;
&#125;
&#125;
&#125;

Alef Arendsen
Apr 4th, 2005, 01:54 AM
In general I don't think reflective based toString() and hashCode() methods are a good idea. Every time I see them people are running into problems like this.

rgds,
Alef Arendsen

Jesse_Dht
Apr 7th, 2005, 09:33 PM
:( ,Do you have a better way to solve the problem?

thanks