instanceof

How to see if an object is an array?

How can I see in Java if an Object is an array without using reflection? And how can I iterate through all items without using reflection? I use Google GWT so I am not allowed to use reflection :( I would love to implement the following methods without using refelection: private boolean isArray(final Object obj) { ??.. } private Str...

Is instanceof considered bad practice? If so, under what circumstances is instanceof still preferable?

Over the years, I've tried to avoid instanceof whenever possible. Using polymorphism or the visitor pattern where applicable. I suppose it simply eases maintenance in some situations... Are there any other drawbacks that one should be aware of? I do however see it here and there in the Java libraries so I suppose it has its place? Under...

Reflection: cast an object to subclass without use instaceof

I have this simple interface/class: public abstract class Message { } public class Message1 extends Message{ } public class Message2 extends Message{ } And an utility class: public class Utility { public void handler(Message m){ System.out.println("Interface: Message"); } public void handler(Message1 m){ Syste...

Avoiding instanceof in Java

Having a chain of "instanceof" operations is considered a "code smell". The standard answer is "use polymorphism". How would I do it in this case? There are a number of subclasses of a base class; none of them are under my control. An analogous situation would be with the Java classes Integer, Double, BigDecimal etc. if (obj instanc...

Why does instanceof seem to work in a static generic function sometimes?

Greetings. This is my first post in this site. I thought that because of type erasure, one could not expect the following code to compile, and indeed, it did not compile on an earlier version of Eclipse. My understanding was that instanceof was a run-time operator and could not know about the generic type which would be, by run-time, ...

method with two parameters which both need to be double dispatched

lets say i have a method which has two parameters. i have been implementing them as: if(aObj instance of Marble) { if(bObj instance of Bomb) { this.resolve((Marble)aObj,(Bomb)bObj); } } as you can see its not a very pretty solution. i plan to implement using double dispatching, but with two parameters which both need d...

javascript instanceof get type from string name

Let's say I have this (assume the name variable is "receiver"): if (!(receiver instanceof com.HTMLReceiver)) { throw new com.IllegalArgumentException( name + " is not an instance of com.HTMLReceiver."); } I'd like to factor this code out into a common method so I could call it like this: Helper.checkInstance(receiver, "co...

Is it possible to use instanceof when passing objects between Threads?

I've run into an issue where instanceof works, and then it doesn't. Going into details is difficult, but I think this might be the problem: Reading this: http://www.theserverside.com/news/thread.tss?thread_id=40229 (search for Thread.currentThread), it seems to imply that, even if the two objects are the same class, if you pass them be...

Parameters implementing a certain interface and having a specific superclass: generics or instanceof?

I would love some help on a particular design. This is the code I wish worked: abstract class Square {...} abstract class Circle {...} interface JPGExportable {...} class SquareJPG extends Square implements JPGExportable {...} class CircleJPG extends Circle implements JPGExportable {...} interface Interface { draw(Square square);...

Is there a better/cleaner way to conditionally create a type than using instanceof? [Java]

Suppose I have: public class FightingZone<MobileSuitso, Background> { private MobileSuitCollection<MobileSuitso> msCollection; private BackgroundInfo<Background> bgInfo; public FightingZone(MobileSuitCollection<MobileSuitso> newCollection, BackgroundInfo<Background> newInfo) { this.msCollection = newCollection; ...

Javascript inheritance - instanceof not working?

I'm writing a simple platform game using javascript and html5. I'm using javascript in an OO manner. To get inheritance working i'm using the following; // http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/ function copyPrototype(descendant, parent) { var sConstructor = parent.toString(); var aMatch = sConstruct...

Java abstract method with abstract parameter and inheritance

Hi all, I recently fumbled into a problem with an API and an implementation where the following type of code appeared: The API is an abstract class: public abstract class A { public A sum(A a) { System.out.println("A.sum(A) called"); return null; } } The implementation is a simple class: public class B extends A { public B s...

instanceof Double/Object won't work :(

Hi folks! I've got a problem with java's instanceof. Here's a gap of code that causes me trouble: LinkedList<Double> currentSummary = summary.getFirst().getQuantiles(); ...more code... while (!currentSummary.isEmpty()){ if (currentSummary.getFirst() instanceof Double){ orderedSummary.add(new ComparableWrapper<Do...

mxml inheritance in Flex. how works "instanceof" and "is"?

Hi2all! Earlier i used following structure: Canvas -> Screen1 Canvas -> Screen2 When I feel need in common logic in my application I do next: Canvas -> Screen Screen -> Screen1 Screen -> Screen2 So when I try to apply it in my code if(child is Screen){ return child.localToGlobal(new Point()).x; } It is not works! When I see ...

Checking if an annotation is of a specific type

I am using reflection to see if an annotation that is attached to a property of a class, is of a specific type. Current I am doing: if("javax.validation.Valid".equals(annotation.annotationType().getName())) { ... } Which strikes me as a little kludgey because it relies on a string that is a fully-qualified class-name. If the namesp...

function __autoload & using require_once / ignore certain instances

Hi everyone, I am using the autoload function for a certain library... But I am trying to implement Doctrine and I am getting a 500 Internal Server Error. I believe its because I am creating = new instance and in the autoload... It checks a different directory. Is there a way to create new instances of classes that will ignore the au...

How to test if one java class extends another at runtime?

How to I test if a is a subclass of b? Class<?> a = A.class; Class<?> b = B.class; ...

Why does this instanceof code work and does not cause a compile time error?

In the following code, the type of x is I (although x also implements J but thats not known at compile time) so why is it that the code at (1) doesn't result in a compile time error. Because at compile time only the type of the reference is considered. public class MyClass { public static void main(String[] args) { I x = ne...

Java objects instanceof

The instructions say: Create an equals method that takes an object reference and returns true if the given object equals this object. * Hint: You'll need 'instanceof' and cast to a (Position) I have: class Position { private double x,y; private int id; public boolean instanceOf(Object a) { boolean isInstance; ...

Java: Unchecked cast from X to Y / how to implement castOrNull

Hi, I have implemented this function: static <X,Y> Y castOrNull(X obj) { try { return (Y)obj; } catch(ClassCastException e) { return null; } } This gives me the compiler warning: Type safety: Unchecked cast from X to Y Which I don't exactly understand. Isn't the try/catch which I am doing here a check for it? Can I...