Unnecessary type casts

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Unnecessary type casts

Ludwig Magnusson

Hello!

Is it possible to use checkstyle to detect unnecessary type casts in java?

Let me show two examples:

 

Ex 1:

String helloString = (String) getHelloString();

 

public String getHelloString() {

  return “hello”;

}

 

--

 

Ex 2:

List aList = (List) getNewList();

 

public ArrayList getNewList() {

  return new ArrayList();

}

 

As you can see the casts (String) and (List) are not necessary.

 

I need this for a project going from java 1.4 to 1.5 where typecasts are no longer necessary as generics become available.

/Ludwig


------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires
February 28th, so secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user
Reply | Threaded
Open this post in threaded view
|

Re: Unnecessary type casts

Ludwig Magnusson

Never mind, I realized this could be done by changing the compiler settings.

 

From: Ludwig Magnusson [mailto:[hidden email]]
Sent: den 2 februari 2011 16:11
To: '[hidden email]'
Subject: Unnecessary type casts

 

Hello!

Is it possible to use checkstyle to detect unnecessary type casts in java?

Let me show two examples:

 

Ex 1:

String helloString = (String) getHelloString();

 

public String getHelloString() {

  return “hello”;

}

 

--

 

Ex 2:

List aList = (List) getNewList();

 

public ArrayList getNewList() {

  return new ArrayList();

}

 

As you can see the casts (String) and (List) are not necessary.

 

I need this for a project going from java 1.4 to 1.5 where typecasts are no longer necessary as generics become available.

/Ludwig


------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires
February 28th, so secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsight-sfd2d
_______________________________________________
Checkstyle-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/checkstyle-user