Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 81104 invoked from network); 2 May 2002 18:17:11 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 2 May 2002 18:17:11 -0000 Received: (qmail 19907 invoked by uid 97); 2 May 2002 18:16:55 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 19810 invoked by alias); 2 May 2002 18:16:54 -0000 Delivered-To: jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 19667 invoked by uid 97); 2 May 2002 18:16:53 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 19598 invoked by uid 98); 2 May 2002 18:16:51 -0000 X-Antivirus: nagoya (v4198 created Apr 24 2002) X-Qmail-Scanner-Mail-From: baliuka@centras.lt via jim.skynet.vl X-Qmail-Scanner: 1.03 (Clean. Processed in 1.354786 secs) Message-ID: <000701c1f205$7e931050$0111010a@user> From: "Juozas Baliuka" To: "Jakarta Commons Developers List" References: <336399CD.5A5A1433.002160F5@netscape.net> Subject: Re: Re: ClassCastException raised in newFactory() method when starting from JUnit test Date: Thu, 2 May 2002 20:16:35 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Hi, "Class.forName(String name)" Documentation about "currentClassLoader" and "Thread" and this sometimes confuses : 1)" Class.forName(className, true, currentLoader) where currentLoader denotes the defining class loader of the current class. " This is about "currentLoader" 2)" Class t = Class.forName("java.lang.Thread") " This is nothing about Thread.currentThread().getContextClassLoader(), it is not very good example in documentation. Generaly: Thread.currentThread().getContextClassLoader() != "currentClassLoader" and "currentClassLoader" != ClassLoader.getSystemClassLoader(); "ContextClassLoader" can be set by application or container. "currentClassLoader" is callers classloader, "current class" is class on top of thread stack. It is not good idea in common code to use "ContextClassLoader", "currentClassLoader" or "SystemClassLoader". I think the best idea to add method with "classLoader" paramater, and to have "sortcut" for "defaut" ClassLoader, "default" can be any of them, it because all of them are "good". Java Class loading mechanism is very trivial: ( class1.getClassLoader().equals(class2.getClassLoader() ) and class1.getName().equals(class2.getName()) ) <==> ( class1.getClassLoader() == class2.getClassLoader() and class1.getName().equals(class2.getName()) )<==> ( class1 == class2 ) ==> ( class1.getName() == class2.getName() ) Class visibility for classloader depends on "protected Class findClass(String name)" implementation. If 1:"ContextClassLoader" and 2:"currentClassLoader" are not the same instances it is very possible some classes with the same name are visible for 1 but not for 2, and some can have the same name but not the same type. ----- Original Message ----- From: "Rice Yeh" To: ""Jakarta Commons Developers List"" Sent: Thursday, May 02, 2002 7:18 PM Subject: RE: Re: ClassCastException raised in newFactory() method when starting from JUnit test > Hi, > newFactory() use thread context classloader to load class. It is like > > clazz = classLoader.loadClass(factoryClass); > return (LogFactory) clazz.newInstance(); > > where classLoader is thread context loader. I am confused with java's classloader's mechanism. Does this problem have anything to do with namespace? It should be no. Isn't? > Another mail said this problem is just like anothe problem rasing ClassNotFound Exception. Could any detail explanation be provided? Thank you. > > Regards, > Rice > > > > "Juozas Baliuka" wrote: > > >Sorry, > >"MyCass obj = (MyCass)Class.forName(MyCass.class.getName());" > >----- Original Message ----- > >From: "Juozas Baliuka" > >To: "Jakarta Commons Developers List" > >Sent: Wednesday, May 01, 2002 9:49 PM > >Subject: Re: ClassCastException raised in newFactory() method when starting > >from JUnit test > > > > > >> Hi, > >> I am not sure about "newFactory()", but in the most problem is like this: > >> > >> MyCass obj = Class.forName(MyCass.class.getName()); > >> > >> This allways throws ClassCastException if this code is loaded not by > >> "SystemClassLoader". > >> In this case we have two diferent classes, the same name but not > >> ClassLoader. > >> > >> This is OK: > >> MyCass obj = Class.forName(MyCass.class.getName(),true, > >> MyCass.class.getClassLoader() ); > >> > >> Both classes have the same name and ClassLoader. > >> > >> if "newFactory()" doe's "Class.forName" it must have alternatyve: > >> "newFactory(ClassLoader cl)" and use "cl" as parameter in "Class.forName". > >> > >> > >> > >> > >> ----- Original Message ----- > >> From: "Rice Yeh" > >> To: ""Jakarta Commons Developers List"" > >> Sent: Wednesday, May 01, 2002 2:48 PM > >> Subject: RE: ClassCastException raised in newFactory() method when > >starting > >> from JUnit test > >> > >> > >> > Hi, > >> > Sorry, I past wrong attachement. The original one will not cause this > >> problem. I attache a new one, which just moves > >> > > >> > Digester d = new Digester(); > >> > System.out.println("OK"); > >> > to testLogging() method. > >> > > >> > Regards, > >> > Rice > >> > > >> > riceyeh@netscape.net (Rice Yeh) wrote: > >> > > >> > >Hi all, > >> > > > >> > > Just wonder why ClassCastException is raised at line 496 of LogFactory > >> in newFactory() method when running as a JUnit test? I found this problem > >> when running RequiredNameTest in commons-validator. When casting a class > >to > >> some superclass, does classloader from other thread form a namespace and > >so > >> can NOT cast it? Java language spec seems not regulate this. I have proven > >> this with a small program. > >> > > Attached are JUnit test files I use to study this problem. > >> > > > >> > >Regards, > >> > >Rice > >> > > > >> > > > >> > >__________________________________________________________________ > >> > >Your favorite stores, helpful shopping tools and great gift ideas. > >> Experience the convenience of buying online with Shop@Netscape! > >> http://shopnow.netscape.com/ > >> > > > >> > >Get your own FREE, personal Netscape Mail account today at > >> http://webmail.netscape.com/ > >> > > > >> > > >> > > >> > __________________________________________________________________ > >> > Your favorite stores, helpful shopping tools and great gift ideas. > >> Experience the convenience of buying online with Shop@Netscape! > >> http://shopnow.netscape.com/ > >> > > >> > Get your own FREE, personal Netscape Mail account today at > >> http://webmail.netscape.com/ > >> > > >> > >> > >> ------------------------------------------------------------------------- - > >-- > >> ---- > >> > >> > >> > /* > >> > * TestSuite.java > >> > * JUnit based test > >> > * > >> > * Created on May 1, 2002, 7:38 PM > >> > */ > >> > > >> > package test; > >> > > >> > import junit.framework.*; > >> > import org.apache.commons.digester.Digester; > >> > > >> > /** > >> > * > >> > * @author Rice Yeh > >> > */ > >> > public class LoggingFromDigestTest extends TestCase > >> > { > >> > > >> > public LoggingFromDigestTest(java.lang.String testName) > >> > { > >> > super(testName); > >> > } > >> > > >> > public static void main(java.lang.String[] args) > >> > { > >> > > >> > > >> > junit.swingui.TestRunner.run(suite()); > >> > } > >> > > >> > public void setUp() > >> > { > >> > > >> > } > >> > > >> > > >> > > >> > public static Test suite() > >> > { > >> > TestSuite suite = new TestSuite(LoggingFromDigestTest.class); > >> > > >> > return suite; > >> > } > >> > > >> > public void testLogging() > >> > { > >> > Digester d = new Digester(); > >> > System.out.println("OK"); > >> > } > >> > > >> > public void tearDown() > >> > { > >> > } > >> > } > >> > > >> > > >> > >> > >> ------------------------------------------------------------------------- - > >-- > >> ---- > >> > >> > >> > -- > >> > To unsubscribe, e-mail: > >> > >> > For additional commands, e-mail: > >> > >> > >> > >> -- > >> To unsubscribe, e-mail: > > > >> For additional commands, e-mail: > > > >> > > > > > >-- > >To unsubscribe, e-mail: > >For additional commands, e-mail: > > > > > > > __________________________________________________________________ > Your favorite stores, helpful shopping tools and great gift ideas. Experience the convenience of buying online with Shop@Netscape! http://shopnow.netscape.com/ > > Get your own FREE, personal Netscape Mail account today at http://webmail.netscape.com/ > > > -- > To unsubscribe, e-mail: > For additional commands, e-mail: > -- To unsubscribe, e-mail: For additional commands, e-mail: