[NSWI080] Fwd: Task 2 - Java - Accessing (non existing) optional field
Petr Tuma
petr.tuma at d3s.mff.cuni.cz
Mon Apr 4 08:50:28 CEST 2022
Hi,
the best place to look are the generated files. With Thrift, they are fairly readable. For example, for this structure definition:
struct Test {
1: optional i32 anInt;
2: optional string aString;
}
You can see this in the generated Test.java class:
public int getAnInt() {
return this.anInt;
}
public Test setAnInt(int anInt) {
this.anInt = anInt;
setAnIntIsSet(true);
return this;
}
public void unsetAnInt() {
__isset_bitfield = org.apache.thrift.EncodingUtils.clearBit(__isset_bitfield, __ANINT_ISSET_I
}
/** Returns true if field anInt is set (has been assigned a value) and false otherwise */
public boolean isSetAnInt() {
return org.apache.thrift.EncodingUtils.testBit(__isset_bitfield, __ANINT_ISSET_ID);
}
...
/** Returns true if field aString is set (has been assigned a value) and false otherwise */
public boolean isSetAString() {
return this.aString != null;
}
...
Petr
On 03/04/2022 19:24, Matúš Maďar wrote:
>
>
> ---------- Forwarded message ---------
> From: *Matúš Maďar* <semicco2 at gmail.com <mailto:semicco2 at gmail.com>>
> Date: Sun, Apr 3, 2022 at 7:23 PM
> Subject: Re: [NSWI080] Task 2 - Java - Accessing (non existing) optional field
> To: Petr Tuma <petr.tuma at d3s.mff.cuni.cz <mailto:petr.tuma at d3s.mff.cuni.cz>>
>
>
> How to use isSet()? I can't find any static method like this. The only Java method isSet() I can find (on Google) is one in java.util.Calendar class. I can't find anything like isSet() similar to PHP.
> Thanks
>
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free. www.avast.com <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
>
> <#m_-7765279413696333517_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> On Sun, Apr 3, 2022 at 6:41 PM Petr Tuma <petr.tuma at d3s.mff.cuni.cz <mailto:petr.tuma at d3s.mff.cuni.cz>> wrote:
>
> Hi,
>
> you should first check whether the field is set with `isSet`. Calling the getter without first checking for presence returns `0` for integers, `0.0` for floats, `null` for strings etc.
>
> Petr
>
>
> On 03/04/2022 01:02, Matúš Maďar wrote:
> > Hello all.
> > I haven't found anything in documentation so I'd like to ask. Did anyone encounter and can explain, what is the expected behaviour in Java, when you try to access optional field in service? E.g. if ItemA doesn't have fieldZ defined; will it throw some exception or will it just return default value of the type of field?
> > Thanks!
> >
> > _______________________________________________
> > NSWI080 mailing list
> > NSWI080 at d3s.mff.cuni.cz <mailto:NSWI080 at d3s.mff.cuni.cz>
> > https://d3s.mff.cuni.cz/mailman/listinfo/nswi080 <https://d3s.mff.cuni.cz/mailman/listinfo/nswi080>
>
>
> _______________________________________________
> NSWI080 mailing list
> NSWI080 at d3s.mff.cuni.cz
> https://d3s.mff.cuni.cz/mailman/listinfo/nswi080
More information about the NSWI080
mailing list