如何从对象获取字段

huangapple go评论97阅读模式
英文:

How to get field from Object

问题

我有一个类:

public class resultJTO extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;
}

我想要获取这个HashMap的特定字段。我的意思是我在 resultJTO 上执行了 get 操作:

Object airport = resultJTO.get("AIRPORT");

我尝试通过以下方式来实现:

Object airport = resultJTO.get("AIRPORT");
Field field = searchJTO.class.getDeclaredField("city");
field.setAccessible(true);
field.get(airport);

但是得到了以下错误信息:

java.lang.IllegalArgumentException: 无法将 java.lang.String 字段 AirportSearchJTO.city 设置为 java.util.HashSet

    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:393)
    at SearchServiceTest.shouldReturnProperMergedAirportIfAvailableInBothSources(SearchServiceTest.java:191)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ...

我应该如何获取例如 city 字段?

英文:

I have a class:

public class resultJTO extends HashMap&lt;String, Object&gt; {

    private static final long serialVersionUID = 1L;
}

I would like to get particular fields of this Hashmap. I mean I'm doing get on resultJTO

 Object airport = resultJTO.get(&quot;AIRPORT&quot;);

I was trying to do it by this way:

Object airport = resultJTO.get(&quot;AIRPORT&quot;);
        Field field = searchJTO.class.getDeclaredField(&quot;city&quot;);
        field.setAccessible(true);
        field.get(airport);

but getting

ava.lang.IllegalArgumentException: Can not set java.lang.String field AirportSearchJTO.city to java.util.HashSet

at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:393)
at SearchServiceTest.shouldReturnProperMergedAirportIfAvailableInBothSources(SearchServiceTest.java:191)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:74)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

what should I do to fetch for example city?

答案1

得分: 1

检查错误消息。它说:无法将java.lang.String字段flightmap.service.module.search.jto.AirportSearchJTO.city设置为java.util.HashSet。因此,它尝试将类型为String的字段设置为类型为HashSet的值。这不仅仅涉及到错误的类型,还涉及到与您想象的不同的内容 - 它不是关于获取值,而是关于设置它。因此,堆栈跟踪显示的内容与您想象的不同。

现在检查这一行:在flightmap.service.module.search.SearchServiceTest.shouldReturnProperMergedAirportIfAvailableInBothSources(SearchServiceTest.java:191)

打开您的SearchServiceTest.java类,转到第191行,看看那里有什么。这就是此堆栈跟踪的原因。再次强调:它是设置值,而不是获取它。

英文:

Inspect the error message. It says: Can not set java.lang.String field flightmap.service.module.search.jto.AirportSearchJTO.city to java.util.HashSet. So it tries to set field of type String to value of type HashSet. It is not only about wrong types, but also about something different than you think — it is not about getting the value, but setting it. So the stacktrace shows something different than you think.

Now inspect this line: at flightmap.service.module.search.SearchServiceTest.shouldReturnProperMergedAirportIfAvailableInBothSources(SearchServiceTest.java:191)

Open your SearchServiceTest.java class, go to line 191 and see what's there. That's the reason for this stacktrace. And again: it sets value, not gets it.

答案2

得分: 0

你的searchResultJTO对象拥有以String为键、AirportSearchJTO为值的HashSet。
你需要像这样在循环中访问这些值:

Object airportCollection = searchResultJTO.get("AIRPORT");
Field field = AirportSearchJTO.class.getDeclaredField("city");
field.setAccessible(true);
if (airportCollection instanceof Collection) {
   for(Object airport : (Collection)airportCollection){
      field.get(airport);
   }
}
英文:

Your searchResultJTO object has HashSet of AirportSearchJTO as values and String as keys.
You need to access the values in a loop like this

Object airportCollection = searchResultJTO.get(&quot;AIRPORT&quot;);
Field field = AirportSearchJTO.class.getDeclaredField(&quot;city&quot;);
field.setAccessible(true);
if (airportCollection instanceof Collection) {
   for(Object airport : (Collection)airportCollection){
      field.get(airport);
   }
}  

huangapple
  • 本文由 发表于 2020年8月21日 17:33:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63520273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定