IntelliJ Plugin for Android Parcelable boilerplate code generation.

Overview

IntelliJ/Android Studio Plugin for Android Parcelable boilerplate code generation

This tool generates an Android Parcelable implementation based on fields in the class.

Installation

Plugin is uploaded to plugin repository. If you like, you can install it manually:

  1. Download ParcelableGenerator release
  2. Open IntelliJ/Android Studio
  3. Preferences -> Plugins -> Install plugin from disk....
  4. Choose the downloaded jar file

Usage

Just press ALT + Insert (or your equivalent keybinding for code generation) in your editor and select Parcelable. It allows you to select the fields to be parceled.

Screenshot

Supported parcelable types

  • Types implementing Parcelable
  • Custom support (avoids Serializable/Parcelable implementation) for: Date, Bundle
  • Types implementing Serializable
  • List of Parcelable objects
  • Enumerations
  • Primitive types: long, short, int, float, double, boolean, byte, String
  • Primitive type wrappers (written with Parcel.writeValue(Object)): Short, Integer, Long, Float, Double, Boolean, Byte
  • Primitive type arrays: boolean[], byte[], char[], double[], float[], int[], long[]
  • List type of any object (Warning: validation is not performed)
  • Map support — Note: this implementation always assumes HashMap is desired
  • SparseArray support

TODO

  • Validation of List arguments
  • Display warning about not serialized fields
  • Active Objects support (Binders and stuff)

Contributors

License

Copyright (C) 2015 Michał Charmas (http://blog.charmas.pl)
Copyright (C) 2015 Dallas Gutauckis (http://dallasgutauckis.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0	     

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Generator doesn´t work

    Generator doesn´t work

    Your plugin doesn´t work with my IntelliJ Setup

    8:53:31 PM PluginException: pl/charmas/parcelablegenerator/ParcelableAction : Unsupported major.minor version 51.0 [Plugin: pl.charmas.parcelablegenerator]: pl/charmas/parcelablegenerator/ParcelableAction : Unsupported major.minor version 51.0 [Plugin: pl.charmas.parcelablegenerator]

    I´m using the latest intelliJ: Screen Shot 2013-02-27 at 8 59 13 PM

    opened by falkorichter 8
  • Issues with List of Parcelables

    Issues with List of Parcelables

    I am having issues with List support. I keep getting NullPointers when I try to read my object. Below is the objects involed:

    Root object I am trying to parcel

    public class MyObjectToStore implements Parcelable {
        public List<ObjectInList> objectsInList;
        public String myObjectValue;
    
        public MyObjectToStore() {
        }
    
        private MyObjectToStore(Parcel in) {
            in.readTypedList(objectsInList, ObjectInList.CREATOR);
            this.myObjectValue = in.readString();
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeTypedList(objectsInList);
            dest.writeString(this.myObjectValue);
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        public static final Creator<MyObjectToStore> CREATOR = new Creator<MyObjectToStore>() {
            public MyObjectToStore createFromParcel(Parcel source) {
                return new MyObjectToStore(source);
            }
    
            public MyObjectToStore[] newArray(int size) {
                return new MyObjectToStore[size];
            }
        };
    }
    

    Inner Parcelable in list

    public class ObjectInList implements Parcelable {
        String objectInListValue;
    
        private ObjectInList(Parcel in) {
            this.objectInListValue = in.readString();
        }
    
        public ObjectInList(String objectInListValue) {
            this.objectInListValue = objectInListValue;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.objectInListValue);
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        public static final Creator<ObjectInList> CREATOR = new Creator<ObjectInList>() {
            public ObjectInList createFromParcel(Parcel source) {
                return new ObjectInList(source);
            }
    
            public ObjectInList[] newArray(int size) {
                return new ObjectInList[size];
            }
        };
    }
    

    Test that shows Exception

    public class MyObjectToStoreTest extends TestCase {
    
        public void testParcelable() {
            MyObjectToStore objectToStore = new MyObjectToStore();
            objectToStore.myObjectValue = "MyObjectValue";
            List<ObjectInList> list = new ArrayList<ObjectInList>();
            list.add(new ObjectInList("Object1"));
            list.add(new ObjectInList("Object2"));
            objectToStore.objectsInList = list;
    
            Parcel parcel = Parcel.obtain();
            objectToStore.writeToParcel(parcel, 0);
            parcel.setDataPosition(0);
    
            MyObjectToStore fromParcel = MyObjectToStore.CREATOR.createFromParcel(parcel);
            assertEquals("MyObjectValue", fromParcel.myObjectValue);
            assertEquals(2, fromParcel.objectsInList.size());
            assertEquals("Object1", fromParcel.objectsInList.get(0).objectInListValue);
            assertEquals("Object2", fromParcel.objectsInList.get(1).objectInListValue);
        }
    }
    

    Exception

    java.lang.NullPointerException
    at android.os.Parcel.readTypedList(Parcel.java:1797)
    at objects.MyObjectToStore.<init>(MyObjectToStore.java:27)
    at objects.MyObjectToStore.<init>(MyObjectToStore.java:8)
    at objects.MyObjectToStore$1.createFromParcel(MyObjectToStore.java:33)
    at objects.MyObjectToStore$1.createFromParcel(MyObjectToStore.java:31)
    at objects.MyObjectToStoreTest.testParcelable(MyObjectToStoreTest.java:24)
    

    It seems to blow up because the list in MyObjectToStore is not being initialized properly by the generated code in private MyObjectToStore(Parcel in). Shouldn't that constructor generate something like

    private MyObjectToStore(Parcel in) {
        objectsInList = new ArrayList<ObjectInList>(); // new
        in.readTypedList(objectsInList, ObjectInList.CREATOR);
        this.myObjectValue = in.readString();
    }
    

    Am I missing something? Is this an unimplemented feature, a bug, or a compromise because the code generator doesn't know what Type of list it is?

    opened by lance0428 5
  • NullPointerException: null

    NullPointerException: null

    I got this

    null
    java.lang.NullPointerException
        at pl.charmas.parcelablegenerator.util.PsiUtils.isOfType(PsiUtils.java:42)
        at pl.charmas.parcelablegenerator.typeserializers.ParcelableSerializerFactory.getSerializer(ParcelableSerializerFactory.java:35)
        at pl.charmas.parcelablegenerator.typeserializers.ChainSerializerFactory.getSerializer(ChainSerializerFactory.java:35)
        at pl.charmas.parcelablegenerator.CodeGenerator.getSerializerForType(CodeGenerator.java:131)
        at pl.charmas.parcelablegenerator.CodeGenerator.generateWriteToParcel(CodeGenerator.java:107)
        at pl.charmas.parcelablegenerator.CodeGenerator.generate(CodeGenerator.java:146)
        at pl.charmas.parcelablegenerator.ParcelableAction$1.run(ParcelableAction.java:50)
        at com.intellij.openapi.command.WriteCommandAction$Simple.run(WriteCommandAction.java:166)
        at com.intellij.openapi.application.RunResult.run(RunResult.java:35)
        at com.intellij.openapi.command.WriteCommandAction$2$1.run(WriteCommandAction.java:114)
        at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1010)
        at com.intellij.openapi.command.WriteCommandAction$2.run(WriteCommandAction.java:111)
        at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:124)
        at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:99)
        at com.intellij.openapi.command.WriteCommandAction.performWriteCommandAction(WriteCommandAction.java:108)
        at com.intellij.openapi.command.WriteCommandAction.execute(WriteCommandAction.java:80)
        at pl.charmas.parcelablegenerator.ParcelableAction.generateParcelable(ParcelableAction.java:47)
        at pl.charmas.parcelablegenerator.ParcelableAction.actionPerformed(ParcelableAction.java:42)
        at com.intellij.ui.popup.PopupFactoryImpl$ActionPopupStep.performAction(PopupFactoryImpl.java:856)
        at com.intellij.ui.popup.PopupFactoryImpl$ActionPopupStep$1.run(PopupFactoryImpl.java:842)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:866)
        at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:654)
        at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:381)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    

    when i try use plugin for my class

    public abstract class SimpleAdapter<T> extends BaseAdapter implements Parcelable {
        public List<T> items;
        public Context context;
    
        public SimpleAdapter(List<T> items, Context context) {
            this.items = items;
            this.context = context;
        }
    
        @Override
        public int getCount() {
            return items.size();
        }
    
        @Override
        public Object getItem(int position) {
            return items.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        public View generateView(int viewId, ViewGroup parent){
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(viewId, parent, false);
    
            return view;
        }
    
    
    }
    

    Where I mistake?

    opened by NickRimmer 4
  • Checkstyle issue

    Checkstyle issue

    Hello

    i'm checking my project with checkstyle to find code related issues. It is warning me about this

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public ComplaintData createFromParcel( Parcel source ) {return new ComplaintData(source);} public ComplaintData[] newArray( int size ) {return new ComplaintData[size];} };

    with the following messages:

    'return' is not preceded with whitespace. '{' is not followed by whitespace. ';' is not followed by whitespace. '}' is not preceded with whitespace.

    Is it possible to fix this issues in the plugin ?

    Thanks a lot

    opened by mgursch 3
  • Add support for Short reference data type

    Add support for Short reference data type

    Just discovered that Short as a reference data type is not supported in this plugin.

    I don't know what to do with primitive short. Parcel doesn't have a method for primitive short. There are three ways I can think of right now.

    • Do a widening conversion to int? Kind of wasteful and there might not be compiler errors if data type gets changed.
    • Autobox to Short?
    • Just treat it as a Serializable?

    I don't know. But this PR should be good for Short.

    opened by aaronweihe 3
  • Can't generate Parcelable for fields in child classes

    Can't generate Parcelable for fields in child classes

    After upgraded to latest version 0.6.3, it seems I can no longer generate Parcelable in child classes regardless of what accessors those fields have.

    For example:

    public class Person {
        String name;
    }
    
    public class Employee extends Person {
        String company;
    }
    

    The Employee class can only generate Parcelable for its own fields, company but not name. It might make sense. But what if the parent class doesn't implement Parcelable at all and I only want child classes to implement Parcelable?

    Is this a design change?

    opened by aaronweihe 3
  • Issue with List<String>

    Issue with List

    In release 0.6.1 generation for fields like private List<String> names; results in something like this:

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeList(this.names);
        }
    
        private Contact(Parcel in) {
            this.names = new ArrayList<List<String>>();
            in.readList(this.names, List<String>.class.getClassLoader());
        }
    

    Which, obviously, does not compile. :) I suggest that for lists of strings you should use writeStringList() and readStringList(), for example:

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeStringList(this.names);
        }
    
        private Contact(Parcel in) {
            this.names = new ArrayList<String>();
            in.readStringList(this.names);
        }
    
    opened by dmitrylavrikov 3
  • Make CREATOR final to prevent it obfuscation

    Make CREATOR final to prevent it obfuscation

    Default proguard config don't obfuscate only public static final CREATOR fields. See file %ANDROID_HOME%/tools/proguard/proguard-android.txt:

    -keep class * implements android.os.Parcelable {
      public static final android.os.Parcelable$Creator *;
    }
    
    opened by danikula 3
  • Updated generator dialog to allow including fields from subclasses

    Updated generator dialog to allow including fields from subclasses

    Reason for change:

    • I was building a library that required a set of pure-java value objects (sans android stuff), and an add-on android library that extended the main library that added Parcelable support.
    • This PR also resolves https://github.com/mcharmas/android-parcelable-intellij-plugin/issues/35 and the cases described there.

    Details:

    • Added option to "Include fields from subclasses". screenshot
    • Option uses psiClass.getAllFields() if checked, and psiClass.getFields() (as before) when unchecked/deselected.
    • Option is unchecked by default.
    • Option only appears if there are additional fields in subclasses.
    • Also did some general refactoring of GenerateDialog.

    Tested:

    • Tested with large classes (Hundreds of fields).
    • Tested with deeply nested classes.
    opened by brentwatson 2
  • Add SuppressWarning annotation for non-param constructor

    Add SuppressWarning annotation for non-param constructor

    Thank you very much for this nice plugin! In my opinion, the non param constructor for the generated class is necessary but the compiler maybe not able to detect this, so could we add SuppressWarning("unused") for the constructor? Thanks again for reading this issue.

    opened by Muyangmin 2
  • Should describeContents return hashCode instead of 0?

    Should describeContents return hashCode instead of 0?

    Hello all,

    I am sure there is a reason for this, but describeContents returns 0, why not return hashCode() instead?

    Neither implementation is that helpful, but I would say hashCode() is better than 0.

    What do you guys think?

    opened by octohub 2
  • Request: support Kotlin too

    Request: support Kotlin too

    Maybe use the "Parcelable support" of "Android Extensions plugin" :

    https://blog.jetbrains.com/kotlin/2017/08/kotlin-1-1-4-is-out/

    It could be great. Your plugin could just use it, and work as a nice UI to generate its code, choosing which fields to be stored&restored, and which not, and in which order, etc...

    An example of usage, using this feature:

    Gradle file :

    apply plugin: 'org.jetbrains.kotlin.android.extensions' androidExtensions { experimental = true }

    sample code of a class with 3 Dates in it:

    @SuppressLint("ParcelCreator")
    @Parcelize
    class MonthItem(val today: Date, val startDate: Date, val endDate: Date) : Parcelable {
    }
    
    opened by AndroidDeveloperLB 0
  • Error writing a list of Parcelable objects with subclasses

    Error writing a list of Parcelable objects with subclasses

    When I need to save a list of parcelable objects whose type has been subclassed, it doesn't save the members of the subclass.

    class A {
      ArrayList<B> list;
      ...
    }
    
    class B {
      ...
    }
    
    class C extends B {
      // members in addition to members of class B that should be saved when saving list
    }
    

    This causes a difficult to trace RunTimeException when unparcelling the list. The RTE is something like

    java.lang.RuntimeException: Parcel [email protected]: Unmarshalling unknown type code YYY at offset ZZZ}

    This happens because of the writeTypedArrayList method. I fixed the error by replacing this method with writeList and the readTypedArrayList method with readList as well.

    Here is a larger writeup done by someone who experienced this same scenario without the plugin https://medium.com/@kcoppock/writing-parcelable-lists-with-inheritance-a739c42c055c

    opened by jtarnoff 0
Releases(v0.7.0)
Owner
Michał Charmas
Michał Charmas
IntelliJ Idea Astor Plugin is a plugin that integrates Astor in Intellij Idea

IntelliJ Idea Astor Plugin IntelliJ Idea Astor Plugin is a plugin that integrates Astor in Intellij Idea. It communicates with a local/remote program

null 4 Aug 28, 2021
Intellij-platform-plugin-template - IntelliJ Platform Plugin Template

IntelliJ Platform Plugin Template TL;DR: Click the Use this template button and

null 0 Jan 1, 2022
Kirill Rakhman 4 Sep 15, 2022
eventbus-intellij-plugin 3.8 0.0 L1 Java Plugin to navigate between events posted by EventBus.

eventbus-intellij-plugin Plugin to navigate between events posted by EventBus. Post to onEvent and onEvent to Post Install There are two ways. Prefere

Shinnosuke Kugimiya 315 Aug 8, 2022
IntelliJ plugin that provides a modern and powerful byte code analyzer tool window.

IntelliJ Byte Code Analyzer Plugin This IntelliJ plugin provides a modern and powerful byte code analyzer tool window. Its supports Java, Kotlin, Groo

Marcel Kliemannel 27 Aug 5, 2022
Intellij Idea Plugin that can convert HTML to Compose for Web code.

HtmlToComposeWebConverter Intellij Idea Plugin that can convert HTML to Compose for Web code. Turn this: Into this: Show some ❤️ and star the repo to

Jens Klingenberg 88 Aug 18, 2022
An IntelliJ IDEA plugin is used to inspire you to write code.

InspireWritingPlugin An IntelliJ IDEA plugin is used to inspire you to write code. Whenever you write code that exceeds the specified character, the p

Airsaid 6 Feb 11, 2021
A plugin for Android Studio and Intellij IDEA that speeds up your day to day android development.

ADB Idea A plugin for Android Studio and Intellij IDEA that speeds up your day to day android development. The following commands are provided: Uninst

Philippe Breault 2k Oct 3, 2022
IntelliJ / Android Studio plugin for Android Holo Colors

This project is not maintained anymore. Holo Colors doesn't make sense since the introduction of Material Design and the ability to set the primary co

Jérôme Van Der Linden 644 Jul 14, 2022
Android Studio & IntelliJ Plugin for sort xml by name="xxx".

AndroidXmlSorter Android Studio & IntelliJ Plugin for sort xml by name="xxx". Options Insert space between difference prefix ('Snake Case', 'Camel Cas

Kaoru Tsutsumishita 103 Sep 22, 2022
IntelliJ plugin that provides some useful utilities to support the daily work with Gradle.

IntelliJ Gradle Utilities Plugin This IntelliJ plugin provides some useful utilities to support the daily work with Gradle. It's available on the offi

Marcel Kliemannel 6 Jul 29, 2022
IntelliJ-based IDEs Protobuf Language Plugin that provides Protobuf language support.

IntelliJ Protobuf Language Plugin Reference Inspired by protobuf-jetbrains-plugin and intellij-protobuf-editor. Descriptor IntelliJ-based IDEs Protobu

Kanro 69 Sep 8, 2022
Plugin for IntelliJ-based IDEs folding root files in the ProjectView

Foldable ProjectView The Foldable ProjectView is a plugin for the IntelliJ-based IDEs that lets you fold files located in the root of your project. Av

Jakub Chrzanowski 39 Sep 10, 2022
✈️ IDE plugin for the IntelliJ platform which adds GitHub Copilot support. (VERY WIP)

JetBrains Copilot GitHub Copilot support for the IntellIJ Platform. Installation Download the latest release. Select the Install Plugin from Disk opti

Koding 157 May 27, 2022
A plugin that adds support for rs2asm files to IntelliJ.

Rs2Asm This plugin adds some simple features when interacting with rs2asm files. Features Syntax highlighting Autocompletion for label names and instr

Joshua Filby 3 Dec 16, 2021
IntelliJ Platform Plugin Template

IntelliJ Platform Plugin Template is a repository that provides a pure template to make it easier to create a new plugin project (check the Creating a repository from a template article).

null 0 Nov 8, 2021
IntelliJ IDEA / PhpStorm InertiaJS Plugin

PhpStorm and IntelliJ IDEA Inertia.js Plugin Provides support in PhpStorm and IntelliJ IDEA Ultimate for Inertia.js. ?? GitHub Issues: feature request

Matthew Hailwood 13 Sep 16, 2022
A playground to development intellij plugin

pluginExporlor Template ToDo list Create a new IntelliJ Platform Plugin Template project. Get familiar with the template documentation. Verify the plu

Woody Hu 0 Nov 23, 2021
Developer ToolBox IntelliJ Plugin

developer-toolbox Template ToDo list Create a new IntelliJ Platform Plugin Template project. Get familiar with the template documentation. Verify the

canarin 0 Dec 11, 2021