Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No ability to suppress reflection required messages #45

Closed
bbqsrc opened this issue Oct 14, 2014 · 20 comments
Closed

No ability to suppress reflection required messages #45

bbqsrc opened this issue Oct 14, 2014 · 20 comments

Comments

@bbqsrc
Copy link

bbqsrc commented Oct 14, 2014

Warning:(25, 24) Parceler: Reflection is required to modify private field: String secret, consider using non-private.
Warning:(25, 24) Parceler: Reflection is required to access private field: String secret, consider using non-private.

I am already using transient on static fields, but I am using @Parcel on a class that I am also using with GSON for serialisation, so I can't add transient to the private fields as that has a different meaning in GSON (it will not serialise the field).

I am using the private access level by design, so I want to be able to suppress this warning. Could you advise me how to do this?

@johncarl81
Copy link
Owner

A couple of things come to mind:

First, if you want Parceler only to ignore certain fields you can use the Parceler specific @Transient annotation in leu of the transient keyword.

Second, not sure if you want this, but if you have getters/setters for your bean's properties, you can use either the Serialization.METHOD technique or annotate your specific getter/setter methods with @ParcelProperty("secret") to tell Parceler to use those instead of the private field.

Other than that I could see about adding a @SupressWarnings('parceler') annotation, but that doesn't exist yet... unless there is a global way to supress warnings with that annotation.

@bbqsrc
Copy link
Author

bbqsrc commented Oct 20, 2014

A @SuppressWarnings annotation would be excellent. If I have to use @Transient, I'll end up having to enter it about 40 times or more in this codebase, and it makes it rather cluttered and makes reasoning about the code significantly harder than I've already made it making GSON and Parceler play nice together. 😄

@johncarl81
Copy link
Owner

I'm a little curious about your gson/parceler model, can you share an example of what you are needing from gson/parcler, particularly around your private data?

To reiterate @Transient, that annotation tells Parceler to ignore that field for serialization. The result will be that the field is not persisted to a Parcelable or read back from it. A side effect is that Parceler will also not warn you about private accesses.

@bbqsrc
Copy link
Author

bbqsrc commented Oct 20, 2014

I misunderstood what @Transient meant then, because as you've just
described it, it won't work for my case anyway.

I want to be able to suppress the warning about reflection because I've
chosen to use private by design, and accept that reflection is slower but
isn't an issue in my use case.

I could potentially use package level access in my use case but I'd rather
not entice someone to directly access the property if it can be avoided. :-)
On Oct 21, 2014 2:32 AM, "John Ericksen" notifications@github.com wrote:

I'm a little curious about your gson/parceler model, can you share an
example of what you are needing from gson/parcler, particularly around your
private data?

To reiterate @transient, that annotation tells Parceler to ignore that
field for serialization. The result will be that the field is not persisted
to a Parcelable or read back from it. A side effect is that Parceler will
also not warn you about private accesses.


Reply to this email directly or view it on GitHub
#45 (comment).

@johncarl81
Copy link
Owner

Ok, for these private fields, do you have non-private getters and/or setters?

@bbqsrc
Copy link
Author

bbqsrc commented Oct 21, 2014

Hmm, yes, and I think I see what you're getting at.

I think my confusion comes from the fact that I have some static classes within a class that are only meant to be used within the context of that outer class, and as such have private access, and no getters or setters.

Unless I am understanding incorrectly, applying @Parcel(Serialization.METHOD) to those inner classes, even though they have no getters or setters, should be enough to solve my warnings, and presumably also optimise the parceling?

Also, thank you for patiently talking through this with me. 😄

Here's a gist of one of the classes that was spitting errors, but isn't now that I've applied the annotations as suggested. I'm yet to test to see what happens.

@bbqsrc
Copy link
Author

bbqsrc commented Oct 21, 2014

I've since had time to test the implications of Serialization.METHOD without adding any other attributes, and it doesn't work for me unfortunately. 😞

Once added to an intent with .putExtra(Parcels.wrap(foo)), .getMode() returns null when the service receives it from startService.

Perhaps it is because there are only getters and no setters? The objects are meant to be immutable you see, hence the builder pattern demonstrated in the gist.

@johncarl81
Copy link
Owner

Thought I'd share that you can mute annotaion processor (Parceler) warnings via -Xlint:-processing.
http://stackoverflow.com/questions/26410667/suppresswarnings-from-a-java-annotation-processor

@johncarl81
Copy link
Owner

@bbqsrc, I think I'm not going to move forward with the @SuppressWarnings feature since there is a feature to suppress warnings from an annotation processor.

As always, let me know if you need anything else.

@IbrahimDisouki
Copy link

Please provide an example for how i can use @ParcelProperty(String value) when i have private filed

@johncarl81
Copy link
Owner

@ibrahim132:

@Parcel
public class PrivateFieldExample{
    @ParcelProperty("value")
    private String exampleValue;
}

@IbrahimDisouki
Copy link

Thank you for speed response, But i'm still have warnings.
My class:
@parcel
public class AboutUs {

@SerializedName(APIConstants.ABOUT_US_AR)
@ParcelProperty(APIConstants.ABOUT_US_AR)
private String aboutUsAr;

@SerializedName(APIConstants.ABOUT_US_EN)
@ParcelProperty(APIConstants.ABOUT_US_EN)
private String aboutUsEn;

public AboutUs() {
}

public String getAboutUsAr() {
    return aboutUsAr;
}

public void setAboutUsAr(String aboutUsAr) {
    this.aboutUsAr = aboutUsAr;
}

public String getAboutUsEn() {
    return aboutUsEn;
}

public void setAboutUsEn(String aboutUsEn) {
    this.aboutUsEn = aboutUsEn;
}

public String getAboutUs() {
    return AppSettings.getInstance(EBIApplication.getInstance())
            .getCurrentLanguage()
            .equals(AppConstants.ARABIC_LANGUAGE_CODE) ?
            getAboutUsAr() : getAboutUsEn();

}

}

Warning:(22, 20) Parceler: Reflection is required to modify private field: String aboutUsAr, consider using non-private.
Warning:(26, 20) Parceler: Reflection is required to modify private field: String aboutUsEn, consider using non-private.
Warning:(22, 20) Parceler: Reflection is required to access private field: String aboutUsAr, consider using non-private.
Warning:(26, 20) Parceler: Reflection is required to access private field: String aboutUsEn, consider using non-private.

@johncarl81
Copy link
Owner

Oh, that's not what you were asking...

I'd seriously consider making your fields non-private as this will significantly slow down marshaling. If you can't and you find the warnings annoying, you could add the -Xlint:-processing option as suggested above.

@IbrahimDisouki
Copy link

Sorry, i thought that was for warnings.
I will try to use the Xlint:-processing
Thank you.

@Aks-4125
Copy link

Aks-4125 commented Dec 6, 2017

@johncarl81 I am still not clear about how to ignore this warning. I tried @SuppressWarnings("parceler") but still getting warning. I am using parceler version 1.1.9
even ParcelProperty("isChecked") is still giving me warning.

private boolean isChecked;

Warning:(14, 21) Parceler: Reflection is required to access private field: boolean isChecked, consider using non-private.

@Aks-4125
Copy link

Aks-4125 commented Apr 9, 2018

@ibrahim132 try this.

@Parcel(Parcel.Serialization.BEAN)

There won't be any warnings.

@aniketmane
Copy link

How to avoid warnings for Singleton classes were the constructor is private. Please help.

@johncarl81
Copy link
Owner

You could use a factory method, I guess.

@johncarl81
Copy link
Owner

FYI, Serializing and deserializing a "singleton" will mean it has a copy, and thus not a Singleton anymore.

@aniketmane
Copy link

Yeah, I was just looking a way to avoid the warnings. Thanks, @johncarl81.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants