简体   繁体   中英

How to get field from Object

I have a class:

public class resultJTO extends HashMap<String, Object> {

    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("AIRPORT");

I was trying to do it by this way:

Object airport = resultJTO.get("AIRPORT");
        Field field = searchJTO.class.getDeclaredField("city");
        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?

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.

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("AIRPORT");
Field field = AirportSearchJTO.class.getDeclaredField("city");
field.setAccessible(true);
if (airportCollection instanceof Collection) {
   for(Object airport : (Collection)airportCollection){
      field.get(airport);
   }
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM