• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can you @SuppressWarnings("unchecked") on a block or something else smaller than a function?

 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a personal dislike of warnings, I think code should compile cleanly. But there are cases when you just have to do things that cause the compiler to whine. Rather than using a @SuppressWarnings("unchecked") to the whole routine, can I just do it for a block?

@SuppressWarnings("unchecked") {
NiceObject no = (NiceObject) niceObject.newInstance();
}

It seems silly to create a separate function with one or two lines just to turn off the warning.

Ideas?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pat,
I don't know about a block, but you can do so on a line. That's smaller than a function

For example:
 
Pat Farrell
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I was going to say "not for me" but its more complicated than that. I am using something weird. Its the Sun JDK 6.1 and Netbeans 6.7

when I try

ArrayList<Person> oldPerList = null;
@SuppressWarnings("unchecked")
oldPerList = (ArrayList<Person>) obj;

I get a compiler error:
/home/pfarrell/sandbox/bbook/trunk/fnfapp/src/java/com/fnfbook/bean/connection/PersonAdd.java:44: <identifier> expected

Yet this, which is functionally identical:

@SuppressWarnings("unchecked")
ArrayList<Person> oldPerList = oldPerlist = (ArrayList<Person>) obj;

works fine.

Thanks
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right - you can't annotate an arbitrary line of code. But you can annotate a declaration - even a declaration of a local variable.

Like you, I prefer to minimize the scope of @SuppressWarnings, so I usually try to either move the offending code to an existing declaration, or create a new method just for the offending code. For example here's a catch-all for most any casting you may want to do, if you want to completely disregard compile-time checking for that cast:

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Right - you can't annotate an arbitrary line of code. But you can annotate a declaration - even a declaration of a local variable.

Like you, I prefer to minimize the scope of @SuppressWarnings, so I usually try to either move the offending code to an existing declaration, or create a new method just for the offending code. For example here's a catch-all for most any casting you may want to do, if you want to completely disregard compile-time checking for that cast:


If you still don't want to suppress warnings for the entire method, a temporary variable will help you out:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic