tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Felipe Schnack <feli...@ritterdosreis.br>
Subject RE: instance of (ot)
Date Thu, 20 Feb 2003 19:26:54 GMT
  Ok, I wasn't clear... let me re-write my mail:
  The problem is that I want to test if an object is subclass of a
java.lang.Class INSTANCE... Nonsense example:

  java.util.Date date1 = new java.util.Date();
  java.sql.Date date2 = new java.sql.Date();
  Class clasz = date1.getClass();
  if (date2 instanceof clasz)
  {
      do something...
  }

  There is a way? The code that made me need to test if an object is subclass
of another one's is quite hard to explain briefly :-)
  I implemented the following, but I'm not very proud of myself:

private boolean instanceOf(Object obj, Class clasz)
{
	if (obj == null || clasz == null) return false;
	Class objClass = obj.getClass();
	Class[] classes = objClass.getClasses();
	if (classes == null) return false;
	for (int i = 0; i<classes.length; i++)
	{
		if (classes[i].equals(clasz)) return true;
	}
	while (objClass != null)
	{
		if (objClass.equals(clasz)) return true;
		objClass = objClass.getSuperclass();
	}
	return false;
}
  
On Thu, 2003-02-20 at 16:13, Kris Schneider wrote:
> Maybe I'm missing something, but Class is a final class.
> 
> Quoting Mike Jackson <mjackson@cdi-hq.com>:
> 
> > Perhaps you ought to post a snippet of what doesn't work.  I personally
> > haven't had any problems with "instanceof", but without seeing what you're
> > doing I don't think any of us can help.
> > 
> > --mikej
> > -=-----
> > mike jackson
> > mjackson@cdi-hq.com
> > 
> > > -----Original Message-----
> > > From: Felipe Schnack [mailto:felipes@ritterdosreis.br]
> > > Sent: Thursday, February 20, 2003 11:01 AM
> > > To: Tomcat Users List
> > > Subject: instance of (ot)
> > >
> > >
> > >   I would like to use instanceof's keyword funcionality.
> > >   The problem is that I want to test if a class is subclass of a
> > > java.lang.Class object... There is a way? Instanceof keyword doesn't
> > > work, so I implemented the following, but I'm not very proud of myself:
> > >
> > > 	private boolean instanceOf(Object obj, Class clasz)
> > > 	{
> > > 		if (obj == null || clasz == null) return false;
> > > 		Class objClass = obj.getClass();
> > > 		Class[] classes = objClass.getClasses();
> > > 		if (classes == null) return false;
> > > 		for (int i = 0; i<classes.length; i++)
> > > 		{
> > > 			if (classes[i].equals(clasz)) return true;
> > > 		}
> > > 		while (objClass != null)
> > > 		{
> > > 			if (objClass.equals(clasz)) return true;
> > > 			objClass = objClass.getSuperclass();
> > > 		}
> > > 		return false;
> > > 	}
> > >
> > >
> > > --
> > >
> > > Felipe Schnack
> > > Analista de Sistemas
> > > felipes@ritterdosreis.br
> > > Cel.: (51)91287530
> > > Linux Counter #281893
> > >
> > > Centro Universitário Ritter dos Reis
> > > http://www.ritterdosreis.br
> > > ritter@ritterdosreis.br
> > > Fone/Fax.: (51)32303341
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> > >
> > 
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> > 
> 
> 
> -- 
> Kris Schneider <mailto:kris@dotech.com>
> D.O.Tech       <http://www.dotech.com/>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
-- 

Felipe Schnack
Analista de Sistemas
felipes@ritterdosreis.br
Cel.: (51)91287530
Linux Counter #281893

Centro Universitário Ritter dos Reis
http://www.ritterdosreis.br
ritter@ritterdosreis.br
Fone/Fax.: (51)32303341


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message