public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* how to deal with non-generic (legacy) code ?
@ 2009-08-04 15:09 David Michel
  2009-08-04 15:20 ` Philip A. Chapman
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: David Michel @ 2009-08-04 15:09 UTC (permalink / raw)
  To: java

Hi All,

We all know that generics have been introduced in jdk 1.5 and that
Sun's javac still accepts raw types for backward compatibility.

With gcj, however, one will always get some warnings like "Vector is a
raw type. References to generic type Vector<T> should be
parameterized" when using raw types. It's easy to write the code with
generics properly so that these warnings do not appear, however, how
does one deal with, for instance, a method from a somewhat older
legacy code that returns a raw type.

Similarly, I'm using the java.lang.reflect package to invoke method
from its string name and I wrote this:

    private static Object callMethod(String cname, String mname,
String funcArg){
        try {
            Class cls = Class.forName(cname);
            Class argType[] = new Class[1];
            argType[0] = String.class;
            Method meth = cls.getMethod(mname, argType);
            Object retObj = meth.invoke(cls.newInstance(),funcArg);
            return retObj;
        }
        catch (Exception e){
            System.err.printf("Error: loading of the specified has
failed:\n%s\n",e);
            return null;
        }
    }

and I get these warnings:

warning: Class is a raw type. References to generic type Class<T>
should be parameterized
	Class cls = Class.forName(cname);
	^^^^^
./src/eu/keep/cli/Cli.java:191: warning: Class is a raw type.
References to generic type Class<T> should be parameterized
	Class argType[] = new Class[1];
	^^^^^
./src/eu/keep/cli/Cli.java:193: warning: Type safety: The method
getMethod(String, Class...) belongs to the raw type Class. References
to generic type Class<T> should be parameterized
	Method meth = cls.getMethod(mname, argType);


What is the proper way to deal with this ?
Is there a way to switch these warnings off ?

Cheers
David

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to deal with non-generic (legacy) code ?
  2009-08-04 15:09 how to deal with non-generic (legacy) code ? David Michel
@ 2009-08-04 15:20 ` Philip A. Chapman
  2009-08-04 15:21 ` Bryce McKinlay
  2009-08-04 15:28 ` Andrew John Hughes
  2 siblings, 0 replies; 5+ messages in thread
From: Philip A. Chapman @ 2009-08-04 15:20 UTC (permalink / raw)
  To: David Michel; +Cc: java

You have two options. Either you can use the @SuppressWarnings 
annotation to tell the compiler "yes, I know what I am doing here." or 
you can use the <?> to match any generic type.

@SuppressWarnings("unchecked")
private static Object callMethod(String cname, String mname, String 
funcArg) {
	try {
		Class cls = Class.forName(cname);
		Class argType[] = new Class[1];
		argType[0] = String.class;
		Method meth = cls.getMethod(mname, argType);
		Object retObj = meth.invoke(cls.newInstance(), funcArg);
		return retObj;
	} catch (Exception e) {
		System.err.printf(
				"Error: loading of the specified has failed:\n%s\n", e);
		return null;
	}
}
	
private static Object callMethod(String cname, String mname, String 
funcArg) {
	try {
		Class<?> cls = Class.forName(cname);
		Class<?> argType[] = new Class[1];
		argType[0] = String.class;
		Method meth = cls.getMethod(mname, argType);
		Object retObj = meth.invoke(cls.newInstance(), funcArg);
		return retObj;
	} catch (Exception e) {
		System.err.printf(
				"Error: loading of the specified has failed:\n%s\n", e);
		return null;
	}
}


David Michel wrote:
> Hi All,
> 
> We all know that generics have been introduced in jdk 1.5 and that
> Sun's javac still accepts raw types for backward compatibility.
> 
> With gcj, however, one will always get some warnings like "Vector is a
> raw type. References to generic type Vector<T> should be
> parameterized" when using raw types. It's easy to write the code with
> generics properly so that these warnings do not appear, however, how
> does one deal with, for instance, a method from a somewhat older
> legacy code that returns a raw type.
> 
> Similarly, I'm using the java.lang.reflect package to invoke method
> from its string name and I wrote this:
> 
>     private static Object callMethod(String cname, String mname,
> String funcArg){
>         try {
>             Class cls = Class.forName(cname);
>             Class argType[] = new Class[1];
>             argType[0] = String.class;
>             Method meth = cls.getMethod(mname, argType);
>             Object retObj = meth.invoke(cls.newInstance(),funcArg);
>             return retObj;
>         }
>         catch (Exception e){
>             System.err.printf("Error: loading of the specified has
> failed:\n%s\n",e);
>             return null;
>         }
>     }
> 
> and I get these warnings:
> 
> warning: Class is a raw type. References to generic type Class<T>
> should be parameterized
> 	Class cls = Class.forName(cname);
> 	^^^^^
> ./src/eu/keep/cli/Cli.java:191: warning: Class is a raw type.
> References to generic type Class<T> should be parameterized
> 	Class argType[] = new Class[1];
> 	^^^^^
> ./src/eu/keep/cli/Cli.java:193: warning: Type safety: The method
> getMethod(String, Class...) belongs to the raw type Class. References
> to generic type Class<T> should be parameterized
> 	Method meth = cls.getMethod(mname, argType);
> 
> 
> What is the proper way to deal with this ?
> Is there a way to switch these warnings off ?
> 
> Cheers
> David


-- 
Philip A. Chapman

Desktop, Web Application, and Enterprise Development
Phone: 251-275-6237

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to deal with non-generic (legacy) code ?
  2009-08-04 15:09 how to deal with non-generic (legacy) code ? David Michel
  2009-08-04 15:20 ` Philip A. Chapman
@ 2009-08-04 15:21 ` Bryce McKinlay
  2009-08-04 15:26   ` David Michel
  2009-08-04 15:28 ` Andrew John Hughes
  2 siblings, 1 reply; 5+ messages in thread
From: Bryce McKinlay @ 2009-08-04 15:21 UTC (permalink / raw)
  To: David Michel; +Cc: java

On Tue, Aug 4, 2009 at 4:09 PM, David Michel <dmichel76@googlemail.com> wrote:

> We all know that generics have been introduced in jdk 1.5 and that
> Sun's javac still accepts raw types for backward compatibility.
>
> With gcj, however, one will always get some warnings like "Vector is a
> raw type. References to generic type Vector<T> should be
> parameterized" when using raw types. It's easy to write the code with
> generics properly so that these warnings do not appear, however, how
> does one deal with, for instance, a method from a somewhat older
> legacy code that returns a raw type.
>
> What is the proper way to deal with this ?
> Is there a way to switch these warnings off ?


Put @SuppressWarnings("unchecked") at the top of the method (or class)
that does the unchecked type parameter conversions.
Bryce

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to deal with non-generic (legacy) code ?
  2009-08-04 15:21 ` Bryce McKinlay
@ 2009-08-04 15:26   ` David Michel
  0 siblings, 0 replies; 5+ messages in thread
From: David Michel @ 2009-08-04 15:26 UTC (permalink / raw)
  To: Bryce McKinlay; +Cc: java

Ahhh thanks a lot ! exactly what I was looking for !

Many thanks

David

2009/8/4 Bryce McKinlay <bmckinlay@gmail.com>:
> On Tue, Aug 4, 2009 at 4:09 PM, David Michel <dmichel76@googlemail.com> wrote:
>
>> We all know that generics have been introduced in jdk 1.5 and that
>> Sun's javac still accepts raw types for backward compatibility.
>>
>> With gcj, however, one will always get some warnings like "Vector is a
>> raw type. References to generic type Vector<T> should be
>> parameterized" when using raw types. It's easy to write the code with
>> generics properly so that these warnings do not appear, however, how
>> does one deal with, for instance, a method from a somewhat older
>> legacy code that returns a raw type.
>>
>> What is the proper way to deal with this ?
>> Is there a way to switch these warnings off ?
>
>
> Put @SuppressWarnings("unchecked") at the top of the method (or class)
> that does the unchecked type parameter conversions.
> Bryce
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: how to deal with non-generic (legacy) code ?
  2009-08-04 15:09 how to deal with non-generic (legacy) code ? David Michel
  2009-08-04 15:20 ` Philip A. Chapman
  2009-08-04 15:21 ` Bryce McKinlay
@ 2009-08-04 15:28 ` Andrew John Hughes
  2 siblings, 0 replies; 5+ messages in thread
From: Andrew John Hughes @ 2009-08-04 15:28 UTC (permalink / raw)
  To: David Michel; +Cc: java

2009/8/4 David Michel <dmichel76@googlemail.com>:
> Is there a way to switch these warnings off ?
>

I've been wondering the same.  -w will turn them all off, but I
couldn't see anything in the frontend for mapping gcj -w options to
the ones used by ecj1.  The documentation still refers to the old gcj
parser AFAICS.

> Cheers
> David
>



-- 
Andrew :-)

Free Java Software Engineer
Red Hat, Inc. (http://www.redhat.com)

Support Free Java!
Contribute to GNU Classpath and the OpenJDK
http://www.gnu.org/software/classpath
http://openjdk.java.net

PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint: F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-08-04 15:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-04 15:09 how to deal with non-generic (legacy) code ? David Michel
2009-08-04 15:20 ` Philip A. Chapman
2009-08-04 15:21 ` Bryce McKinlay
2009-08-04 15:26   ` David Michel
2009-08-04 15:28 ` Andrew John Hughes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).