[ANN] AutoBoxingPlugin 0.3.1

Changes:
Added autoboxing of arrays to collections and vice versa.

This is the full list of intentions:

Converts primitive types to their wrapper classes depending on the
current context. For example,

Integer integer = 10; => Integer integer = new Integer(10);
integer = i + 2; => integer = new Integer(i + 2);
Long longer = 100L; => Long longer = new Long(100L);
Double aDouble = Math.PI/2; => Double aDouble = new Double(Math.PI/2);

Converts string objects to wrapper classes. For example,

Integer integer = "rere"; => Integer integer = Integer.valueOf("rere");
Boolean aBoolean = "true"; => Boolean aBoolean =
Boolean.valueOf("true");

Converts string objects to primitive types. For example,

long aLong = "3423"; => long aLong = Long.parseLong("3423");
boolean bool = "true"; => boolean bool =
Boolean.valueOf("true").booleanValue();

Converts collection to array and vice versa. For example,

Integer[] integers = collection; => Integer[] integers =
(Integer[])collection.toArray(new Integer[0]);
Collection collection = new Integer[12]; => Collection collection =
Arrays.asList(new Integer[12]);

Plugin URL: http://www.intellij.org/twiki/bin/view/Main/AutoBoxingPlugin

/kesh

8 comments
Comment actions Permalink

Cool plugin. Now most good ideas for intentions are already taken :D

However, there seems to be a little problem. I occasionally get the exception
below. I too a quick glance at the source but didn't find anything suspicious.
Because it only happens randomly, it might be a race condition inside IDEA.

Sascha


java.lang.NullPointerException
at org.intellij.autoboxing.PsiUtil.isCollectionInterface(PsiUtil.java:164)
at org.intellij.autoboxing.PsiUtil.doesImplementCollection(PsiUtil.java:152)
at org.intellij.autoboxing.PsiUtil.doesImplementCollection(PsiUtil.java:144)
at org.intellij.autoboxing.PsiUtil.findPsiTypeOfExpression(PsiUtil.java:128)
at org.intellij.autoboxing.ToWrapperIntention.isAvailable(ToWrapperIntention.java:17)
at com.intellij.codeInsight.k.a.ce.c(ce.java:44)
at com.intellij.codeInsight.k.a.ce.b(ce.java:96)
at com.intellij.codeInsight.k.a.cn.run(cn.java:3)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)
at com.intellij.ide.t.a(t.java:116)
at com.intellij.ide.t.dispatchEvent(t.java:16)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.tzambalayev.util.ReflectUtil.invoke(ReflectUtil.java:13)
at
org.tzambalayev.ideaplugins.tabswitch.EventQueueDecorator.dispatchEvent(EventQueueDecorator.java:55)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)


0
Comment actions Permalink

Great!!!

what about:

Integer a = new Integer(11);
int b = a; //<-- intention to a.intValue()


0
Comment actions Permalink

This is really strange. Either PsiClass.getInterfaces() returns an array
which has null as one of its members, or PsiClass.getQualifiedName()
returns null. Anyway I fixed it in the version 0.3.3. Can you get it
from the plugin web page and check whether this exception disappeared?

Thanks,
/kesh

Sascha Weinreuter wrote:

Cool plugin. Now most good ideas for intentions are already taken :D

However, there seems to be a little problem. I occasionally get the exception
below. I too a quick glance at the source but didn't find anything suspicious.
Because it only happens randomly, it might be a race condition inside IDEA.

Sascha


java.lang.NullPointerException
at org.intellij.autoboxing.PsiUtil.isCollectionInterface(PsiUtil.java:164)
at org.intellij.autoboxing.PsiUtil.doesImplementCollection(PsiUtil.java:152)
at org.intellij.autoboxing.PsiUtil.doesImplementCollection(PsiUtil.java:144)
at org.intellij.autoboxing.PsiUtil.findPsiTypeOfExpression(PsiUtil.java:128)
at org.intellij.autoboxing.ToWrapperIntention.isAvailable(ToWrapperIntention.java:17)
at com.intellij.codeInsight.k.a.ce.c(ce.java:44)
at com.intellij.codeInsight.k.a.ce.b(ce.java:96)
at com.intellij.codeInsight.k.a.cn.run(cn.java:3)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)
at com.intellij.ide.t.a(t.java:116)
at com.intellij.ide.t.dispatchEvent(t.java:16)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.tzambalayev.util.ReflectUtil.invoke(ReflectUtil.java:13)
at
org.tzambalayev.ideaplugins.tabswitch.EventQueueDecorator.dispatchEvent(EventQueueDecorator.java:55)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)


0
Comment actions Permalink

PsiClass.getQualifiedName() returns null many classes (anonymous, local...)

Friendly,
Dmitry

kesh wrote:

This is really strange. Either PsiClass.getInterfaces() returns an array
which has null as one of its members, or PsiClass.getQualifiedName()
returns null. Anyway I fixed it in the version 0.3.3. Can you get it
from the plugin web page and check whether this exception disappeared?

Thanks,
/kesh

Sascha Weinreuter wrote:

>> Cool plugin. Now most good ideas for intentions are already taken :D
>>
>> However, there seems to be a little problem. I occasionally get the
>> exception below. I too a quick glance at the source but didn't find
>> anything suspicious. Because it only happens randomly, it might be a race
>> condition inside IDEA.
>>
>> Sascha
>>
>>
>> java.lang.NullPointerException
>> at
>>
org.intellij.autoboxing.PsiUtil.isCollectionInterface(PsiUtil.java:164)
>> at
>>
org.intellij.autoboxing.PsiUtil.doesImplementCollection(PsiUtil.java:152)
>> at
>>
org.intellij.autoboxing.PsiUtil.doesImplementCollection(PsiUtil.java:144)
>> at
>>
org.intellij.autoboxing.PsiUtil.findPsiTypeOfExpression(PsiUtil.java:128)
>> at
>>
org.intellij.autoboxing.ToWrapperIntention.isAvailable(ToWrapperIntention.java:17)
>> at com.intellij.codeInsight.k.a.ce.c(ce.java:44) at
>> com.intellij.codeInsight.k.a.ce.b(ce.java:96) at
>> com.intellij.codeInsight.k.a.cn.run(cn.java:3) at
>> java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
>> at java.awt.EventQueue.dispatchEvent(EventQueue.java:448) at
>> com.intellij.ide.t.a(t.java:116) at
>> com.intellij.ide.t.dispatchEvent(t.java:16) at
>> sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source) at
>>
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>> at java.lang.reflect.Method.invoke(Method.java:324) at
>> org.tzambalayev.util.ReflectUtil.invoke(ReflectUtil.java:13) at
>>
org.tzambalayev.ideaplugins.tabswitch.EventQueueDecorator.dispatchEvent(EventQueueDecorator.java:55)
>> at
>>
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)
>> at
>>
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
>> at
>>
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
>> at
>>
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
>> at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
>>
>>

--
Dmitry Lomov
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

I didn't know that. Then it explains NPE.
Thanks.

/kesh

Dmitry Lomov wrote:

PsiClass.getQualifiedName() returns null many classes (anonymous, local...)

Friendly,
Dmitry

kesh wrote:

>>This is really strange. Either PsiClass.getInterfaces() returns an array
>>which has null as one of its members, or PsiClass.getQualifiedName()
>>returns null. Anyway I fixed it in the version 0.3.3. Can you get it
>>from the plugin web page and check whether this exception disappeared?
>>
>>Thanks,
>>/kesh
>>
>>Sascha Weinreuter wrote:
>>
>>>Cool plugin. Now most good ideas for intentions are already taken :D
>>>
>>>However, there seems to be a little problem. I occasionally get the
>>>exception below. I too a quick glance at the source but didn't find
>>>anything suspicious. Because it only happens randomly, it might be a race
>>>condition inside IDEA.
>>>
>>>Sascha
>>>
>>>
>>>java.lang.NullPointerException
>>> at
>>>


org.intellij.autoboxing.PsiUtil.isCollectionInterface(PsiUtil.java:164)

>>> at
>>>


org.intellij.autoboxing.PsiUtil.doesImplementCollection(PsiUtil.java:152)

>>> at
>>>


org.intellij.autoboxing.PsiUtil.doesImplementCollection(PsiUtil.java:144)

>>> at
>>>


org.intellij.autoboxing.PsiUtil.findPsiTypeOfExpression(PsiUtil.java:128)

>>> at
>>>


org.intellij.autoboxing.ToWrapperIntention.isAvailable(ToWrapperIntention.java:17)

>>> at com.intellij.codeInsight.k.a.ce.c(ce.java:44) at
>>> com.intellij.codeInsight.k.a.ce.b(ce.java:96) at
>>> com.intellij.codeInsight.k.a.cn.run(cn.java:3) at
>>> java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
>>> at java.awt.EventQueue.dispatchEvent(EventQueue.java:448) at
>>> com.intellij.ide.t.a(t.java:116) at
>>> com.intellij.ide.t.dispatchEvent(t.java:16) at
>>> sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source) at
>>>


sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

>>> at java.lang.reflect.Method.invoke(Method.java:324) at
>>> org.tzambalayev.util.ReflectUtil.invoke(ReflectUtil.java:13) at
>>>


org.tzambalayev.ideaplugins.tabswitch.EventQueueDecorator.dispatchEvent(EventQueueDecorator.java:55)

>>> at
>>>


java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)

>>> at
>>>


java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)

>>> at
>>>


java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)

>>> at
>>>


java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)

>>> at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
>>>
>>>



0
Comment actions Permalink

I thought about that. It will be available in the next build.

/kesh

Daniel wrote:

Great!!!

what about:

Integer a = new Integer(11);
int b = a; //<-- intention to a.intValue()


0
Comment actions Permalink

Testing...

String s;
s = "";

Why offer to convert either s or "" to string?

Also:

s = object;
s = 10;

Converting object to String.valueOf(object) and 10 to String.valueOf(10)
would be useful (no intention now for this).

Or is convert to string still work in progress?

-



Integer[] integers = (Integer[]) collection.toArray(new Integer[0]);

Suggestion - use collection.size():

Integer[] integers = (Integer[]) collection.toArray(new
Integer[collection.size()]);


Note that this intention duplicates the "toar" live template:

Collection collection = getCollection();
toar (space) (control-alt-v)

is also very simple :)



Also:

Integer[] integers = collection

The intention is unavailable when the cursor is after "collection", having
to type the semi-colon and "go back" to use it is uncomfortable. Don't know
if the api allows detection of this case, as many of the idea supplied
intentions also suffer from this defect.

If the intention is available after "collections" it will be much more
useful and more users will change from using the live template


I have yet to test most of the other cases, but I'm liking this plugin.

Carlos


0
Comment actions Permalink

Carlos Costa e Silva wrote:

Testing...

String s;
s = "";


Yes, I missed that one. It will be fixed in the next build.


Why offer to convert either s or "" to string?

Also:

s = object;
s = 10;

Converting object to String.valueOf(object) and 10 to String.valueOf(10)
would be useful (no intention now for this).

Or is convert to string still work in progress?


I have never thought about these intentions before. I guess, they are
quite useful, so they will be available in the next build as well.


----------

Integer[] integers = (Integer[]) collection.toArray(new Integer[0]);

Suggestion - use collection.size():

Integer[] integers = (Integer[]) collection.toArray(new
Integer[collection.size()]);


Note that this intention duplicates the "toar" live template:

Collection collection = getCollection();
toar (space) (control-alt-v)

is also very simple :)


I implemented this intention for the sake of completeness. Basically you
can use both of them depending on what you prefer. I think that
intentions are more noticeable and I always forget that there is such
active template. I will make your suggestion in the next build.


Also:

Integer[] integers = collection

The intention is unavailable when the cursor is after "collection", having
to type the semi-colon and "go back" to use it is uncomfortable. Don't know
if the api allows detection of this case, as many of the idea supplied
intentions also suffer from this defect.

If the intention is available after "collections" it will be much more
useful and more users will change from using the live template


Yeah, for some cases, it is not that easy to find an appropriate context
in the psi tree . For example, in the case like this _Integer[] integers
= collection|_ the cursor is pointing to the whitespace psi element
which is not part of the declaration statement but it is the next
sibling to it. So the plugin has to go back and search for the actual
context. Anyway, I will try to fix that in the next build.


I have yet to test most of the other cases, but I'm liking this plugin.

Carlos


You did really good testing and thanks for the kind words.
/kesh

0

Please sign in to leave a comment.