From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11037 invoked by alias); 4 Aug 2009 15:20:48 -0000 Received: (qmail 11026 invoked by uid 22791); 4 Aug 2009 15:20:48 -0000 X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=BAYES_00 X-Spam-Check-By: sourceware.org Received: from mail-yw0-f178.google.com (HELO mail-yw0-f178.google.com) (209.85.211.178) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 04 Aug 2009 15:20:41 +0000 Received: by ywh8 with SMTP id 8so4989675ywh.8 for ; Tue, 04 Aug 2009 08:20:39 -0700 (PDT) Received: by 10.90.65.16 with SMTP id n16mr2989858aga.99.1249399238997; Tue, 04 Aug 2009 08:20:38 -0700 (PDT) Received: from ?192.168.50.100? (user-24-214-18-247.knology.net [24.214.18.247]) by mx.google.com with ESMTPS id 6sm32332agb.68.2009.08.04.08.20.37 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 04 Aug 2009 08:20:38 -0700 (PDT) Message-ID: <4A7851C4.9000500@pcsw.us> Date: Tue, 04 Aug 2009 15:20:00 -0000 From: "Philip A. Chapman" User-Agent: Thunderbird 2.0.0.22 (X11/20090608) MIME-Version: 1.0 To: David Michel CC: java@gcc.gnu.org Subject: Re: how to deal with non-generic (legacy) code ? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact java-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: java-owner@gcc.gnu.org X-SW-Source: 2009-08/txt/msg00004.txt.bz2 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 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 > 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 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 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