Re: how to deal with non-generic (legacy) code ?

"Philip A. Chapman" <[email protected]>
Newsgroups gmane.comp.gcc.java.devel
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.